A Browserify plugin that allows you to split a bundle into separate files in such a way that module files can be concatenated arbitrarily.
This is currently in development. Use with caution!
npm install browserify-splitter --save
// a.js
require('lodash');
require('./c.js')
// b.js
// do something
const splitter = require('browserify-splitter');
browserify()
.require('./a.js')
.require('./b.js')
.plugin(splitter, {
writeToDir: 'public/js'
})
.bundle();
This will write the five following files to public/js
:
prelude.js
- Javascript that starts a bundle.a.js
- Module chunk file fora
module.b.js
- Module chunk file forb
module.c.js
- Module chunk file forc
module.lodash.js
- Module chunk file forlodash
.postlude.js
- Javascript that ends the bundle.
You could generate the entire bundle by concatenating all of these files together. If you didn't need b.js
, you could leave it out, and the resulting bundle will still be valid. You can include or exclude any modules from the bundle as long as you include prelude
at the beginning and postlude
at the end.
writeToDir
: Required. String.verbose
: Boolean. Log all file writes. Defaults tofalse
.
Oftentimes, developers use Browserify to precompile client JS during a build step. However, what happens when you don't know what the client needs until runtime? You have a few options:
- Include all the JS the client could need, though this may result in huge bundles.
- Bundle only modules we know the client usually needs, and have the client fetch other JS as needed via async requests, though this could mean dozens of async requests.
- Bundle at runtime, on request, though this is not performant, especially if you're transpiling, uglifying, or making other transformations.
Browserify-splitter lets you leave all the costly parts of bundle generation -- JS parsing, dependency tracing, transpiling, and transforming -- to the build step, and reap the benefits of on-the-fly bundling without sacrificing performance. By combining the plugin with browserify-extract-registy, you can export the dependency registry during your build and use it at runtime to compute dependencies based on the user's request, concatenate the appropriate modules, and give this dynamically generated bundle to the user.