What it is and why you should care
Really, bundle build tool.
function(require, exports, module) {
var dep1 = require('./dep1');
export.foo = dep1.whatever('yus');
module.exports = 'overwritten';
}
var dep1 = require('./dep1');
export.foo = dep1.whatever('yus');
module.exports = 'overwritten';
× 100
require('./style.css');
.foo {
color: red;
}
var d = document,
s = d.createElement('style');
s.appendChild(
d.createTextNode(
'.foo {\n color: red;\n}'
)
);
d.head.appendChild(s);
index.js
style.css
require('./style.less');
.foo {
.bar {
color: red;
}
}
var s = document.createElement('style');
s.appendChild(d.createTextNode(
'.foo .bar {\n color: red;\n}'
));
document.head.appendChild(s);
index.js
style.less
.foo .bar { color: red; }
* in the main bundle
Hot Module Replacement (HMR)
exchanges, adds, or removes modules
while an application is running
without a page reload.
> webpack-dev-server
function init() {
var app = require('./app');
app.render(document.body);
}
// initial render:
init();
// if dev server & HMR enabled:
if (module.hot) {
// on update, re-init:
module.hot.accept('./app', init);
}
babel-loader
ES2015 today
css-loader
CSS Modules
style-loader
HMR for CSS
url-loader
Data URIs
extract-text-plugin
bundle.js & bundle.css
file-loader
Static Assets
worker-loader
Worker chunks
import h from 'vhtml';
/** @jsx h */
const UI = (
<ul class="things">
<li>One</li>
<li>Two</li>
</ul>
);
document.body.innerHTML = UI;
var h = require('vhtml');
var UI =
h( 'ul', {
'class': 'things'
},
h('li', null, 'One'),
h('li', null, 'Two')
);
document.body.innerHTML = UI;
.foo {
background: url('cat.gif');
}
.foo {
background: url(
'cat.abc1234.gif'
);
}
build/cat.abc1234.gif
.foo {
background: url('cat.gif');
}
.foo {
background: url(
'data:image/gif;base64,..'
);
}
@import url('./bar.css');
.foo {
background: url(cat.gif);
}
.foo {
background: url(cat.gif);
}
.bar {
color: red;
}
.bar {
color: red;
}
bar.css
bundle.css
import Burner from 'worker!./burner';
let burner = new Burner();
burner.onmessage = out => {
console.log(out);
};
burner.postMessage('burn');
onmessage = action => {
if (action==='burn') {
burn();
postMessage('done');
}
};
function burn() {
let s = Date.now();
while(Date.now()-s < 5000);
}
index.js
burner.js
import webpack from 'webpack';
module.exports = {
entry: './src/index.js',
output: {
path: './build',
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel'
},
// etc
]
},
plugins: [
new webpack.DefinePlugin({
DEBUG: true
}),
],
devtool: 'source-map'
};
webpack.config.babel.js
Initial module to load
Destination for bundle(s)
Match modules to loaders
String or Regex filters
Loader to apply to filtered modules
Plugins
(affect the whole build)
Configure source map type
devServer: {
port: process.env.PORT || 8080,
host: '0.0.0.0',
contentBase: './src',
historyApiFallback: true,
proxy: [
{
path: '/foo/**',
target: 'http://target.com'
}
]
}
webpack.config.babel.js
(webpack-dev-server section)
Address to listen on
Fall-through static dir
(for non-bundle requests)
Configure built-in http-proxy
(for upstreams, rewrites, CORS)
Serve index.html for all URLs
(for pushState routing)
webpack-dev-server --inline --hot
Use Websocket-based live-reload
Enable Hot Module Replacement
webpack -p
Minify, optimize & de-dupe
Starts in-memory dev web server.
Changes trigger fast partial rebuilds.
Builds to config.output.path
Test Runners
*mocha doesn't really need it
Tooling
FE Tech
... so just shell scripts
{
"name": "my-great-module",
"scripts": {
"dev": "webpack-dev-server --inline --hot",
"prebuild": "mkdir -p build && cp -r src/assets build",
"build": "NODE_ENV=production webpack -p",
"prestart": "npm run build",
"start": "http-server build"
}
}
npm run build
PORT=1337 npm run dev