問題描述
我正在嘗試使用 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
這篇關于Uglify SyntaxError: Unexpected token: punc ())的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!