ERROR from UglifyJs: SyntaxError: Unexpected token: operator (>)

ERROR from UglifyJs: SyntaxError: Unexpected token: operator (>)

我在尝试 运行 我的 webpack 用于生产时遇到错误。

ERROR in js/main.21dbce548a76ffc14cfb.js from UglifyJs
SyntaxError: Unexpected token: operator (>) [./~/tmi.js/lib/utils.js:3,0][js/main.21dbce548a76ffc14cfb.js:3529,20]

utils.js:3,0(与我缩小后的 js 相同)是:

// Return the second value if the first value is undefined..
    get: (obj1, obj2) => { return typeof obj1 === "undefined" ? obj2 : obj1; },

所以我假设抛出错误是因为它正在读取 ES6 但它不理解 ES6? (箭头函数)

我没看出哪里出了问题,这是我的 webpack.config.js

// changed some loader syntax after reading
// https://webpack.js.org/how-to/upgrade-from-webpack-1/

const path = require(`path`);

const webpack = require(`webpack`);
const {UglifyJsPlugin} = webpack.optimize;

const CopyWebpackPlugin = require(`copy-webpack-plugin`);
const ExtractTextWebpackPlugin = require(`extract-text-webpack-plugin`);
const configHtmls = require(`webpack-config-htmls`)();

const extractCSS = new ExtractTextWebpackPlugin(`css/style.css`);

// change for production build on different server path
const publicPath = `/`;

// hard copy assets folder for:
// - srcset images (not loaded through html-loader )
// - json files (through fetch)
// - fonts via WebFontLoader

const copy = new CopyWebpackPlugin([{
  from: `./src/assets`,
  to: `assets`
}], {
  ignore: [ `.DS_Store` ]
});

const config = {

  entry: [
    `./src/css/style.css`,
    `./src/js/script.js`
  ],

  resolve: {
    // import files without extension import ... from './Test'
    extensions: [`.js`, `.jsx`, `.css`]
  },

  output: {
    path: path.join(__dirname, `server`, `public`),
    filename: `js/[name].[hash].js`,
    publicPath
  },

  devtool: `sourcemap`,

  module: {

    rules: [
      {
        test: /\.css$/,
        loader: extractCSS.extract([
          {
            loader: `css`,
            options: {
              importLoaders: 1
            }
          },
          {
            loader: `postcss`
          }
        ])
      },
      {
        test: /\.html$/,
        loader: `html`,
        options: {
          attrs: [
            `audio:src`,
            `img:src`,
            `video:src`,
            `source:srcset`
          ] // read src from video, img & audio tag
        }
      },
      {
        test: /\.(jsx?)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: `babel`
          },
          {
            loader: `eslint`,
            options: {
              fix: true
            }
          }
        ]
      },
      {
        test: /\.(svg|png|jpe?g|gif|webp)$/,
        loader: `url`,
        options: {
          limit: 1000, // inline if < 1 kb
          context: `./src`,
          name: `[path][name].[ext]`
        }
      },
      {
        test: /\.(mp3|mp4)$/,
        loader: `file`,
        options: {
          context: `./src`,
          name: `[path][name].[ext]`
        }
      }
    ]

  },

  plugins: [
    extractCSS,
    copy
  ]

};

if(process.env.NODE_ENV === `production`){

  //image optimizing
  config.module.rules.push({
    test: /\.(svg|png|jpe?g|gif)$/,
    loader: `image-webpack`,
    enforce: `pre`
  });

  config.plugins = [
    ...config.plugins,
    new UglifyJsPlugin({
      sourceMap: true, // false returns errors.. -p + plugin conflict
      comments: false
    })
  ];

}

config.plugins = [...config.plugins, ...configHtmls.plugins];

module.exports = config;

UglifyJs2 有一个 Harmony 分支,它接受要缩小的 ES6 语法。此时,你需要创建一个webpack的fork,并将webpack指向那个fork。

我最近回答了几个类似的问题。请查看 or 了解详细说明。

OP 的错误来自 UglifyJs,正如在接受的答案中解决的那样,此页面的某些人可能会从 babel 获得错误,在这种情况下,修复它:将 "presets": ["es2015"] 添加到 options.presets babel-loader 部分,或者到 .babelrc.

在我的例子中,我使用的是 webpack 版本 1.14 我得到了 git ref

的帮助

步骤:

  1. 安装 yarn add uglifyes-webpack-plugin(并删除 yarn remove uglifyjs-webpack-plugin
  2. 然后安装yarn add uglify-js-es6
  3. 在 webpack.config.js 文件中将 new webpack.optimize.UglifyJsPlugin 更改为 new UglifyJsPlugin

然后我就可以构建了。谢谢