Back to Webpack Js Org

Mode

src/content/configuration/mode.mdx

4.41.22.6 KB
Original Source

Providing the mode configuration option tells webpack to use its built-in optimizations accordingly.

string = 'production': 'none' | 'development' | 'production'

Usage

Provide the mode option in the config:

js
export default {
  mode: "development",
};

or pass it as a CLI argument:

bash
webpack --mode=development

The following string values are supported:

OptionDescription
developmentSets process.env.NODE_ENV on DefinePlugin to value development. Enables useful names for modules and chunks.
productionSets process.env.NODE_ENV on DefinePlugin to value production. Enables deterministic mangled names for modules and chunks, FlagDependencyUsagePlugin, FlagIncludedChunksPlugin, ModuleConcatenationPlugin, NoEmitOnErrorsPlugin and TerserPlugin.
noneOpts out of any default optimization options

If not set, webpack sets production as the default value for mode.

T> If mode is not provided via configuration or CLI, CLI will use any valid NODE_ENV value for mode.

Mode: development

js
// webpack.development.config.js
export default {
  mode: "development",
};

Mode: production

js
// webpack.production.config.js
export default {
  mode: "production",
};

Mode: none

js
// webpack.custom.config.js
export default {
  mode: "none",
};

If you want to change the behavior according to the mode variable inside the webpack.config.js, you have to export a function instead of an object:

js
const config = {
  entry: "./app.js",
  // ...
};

export default (env, argv) => {
  if (argv.mode === "development") {
    config.devtool = "source-map";
  }

  if (argv.mode === "production") {
    // ...
  }

  return config;
};