Webpack 4.1.1和UglifyJS2的问题 - 生产模式破坏了引发未捕获错误的代码

时间:2018-03-15 01:20:10

标签: reactjs webpack codemirror uglifyjs

我已将项目升级到Webpack 4.1.1并遇到了一个我似乎无法解决的UglifyJS错误。

此处的最低可重现回购:https://github.com/jamesopti/uglifyjs-webpack-issue

虽然我认为这是一个错误,但我想尝试配置UglifyJS来解决它。到目前为止没有运气,尽管如下所示。

uglifyOptions: {
  mangle: false,
  keep_classnames: true,
  keep_fnames: true,
},

有什么想法吗?

Main.js代码:

import React from 'react';

import { render } from 'react-dom';
import ReactCodeMirror from 'react-codemirror';

// Enables syntax highlighting for javascript
require('codemirror/mode/javascript/javascript');
// Enables linting for javascript
require('codemirror/addon/lint/lint');
require('codemirror/addon/lint/javascript-lint');

window.JSHINT = require('jshint').JSHINT;

const defaultOptions = {
  lint: true,
};

render(
  <ReactCodeMirror
    options={defaultOptions}
    value={'some\ncode'}
  />, document.querySelector('#root')
);

Webpack配置:

  entry: './src/main.js',

  mode: 'development',

  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },

  module: {
    rules: [
      { test: /\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader' } ] },
      { test: [/\/src\/(?:.*)\.js$/], use: { loader: 'babel-loader' } },
    ]
  },

  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        uglifyOptions: {
          mangle: false,
          keep_classnames: true,
          keep_fnames: true,
        },
      }),
    ]
  }

Error rendering component

1 个答案:

答案 0 :(得分:0)

这是UglifyJS中的一个错误(报告here)。

最小可重现代码:

(function(mod) {
  mod();
})(function() {
  function getMaxSeverity(ax, bx) {
    if (ax === "error") {
      return ax;
    } else {
      return bx;
    }
  }
  function main() {
    var arr = ['hey'];
    for (var i = 0; i < arr.length; i++) {
      console.log(getMaxSeverity('one', 'two'));
    }
  }
  main();
});

解决方法是压缩内联选项

optimization: {
  minimizer: [
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          inline: 1,
        }
      }
    })
  ]
}