Skip to content

A Browserify plugin for splitting a bundle into separate files that can be concatenated arbitrarily

Notifications You must be signed in to change notification settings

cperryk/browserify-splitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

browserify-splitter

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!

Installation

npm install browserify-splitter --save

Usage

// 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 for a module.
  • b.js - Module chunk file for b module.
  • c.js - Module chunk file for c module.
  • lodash.js - Module chunk file for lodash.
  • 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.

Plugin Configuration

  • writeToDir: Required. String.
  • verbose: Boolean. Log all file writes. Defaults to false.

What's this for?

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.

Similar packages

About

A Browserify plugin for splitting a bundle into separate files that can be concatenated arbitrarily

Resources

Stars

Watchers

Forks

Packages

No packages published