1

我正在使用 Preact/ES6/Webpack 构建一个 Chrome 扩展。我使用 2 种配置之一进行打包:debug使用 ESLint、Babel 和 CSS + JSON 加载器,prod添加 2 个插件:UglifyJS和 Zip。这是webpack.config.js

const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const WebpackCleanupPlugin = require('webpack-cleanup-plugin');
const ZipPlugin = require('zip-webpack-plugin');
const manifest = require('./src/manifest');

let options = {
    // entry file - starting point for the app
    entry: {
    popup: './src/scripts/popup.js',
    options: './src/scripts/options.js',
    background: './src/scripts/background.js'
  },

    // where to dump the output of a production build
    output: {
    path: path.join(__dirname, 'build'),
    filename: 'scripts/[name].js'
  },

    module: {
        rules: [
      {
        test: /\.jsx?/i,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
        },
        enforce: 'pre'
    },
    {
        test: /\.jsx?/i,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
            presets: [
            ['env', {
              'targets': {
                'chrome': 52
              }
            }]
          ],
            plugins: [
            ['transform-react-jsx'],
            ['module-resolver', {
              'root': ['.'],
              'alias': {
                'react': 'preact-compat',
                'react-dom': 'preact-compat'
              }
            }]
                    ]
                }
            },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.json$/,
        use: 'json-loader'
      }
        ]
    },

  plugins: [
    new WebpackCleanupPlugin(),
    new CopyWebpackPlugin([
      {from: './src/_locales', to: '_locales'},
      {from: './src/icons', to: 'icons'},
      {from: './src/manifest.json', flatten: true},
      {from: './src/*.html', flatten: true}
    ])
  ],

    // enable Source Maps
    devtool: 'source-map',
};

if(process.env.NODE_ENV === 'production') {
  options.plugins.push(
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
        drop_console: false,
        screw_ie8: true,
        conditionals: true,
        unused: true,
        comparisons: true,
        sequences: true,
        dead_code: true,
        evaluate: true,
        if_return: true,
        join_vars: true,
      },
      output: {
        comments: false,
      },
    }),
    new ZipPlugin({
      path: '../dist',
      filename: `${manifest.name} ${manifest.version}.zip`
    })
  );
}

console.log(`This is a ${process.env.NODE_ENV === 'production' ? 'production' : 'development'} build with ${options.plugins.length} plugins`);

module.exports = options;

但是当我运行时prod,我得到以下错误:

ERROR in scripts/popup.js from UglifyJs
Unexpected token: name (getLatestValues) [scripts/popup.js:29428,4]

ERROR in scripts/options.js from UglifyJs
Unexpected token: name (getLatestValues) [scripts/options.js:29428,4]

ERROR in scripts/background.js from UglifyJs
Unexpected token: name (getLatestValues) [scripts/background.js:28678,4]

值得一提的getLatestResults是,这并不是我的代码中唯一在await它前面的函数。此外,它应该只出现在background.js其他入口点不应该调用它的情况下。

另外值得一提的是,如果我只是对UglifyJS代码进行注释,生成的压缩扩展名效果很好。

我缺少什么配置来使这些错误消失?

4

2 回答 2

1

事实证明,目前(5/2017)内置webpack.optimize.UglifyJsPlugin不支持 ES6。当 Babel 编译await/async时,它会将它们转换为generators,这会导致UglifyJS抛出错误。

本文UglifyJS中列出了几种替代方案,但我希望 Webpack 人员能够更新插件,并且我能够保持我的代码完好无损。

TL;DR:我的代码没问题;UglifyJS不支持 ES6;将来会支持,或将被替代品取代。

于 2017-05-15T22:16:14.187 回答
0

我已经替换uglify-jsuglify-esES6 兼容性:

https://www.npmjs.com/package/uglify-es

对我来说很好!

于 2018-12-11T10:08:37.273 回答