Uglify SyntaxError: Unexpected token: punc ())
我正在尝试使用 gulp 来缩小包含 JS 文件的文件夹.但是,其中一个文件存在上述错误,导致无法对其进行缩小.
I am trying to use gulp in order to minify a folder containing JS files. However, one of the files has the above error, preventing it from being minified.
我设法捕获并打印了错误,我在此处部分打印了该错误:
I managed to catch and print the error, which I've partially printed here:
JS_Parse_Error {
message: 'SyntaxError: Unexpected token: punc ())',
filename: 'ex.js',
line: 189,
col: 25,
pos: 6482,
stack: Error
at new JS_Parse_Error (eval at <anonymous> ... )
plugin: 'gulp-uglify',
fileName: '.../js/ex.js',
showStack: false
}
相关文件包含以下内容,已缩短:
The file in question contains the following, shortened:
function() {
...
$.confirm({
buttons: {
confirm: function() {
$.post('/ajax-handler', {
...
})
.done( function(response) {
var data = filterResponse(response);
if (data['status'] == 'success') {
sleep(1000).then(() => {
* ...
});
sleep(5000).then(() => {
...
});
} else {
console.log('Oops!');
}
})
.fail( function(err, status, response) {
...
});
},
cancel: function() {}
}
});
...
}
我在上面添加了*"以指示 JS_Parse_Error 列出的确切位置.
I added the "*" above in order to indicate the exact position listed by JS_Parse_Error.
推荐答案
//更新
来自评论~ @imolit
切换回 uglify-js(uglify-es 已弃用,如果需要 uglify ES6 代码请使用 terser-webpack-plugin).
v2.0.0 (2018-09-14) - BREAKING CHANGES (link)
Switch back to uglify-js (uglify-es is abandoned, if you need uglify ES6 code please use terser-webpack-plugin).
<小时>
我希望你能从这个适用于 webpack 的解决方案中得到启发.(以下链接)
I hope you can get inspired by this solution which works with webpack. (link below)
UglifyJS 有两个版本 - ES5 和 ES6 (Harmony),在 git 上查看
ES5 版本默认包含所有插件,但如果您明确安装 Harmony 版本,这些插件将使用它.
There are two versions of UglifyJS - ES5 and ES6 (Harmony), see on git
ES5 version comes by default with all the plugins, but if you install a Harmony version explicitly, those plugins will use it instead.
package.json
package.json
"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"
或
npm install --save uglify-js@github:mishoo/UglifyJS2#harmony
yarn add git://github.com/mishoo/UglifyJS2#harmony --dev
<小时>
网页包
要与 webpack 一起使用,还要安装 webpack 插件
Webpack
To use it with webpack install also the webpack plugin
npm install uglifyjs-webpack-plugin --save-dev
yarn add uglifyjs-webpack-plugin --dev
然后导入手动安装的插件
then import the manually installed plugin
var UglifyJSPlugin = require('uglifyjs-webpack-plugin');
并在代码中替换它
- new webpack.optimize.UglifyJsPlugin({ ... })
+ new UglifyJSPlugin({ ... })
<小时>
有关更多 webpack 信息(安装/使用),请参阅 https://github.com/webpack-contrib/uglifyjs-webpack-plugin#install
相关文章