# webpack-howto **Repository Path**: mirrors_kidjp85/webpack-howto ## Basic Information - **Project Name**: webpack-howto - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-09 - **Last Updated**: 2026-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## 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](https://www.npmjs.com/package/babel-loader) 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') instead of require('file.coffee') extensions: ['', '.js', '.json', '.coffee'] } }; ``` ## 5. Stylesheets and images First update your code to `require()` your static assets (named as they would with node's `require()`): ```js require('./bootstrap.css'); require('./myapp.less'); var img = document.createElement('img'); img.src = require('./glyph.png'); ``` When you require CSS (or less, etc), webpack inlines the CSS as a string inside the JS bundle and `require()` will insert a `