Skip to content

Commit

Permalink
Prevent ES Module processing from affecting non-RailsAdmin assets
Browse files Browse the repository at this point in the history
Refs. #3488
  • Loading branch information
mshibuya committed Aug 23, 2022
1 parent 8891c28 commit f8219bf
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 43 deletions.
4 changes: 2 additions & 2 deletions lib/rails_admin/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/rails_admin/support/es_module_processor.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 0 additions & 37 deletions lib/rails_admin/support/esmodule_preprocessor.rb

This file was deleted.

3 changes: 1 addition & 2 deletions spec/dummy_app/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
1 change: 1 addition & 0 deletions spec/dummy_app/app/assets/javascripts/rails-ujs.esm.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= depend_on_asset('rails-ujs').to_s.gsub(/context = this/, 'context = (this || globalThis)') %>
2 changes: 1 addition & 1 deletion spec/dummy_app/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</head>
Expand Down
4 changes: 3 additions & 1 deletion src/rails_admin/i18n.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
var I18n = {
locale: null,
translations: null,
init(locale, translations) {
Expand All @@ -14,3 +14,5 @@ export default {
return this.translations[key] || humanize;
},
};

export { I18n as default };

0 comments on commit f8219bf

Please sign in to comment.