Ruby on Rails 错误:Uglifier::Error:意外的标记:运算符 (<)

问题描述 投票:0回答:4

每当我在命令行中运行

rake assets:precompile RAILS_ENV=production
时,我都会收到此错误,该错误阻止我预编译我的资产。

以下是命令行中显示的完整错误:

rake aborted!
Uglifier::Error: Unexpected token: operator (<)
/usr/local/rvm/gems/ruby-2.3.4/gems/uglifier-4.1.4/lib/uglifier.rb:234:in parse_result'
/usr/local/rvm/gems/ruby-2.3.4/gems/uglifier-4.1.4/lib/uglifier.rb:216:in run_uglifyjs'
/usr/local/rvm/gems/ruby-2.3.4/gems/uglifier-4.1.4/lib/uglifier.rb:168:in compile'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/compressing.rb:65:in block in js_compressor='
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy_proc_processor.rb:31:in call'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:75:in call_processor'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:57:in block in call_processors'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in reverse_each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in call_processors'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:134:in load_from_unloaded'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:60:in block in load'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:317:in fetch_asset_from_dependency_cache'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/loader.rb:44:in load'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:20:in block in initialize'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:47:in load'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/base.rb:66:in find_asset'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/base.rb:73:in find_all_linked_assets'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:142:in block in find'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:114:in block (2 levels) in logical_paths'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:228:in block in stat_tree'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:212:in block in stat_directory'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in stat_directory'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:227:in stat_tree'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in block in logical_paths'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in each'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in logical_paths'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:140:in find'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:185:in compile'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:68:in block (3 levels) in define'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-3.7.1/lib/rake/sprocketstask.rb:147:in with_logger'
/usr/local/rvm/gems/ruby-2.3.4/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:67:in block (2 levels) in define'
/usr/local/rvm/gems/ruby-2.3.4/gems/rake-12.3.0/exe/rake:27:in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in eval'
/usr/local/rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in <main>'
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

我到底需要做什么来修复这个错误?

ruby-on-rails ruby ruby-on-rails-4 rubygems uglifier
4个回答
14
投票

我遇到了同样的错误。有趣的是,它是由 Rails 本身推荐用于 ActiveStorage 的 direct_upload.js 文件触发的!我在这里找到了解决方案:https://github.com/lautis/uglifier/issues/127

尝试更换

config.assets.js_compressor = :uglifier

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

config/environments/production.rb


1
投票

您始终可以通过使用 uglifier 编译器解析 JS 文件来检查它。 打开你的 Rails 控制台

const JS_FOLDER = [file1_path, file2_path];
JS_FOLDER.each do. |file_name|
  puts "#{file_name}"
  puts  Uglifier.compile(File.read(file_name))
end

您将看到损坏的文件。


0
投票

该跟踪表明您的 JavaScript 文件之一存在语法错误。如果您查看浏览器的控制台,它可能会告诉您错误所在。

如果没有,我建议删除 JS 文件,直到找到罪魁祸首,然后注释掉有问题的文件中的行,直到找到错误。二分查找法可能会有所帮助;首先删除大约一半的 JS 文件,然后尝试预编译。如果错误仍然出现,则删除剩余的一半并重试。当错误停止发生时,您就知道哪一组文件包含有问题的文件,因此您可以备份一个步骤,删除除该组之外的所有内容,然后再次尝试重新编译。最终你应该找出哪个文件包含错误。

将其范围缩小到单个文件后,您可以使用相同的技术,注释掉文件中大约一半的行,重新编译等。


0
投票

Uglifier引入了Harmony模式来兼容ES6,但UglifierJS从未实现完全兼容。相反,建议使用 Terser gem。

因此,将您的

config/environments/production.rb
更改为使用

config.assets.js_compressor = :terser

相反。


Terser 于 2020 年诞生

2021 年 1 月 1 日,Uglifier 存储库所有者lautis已经建议从 Uglifier 的Harmony 模式切换到 Terser。

同年 11 月,Rails 团队提出了 Terser 的建议。

© www.soinside.com 2019 - 2024. All rights reserved.