【问题标题】:UglifyJs throws error: Cannot convert undefined or null to objectUglifyJs 抛出错误:无法将 undefined 或 null 转换为对象
【发布时间】:2018-11-21 17:35:19
【问题描述】:

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

无法将 undefined 或 null 转换为对象

来自 UglifyJs TypeError 的 app.min.js 中的错误:无法转换未定义 或 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 [as evaluate] (eval at (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :9761:28) 在评估(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :13531:23) 在 AST_Dot.eval [作为优化](评估在 (.../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) 在评估(评估在(.../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) 在评估(评估在(.../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) 在评估(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4586:25) 在 doit(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :130:23) 在 MAP(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :156:52) 在do_list(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4585:16) 在评估(评估在(.../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) 在评估(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4586:25) 在 doit(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :130:23) 在 MAP(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :156:52) 在do_list(评估在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1), :4585:16) 在评估(评估在(.../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) 在评估(评估在(.../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) 在评估(评估在(.../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) 在评估(评估在(.../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)“index.html”的子 html-webpack-plugin: 1 项资产 入口点未定义 = 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 错误! errno 2 npm 错误! neon@0.0.0 产品:webpack -p npm 错误!退出状态 2 npm ERR! npm 错误!在 neon@0.0.0 prod 脚本中失败。 npm 错误!这是 npm 可能不是问题。可能有额外的日志记录 上面的输出。

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

【问题讨论】:

    标签: javascript webpack uglifyjs


    【解决方案1】:

    删除pure_getters 或将pure_getters 传递为“严格”

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

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

    【讨论】:

    • 感谢您的回复。我试了一下,也遇到了同样的错误。
    • 浏览器执行压缩脚本时遇到问题can not create property return on number。当我删除pure_getters 时,它工作正常。你知道为什么会这样吗?非常感谢。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-08-15
      • 2021-07-29
      • 2020-07-19
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 2023-02-06
      • 2020-03-28
      相关资源
      最近更新 更多