简体   繁体   English

Uglify SyntaxError:意外的标记:punc ())

[英]Uglify SyntaxError: Unexpected token: punc ())

I am trying to use gulp in order to minify a folder containing JS files.我正在尝试使用 gulp 来缩小包含 JS 文件的文件夹。 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\n    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() {}
    }
 });
  ...
}

I added the "*" above in order to indicate the exact position listed by JS_Parse_Error.我在上面添加了“*”以指示 JS_Parse_Error 列出的确切位置。

// Update // 更新

From the comments ~ @imolit 来自评论~ @imolit

v2.0.0 (2018-09-14) - BREAKING CHANGES (link) v2.0.0 (2018-09-14) - 重大变化(链接)

Switch back to uglify-js (uglify-es is abandoned, if you need uglify ES6 code please use terser-webpack-plugin ).切换回uglify-js (uglify-es 已被放弃,如果您需要 uglify ES6 代码请使用terser-webpack-plugin )。


Original answer before the update...更新前的原始答案...

I hope you can get inspired by this solution which works with webpack.我希望你能从这个与 webpack 一起工作的解决方案中得到启发。 (link below) (下方链接)

Simply teach UglifyJS ES6简单教 UglifyJS ES6

There are two versions of UglifyJS - ES5 and ES6 (Harmony), see on git UglifyJS 有两个版本 - ES5ES6 (Harmony),参见 git
ES5 version comes by default with all the plugins, but if you install a Harmony version explicitly, those plugins will use it instead. ES5 版本默认包含所有插件,但如果您明确安装 Harmony 版本,这些插件将改为使用它。

package.json包.json

"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"

or要么

npm install --save uglify-js@github:mishoo/UglifyJS2#harmony

yarn add git://github.com/mishoo/UglifyJS2#harmony --dev

Webpack网页包

To use it with webpack install also the webpack plugin要将它与 webpack 一起使用,还要安装 webpack 插件

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');

and replace it in code并在代码中替换它

-  new webpack.optimize.UglifyJsPlugin({ ... })
+  new UglifyJSPlugin({ ... })

For more webpack info (Installation/Usage) see https://github.com/webpack-contrib/uglifyjs-webpack-plugin#install有关更多 webpack 信息(安装/使用),请参阅https://github.com/webpack-contrib/uglifyjs-webpack-plugin#install

npm install uglifyjs-webpack-plugin --save-dev is not enough npm install uglifyjs-webpack-plugin --save-dev是不够的

The main problem is "uglifyjs-webpack-plugin": "^0.4.6" in webpack's package.json主要问题是webpack 的 package.json中的"uglifyjs-webpack-plugin": "^0.4.6"

According to semver , ^0.4.6:= >=0.4.6 <0.5.0 .根据semver^0.4.6:= >=0.4.6 <0.5.0 Because of the leading zero, webpack will never use the 1.0.0-beta.2 .由于前导零, webpack永远不会使用1.0.0-beta.2

So after running npm i -D uglifyjs-webpack-plugin@beta , you need to do one more step which is rm -rf node_modules/webpack/node_modules/uglifyjs-webpack-plugin .所以在运行npm i -D uglifyjs-webpack-plugin@beta之后,你需要再做一步,即rm -rf node_modules/webpack/node_modules/uglifyjs-webpack-plugin Then webpack will pick up the version from node_modules/uglifyjs-webpack-plugin instead of node_modules/webpack/node_modules/uglifyjs-webpack-plugin然后 webpack 将从node_modules/uglifyjs-webpack-plugin而不是node_modules/webpack/node_modules/uglifyjs-webpack-plugin

Update on 2018-04-18: webpack v4 does not have this issue 2018-04-18更新:webpack v4没有这个问题

Add the babel-preset-es2015 dependency to fix this.添加babel-preset-es2015依赖来解决这个问题。

And also add 'es2015' in .babelrc file.并在.babelrc文件中添加'es2015'

json
{
    "presets": ["es2015"]
}

I am having the same issue, i found a great answers here that helped me to reach the the file that was causing the error.我遇到了同样的问题,我在这里找到了一个很好的答案,帮助我找到了导致错误的文件。

Go to Rails Console and Paste:转到 Rails 控制台并粘贴:

JS_PATH = "app/assets/javascripts/**/*.js";
Dir[JS_PATH].each do |file_name|
  puts "\n#{file_name}"
  puts Uglifier.compile(File.read(file_name))
end

Hope it helps someone!希望它能帮助别人!

If you got this error using Grunt (grunt-contrib-uglify) the solution is to install ES6 version of the plugin:如果您使用 Grunt (grunt-contrib-uglify) 时出现此错误,解决方案是安装 ES6 版本的插件:

npm install grunt-contrib-uglify-es --save-dev

For me it had nothing to do with Uglify not working correctly, but rather a dependency (in this case empty-promise) that has not been compiled to ES5 yet.对我来说,这与 Uglify 无法正常工作无关,而是尚未编译为 ES5 的依赖项(在本例中为 empty-promise)。 As we just imported the raw source file, but babel is only transpiling files outside of node_modules, uglify got confused by the ES6 syntax.由于我们刚刚导入原始源文件,但 babel 仅转译 node_modules 之外的文件,uglify 被 ES6 语法搞糊涂了。

Simply check if any dependency you've recently added might not have a "dist" build.只需检查您最近添加的任何依赖项是否可能没有“dist”构建。

Add stage-3 to presets in.babelrc file.将 stage-3 添加到 presets in.babelrc 文件中。

{
  "presets": [
    "stage-3"
  ]
}

I am trying to use gulp in order to minify a folder containing JS files.我正在尝试使用gulp来缩小包含JS文件的文件夹。 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\n    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() {}
    }
 });
  ...
}

I added the "*" above in order to indicate the exact position listed by JS_Parse_Error.我在上面添加了“ *”,以指示JS_Parse_Error列出的确切位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError:意外的标记:punc()) - SyntaxError: Unexpected token: punc ()) 消息:'意外的令牌:punc(。)',同时在grunt中使用uglify - message: 'Unexpected token: punc (.)', while using uglify in grunt 意外的令牌运算符«=»,预期的punc«,» - Unexpected token operator «=», expected punc «,» ExecJS :: RuntimeError:SyntaxError:意外的令牌:get @(execjs)中的punc()):3538:630 - ExecJS::RuntimeError: SyntaxError: Unexpected token: punc ()) from get@(execjs):3538:630 错误:在最小化angularjs应用程序时出现了意外的标记punc«)»,预期的punc«,» - Error: Unexpected token punc «)», expected punc «,» while minifying angularjs app 意外的标记 punc «(»,从 UglifyJS 创建块时预期的 punc - Unexpected token punc «(», expected punc when creating chunk from UglifyJS ExecJS::ProgramError: 运行 rake 资产时出现意外的标记 punc «(»,预期的 punc «:»:在生产中预编译 - ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» when running rake assets:precompile on production 来自 UglifyJs 的 build.js 中的错误意外标记:punc (() - ERROR in build.js from UglifyJs Unexpected token: punc (() 未捕获到的SyntaxError:意外令牌, - Uncaught SyntaxError: Unexpected token , 语法错误:意外的令牌 } - SyntaxError : Unexpected token }
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM