RAILS ASSETS PIPELINE
When running rake assets:precompile RAILS_ENV=production over ES6 syntax, assets pipeline got broken. If we instead call rake assets:precompile with environment set to RAILS_ENV=development it perform fine.
Summarizing
SUCCESS | FAIL |
---|---|
rake assets:precompile | rake assets:precompile RAILS_ENV=production |
RAILS_ENV=devlopment | RAILS_ENV=development |
`bundle install...
The Gemfile's dependencies are satisfied
yarn install...
yarn install v1.22.19
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.41s.
yarn install v1.22.19
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.33s.
yarn run v1.22.19
$ webpack --config ./config/webpack/webpack.config.js
asset application.js 66 KiB [compared for emit] [minimized] (name: application) 1 related asset
runtime modules 718 bytes 3 modules
orphan modules 26.4 KiB [orphan] 1 module
built modules 92.4 KiB [built]
modules by path ./app/javascript/ 27 KiB
./app/javascript/application.js + 1 modules 26.7 KiB [built] [code generated]
./app/javascript/channels/index.js 212 bytes [built] [code generated]
./app/javascript/channels/ sync _channel.js$ 160 bytes [built] [code generated]
modules by path ./node_modules/ 65.3 KiB
./node_modules/@rails/ujs/lib/assets/compiled/rails-ujs.js 27.7 KiB [built] [code generated]
./node_modules/turbolinks/dist/turbolinks.js 37.6 KiB [built] [code generated]
webpack 5.75.0 compiled successfully in 2534 ms
Done in 3.60s.
yarn install v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 18.62s.
rake aborted!
Uglifier::Error: Unexpected token: punc ((). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).
--
584
585 App.cable = ActionCable.createConsumer();
586
587 }).call(this);
588
589 // app/javascript/channels/appearance_channel.js
590 App.appearance = App.cable.subscriptions.create("AppearanceChannel", {
591 // Called once when the subscription is created.
=> initialized() {
593 this.update = this.update.bind(this)
594 },
595
596 // Called when the subscription is ready for use on the server.
597 connected() {
598 this.install()
599 this.update()
600 },
==
/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/uglifier_compressor.rb:53:in `call'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/uglifier_compressor.rb:28: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:134:in `block in find'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:133:in `each'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:133:in `find'
/usr/local/bundle/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:186:in `compile'
/usr/local/bundle/gems/sprockets-rails-3.4.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.4.2/lib/sprockets/rails/task.rb:66:in `block (2 levels) in define'
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/cli.rb:491:in `exec'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/cli.rb:34:in `dispatch'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/cli.rb:28:in `start'
/usr/local/bundle/gems/bundler-2.4.1/exe/bundle:45:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-2.4.1/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-2.4.1/exe/bundle:33:in `<top (required)>'
/usr/local/bundle/bin/bundle:25:in `load'
/usr/local/bundle/bin/bundle:25:in `<main>'
Tasks: TOP => assets:precompile`
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
#uby '3.2.0'
ruby '~>3.0.5'
#---- alectrico < 2023
#
gem 'state_machines', :require => 'state_machines/core' #usado por el modelo User
gem 'state_machines-activerecord' #usado por el modelo User
gem 'devise' #usado como autenticación
gem 'rack-cors', :require => 'rack/cors' #usado para combatir cross site attacks
gem 'dkim' #para que los correos emitidos no sean envíados a spam
gem 'delayed_job'
gem 'dragonfly'
gem 'logging-email'
gem 'logging-rails'
gem 'transbank-sdk', '~> 1.4'
gem 'paypal-sdk-rest'
gem 'wicked_pdf'
gem 'jwt'
gem 'dotenv' #usado por braintree como inicializador
#--solo para precompilar assets, no usar en production
gem 'bootstrap-sass', '3.3.7'
gem 'jquery-rails'
gem 'coffee-rails', '~> 4.2'
gem "sprockets-rails"
gem 'uglifier' #se usa solo para generar assets precompilados
gem 'terser'
#---
#-------- pedidos por los controllers
gem 'origami' #para hacer pdf
gem 'prawn' #para hacer pdf
gem 'httparty' #para enviar mensajes waba
group :test, :development do
gem 'factory_bot_rails', '~> 4.0' #usado por rspec para los factories
gem 'capybara' #usado por cucumber para las pruebas
gem 'shoulda-matchers' #usado por rspec para pruebas
gem 'faker' #usado por rspec para construir factories a partir de modelos de datos
end
#depenencias de assets
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.7'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 6.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 6'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.11'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
group :development, :test do
gem 'bundle-audit'
gem 'brakeman'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'pry-rails'
gem 'rspec-rails', '~> 6.0.1'
gem "rubocop", require: false
gem "rubocop-rails", require: false
end
group :development,:production, :test do
gem 'rack-mini-profiler', '~> 3.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# Ruby 3.1 no longer includes these
gem 'net-imap'
gem 'net-pop'
gem 'net-smtp'
gem 'jsbundling-rails'
production.rb
Rails.application.configure do
config.exceptions_apps = self.routes
config.action_view.embed_authenticity_token_in_remote_forms = true
config.action_mailer.asset_host = config.domain["name_http"]
config.action_mailer.default_url_options = { :host => config.domain['name_https'],:only_path => false}
config.cache_classes = true
config.eager_load = false
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.serve_static_assets = true
config.force_ssl = true
config.log_level = ENV['CONFIG_LOG_LEVEL'].present? ? ENV['CONFIG_LOG_LEVEL'].downcase.to_sym : :fatal
config.log_tags = [ :request_id ]
config.action_mailer.perform_caching = true
config.action_mailer.raise_delivery_errors = false
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
config.active_record.dump_schema_after_migration = false
config.log_to = %w[stdout]
config.show_log_configuration = false
end
I was trying to precompile assets on local to serve on production.
2
Answers
I got success by changing to config.assets.js_compresor = :terser on production.rb and then calling rake assets:clobber.
Very thanks to @markets that insist on their solution that finally proved to be the right one.
You have basically 2 options to fix this:
If you want to use Uglifier gem, change to:
config.assets.js_compressor = Uglifier.new(harmony: true)
, see: https://github.com/lautis/uglifier#railsYou can also use Terser gem:
config.assets.js_compressor = :terser
(in fact, newest Rails switched to Terser, see: https://github.com/rails/rails/pull/42622)