Skip to content

Commit

Permalink
Initial Commit v0.1.0
Browse files Browse the repository at this point in the history
Signed-off-by: cosrnos <[email protected]>
  • Loading branch information
cosrnos committed Dec 5, 2015
0 parents commit 661438c
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
bower_components
node_modules
_build
dist
155 changes: 155 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"use strict";

// Gulp Dependencies
const gulp = require('gulp');
const babel = require('gulp-babel');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const replace = require('gulp-replace');
const sourcemaps = require('gulp-sourcemaps');

const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const del = require('del').sync;

const browserify = require('browserify');
const _ = require('lodash');

const config_defaults = {
BUILD_DESTINATION: '_build',
PACKAGE_NAME: 'pacmod',
DIST_FOLDER: '',
MAIN_PACKAGE: 'main'
};

// Config Vars
let tinylr;
let Config;

// Helpers
function load_config() {
let config_file = {};

try {
config_file = JSON.parse(require('fs').readFileSync(__dirname + '/pacmod.json', 'utf8'));
} catch (e) {
let warn_obj;

if (e.code === 'ENOENT') {
warn_obj = "No pacmod.json file found in `" + __dirname + "`. Using pacmod default settings...";
} else {
warn_obj = (e && e.stack) || e;
}

console.log("WARNING: ", warn_obj);
}

return _.defaults({}, config_file, config_defaults);
}

function handle_error(source) {
return (err) => {
console.log("Error encountered in `" + source + "` task: ", (err && err.stack) || err);
}
}

function get_dist_js_folder() {
return ("dist" + Config.DIST_FOLDER);
}

function notifyLiveReload(event) {
let fileName = require('path').relative(__dirname, event.path);

console.log("Triggering live reload...");

tinylr.changed({
body: {
files: [fileName]
}
});
}

Config = load_config();

// Cleaning Tasks
gulp.task('clean-build', function () {
return del([Config.BUILD_DESTINATION]);
});

gulp.task('clean-dist', function () {
return del(['dist']);
});

gulp.task('bundle-commonjs-clean', function () {
return del([Config.BUILD_DESTINATION + '/commonjs']);
});

// Copy Tasks
gulp.task('copy-public', function () {
var script_path = Config.DIST_FOLDER + '/' + Config.PACKAGE_NAME + '.js?t=' + (+new Date());
return gulp.src(['public/**/*'])
.pipe(replace('__INCLUDE_MAIN__', script_path))
.pipe(replace('__PACKAGE_NAME__', Config.PACKAGE_NAME))
.pipe(gulp.dest('dist/'))
.on('error', handle_error('copy-public'));
});

// Build Tasks
gulp.task('es6-commonjs', function () {
return gulp.src(['packages/**/lib/*.js', 'packages/**/lib/**/*.js'])
.pipe(rename(function (path) {
path.dirname = path.dirname.replace('/lib', '');
}))
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['es2015']
}))
.on('error', function (err) {
console.log("Error in es6 transpile: ", (err && err.stack) || err);
this.emit('end');
})
.pipe(gulp.dest(Config.BUILD_DESTINATION + '/tmp'))
.on('error', handle_error('es6-commonjs'));
});

gulp.task('commonjs-bundle', ['bundle-commonjs-clean', 'es6-commonjs'], function () {
return browserify([Config.BUILD_DESTINATION + '/tmp/' + Config.MAIN_PACKAGE + '/index.js'])
.on('error', handle_error('commonjs-bundle::browserify'))
.bundle()
.on('error', handle_error('commonjs-bundle::browserify-bundle'))
.pipe(source(Config.PACKAGE_NAME + '.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(rename(Config.PACKAGE_NAME + '.js'))
.pipe(gulp.dest(Config.BUILD_DESTINATION))
.pipe(gulp.dest(get_dist_js_folder()))
.on('error', handle_error('commonjs-bundle'));
});

// Serve Tasks
gulp.task('express', function () {
let express = require('express');
let app = express();
app.use(require('connect-livereload')({ port: 35729 }));
app.use(express.static(__dirname + '/dist'));
app.listen(4000, '0.0.0.0');
});

gulp.task('watch', function () {
gulp.watch('packages/**/*.js', ['build']);
gulp.watch('dist/**/*', notifyLiveReload);
gulp.watch('public/**/*', ['copy-public'], notifyLiveReload);
});

gulp.task('livereload', function () {
tinylr = require('tiny-lr')();
tinylr.listen(35729);
});

// Aliases
gulp.task('clean', ['clean-build', 'clean-dist'])
gulp.task('build', ['commonjs-bundle', 'copy-public']);
gulp.task('serve', ['express', 'livereload', 'watch']);
gulp.task('dev', ['clean', 'build', 'serve']);

gulp.task('default', ['dev']);
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "pacmod",
"version": "0.1.0",
"description": "A package module development environment",
"main": "index.js",
"scripts": {
"test": "echo \"Testing functionality missing\" && exit 0"
},
"author": "Alex Roth <[email protected]> (http://cosrnos.com/)",
"license": "ISC",
"devDependencies": {
"babel-preset-es2015": "^6.3.13",
"browserify": "^12.0.1",
"connect-livereload": "^0.5.4",
"del": "^2.1.0",
"express": "^4.13.3",
"gulp-babel": "^6.1.1",
"gulp-browserify": "^0.5.1",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"gulp-requirejs": "^0.1.3",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^1.5.1",
"lodash": "^3.10.1",
"tiny-lr": "^0.2.1",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
}
}
10 changes: 10 additions & 0 deletions packages/main/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* This is the main entry point of your application. You can require other packages by using the
* Import statement. For instance, if you defined a foo package you can require it using
*
* ```
* import foo from "./foo"
* ```
*/

console.log("Welcome to pacmod");
10 changes: 10 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>__PACKAGE_NAME__</title>
</head>
<body>
<script src="__INCLUDE_MAIN__">
</script>
</body>
</html>
54 changes: 54 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
pacmod - A package module environment
======================================

pacmod is a package module environment that can be used to develop browser modules with es6. Using babel and browserify, pacmod will compile your packages into a single distribution file.

```
Warning: Please note that releases before 1.0.0 may not be backwards compatable
```


## Usage

At the moment, pacmod the pacmod files should be downloaded and put into your project directory. Once complete, run

```
npm install -g gulp
npm install
```

You can then use pacmod by running gulp. The main entry point of pacmod defaults to the <code>index.js</code> file of the main package.

You may also put files in the <code>/public</code> folder to be included in the /dist directory

## Configuration

To configure pacmod, create a pacmod.json file in your project directory. The following configuration options are available:

### <code>BUILD_DESTINATION</code>
Default: <code>'_build'</code>
The directory to use for temporary build files. You should have this directory in your project's <code>.gitignore</code>

### <code>PACKAGE_NAME</code>
Default: <code>'pacmod'</code>
The name of your module and the subsequent file to be built

### <code>DIST_FOLDER</code>
Default: <code>''</code>
The folder relative to <code><Project-directory>/dist</code> that the compiled JS file should be moved to

### <code>MAIN_PACKAGE</code>
Default: <code>'main'</code>
The name of the package that should be used as the main entry point for the application. This file will be invoked on script load.

## Package Structure

Packages are defined as self contained components or module packs that are distributed with their own unit tests. Each package should be independant and have no dependencies outside of itself. The package folder structure is

```
- /package-name
--- /lib
----- /index.js (entry point)
--- /tests
--- /readme.md (optional)
```

0 comments on commit 661438c

Please sign in to comment.