Javascript Webpack 4 - 如何配置最小化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49053215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:18:47  来源:igfitidea点击:

Webpack 4 - How to configure minimize?

javascriptwebpackwebpack-4

提问by csvan

Webpack 4 comes with the following statement:

Webpack 4 附带以下声明:

webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.

webpack.optimize.UglifyJsPlugin 已被移除,请使用 config.optimization.minimize 代替。

Fair enough, but I cannot find any information about configuring the UglifyJsPlugin instance running under the hood, for example to change the cache directory. Can this be done?

很公平,但我找不到有关配置在后台运行的 UglifyJsPlugin 实例的任何信息,例如更改缓存目录。这能做到吗?

采纳答案by Beau

It's not possible to modify the default configuration.

无法修改默认配置。

You can use the optimization.minimizersetting to instantiate your own UglifyJsPlugin, however. Using 4.0 we used this example to get source maps even when modeis set to 'production'for example (no longer necessary as of 4.1.1):

但是,您可以使用该optimization.minimizer设置来实例化您自己的UglifyJsPlugin。使用 4.0 我们使用这个例子来获取源映射,即使mode设置'production'为例如(从 4.1.1 开始不再需要):

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  optimization: {
    minimizer: [
      // we specify a custom UglifyJsPlugin here to get source maps in production
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        uglifyOptions: {
          compress: false,
          ecma: 6,
          mangle: true
        },
        sourceMap: true
      })
    ]
  }
};

回答by Nafis

Without adding uglifyjs-webpack-plugin, you can just add this to the end of your webpack.prod.config.jsfile:

无需添加uglifyjs-webpack-plugin,您只需将其添加到webpack.prod.config.js文件的末尾即可

 optimization: {
   minimize: false
 }

回答by Sarath Ak

You can try this

你可以试试这个

npm install uglifyjs-webpack-plugin --save-dev

webpack.config.js

webpack.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  optimization: {
    minimizer: [new UglifyJsPlugin()],
  },
};

webpack documentation

网络包文档

回答by Rafael Corrêa Gomes

Just run:

赶紧跑:

yarn add uglifyjs-webpack-plugin --dev

Reference: Alfonso Pérezanswer

参考:Alfonso Pérez回答

回答by Tanner Hallman

For those coming behind me, realized this misleading error was not related to my correct webpack config, but actually, the offline-pluginwas out of date and causing this issue. It needed to be upgraded. See github issue: https://github.com/webpack-contrib/uglifyjs-webpack-plugin/issues/234#issuecomment-369134047

对于那些跟在我后面的人,意识到这个误导性错误与我正确的 webpack 配置无关,但实际上,它offline-plugin已经过时并导致了这个问题。它需要升级。见github问题:https: //github.com/webpack-contrib/uglifyjs-webpack-plugin/issues/234#issuecomment-369134047

回答by KorHosik

You should check poption: https://webpack.js.org/guides/production/#cli-alternatives: this flag tells Webpack to optimize your build for production environment. You can use it with the new "production" modefor a smaller build.

您应该检查p选项:https://webpack.js.org/guides/production/#cli-alternatives:此标志告诉 Webpack 为生产环境优化您的构建。您可以将它与新的“生产”一起mode用于较小的构建。