Uglifier::Error: Unexpected tokenpunc «;», 预期的punc «,»

Posted

技术标签:

【中文标题】Uglifier::Error: Unexpected tokenpunc «;», 预期的punc «,»【英文标题】:Uglifier::Error: Unexpected token punc «;», expected punc «,» 【发布时间】:2019-06-22 12:54:56 【问题描述】:

在 heroku 上部署我的 Rails 应用程序时出现以下错误,

远程:捆绑完成(163.81 秒) 远程:清理捆绑器缓存。 远程:-----> 安装 node-v8.10.0-linux-x64 远程: -----> 检测 rake 任务 远程:-----> 为 Rails 资产管道准备应用程序 远程:运行:耙资产:预编译 远程:系统中未检测到纱线可执行文件。 远程:在 https://yarnpkg.com/en/docs/install 下载 Yarn 远程:rake 中止! 远程:Uglifier::Error:意外的标记 punc «;»,预期的 punc «,» 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/uglifier-4.1.20/lib/uglifier.rb:234:in `parse_result' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/uglifier-4.1.20/lib/uglifier.rb:216:in `run_uglifyjs' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/uglifier-4.1.20/lib/uglifier.rb:168:in `compile' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/compressing.rb:65:in `block in js_compressor=' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/legacy_proc_processor.rb:31:in `call' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:75:in `call_processor' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `reverse_each' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `call_processors' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:134:in `load_from_unloaded' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:60:in `blockin load' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:66:in `find_asset' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:73:in `find_all_linked_assets' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:142:in `block in find' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:228:in `block in stat_tree' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:212:in `block in stat_directory' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:209:in `each' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:209:in `stat_directory' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:227:in `stat_tree' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:105:in `each' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:105:in `block in logical_paths' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:104:in `each' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:104:in `logical_paths' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:140:in `find' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:186:in `compile' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:68:in `block (3 levels) in define' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-3.7.2/lib/rake/sprocketstask.rb:147:in `with_logger' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:67:in `block (2 levels) in define' 远程:/tmp/build_eb48b48285b82dae89f9dfd764c51c22/vendor/bundle/ruby/2.4.0/gems/rake-12.3.2/exe/rake:27:in `' 远程:任务:TOP => 资产:预编译 远程:(通过使用 --trace 运行任务查看完整跟踪) 偏僻的: 偏僻的: ! 偏僻的: !预编译资产失败。 偏僻的: ! 偏僻的: ! Push 被拒绝,无法编译 Ruby 应用程序。 偏僻的: 偏僻的: !推送失败 远程:验证部署...

我已经将production.rb中的行修改为,

config.assets.js_compressor = Uglifier.new(harmony: true)

但错误仍然存​​在。

【问题讨论】:

试试这个***.com/questions/30422437/… 该命令的返回是:Uglifier::Error: Unexpected token eof «undefined», expected punc «,»。要使用 ES6 语法,必须使用 Uglifier.new(:harmony => true) 启用和声模式。来自 (irb):18:in block in irb_binding' from (irb):16:in each' 来自 (irb):16 【参考方案1】:

看起来问题的出现是因为您的 Sprockets 或 Rails 版本无法与较新的 Uglifier 很好地配合使用。至少在 Rails 3.2.22 中,.map 文件被生成,然后被视为下一个后缀。例如。 Sprockets 尝试将 application.js.map 压缩为 js 文件。

一种解决方案是告诉 Sprockets 通过在您的 config/environments/production.rb 文件(或您喜欢的任何配置文件)中添加一行来忽略地图文件:

config.assets.precompile[0] =  Proc.new  |path| !File.extname(path).in?(['.js', '.css', '.map']) 

在复制粘贴上面的代码之前,您应该确认您正在替换预先存在的config.assets.precompile 数组中的正确项目。该数组包含确定处理哪些文件的过程。对我来说,数组中有两项,第一项如下:

#<Proc:0x0055819620f6f8@/home/me/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-3.2.22/lib/rails/application/configuration.rb:48>

查看指定位置 (gems/railties-3.2.22/lib/rails/application/configuration.rb:48) 以了解上述过程的定义:

Proc.new |path| !File.extname(path).in?(['.js', '.css']) 

...选择每个非 js、非 css 文件。将'.map' 添加到该排除列表将阻止.map 文件通过压缩器。

【讨论】:

以上是关于Uglifier::Error: Unexpected tokenpunc «;», 预期的punc «,»的主要内容,如果未能解决你的问题,请参考以下文章

Vichuploader 破坏实体 - 意外的 EOF

MongoDB/PyMongo:查询多个条件 - 意外结果

Google 内部应用共享 - 意外错误

一元问号 (?) 运算符有啥作用?

为啥“import vue”行显示“Uncaught SyntaxError”?

Android Build 使用 Proguard 和 Gradle 失败