Skip to content

Instantly share code, notes, and snippets.

@jbardon
Last active February 15, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbardon/cc012851014fb3cada8d5615be7fed1a to your computer and use it in GitHub Desktop.
Save jbardon/cc012851014fb3cada8d5615be7fed1a to your computer and use it in GitHub Desktop.
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var nodeResolve = require('resolve');
/**
* Make a browserify bundle
* @param isAppBundle true to bundle application without dependencies, false to bundle dependencies only
* @returns {*} browserify bundle
*/
var makeBundle = function (isAppBundle) {
var browerifyParams = {
debug: true // generate source map
};
// Define if application must be included in bundle
if(isAppBundle) {
browerifyParams.entries = ['./src/scripts/index.js'];
}
// Define if dependencies must be included in bundle
var bundleConfig = browserify(browerifyParams);
var browserifyVendorFunction = (isAppBundle) ? bundleConfig.external.bind(bundleConfig) : bundleConfig.require.bind(bundleConfig);
// Read package.json and get dependencies package ids
var packageManifest = require('package.json');
var packageIds = Object.keys(packageManifest.dependencies) || [];
packageIds.forEach(function (id) {
browserifyVendorFunction(nodeResolve.sync(id), {expose: id});
});
// Generate bundle
var bundleName = (isAppBundle ? 'bundle' : 'vendor') + '.js';
return bundleConfig.bundle()
.pipe(source(bundleName))
.pipe(gulp.dest('./dist'));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment