模式(mode)

提供 mode 配置选项,告知 webpack 使用相应环境的内置优化。

string

可能的值有:none, developmentproduction(默认)。

用法

只需在配置对象中提供 mode 选项:

  1. module.exports = {
  2. mode: 'production'
  3. };

或者从 CLI 参数中传递:

  1. webpack --mode=production

支持以下字符串值:

选项

描述

选项

development

描述

会将 DefinePluginprocess.env.NODE_ENV 的值设置为 development。启用 NamedChunksPluginNamedModulesPlugin

选项

production

描述

会将 DefinePluginprocess.env.NODE_ENV 的值设置为 production。启用 FlagDependencyUsagePlugin, FlagIncludedChunksPlugin, ModuleConcatenationPlugin, NoEmitOnErrorsPlugin, OccurrenceOrderPlugin, SideEffectsFlagPluginTerserPlugin

选项

none

描述

退出任何默认优化选项

如果没有设置,webpack 会将 mode 的默认值设置为 production。模式支持的值为:

记住,设置 NODE_ENV 并不会自动地设置 mode

mode: development

  1. // webpack.development.config.js
  2. module.exports = {
  3. + mode: 'development'
  4. - devtool: 'eval',
  5. - cache: true,
  6. - performance: {
  7. - hints: false
  8. - },
  9. - output: {
  10. - pathinfo: true
  11. - },
  12. - optimization: {
  13. - namedModules: true,
  14. - namedChunks: true,
  15. - nodeEnv: 'development',
  16. - flagIncludedChunks: false,
  17. - occurrenceOrder: false,
  18. - sideEffects: false,
  19. - usedExports: false,
  20. - concatenateModules: false,
  21. - splitChunks: {
  22. - hidePathInfo: false,
  23. - minSize: 10000,
  24. - maxAsyncRequests: Infinity,
  25. - maxInitialRequests: Infinity,
  26. - },
  27. - noEmitOnErrors: false,
  28. - checkWasmTypes: false,
  29. - minimize: false,
  30. - },
  31. - plugins: [
  32. - new webpack.NamedModulesPlugin(),
  33. - new webpack.NamedChunksPlugin(),
  34. - new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("development") }),
  35. - ]
  36. }

mode: production

  1. // webpack.production.config.js
  2. module.exports = {
  3. + mode: 'production',
  4. - performance: {
  5. - hints: 'warning'
  6. - },
  7. - output: {
  8. - pathinfo: false
  9. - },
  10. - optimization: {
  11. - namedModules: false,
  12. - namedChunks: false,
  13. - nodeEnv: 'production',
  14. - flagIncludedChunks: true,
  15. - occurrenceOrder: true,
  16. - sideEffects: true,
  17. - usedExports: true,
  18. - concatenateModules: true,
  19. - splitChunks: {
  20. - hidePathInfo: true,
  21. - minSize: 30000,
  22. - maxAsyncRequests: 5,
  23. - maxInitialRequests: 3,
  24. - },
  25. - noEmitOnErrors: true,
  26. - checkWasmTypes: true,
  27. - minimize: true,
  28. - },
  29. - plugins: [
  30. - new TerserPlugin(/* ... */),
  31. - new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }),
  32. - new webpack.optimize.ModuleConcatenationPlugin(),
  33. - new webpack.NoEmitOnErrorsPlugin()
  34. - ]
  35. }

mode: none

  1. // webpack.custom.config.js
  2. module.exports = {
  3. + mode: 'none',
  4. - performance: {
  5. - hints: false
  6. - },
  7. - optimization: {
  8. - flagIncludedChunks: false,
  9. - occurrenceOrder: false,
  10. - sideEffects: false,
  11. - usedExports: false,
  12. - concatenateModules: false,
  13. - splitChunks: {
  14. - hidePathInfo: false,
  15. - minSize: 10000,
  16. - maxAsyncRequests: Infinity,
  17. - maxInitialRequests: Infinity,
  18. - },
  19. - noEmitOnErrors: false,
  20. - checkWasmTypes: false,
  21. - minimize: false,
  22. - },
  23. - plugins: []
  24. }

如果要根据 webpack.config.js 中的 mode 变量更改打包行为,则必须将配置导出为一个函数,而不是导出为一个对象:

  1. var config = {
  2. entry: './app.js'
  3. //...
  4. };
  5. module.exports = (env, argv) => {
  6. if (argv.mode === 'development') {
  7. config.devtool = 'source-map';
  8. }
  9. if (argv.mode === 'production') {
  10. //...
  11. }
  12. return config;
  13. };

进一步阅读


贡献人员

EugeneHlushko EugeneHlushko Fental Fental byzyk byzyk mrichmond mrichmond