From f8219bf3bc508c5e42f5d044cf6355a56726f8b2 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Shibuya Date: Tue, 23 Aug 2022 22:46:44 +0900 Subject: [PATCH] Prevent ES Module processing from affecting non-RailsAdmin assets Refs. #3488 --- lib/rails_admin/engine.rb | 4 +- .../support/es_module_processor.rb | 23 ++++++++++++ .../support/esmodule_preprocessor.rb | 37 ------------------- .../app/assets/javascripts/application.js | 3 +- .../assets/javascripts/rails-ujs.esm.js.erb | 1 + .../app/views/layouts/application.html.erb | 2 +- src/rails_admin/i18n.js | 4 +- 7 files changed, 31 insertions(+), 43 deletions(-) create mode 100644 lib/rails_admin/support/es_module_processor.rb delete mode 100644 lib/rails_admin/support/esmodule_preprocessor.rb create mode 100644 spec/dummy_app/app/assets/javascripts/rails-ujs.esm.js.erb diff --git a/lib/rails_admin/engine.rb b/lib/rails_admin/engine.rb index 1532c8bcf8..f61a0c2ecf 100644 --- a/lib/rails_admin/engine.rb +++ b/lib/rails_admin/engine.rb @@ -43,8 +43,8 @@ class Engine < Rails::Engine rails_admin/application.css ] app.config.assets.paths << RailsAdmin::Engine.root.join('src') - require 'rails_admin/support/esmodule_preprocessor' - Sprockets.register_preprocessor 'application/javascript', RailsAdmin::ESModulePreprocessor + require 'rails_admin/support/es_module_processor' + Sprockets.register_bundle_processor 'application/javascript', RailsAdmin::Support::ESModuleProcessor when :importmap self.importmap = Importmap::Map.new.draw(app.root.join('config/importmap.rails_admin.rb')) end diff --git a/lib/rails_admin/support/es_module_processor.rb b/lib/rails_admin/support/es_module_processor.rb new file mode 100644 index 0000000000..df69c07142 --- /dev/null +++ b/lib/rails_admin/support/es_module_processor.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module RailsAdmin + module Support + class ESModuleProcessor + def self.instance + @instance ||= new + end + + def self.call(input) + instance.call(input) + end + + def initialize; end + + def call(input) + return unless input[:name] == 'rails_admin/application' + + input[:data].gsub(/^((?:import|export) .+)$/) { "// #{Regexp.last_match(1)}" } + end + end + end +end diff --git a/lib/rails_admin/support/esmodule_preprocessor.rb b/lib/rails_admin/support/esmodule_preprocessor.rb deleted file mode 100644 index 7614409671..0000000000 --- a/lib/rails_admin/support/esmodule_preprocessor.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -module RailsAdmin - class ESModulePreprocessor - def self.instance - @instance ||= new - end - - def self.call(input) - instance.call(input) - end - - def initialize; end - - def call(input) - return unless RailsAdmin.config.asset_source == :sprockets - - data = input[:data] - - if input[:filename].start_with? RailsAdmin::Engine.root.join('src').to_s - data.gsub!(/^(import .+)$/) { "// #{Regexp.last_match(1)}" } - data.gsub!(/^(export +default +{)$/) do - case File.basename(input[:filename]) - when 'i18n.js' - "/* #{Regexp.last_match(1)} */ window.I18n = {" - else - raise "Unable to preprocess file: #{input[:filename]}" - end - end - elsif input[:filename] =~ %r{turbo-rails.+/turbo\.js$} - data.gsub!(/^(export .+)$/) { "// #{Regexp.last_match(1)}" } - end - - {data: data} - end - end -end diff --git a/spec/dummy_app/app/assets/javascripts/application.js b/spec/dummy_app/app/assets/javascripts/application.js index b2a8b536b1..e844b587d7 100644 --- a/spec/dummy_app/app/assets/javascripts/application.js +++ b/spec/dummy_app/app/assets/javascripts/application.js @@ -10,6 +10,5 @@ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD // GO AFTER THE REQUIRES BELOW. // -//= require rails-ujs +//= require rails-ujs.esm //= require turbo -//= require_tree . diff --git a/spec/dummy_app/app/assets/javascripts/rails-ujs.esm.js.erb b/spec/dummy_app/app/assets/javascripts/rails-ujs.esm.js.erb new file mode 100644 index 0000000000..f05a98c02b --- /dev/null +++ b/spec/dummy_app/app/assets/javascripts/rails-ujs.esm.js.erb @@ -0,0 +1 @@ +<%= depend_on_asset('rails-ujs').to_s.gsub(/context = this/, 'context = (this || globalThis)') %> \ No newline at end of file diff --git a/spec/dummy_app/app/views/layouts/application.html.erb b/spec/dummy_app/app/views/layouts/application.html.erb index 3a85d9970e..ddf532edd0 100644 --- a/spec/dummy_app/app/views/layouts/application.html.erb +++ b/spec/dummy_app/app/views/layouts/application.html.erb @@ -9,7 +9,7 @@ <%= javascript_importmap_tags %> <% else %> <%= stylesheet_link_tag "application", media: "all" %> - <%= javascript_include_tag "application" %> + <%= javascript_include_tag "application", type: 'module' %> <% end %> <%= csrf_meta_tags %> diff --git a/src/rails_admin/i18n.js b/src/rails_admin/i18n.js index 67ee6a4ddb..e5eaec915b 100644 --- a/src/rails_admin/i18n.js +++ b/src/rails_admin/i18n.js @@ -1,4 +1,4 @@ -export default { +var I18n = { locale: null, translations: null, init(locale, translations) { @@ -14,3 +14,5 @@ export default { return this.translations[key] || humanize; }, }; + +export { I18n as default };