webpack 4禁用uglifyjs-webpack-plugin

时间:2018-07-10 11:00:21

标签: webpack uglifyjs webpack-4 webpack-plugin

最近2天内我遇到了this问题。因此,我决定从Webpack构建过程中完全禁用 uglifyjs-webpack-plugin 。我在webpack 4上找不到任何东西。

2 个答案:

答案 0 :(得分:20)

module.exports = {
    optimization:{
        minimize: false, // <---- disables uglify.
        // minimizer: [new UglifyJsPlugin()] if you want to customize it.
    }
}

答案 1 :(得分:0)

如果您要管理单个webpack.config.js并根据环境使用package.json npmscripts。您也可以使用这种方法。

您可以执行以下操作:创建一个defaultplugins数组并检查环境,如果环境是prod,则推送到数组,否则使用defaultplugins。如示例所示:

package.json

"config-prod": "webpack --env.NODE_ENV=prod --parallel build-webpack",
"build-prod": "npm run config-prod"

webpack.config.js 仅添加了相关部分,因此易于阅读

var webpack = require("webpack");
var path = require("path");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpackUtilities = require("./webpack.utilities");

module.exports = (env) => {
  var defaultplugins = [
    new webpack.DefinePlugin({
       ///// section deleted
    }),
    new MiniCssExtractPlugin({
      ///// section deleted
    }),
  ];

  return {
    mode: env.NODE_ENV == "prod" ? "production" : "development",
    devtool: env.NODE_ENV == "prod" ? "" : "source-map";,
    entry: {
      ///// section deleted
    },
    output: {
         ///// section deleted
    },
    module: {
      rules: [
        {
          ///// section deleted
        },
      ],
    },
    plugins:
      env.NODE_ENV == "prod"
        ? [...defaultplugins, new UglifyJSPlugin()]
        : [...defaultplugins],
    resolve: {
      extensions: [".js", ".jsx", ".scss", ".css"],
    },
  };
};