Skip to content

Instantly share code, notes, and snippets.

@RJ
Created May 30, 2014 14:46
Show Gist options
  • Save RJ/a02c2eff0b2b6103c47b to your computer and use it in GitHub Desktop.
Save RJ/a02c2eff0b2b6103c47b to your computer and use it in GitHub Desktop.
gulp.task('flatten-js', function() {
return gulp.src(['app/*.js', './app/**/*.js'])
.pipe(flatten())
.pipe(gulp.dest('./build/js-app'))
});
function bundleEntryPoint(entryfile) {
process.env.NODE_PATH = ( process.env.NODE_PATH ?
process.env.NODE_PATH + ':' : '')
+ './build/js-app/'
var bundleName = entryfile.replace(/.*\//,'').replace(/\.js/, '-bundle.js');
var watching = false;
var bundleMethod = watching ? watchify : browserify;
var bundler = bundleMethod({
entries: [entryfile],
bundleExternal: true,
fullPaths: false
});
glob.sync('*.js', {cwd: './build/js-app/'}).forEach(function(module) {
var expose = module.split('.').shift();
bundler.require('./build/js-app/' + module, {expose: expose})
});
// this is the equivalent of browserify --bare -r allmods.js:allmods
// ie, it inserts no node shims whatsoever.
var bundleOpts = {
detectGlobals: true,
insertGlobals: false,
insertGlobalVars: {},
ignoreMissing: false,
debug: false
};
var bundle = function() {
return bundler
.bundle(bundleOpts)
.pipe(source(bundleName))
.pipe(gulp.dest('./build'));
}
if (watching) {
bundler.on('update', bundle);
}
return bundle();
}
// Compile (if jsx) and copy all javascript into build directory
// Optional minification/compression could go here
gulp.task('main.js', ['flatten-js'], function() {
return bundleEntryPoint('./build/js-app/main.js');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment