Skip to content

Commit

Permalink
Update assets generator to use importmap
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Feb 27, 2024
1 parent 16d722b commit 2c4cf8d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pin_all_from File.expand_path('../app/assets/javascript', __dir__)
8 changes: 8 additions & 0 deletions lib/blacklight_range_limit/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ class Engine < Rails::Engine
config.before_configuration do
Blacklight::Configuration::FacetField.prepend BlacklightRangeLimit::FacetFieldConfigOverride
end

initializer 'blacklight_range_limit.assets', before: 'assets' do |app|
app.config.assets.precompile << 'blacklight_range_limit/blacklight_range_limit.esm.js'
end

initializer 'blacklight_range_limit.importmap', before: 'importmap' do |app|
app.config.importmap.paths << Engine.root.join('config/importmap.rb') if app.config.respond_to?(:importmap)
end
end
end
40 changes: 36 additions & 4 deletions lib/generators/blacklight_range_limit/assets_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module BlacklightRangeLimit
class AssetsGenerator < Rails::Generators::Base
source_root File.join(BlacklightRangeLimit::Engine.root, 'app', 'assets')

def assets
def copy_styles
application_css = Dir["app/assets/stylesheets/application{.css,.scss,.css.scss}"].first

if application_css
Expand All @@ -31,7 +31,42 @@ def assets
else
say_status "warning", "Can not find application.css, did not insert our require", :red
end
end

def assets
if using_importmap?
pin_javascript_dependencies
import_javascript
else
install_sprockets_dependencies
end
end


private

def root
@root ||= Pathname(destination_root)
end

def using_importmap?
@using_importmap ||= root.join('config/importmap.rb').exist?
end

def pin_javascript_dependencies
say 'blacklight-range-limit importmap asset generation'

append_to_file 'config/importmap.rb', <<~RUBY
pin "blacklight-range-limit", to: "blacklight-range-limit/blacklight-range-limit.esm.js"
RUBY
end

def import_javascript
append_to_file 'app/javascript/application.js', "\nimport \"blacklight-range-limit\""
end

# NOTE: This is expected to fail in Rails 7.1+
def install_sprockets_dependencies
append_to_file "app/assets/javascripts/application.js" do
%q{
Expand All @@ -42,8 +77,5 @@ def assets
}
end
end



end
end

0 comments on commit 2c4cf8d

Please sign in to comment.