+0
Daily
9,989
stars
672
forks
257
watchers
54
prs
46
issues
0
releases
README
webpack-howto
Goal of this guide
This is a cookbook of how to get things done with webpack. This includes most things we use at Instagram and nothing we don't use.
My advice: start with this as your webpack docs, then look at the official docs for clarification.
Prerequisites
You know browserify, RequireJS or something similar
You see the value in:
Bundle splitting
Async loading
Packaging static assets like images and CSS
1. Why webpack?
It's like browserify but can split your app into multiple files. If you have multiple pages in a single-page app, the user only downloads code for just that page. If they go to another page, they don't redownload common code.
It often replaces grunt or gulp because it can build and bundle CSS, preprocessed CSS, compile-to-JS languages and images, among other things.
It supports AMD and CommonJS, among other module systems (Angular, ES6). If you don't know what to use, use CommonJS.
2. Webpack for Browserify people
These are equivalent:
js
browserify main.js bundle.js
js
webpack main.js bundle.js
However, webpack is more powerful than Browserify, so you generally want to make a webpack.config.js to keep things organized:
js
// webpack.config.js
module.exports = {
entry: './main.js',
output: {
filename: 'bundle.js'
}
};
This is just JS, so feel free to put Real Code in there.
3. How to invoke webpack
Switch to the directory containing webpack.config.js and run:
webpack for building once for development
webpack -p for building once for production (minification)
webpack --watch for continuous incremental build in development (fast!)
webpack -d to include source maps
4. Compile-to-JS languages
webpack's equivalent of browserify transforms and RequireJS plugins is a loader. Here's how you can teach webpack to load CoffeeScript and Facebook JSX+ES6 support (you must npm install babel-loader coffee-loader):
See also the babel-loader installation instructions for additional dependencies (tl;dr run npm install babel-core babel-preset-es2015 babel-preset-react).
js
// webpack.config.js
module.exports = {
entry: './main.js',
output: {
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.coffee$/, loader: 'coffee-loader' },
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
};
To enable requiring files without specifying the extension, you must add a resolve.extensions parameter specifying which files webpack searches for:
js
// webpack.config.js
module.exports = {
entry: './main.js',
output: {
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.coffee$/, loader: 'coffee-loader' },
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
resolve: {
// you can now require('file') Top issues
#18Multiple entry-points with React Router12 comments · 2014-11-04#46How to inject scripts to index html file in production?10 comments · 2015-12-09#30"Module not found: Error: Cannot resolve module" for react-router and react7 comments · 2015-07-05#45Require a image in CSS5 comments · 2015-11-16#27how do we load json4 comments · 2015-06-14#54Loaders: worth discussing "-loader" suffix?3 comments · 2016-01-27#53Best practices for depenendencies vs devDependencies3 comments · 2016-01-22#21How to compile to memory3 comments · 2015-01-22