liveBook · Manning

Browserify

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A JavaScript module bundler that resolves dependencies starting from an entry file and produces a bundled file for use in browser or Node.js environments.

Browserify bundles JavaScript modules into one file starting from an entry point. It lets code keep a Node.js-style module structure during development and then run in the browser. section 3.3 section 3.0

It analyzes imports from the main file, builds a dependency tree, and emits a bundled file with scoping and namespacing preserved. That bundle can be included in a webpage, and it can also work with Node.js-style packages through transforms. section 3.3 section 3.4

Use Browserify here to:

  • Turn modular source files into a single browser-ready bundle. section 3.3
  • Pair Babel with babelify so ES2015 files are transpiled before bundling. section 3.4
  • Keep using Node.js module patterns while targeting the browser. section 3.3

Typical Browserify flow

A project places source files under src, sets an entry file such as src/index.js, and runs browserify src/index.js --transform babelify --outfile dist/bundle.js --debug. The entry file imports another module, Browserify follows that import, babelify transpiles first, and the output lands in dist/bundle.js with source maps. section 3.5 section 3.4

$ browserify src/index.js --transform babelify --outfile dist/bundle.js
--debug

Shows the bundle command with entry file, transform, output file, and debug flag. section 3.5

<!DOCTYPE html>
<html>
<head>
  <title>Babelify Example</title>
</head>
<body>
  <h1>Hello, ES6!</h1>

  <script src="dist/bundle.js"></script>
</body>
</html>

Shows the bundled script being loaded into a webpage. section 3.5

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Explains entry-point bundling, dependency tracing, and babelify transforms, but stops short of broader module-system theory.
Examples
Uses a small app.js and index.js pair, a browserify command, and a webpage that loads dist/bundle.js.
What it assumes
Assumes basic module imports and JavaScript file structure, while introducing Browserify and its transform setup from scratch.
What you can do after
Build it into a single bundled file that can run in a webpage while preserving module scoping.
How it connects
Connects Browserify with Babel, babelify, Node.js modules, ES2015 modules, and source maps.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.