petehunt /webpack-howto

★ 9,989⑂ 672👁 257⑃ 54● 46▤ 0
JavaScriptCreated 2014-07-17
+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

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON