简体   繁体   中英

Rails won't precompile assets due to Uglifier error (Uglifier::Error)

When running RAILS_ENV=production rake assets:precompile , I get an error about Uglifier, as shown below:

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# 

It turns out it's due to ckeditor5 being used within my application. If I remove the following line from my application.js file, then it precompiles just fine:

//= require ckeditor5/build/ckeditor.js

I have ran into this situation before and had to just simply modify my config/environment/production.rb file to modify this:

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

to

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

This has worked for quite some time, but now I'm running into the same error. The solution proposed at Ruby on Rails Error: Uglifier::Error: Unexpected token: operator (<) suggests doing the exact same thing, but this is no longer working for me.

I encountered this in 2 different Rails apps (same parse_result message and version of uglifier) and found it doesn't support ES6, so some recently updated dependencies were no longer getting compressed by uglifier. I solved it by switching to terser , which wasn't a difficult conversion:

https://github.com/publiclab/plots2/issues/11232 , fix in https://github.com/publiclab/plots2/pull/11243

To workaround this problem, I took ckeditor5 out of the application.js file, relocated its folder from app/javascripts/ckeditor5 to public/ckeditor5 and then added a line in my application.html.erb layouts file:

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

Solved my problem.

If you are using

webpacker > 5.0

resolved_paths has been replaced with the additional_paths for reference click here

In /config/webpacker.yml

replace resolved_paths with additional_paths

Easy solution: switch to "Terser"

Here's the ruby wrapper for Terser: https://github.com/ahorek/terser-ruby .

I don't think Uglifier is being maintained anymore (but I could be wrong about that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM