Javascript 将UglifyJS与Preact、网页包2一起使用时出现意外令牌错误
我正在用Preact/ES6/Webpack构建一个Chrome扩展。我使用两种配置之一进行打包:Javascript 将UglifyJS与Preact、网页包2一起使用时出现意外令牌错误,javascript,babeljs,webpack-2,uglifyjs,preact,Javascript,Babeljs,Webpack 2,Uglifyjs,Preact,我正在用Preact/ES6/Webpack构建一个Chrome扩展。我使用两种配置之一进行打包:debug使用ESLint、Babel和CSS+JSON加载程序,prod添加了两个插件:UglifyJS和Zip。下面是webpack.config.js: const webpack = require('webpack'); const path = require('path'); const CopyWebpackPlugin = require('copy-webpack-plugin'
debug
使用ESLint、Babel和CSS+JSON加载程序,prod
添加了两个插件: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
并不是我的代码中唯一前面有wait
的函数。另外,它应该只出现在background.js
中,因为其他入口点不应该调用它
另外值得一提的是,如果我只对UglifyJS
代码进行注释,那么得到的压缩扩展工作得很好
为了消除这些错误,我缺少了什么配置块?结果是目前(2017年5月)内置的
webpack.optimize.UglifyJsPlugin
不支持ES6。当Babel Transilesawait/async
时,它会将它们转换为generator
s,从而导致UglifyJS
抛出错误
中列出的UglifyJS
有几种替代方法,但我希望Webpack的人会更新插件,这样我就可以让我的代码完好无损了
TL;DR:我的代码正常
UglifyJS
不支持ES6;将来将支持,或将被替代品取代。为了ES6兼容性,我已将uglify js
替换为uglify es
:
对我来说很好 看起来丑八怪不知怎么跑在巴贝尔前面。或者它不喜欢巴贝尔的输出格式。什么是
async
/await
编译到的生成器?我猜uglify不知道ES6yield
关键字。我相信它会被翻译成generators
。但是,我的代码中还有其他使用async/await
的位置,它们由于某种原因没有被调用。(除非Babel以不同的方式实现不同的调用,即类方法与常规函数)。