Skip to content

Instantly share code, notes, and snippets.

@dustMason
Last active August 29, 2015 14:11
Show Gist options
  • Save dustMason/258ba9c55050987270b2 to your computer and use it in GitHub Desktop.
Save dustMason/258ba9c55050987270b2 to your computer and use it in GitHub Desktop.
Gulp / Browserify
...
var makeScriptBundler = function(options) {
var cache = {};
// load the cache from disk
if (fs.existsSync(options.cache)) { cache = require(options.cache); }
var mainScriptPackageCache = {};
var bundler = browserify({
entries: options.entries,
extensions: ['.coffee'],
cache: cache,
// packageCache: options.packageCache,
fullPaths: true
});
if (options.require) { bundler.require(options.require); }
if (options.external) { bundler.external(options.external); }
bundler.on("dep", function(dep) {
if (typeof dep.id === "string") {
cache[dep.id] = dep;
cache[dep.id].hash = crypto.createHash('md5').update(dep.source).digest('hex');
}
});
bundler.on("bundle", function(b) {
b.on("end", function() {
fs.writeFile(options.cache, JSON.stringify(cache));
console.log("wrote cache", options.cache);
});
});
return function() {
var stream = bundler.bundle();
return stream
.pipe(source(options.source+".js"))
.pipe(streamify(uglify()))
.pipe(gulp.dest(options.destination));
};
};
gulp.task('main_script', function() {
var requires = glob.sync("./src/js/!(main).coffee").map(function(f){
return {file: f, expose: path.basename(f, '.coffee')};
});
var bundle = makeScriptBundler({
entries: ['./src/js/main.coffee'],
source: 'main',
destination: './build/js',
require: requires,
cache: "./cache/main.js.cache.json",
// packageCache: mainScriptPackageCache
fullPaths: true
});
return bundle();
});
gulp.task('separate_page_scripts', ['main_script'], function() {
return glob.sync("./src/js/separate-pages/*.coffee").forEach(function(file) {
var bundle = makeScriptBundler({
entries: [file],
source: 'separate-pages/' + path.basename(file, ".coffee"),
destination: './build/js',
external: glob.sync("./src/js/*.coffee"),
cache: "./cache/" + path.basename(file, ".coffee") + ".js.cache.json"
// packageCache: separatePageScriptsPackageCache
});
return bundle();
});
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment