lautis / uglifier

Ruby wrapper for UglifyJS JavaScript compressor.
http://www.rubydoc.info/gems/uglifier
MIT License
614 stars 81 forks source link

Uglifier error in production mode on RoR 7.0.3.1 with Bootstrap 5.2 #187

Open Sega100500 opened 2 years ago

Sega100500 commented 2 years ago

Ruby 3.1.2 Ruby on Rails 7.0.3.1 gem Uglifier 4.2.0 Third party library: Bootstrap 5.2 ( new version 5.2 released )

With bootstrap 5.1.3 (previous version) all works fine!

app/assets/config/manifest.js :

//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css

//= link lib/bootstrap/5.2.0/js/bootstrap.bundle.min.js
//= link lib/bootstrap/5.2.0/js/bootstrap.min.js
//= link lib/bootstrap/5.2.0/css/bootstrap.min.css

in view stylesheets and javascripts are attached via stylesheet_link_tag and javascript_include_tag

config/environments/production.rb :

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

log/production.log :

I, [2022-08-04T20:29:16.542499 #11922]  INFO -- : [425e1b3d-5d27-4e00-afdf-d698f79e50ab] Started GET "/" for 127.0.0.1 at 2022-08-04 20:29:16 +0500
I, [2022-08-04T20:29:16.545698 #11922]  INFO -- : [425e1b3d-5d27-4e00-afdf-d698f79e50ab] Processing by ApplicationBaseController#index as HTML
I, [2022-08-04T20:29:16.554844 #11922]  INFO -- : [425e1b3d-5d27-4e00-afdf-d698f79e50ab]   Rendered application_base/index.html.erb within layouts/application (Duration: 0.9ms | Allocations: 314)
I, [2022-08-04T20:29:33.586606 #11922]  INFO -- : [425e1b3d-5d27-4e00-afdf-d698f79e50ab]   Rendered layout layouts/application.html.erb (Duration: 17032.8ms | Allocations: 628213)
I, [2022-08-04T20:29:33.586988 #11922]  INFO -- : [425e1b3d-5d27-4e00-afdf-d698f79e50ab] Completed 500 Internal Server Error in 17041ms (ActiveRecord: 0.7ms | Allocations: 630135)
F, [2022-08-04T20:29:33.587842 #11922] FATAL -- : [425e1b3d-5d27-4e00-afdf-d698f79e50ab]   
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] ActionView::Template::Error (
):
[425e1b3d-5d27-4e00-afdf-d698f79e50ab]   
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] uglifier (4.2.0) lib/uglifier.rb:291:in `parse_result'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] uglifier (4.2.0) lib/uglifier.rb:221:in `run_uglifyjs'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] uglifier (4.2.0) lib/uglifier.rb:166:in `compile'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/compressing.rb:84:in `block in js_compressor='
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:84:in `call_processor'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:66:in `block in call_processors'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:65:in `reverse_each'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:65:in `call_processors'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:182:in `load_from_unloaded'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:59:in `block in load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:337:in `fetch_asset_from_dependency_cache'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:43:in `load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `block in load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `fetch'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/bundle.rb:32:in `block in call'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] /home/ruby/.rbenv/versions/3.1.2/lib/ruby/3.1.0/set.rb:511:in `each_key'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] /home/ruby/.rbenv/versions/3.1.2/lib/ruby/3.1.0/set.rb:511:in `each'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/bundle.rb:31:in `call'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:84:in `call_processor'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:66:in `block in call_processors'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:65:in `reverse_each'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:65:in `call_processors'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:182:in `load_from_unloaded'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:59:in `block in load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:337:in `fetch_asset_from_dependency_cache'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:43:in `load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `block in load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `fetch'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/bundle.rb:32:in `block in call'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] /home/ruby/.rbenv/versions/3.1.2/lib/ruby/3.1.0/set.rb:511:in `each_key'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] /home/ruby/.rbenv/versions/3.1.2/lib/ruby/3.1.0/set.rb:511:in `each'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/bundle.rb:31:in `call'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:84:in `call_processor'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:66:in `block in call_processors'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:65:in `reverse_each'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/processor_utils.rb:65:in `call_processors'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:182:in `load_from_unloaded'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:59:in `block in load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:337:in `fetch_asset_from_dependency_cache'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/loader.rb:43:in `load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `block in load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `fetch'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/cached_environment.rb:44:in `load'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/base.rb:81:in `find_asset'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/base.rb:88:in `find_all_linked_assets'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/manifest.rb:125:in `each'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/manifest.rb:125:in `to_a'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] sprockets (4.1.1) lib/sprockets/manifest.rb:125:in `block (2 levels) in find'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/safe_task_executor.rb:24:in `block in execute'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/synchronization/mutex_lockable_object.rb:47:in `block in synchronize'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/synchronization/mutex_lockable_object.rb:47:in `synchronize'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/synchronization/mutex_lockable_object.rb:47:in `synchronize'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/safe_task_executor.rb:22:in `execute'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/promise.rb:564:in `block in realize'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:352:in `run_task'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:343:in `block (3 levels) in create_worker'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `loop'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `block (2 levels) in create_worker'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:333:in `catch'
[425e1b3d-5d27-4e00-afdf-d698f79e50ab] concurrent-ruby (1.1.10) lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:333:in `block in create_worker'

All other compressors are works with errors in this case

Sega100500 commented 2 years ago

Some refer to the need to use a terser , but terser does'nt work properly at this case too.

dave105010 commented 2 years ago

Sorry I found this issue while looking into something else about terser/rails on Google. What's the problem here exactly? rails assets:precompile generates the minified JS when the command is explicitly executed on terminal? Why uglifier is running on application runtime anyway? So weird. Maybe it shouldn't run at all. Uglifier devs should fix that.

Does the minified js file work when you remove uglifier gem and rebooted the application?

Sega100500 commented 2 years ago

@dave105010 Because I don't use rails assets:precompile any time but I use config.assets.compile = true in config file (see the issue).

In development mode, everything works fine until :uglifier is used. The JS code and CSS styles has nothing to do with it at all.

Jaco-Pretorius commented 2 years ago

+1 Also seeing this exact same issue when upgrading Bootstrap in both Development and Production

I'm seeing this when I run assets:precompile:

rails aborted!
Uglifier::Error:
/Users/my-username/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/uglifier-4.2.0/lib/uglifier.rb:293:in `parse_result'
/Users/my-username/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/uglifier-4.2.0/lib/uglifier.rb:221:in `run_uglifyjs'
/Users/my-username/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/uglifier-4.2.0/lib/uglifier.rb:166:in `compile'

The result parameter in parse_result (which I found by adding some debug statements) is {"error"=>{}}

itsalongstory commented 2 years ago
# gem 'uglifier', '~> 4.2'
gem 'terser', '~> 1.1', '>= 1.1.12'
# config.assets.js_compressor = Uglifier.new(harmony: true)
config.assets.js_compressor = :terser
Sega100500 commented 2 years ago

@itsalongstory terser have same problem. It' not solution!

itsalongstory commented 2 years ago

terser have same problem. It' not solution!

So, how did you get Uglifier to handle Bootstrap 5.2 correctly now?

Sega100500 commented 2 years ago

@itsalongstory No way! I don't see a solution, so I stayed on Bootstrap v5.1.3 for now. The fact is that technology is advancing, and RoR is no longer keeping up with them.

itsalongstory commented 2 years ago

terser have same problem. It' not solution!

Can you describe a little bit about the problems you've encountered with terser?

Sega100500 commented 2 years ago

@itsalongstory Same problem as uglifier - not work correctly when I use Bootstrap 5.2

itsalongstory commented 2 years ago

Same problem as uglifier - not work correctly when I use Bootstrap 5.2

In my project, uglifier does not work with Bootstrap 5.2, but terser works fine with Bootstrap 5.2

Jaco-Pretorius commented 2 years ago

No way! I don't see a solution, so I stayed on Bootstrap v5.1.3 for now. The fact is that technology is advancing, and RoR is no longer keeping up with them.

I think we should first understand why it's blowing up. It's not clear to me if the issue is in uglifier or in Bootstrap.

itsalongstory commented 2 years ago

No way! I don't see a solution, so I stayed on Bootstrap v5.1.3 for now. The fact is that technology is advancing, and RoR is no longer keeping up with them.

I think we should first understand why it's blowing up. It's not clear to me if the issue is in uglifier or in Bootstrap.

I think the problem is uglifier doesn't full support ES6, and Bootstrap 5.2 has some new ES6 code.

Jaco-Pretorius commented 2 years ago

@itsalongstory Ah, ok - that explains it.

The README does say UglifyJS only works with ES5. If you need to compress ES6, [ruby-terser](https://github.com/ahorek/terser-ruby) is a better option.

I switched to terser and that solved my problem, so I think that's the resolution for this issue. If terser doesn't work for you I would suggest opening an issue against the terser repo.

bekkou68 commented 7 months ago

I faced a similar problem. And I solved the problem by switching to terser. Thanks.

guilhermeyo commented 4 months ago

@bekkou68 me too, thanks.