Webpack
Anatomy of a Module Bundler
Tim Doherty
Who am I?
Tim Doherty
Software Architect at
Organizer, Santa Barbara JavaScript
Vice President, Paradise Dive Club
What is Webpack?
?
?
What is Webpack?
Webpack is a module bundler.
Webpack takes modules with dependencies and generates static assets representing those modules.
?
Modules
In the beginning, there were no modules...
Over the years, various solutions have filled the void:
https://www.flickr.com/photos/nicasaurusrex/1363221060
Then there were modules, but...
Module specs arose, but they were disparate:
Then there were native modules, but...
JavaScript finally got native modules with the arrival of ES2015, but...
Meanwhile, the Module Bundler Arose
Why Webpack?
Why Webpack?
Why another module bundler?
Why Webpack?
Basics
Basics
Webpack takes one or more entry points as input, traverses their dependency trees, and produces one or more output bundles, or “chunks.”
Basics
feature.js:�console.log('feature module');
entry.js:�console.log('entry requires ' + require('./feature'));
$ webpack ./entry.js bundle.js
Basics
bundle.js:
�(function(modules) {� /** webpack runtime **/�})([� function (module, exports) {� /** entry.js **/� },� function (module, exports) {� /** feature.js **/� }�]);
Configuration
Configuration
The webpack CLI is concise and effective. For all but the simplest applications, however, a configuration file will make your life much easier.
The main config sections are:
Configuration
module.exports = {� entry: "./entry.js",� output: {� path: __dirname,� filename: "bundle.js"� },� plugins: [],� module: {� loaders: []� }�};
Debugging
Debugging
The “devtool” config option gives various levels of debugging support. Each represents a balance between debug experience and speed/performance.
Loaders
Loaders
Webpack can only process JavaScript natively, which already has modules
So, a build tool for my other resources?
Loaders
Webpack uses loaders to transform other resources into modules:
This gives us a unified paradigm, in which every resource is a module.
In many cases this means that Webpack can, at least partially, replace build tools.
Loaders
At its simplest, a loader is just a function that takes a source file contents as a string or buffer, and returns a string or buffer.
module.exports = function(source) {� return source;�};
Produces:
function(module, exports) {� module.exports = '<source string>';�}
Loaders
More typically, loaders transform the source and, optionally, include runtime code
Take the css-loader, for example:
Loaders
Inline:�var myCss = require('style!css!./styles.css');
By config:�loaders: [{� test: /\.css$/,� loaders: ['style', 'css']�}]
Both:�var myCssString = require('!!raw!./styles.css');
Loaders
Loaders are also commonly used to to transpile other languages to JS:
Plugins
Plugins
Plugins can be used extend to Webpack’s functionality, and are typically used for post-processing the resulting bundle(s).
Much of Webpack’s “built-in” functionality is actually provided by bundled plugins:
Plugins
Plugins are instanceable objects with an “apply” method, inside which authors can hook into the webpack compiler internals.
function MyPlugin(options) {� // Setup the plugin instance with options...�}��MyPlugin.prototype.apply = function(compiler) {� compiler.plugin('done', function() {� console.log('This is my plugin.'); � });�};��module.exports = MyPlugin;
Plugins
Plugins are installed by adding instances to the webpack config plugins array
module.exports = {� ...� plugins: [� new webpack.HotModuleReplacementPlugin()� ]� ...�};
Code Splitting
Code Splitting
Webpack supports code splitting, or asynchronous dependency loading.
This allows you to optimize for large SPAs or multi-page applications.
*webpack 2 will support System.import for code splitting
Code Splitting
var btn = document.querySelector('#my-btn');��btn.addEventListener('click', function (e) {� require.ensure(['./lazy'], function (require) {� var lazyModule = require('./lazy');� /** do something with lazyModule **/� });�});
Dev Server
Webpack Development Server
Wepack offers an ExpressJS-based development server, with middleware to serve your bundle(s), and Socket.IO to update the browser.
Automatic Refresh
iFrame mode - http://<host>:<port>/webpack-dev-server/<path>
Inline mode - http://<host>:<port>/<path>
Hot Module Replacement (HMR)
Hot Module Replacement
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running without a page reload. HMR is like LiveReload for individual modules.
Demo
Questions?
Thanks!
@TimCDoherty
@sbjavascript