From c4c67f6129311c74f0eac066d790f11fb93bdc5e Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Thu, 8 Aug 2024 13:01:27 -0400 Subject: [PATCH] Wrap the uglifier compressor with harmony support * Allows es6 code that regular Uglifier doesn't support * Avoids requiring Uglifier in all rails processes at application startup. * Uglifier requires a js runtime such as node * JS runtime can continue to be only available in development and for production pre-compilation This is a better solution than https://github.com/ManageIQ/manageiq-ui-classic/pull/9244 Note, the original es6 workaround suggested by Uglifier is to use this harmony support: https://www.github.com/lautis/uglifier/issues/127 --- lib/manageiq/ui/classic/engine.rb | 13 +++++++++---- lib/manageiq/ui/classic/js_compressor.rb | 13 +++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 lib/manageiq/ui/classic/js_compressor.rb diff --git a/lib/manageiq/ui/classic/engine.rb b/lib/manageiq/ui/classic/engine.rb index b3a55c477a4..cd957b42932 100644 --- a/lib/manageiq/ui/classic/engine.rb +++ b/lib/manageiq/ui/classic/engine.rb @@ -37,10 +37,15 @@ class Engine < ::Rails::Engine config.assets.paths << root.join('vendor', 'assets', 'stylesheets').to_s if Rails.env.production? || Rails.env.test? - # Workaround rails 7 + es6 syntax in some js causing uglifier errors by running harmony mode - # See: https://www.github.com/lautis/uglifier/issues/127 - require 'uglifier' - config.assets.js_compressor = Uglifier.new(:harmony => true) + config.assets.configure do |env| + # Workaround rails 7 + es6 syntax in some js causing uglifier errors by running harmony mode + # See: https://www.github.com/lautis/uglifier/issues/127 + # Note, we're purposely using our own compressor to avoid requiring uglifier at application boot time + # since this require a js runtime such as node. + require 'manageiq/ui/classic/js_compressor' + env.register_compressor 'application/javascript', :manageiq_ui_classic_js_compressor, ManageIQ::UI::Classic::JsCompressor + end + config.assets.js_compressor = :manageiq_ui_classic_js_compressor end def self.vmdb_plugin? diff --git a/lib/manageiq/ui/classic/js_compressor.rb b/lib/manageiq/ui/classic/js_compressor.rb new file mode 100644 index 00000000000..91a002c57a5 --- /dev/null +++ b/lib/manageiq/ui/classic/js_compressor.rb @@ -0,0 +1,13 @@ +module ManageIQ + module UI + module Classic + class JsCompressor < Sprockets::UglifierCompressor + def initialize(options = {}) + warn "\e[33m** Using #{self.class.name} with Uglifier.new(:harmony => true) from: #{__FILE__}:#{__LINE__}\e[0m" + options[:harmony] = true + super(options) + end + end + end + end +end