ruby-on-rails 由于Uglifier错误(Uglifier::Error),Rails不会预编译资产

lfapxunr  于 2023-03-31  发布在  Ruby
关注(0)|答案(5)|浏览(178)

当运行RAILS_ENV=production rake assets:precompile时,我得到一个关于Uglifier的错误,如下所示:

root@4d24cd4a5e73:/myapp# RAILS_ENV=production rake assets:precompile
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
I, [2021-01-05T03:02:20.063081 #1157]  INFO -- : Writing /myapp/public/assets/devise-47cffe5a2e3ba6e30007c165a43ffece5c9f22dffd9aa74fd1d16997604531d6.js
I, [2021-01-05T03:02:20.064339 #1157]  INFO -- : Writing /myapp/public/assets/devise-47cffe5a2e3ba6e30007c165a43ffece5c9f22dffd9aa74fd1d16997604531d6.js.gz
I, [2021-01-05T03:02:20.891999 #1157]  INFO -- : Writing /myapp/public/assets/devise-21d500da162dd967a369767d6e63a850741756ad45ad5ee5585b3000404b5aec.css
I, [2021-01-05T03:02:20.892583 #1157]  INFO -- : Writing /myapp/public/assets/devise-21d500da162dd967a369767d6e63a850741756ad45ad5ee5585b3000404b5aec.css.gz
I, [2021-01-05T03:02:23.132038 #1157]  INFO -- : Writing /myapp/public/assets/theme_metrica-d858c65c360f07333101ab2929374a9a0a32010684107f1342b8bd3fd904619f.css
I, [2021-01-05T03:02:23.132473 #1157]  INFO -- : Writing /myapp/public/assets/theme_metrica-d858c65c360f07333101ab2929374a9a0a32010684107f1342b8bd3fd904619f.css.gz
I, [2021-01-05T03:02:24.038975 #1157]  INFO -- : Writing /myapp/public/assets/default-d59db161d4cc7a0b511555c8e661e0d8b2b4b03baa22106b7e07cbf356664725.js
I, [2021-01-05T03:02:24.039591 #1157]  INFO -- : Writing /myapp/public/assets/default-d59db161d4cc7a0b511555c8e661e0d8b2b4b03baa22106b7e07cbf356664725.js.gz
rake aborted!
Uglifier::Error:
/usr/local/bundle/gems/uglifier-4.2.0/lib/uglifier.rb:291:in `parse_result'
/usr/local/bundle/gems/uglifier-4.2.0/lib/uglifier.rb:221:in `run_uglifyjs'
/usr/local/bundle/gems/uglifier-4.2.0/lib/uglifier.rb:166:in `compile'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/compressing.rb:65:in `block in js_compressor='
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/legacy_proc_processor.rb:31:in `call'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:75:in `call_processor'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `call_processors'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/loader.rb:60:in `block in load'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/base.rb:66:in `find_asset'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/base.rb:73:in `find_all_linked_assets'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:142:in `block in find'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:228:in `block in stat_tree'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:212:in `block in stat_directory'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:209:in `each'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:209:in `stat_directory'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/path_utils.rb:227:in `stat_tree'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:105:in `each'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:105:in `block in logical_paths'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:104:in `each'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/legacy.rb:104:in `logical_paths'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:140:in `find'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:186:in `compile'
/usr/local/bundle/gems/sprockets-rails-3.2.2/lib/sprockets/rails/task.rb:67:in `block (3 levels) in define'
/usr/local/bundle/gems/sprockets-3.7.2/lib/rake/sprocketstask.rb:147:in `with_logger'
/usr/local/bundle/gems/sprockets-rails-3.2.2/lib/sprockets/rails/task.rb:66:in `block (2 levels) in define'
/usr/local/bundle/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
root@4d24cd4a5e73:/myapp#

原来是因为在我的应用程序中使用了ckeditor5。如果我从application.js文件中删除以下行,那么它可以正常预编译:

//= require ckeditor5/build/ckeditor.js

我以前遇到过这种情况,不得不简单地修改我的config/environment/production.rb文件来修改它:

# Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier

# Compress JavaScripts and CSS.
  config.assets.js_compressor = Uglifier.new(:harmony => true)

这已经工作了很长一段时间,但现在我遇到了同样的错误。在Ruby on Rails Error: Uglifier::Error: Unexpected token: operator (<)上提出的解决方案建议做完全相同的事情,但这对我不再起作用。

lx0bsm1f

lx0bsm1f1#

我在两个不同的Rails应用中遇到了这个问题(相同的parse_result消息和uglifier版本),发现它不支持ES6,所以一些最近更新的依赖项不再被uglifier压缩。我通过切换到terser解决了这个问题,这并不是一个困难的转换:
https://github.com/publiclab/plots2/issues/11232,在https://github.com/publiclab/plots2/pull/11243中修复

r7s23pms

r7s23pms2#

简单解决方案:切换到“Terser”

下面是Terser的ruby Package 器:https://github.com/ahorek/terser-ruby .
我不认为Uglifier正在维护了(但我可能是错的)。

tzdcorbm

tzdcorbm3#

为了解决这个问题,我将ckeditor5application.js文件中取出,将其文件夹从app/javascripts/ckeditor5重新定位到public/ckeditor5,然后在application.html.erb布局文件中添加一行:

<%= javascript_include_tag "/ckeditor5/build/ckeditor.js", media: :all, 'data-turbolinks-track': "reload" %>

解决了我的问题。

qyswt5oh

qyswt5oh4#

如果您正在使用
Webpacker〉5.0
resolved_paths已替换为additional_paths以供参考单击此处
在/config/webpacker.yml中
resolved_paths替换为additional_paths

erhoui1w

erhoui1w5#

这就是我的工作:
在production.rb中更改此行:

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

致:

config.assets.configure do |env|
  env.js_compressor  = :uglifier # or :closure, :yui
  env.css_compressor = :sass   # or :yui
end

来源:https://github.com/lautis/uglifier/issues/185

相关问题