将 UglifyJS 与 Preact、Webpack 2 一起使用时出现意外令牌错误
Unexpected Token error using UglifyJS with Preact, Webpack 2
我正在使用 Preact/ES6/Webpack 构建一个 Chrome 扩展。我使用两种配置之一进行打包: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
代码,生成的压缩扩展效果很好。
我缺少什么配置来消除这些错误?
事实证明当前(5/2017)内置的webpack.optimize.UglifyJsPlugin
不支持ES6。当 Babel 转译 await/async
时,它会将它们变成 generator
s,这会导致 UglifyJS
抛出错误。
this article 中列出了 UglifyJS
的几个替代方案,但我希望 Webpack 人员会更新插件,这样我就可以完整地保留我的代码。
TL;DR: 我的代码没问题; UglifyJS
不支持ES6;以后会支持,或者会被alternative替代。
为了 ES6 兼容性,我已将 uglify-js
替换为 uglify-es
:
https://www.npmjs.com/package/uglify-es
对我来说很好用!
我正在使用 Preact/ES6/Webpack 构建一个 Chrome 扩展。我使用两种配置之一进行打包: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
代码,生成的压缩扩展效果很好。
我缺少什么配置来消除这些错误?
事实证明当前(5/2017)内置的webpack.optimize.UglifyJsPlugin
不支持ES6。当 Babel 转译 await/async
时,它会将它们变成 generator
s,这会导致 UglifyJS
抛出错误。
this article 中列出了 UglifyJS
的几个替代方案,但我希望 Webpack 人员会更新插件,这样我就可以完整地保留我的代码。
TL;DR: 我的代码没问题; UglifyJS
不支持ES6;以后会支持,或者会被alternative替代。
为了 ES6 兼容性,我已将 uglify-js
替换为 uglify-es
:
https://www.npmjs.com/package/uglify-es
对我来说很好用!