externalize
Create external Browserify bundles for lazy asynchronous loading
Introduction to asynchronous module loading with Browserify:
http://esa-matti.suuronen.org/blog/2013/04/15/asynchronous-module-loading-with-browserify/
Install
npm install externalize
API
The module exports a single function
externalize(
<parent bundle or array of parent bundles>,
<bundle or arrays of bundles to be externalized from the parent bundles>,
<callback fucntion>
);
Example
Create two bundles where the second one is a subset of the parent and call
externalize(parent, subset, callback)
on them. It will do following:
- Moves all modules that are used in both to the parent one
- Removes those modules from the parent one that are explicitly requireable in the subset one
- It generally tries to do the "right thing"
in code:
var fs = ;var browserify = ;var externalize = ; // Parent bundle with an entry pointvar parent = ; // Make subset bundle from external.js by making it explicitly requireablevar subset = ; // Remove the subset bundle code from the parent;
index.js:
// require("./external");// Would not work here because external.js is externalized to the subset bundle // Use any script loader to load the subset bundle to make the require work// againjQuery;
external.js:
moduleexports = "external module contents";
Script loaders
You need a script loader to load the external bundles. There is one in jQuery. If you don't use jQuery you can grab $script.js from npm or implement your own for modern browsers easily.
Some others include: