Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active September 14, 2015 05:35
Show Gist options
  • Select an option

  • Save ruxo/fa0ec3d549347b495289 to your computer and use it in GitHub Desktop.

Select an option

Save ruxo/fa0ec3d549347b495289 to your computer and use it in GitHub Desktop.
Browserify with Gulp
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var source = require('vinyl-source-stream');
var R = require('ramda');
// Browserify example from: https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md
var browserify = require('browserify');
var watchify = require('watchify');
var opts = watchify.args;
// [string, string] ➔ [string, Bundle]
var createBundle = R.curry(function(wrapper, config){
var entry = config[0];
var target = config[1];
return [target, wrapper(browserify(entry, opts))];
});
// string ➔ [string, Bundle] ➔ void ➔ Stream
var buildBundle = R.curry(function(targetDir, b, _){
var target = b[0];
var bundle = b[1];
return bundle.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source(target))
.pipe(gulp.dest(targetDir));
});
// [string, Bundle] ➔ [string, Bundle]
var setupBundle = R.curry(function(bundleBuilder, bundle){
var b = bundle[1];
b.on('update', bundleBuilder(bundle));
b.on('log', gutil.log);
return bundle;
});
// (Bundle ➔ Bundle) ➔ string ➔ [string, string] ➔ void ➔ Stream
var buildBrowserifyTask = R.curry(function(wrapper, targetDir){
var bundleBuilder = buildBundle(targetDir);
return R.pipe(
createBundle(wrapper),
setupBundle(bundleBuilder),
bundleBuilder
);
});
// string ➔ [string, string] ➔ void ➔ Stream
// string = target directory
// [string, string] = [main file (with path), target filename]
// void ➔ Stream = Gulp task function
module.exports.watchBrowserify = buildBrowserifyTask(watchify);
// string ➔ [string, string] ➔ void ➔ Stream
module.exports.compileBrowserify = buildBrowserifyTask(R.identity);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment