UglifyJs throws error: Cannot convert undefined or null to object

UglifyJs throws error: Cannot convert undefined or null to object

我尝试了上一个 Webpack 版本,但是当我使用 UglifyJsPlugin 时, 我收到错误:

Cannot convert undefined or null to object

ERROR in app.min.js from UglifyJs TypeError: Cannot convert undefined or null to object at defineSetter () at AST_Dot.eval [as _eval] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :9960:67) at AST_Dot.eval [as evaluate] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :9761:28) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :13531:23) at AST_Dot.eval [as optimize] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7677:23) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7660:24) at AST_Dot.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4687:43) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7655:9) at AST_Call.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4598:31) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7655:9) at AST_SimpleStatement.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4586:25) at doit (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :130:23) at MAP (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :156:52) at do_list (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4585:16) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4682:25) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7655:9) at AST_Function.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4586:25) at doit (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :130:23) at MAP (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :156:52) at do_list (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4585:16) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4688:21) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7655:9) at AST_Call.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4713:43) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7655:9) at AST_UnaryPrefix.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4718:33) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7655:9) at AST_Binary.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) at eval (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4718:33) at Compressor.before (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :7655:9) at AST_Assign.eval [as transform] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4570:35) Child html-webpack-plugin for "index.html": 1 asset Entrypoint undefined = index.html [0] ./node_modules/html-webpack-plugin/lib/loader.js!./index-template.ejs 651 bytes {0} [built] [2] (webpack)/buildin/global.js 489 bytes {0} [built] [3] (webpack)/buildin/module.js 497 bytes {0} [built] + 1 hidden module npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! neon@0.0.0 prod: webpack -p npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the neon@0.0.0 prod script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR!
.../.npm/_logs/2018-06-12T10_12_21_278Z-debug.log

webpack.config.js

const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const path = require('path');
const WebpackNotifierPlugin = require('webpack-notifier');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const TARGET = process.env.npm_lifecycle_event;
console.log(`target event is ${TARGET}`);

let outputFileName = 'app';
outputFileName += TARGET === 'prod' ? '.min.js' : '.js';

const common = {
  entry: ['babel-polyfill', './index.jsx'],
  output: {
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.js[x]?$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
  plugins: [
    new webpack.ProvidePlugin({
      jQuery: 'jquery',
      $: 'jquery',
      jquery: 'jquery',
      'window.jQuery': 'jquery',
    }),
    new WebpackNotifierPlugin(),
  ],
  resolve: {
    modules: [
      path.resolve('.'),
      path.resolve('script'),
      path.resolve('script', 'views'),
      'node_modules',
    ],
    extensions: ['.js', '.jsx', '.json'],
  },
};

if (TARGET === 'prod' || !TARGET) {
  module.exports = webpackMerge(common, {
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: outputFileName,
    },
    module: {
      rules: [
        {
          test: /\.scss$/,
          loaders: [
            'style-loader',
            'css-loader',
            {
              loader: 'postcss-loader',
              options: {
                config: {
                  path: 'postcss.config.js',
                },
              },
            },
            'sass-loader',
          ],
        },
        {
          test: /\.less$/,
          loaders: ['style-loader', 'css-loader', 'less-loader'],
        },
        {
          test: /\.css$/,
          use: ['style-loader', 'css-loader'],
        },
        {
          test: /\.(eot|ttf|svg|gif|png|jpg|otf|woff|woff2)$/,
          loader: 'file-loader',
        },
      ],
    },
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          uglifyOptions: {
            parallel: true,
            compress: {
              warnings: false, // Suppress uglification warnings
              pure_getters: true,
              unsafe: true,
              unsafe_comps: true,
              conditionals: true,
              unused: true,
              comparisons: true,
              sequences: true,
              dead_code: true,
              evaluate: true,
              if_return: true,
              join_vars: true,
            },
            output: {
              comments: false,
            },
            exclude: [/\.min\.js$/gi], // skip pre-minified libs
          },
        }),
      ],
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify('production'),
      }),
      new HtmlWebpackPlugin({
        title: 'test-project',
        template: 'index-template.ejs',
      }),
      new webpack.optimize.AggressiveMergingPlugin(),
      new webpack.optimize.OccurrenceOrderPlugin(),
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      new CompressionPlugin({
        asset: '[path].gz[query]',
        algorithm: 'gzip',
        test: /\.js$|\.css$|\.html$/,
        threshold: 10240,
        minRatio: 0,
      }),
    ],
  });
}

package.json

"dependencies": {
    "axios": "^0.18.0",
    "bignumber.js": "^7.2.1",
    "bootstrap": "^4.1.1",
    "classnames": "^2.2.5",
    "font-awesome": "^4.7.0",
    "jquery": "^3.3.1",
    "koa": "^2.5.1",
    "koa-send": "^4.1.3",
    "koa-static": "^4.0.3",
    "lodash": "^4.17.10",
    "moment": "^2.22.1",
    "popper.js": "^1.14.3",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-redux": "^5.0.7",
    "react-slidedown": "^1.3.0",
    "react-toastify": "^4.1.0",
    "redux": "^4.0.0",
    "redux-saga": "^0.16.0"
  },
  "devDependencies": {
    "autoprefixer": "^8.4.1",
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-eslint": "^8.2.3",
    "babel-loader": "^7.1.4",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "compression-webpack-plugin": "^1.1.11",
    "css-loader": "^0.28.11",
    "duplicate-package-checker-webpack-plugin": "^3.0.0",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-plugin-import": "^2.11.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.7.0",
    "file-loader": "^1.1.11",
    "friendly-errors-webpack-plugin": "^1.7.0",
    "html-webpack-plugin": "^3.2.0",
    "http-server": "^0.11.1",
    "less": "^3.0.2",
    "less-loader": "^4.1.0",
    "node-sass": "^4.9.0",
    "nodemon": "^1.17.5",
    "npm-install-webpack-plugin": "^4.0.5",
    "postcss": "^6.0.22",
    "postcss-loader": "^2.1.4",
    "redux-devtools-extension": "^2.13.2",
    "sass-loader": "^7.0.1",
    "sass-resources-loader": "^1.3.3",
    "style-loader": "^0.21.0",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "url-loader": "^1.0.1",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.1.2",
    "webpack-dev-server": "^3.1.4",
    "webpack-merge": "^4.1.2",
    "webpack-notifier": "^1.6.0"
  }

项目结构:

project
├── script
│   ├── actions
│   ├── components
│   ├── constants
│   ├── sagas
│   ├── state
│   ├── utils
│   ├── views
│   └── server.js
├── postcss.config.js
├── index.html
├── index.jsx
├── postcss.config.js
├── webpack.config.js

删除 pure_getters 或将 pure_getters 作为 "strict"

传递

pure_getters (default: "strict") -- If you pass true for this, UglifyJS will assume that object property access (e.g. foo.bar or foo["bar"]) doesn't have any side effects. Specify "strict" to treat foo.bar as side-effect-free only when foo is certain to not throw, i.e. not null or undefined.

https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options

我移动了 exclude 选项并且有效:

new UglifyJsPlugin({
        uglifyOptions: {
          compress: {
            warnings: false, // Suppress uglification warnings
            unsafe: true,
            unsafe_comps: true,
            conditionals: true,
            unused: true,
            comparisons: true,
            sequences: true,
            dead_code: true,
            evaluate: true,
            if_return: true,
            join_vars: true,
          },
          output: {
            comments: false,
          },
        },
        exclude: [/\.min\.js$/gi], // skip pre-minified libs
      }),