UglifyJs抛出错误:无法将undefined或null转换为对象

时间:2018-06-12 10:38:59

标签: javascript webpack uglifyjs

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

  

无法将undefined或null转换为对象

     

来自UglifyJs的app.min.js中的错误TypeError:无法转换undefined   或null对象       在 defineSetter ()       在AST_Dot.eval [as _eval](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :9960:67)       在AST_Dot.eval [作为评估](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :9761:28)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :13531:23)       在AST_Dot.eval [as optimize](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7677:23)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7660:24)       在AST_Dot.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4687:43)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7655:9)       在AST_Call.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4598:31)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7655:9)       在AST_SimpleStatement.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)       在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)       在MAP(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :156:52)       在do_list(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4585:16)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4682:25)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7655:9)       在AST_Function.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)       在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)       在MAP(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :156:52)       在do_list(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4585:16)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4688:21)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7655:9)       在AST_Call.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4713:43)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7655:9)       在AST_UnaryPrefix.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4718:33)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7655:9)       在AST_Binary.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)       在eval(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4718:33)       在Compressor.before(eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :7655:9)       在AST_Assign.eval [as transform](eval at(... / node_modules / uglifyjs-webpack-plugin / node_modules / uglify-es / tools / node.js:21:1),   :4570:35)儿童html-webpack-plugin for" index.html":        1资产       入口点undefined = index.html       [0] ./node_modules/html-webpack-plugin/lib/loader.js!。/ index-template.ejs   651字节{0} [已建]       [2](webpack)/buildin/global.js 489字节{0} [已建]       [3](webpack)/buildin/module.js 497字节{0} [已建]           + 1隐藏模块npm ERR!代码ELIFECYCLE npm ERR!错误2 npm ERR! neon@0.0.0 prod:webpack -p npm ERR!退出状态2 npm ERR!   错误的ERR!在neon@0.0.0 prod脚本失败。错误的ERR!这是   可能不是npm的问题。可能还有其他日志记录   以上输出。

     

npm ERR!可以在以下位置找到此运行的完整日志: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

2 个答案:

答案 0 :(得分:0)

删除pure_getters或将pure_getters作为“严格”

传递
  

pure_getters(默认值:“strict”) - 如果为此传递true,UglifyJS将假定对象属性访问(例如foo.bar或foo [“bar”])没有任何副作用。指定“strict”以仅在foo确定不抛出时将foo.bar视为无副作用,即不为null或未定义。

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

答案 1 :(得分:0)

我移动了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
      }),