Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 38 ยท Optimization and Caching

Production Mode and Minification

Learn what webpack mode production switches on: minification with Terser, DefinePlugin constants, dead-code removal, CSS minification and the right devtool setting.

One line in your config, mode: 'production', changes more about the output than almost any other setting. It turns on minification, sets process.env.NODE_ENV to "production" inside your code, enables module concatenation and several other optimizations. In this lesson you will see what each switch does, how to customize minification for JavaScript and CSS, and how to choose a devtool so you can still debug production errors.

What mode actually does

mode is shorthand for a bundle of defaults. You can override any of them.

Setting development production
optimization.minimize false true (Terser for JS)
process.env.NODE_ENV "development" "production"
optimization.concatenateModules false true (scope hoisting)
optimization.usedExports false true (marks unused exports)
optimization.moduleIds / chunkIds named deterministic
devtool eval false (none)

If you do not set mode, webpack warns and defaults to production. Many teams keep one config file and branch on argv.mode, or use separate webpack.dev.js and webpack.prod.js files merged with webpack-merge.

JavaScript
// webpack.config.js (read-only example)
module.exports = (env, argv) => ({
  mode: argv.mode === 'production' ? 'production' : 'development',
  entry: './src/index.js',
  devtool: argv.mode === 'production' ? 'source-map' : 'eval-cheap-module-source-map',
});

Minification with Terser

In production, webpack uses TerserWebpackPlugin. Terser parses your code, shortens variable names (mangling), removes whitespace and comments, folds constants and deletes unreachable branches (dead-code elimination). You customize it through optimization.minimizer:

JavaScript
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  optimization: {
    minimizer: [
      new TerserPlugin({
        extractComments: false,
        terserOptions: {
          compress: { drop_console: true, passes: 2 },
          format: { comments: false },
        },
      }),
    ],
  },
};

drop_console removes calls such as console.log; use it only if you are sure no one relies on them. Setting minimizer replaces the defaults, so if you also want CSS minified you must list a CSS minimizer too (next section). A trick: write '...' in the array to keep webpack's default minimizers and add yours.

Minifying CSS

CSS extracted with MiniCssExtractPlugin is minified with css-minimizer-webpack-plugin:

JavaScript
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');

optimization: {
  minimizer: ['...', new CssMinimizerPlugin()],
}

Constants and dead code

Webpack's DefinePlugin replaces identifiers with fixed values at build time. In production mode it already defines process.env.NODE_ENV. When the replaced condition is a constant, the minifier can drop the dead branch, and any imports used only inside that branch disappear too.

Node.js Stand-in: constants plus dead-branch removal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The real Terser is far more capable (it renames identifiers, inlines functions and proves conditions), but the flow is the same: define constants first, then minify.

Libraries use this trick heavily: React ships development warnings behind process.env.NODE_ENV !== 'production', so in production those branches vanish, which is why production React is much smaller.

Scope hoisting

When concatenateModules is on (the default in production), webpack puts several modules in one function scope instead of wrapping each in its own function. This removes wrapper overhead and lets the minifier shorten more names. It only works with ES modules, which is another reason to avoid compiling imports to CommonJS with Babel (see tree-shaking).

Choosing a devtool

Minified code is unreadable in stack traces. A source map connects it back to your original files. The devtool option controls how maps are produced:

Value Use
false No maps (production default). Smallest, no debugging aid.
source-map Separate .map files. Best quality; upload to an error tracker and do not serve publicly if you want to keep sources private.
hidden-source-map Maps are generated but the bundle has no //# sourceMappingURL comment, for error-tracker-only use.
eval-cheap-module-source-map Fast rebuilds in development.
nosources-source-map Maps without source content; stack traces work, original code is not exposed.

Never use an eval-* value in production: it is slow and can break strict Content Security Policies.

A quick size comparison

Node.js Stand-in: gzip makes minified code smaller again

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Minification gives the largest win on raw size; on top of it, the server should compress with gzip or Brotli. Webpack can pre-compress files at build time with compression-webpack-plugin so your server serves ready-made .br files.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these is turned on by mode: 'production' by default?

    Production mode enables minimize, defines NODE_ENV as production, concatenates modules and uses deterministic ids.
  2. 2. What does the '...' entry in optimization.minimizer do?

    Setting minimizer replaces the defaults. The '...' spread keeps them so you can add a CSS minimizer without losing Terser.
  3. 3. Which devtool is suited to uploading maps to an error tracker without exposing them in the bundle?

    hidden-source-map generates the files but leaves out the sourceMappingURL comment, so browsers do not fetch them.
  4. 4. Why does React's production build omit dev warnings?

    DefinePlugin replaces process.env.NODE_ENV with a literal, then the minifier removes the unreachable branches.

Summary

  • mode selects a set of defaults; production enables minification, constant folding and module concatenation.
  • Terser handles JS and css-minimizer-webpack-plugin handles CSS; use '...' to extend, not replace, the defaults.
  • DefinePlugin plus dead-code elimination removes development-only code.
  • Pick devtool per environment and never ship eval-* maps.
  • Compress with gzip or Brotli on top of minification.

Next: Tree Shaking and Side Effects