Last active
February 15, 2018 15:48
-
-
Save jbardon/cc012851014fb3cada8d5615be7fed1a to your computer and use it in GitHub Desktop.
## - Split bundle (https://medium.com/keepitlazy/split-up-webpack-browserify-bundle-191fb8e74b24)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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