Quick reference

Webpack Configuration Cheat Sheet

webpack 5 configuration plus a clearly separated webpack 3 legacy reference covering Babel, CSS, PostCSS and the development server.

· Published · Updated

Scope. webpack 5 for a browser application. Configuration uses CommonJS in webpack.config.cjs, including projects with "type": "module".

webpack 5 configuration

// webpack.config.cjs
const path = require('node:path');
module.exports = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
  },
};
npm install --save-dev webpack@5 webpack-cli
npx webpack --config webpack.config.cjs

Create src/index.js before running. Reference dist/app.js from your HTML. clean removes prior output files: dedicate dist to generated assets.

Loaders and assets

Requirement Configuration
Import CSS Rule matching CSS with use: ['style-loader', 'css-loader']
Emit image files Rule matching image extensions with type: 'asset/resource'
Resolve TypeScript imports Configure a TypeScript loader and resolve.extensions

Loaders such as style-loader, css-loader and ts-loader require separate installation. A TypeScript loader also needs suitable compiler configuration.

Development

Use mode: 'development' and devtool: 'source-map' when debugging. A production build enables production defaults; it does not automatically translate all modern syntax for older browsers. Development servers require separate setup.

Reference

webpack concepts.

webpack 3 legacy reference

This legacy reference targets webpack 3, including Babel 6 and historical loader configuration. Do not mix these dependency names and CLI options with the webpack 5 examples above. Ellipses indicate configuration fragments.

Basic configuration

webpack.config.js

module.exports = {
  context: __dirname,
  entry: './src/app.js',
  output: {
    path: __dirname + '/public',
    filename: 'app.js'
  }
}

Terminal

npm install --save-dev webpack@3
webpack Build
webpack -p Production build
webpack --watch Watch mode

This compiles src/app.js into public/app.js. Use ./node_modules/.bin/webpack when the local binary is not on PATH.

Multiple entry files

webpack.config.js

module.exports = {
  entry: {
    app: './src/app.js',
    vendor: './src/vendor.js'
  },
  output: {
    path: __dirname + '/public',
    filename: '[name].js'
  }
}

This produces app.js and vendor.js.

Loaders

Babel

Terminal

npm install --save-dev \
  babel-loader \
  babel-preset-env \
  babel-preset-react

webpack.config.js

module.exports = {
  ···
  module: {
    rules: [
      { test: /\.js$/,
        exclude: /node_modules/,
        use: [
          { loader: 'babel-loader' }
        ]
      }
    ]
  }
}

.babelrc

{
  "presets": [ "env", "react" ]
}

Add support for Babel.

CSS

Terminal

npm install --save-dev \
  css-loader \
  style-loader

webpack.config.js

module.exports = {
  ···
  module: {
    rules: [
     { test: /\.css$/,
       exclude: /node_modules/,
       use: [
         { loader: 'style-loader' },
         { loader: 'css-loader' }
       ]
      }
    ]
  }
}

Your JavaScript

import './styles.css'
// or:
require('./styles.css')

This enables CSS imports from JavaScript and injects the bundled styles into the page at runtime.

PostCSS

Terminal

npm install --save-dev \
  postcss-loader \
  postcss-cssnext

webpack.config.js

···
// Inside module.rules[]:
{ test: /\.css$/,
  exclude: /node_modules/,
  use: [
    { loader: 'style-loader' },
    { loader: 'css-loader' },
    { loader: 'postcss-loader' }
···

postcss.config.js

module.exports = {
  plugins: [
    require('postcss-cssnext')()
  ]
}

This example uses postcss-cssnext to transform CSS using the historical cssnext plugin.

Other features

Development server

package.json

{ ···
  "scripts": {
    "dev": "webpack-dev-server"
  }
}

Terminal

npm install --save-dev \
  webpack-dev-server
npm run dev

This starts a development HTTP server, normally on port 8080.

Related topics