Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport] use importmap if available #2838

Draft
wants to merge 1 commit into
base: release-7.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/views/layouts/blacklight/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
<%= opensearch_description_tag application_name, opensearch_catalog_url(format: 'xml') %>
<%= favicon_link_tag %>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>

<% if defined? Importmap %>
<%= javascript_importmap_tags %>
<% else %>
<%= javascript_include_tag "application" %>
<% end %>
<%= csrf_meta_tags %>
<%= content_for(:head) %>
</head>
Expand Down
72 changes: 50 additions & 22 deletions lib/generators/blacklight/assets_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ def appease_rails7
# Add sprockets javascript if needed
def create_sprockets_javascript
# Rails 5 already has an application.js file
return if Rails.version < '6'
return if Rails.version < '6' && !using_importmap?

create_file 'app/assets/javascripts/application.js' do
<<~CONTENT
//= require jquery3
//= require rails-ujs
#{'//= require turbolinks' if Rails.version < '7'}
CONTENT
Expand All @@ -41,34 +40,63 @@ def appease_sprockets4
empty_directory 'app/assets/images'
end

# rubocop:disable Metrics/MethodLength
def assets
copy_file "blacklight.scss", "app/assets/stylesheets/blacklight.scss"

# Ensure this method is idempotent
return if has_blacklight_assets?

gem 'jquery-rails'
contents = "\n//\n// Required by Blacklight\n"
contents += "//= require popper\n"
contents += "// Twitter Typeahead for autocomplete\n"
contents += "//= require twitter/typeahead\n"
contents += "//= require bootstrap\n"
contents += "//= require blacklight/blacklight\n"

marker = if turbolinks?
'//= require turbolinks'
else
'//= require rails-ujs'
end

insert_into_file "app/assets/javascripts/application.js", after: marker do
contents
end

insert_into_file "app/assets/javascripts/application.js", before: '//= require rails-ujs' do
"//= require jquery3\n"
if using_importmap?
append_to_file 'config/importmap.rb' do
<<~CONTENT
pin "@popperjs/core", to: "https://ga.jspm.io/npm:@popperjs/[email protected]/dist/umd/popper.min.js"
pin "bootstrap", to: "https://ga.jspm.io/npm:[email protected]/dist/js/bootstrap.js"
pin "jquery", to: "https://ga.jspm.io/npm:[email protected]/dist/jquery.js"
pin "blacklight", to: "blacklight/blacklight.js"
pin "dialog-polyfill", to: "https://ga.jspm.io/npm:[email protected]/dist/dialog-polyfill.js"
CONTENT
end

append_to_file 'app/javascript/application.js' do
<<~CONTENT
import $ from "jquery"
import bootstrap from "bootstrap"
window.bootstrap = bootstrap // Required for Blacklight 7 so it can manage the modals
window.$ = $ // required as long as blacklight requires jquery
import "blacklight"
import dialogPolyfill from "dialog-polyfill"
Blacklight.onLoad(() => {
var dialog = document.querySelector('dialog');
dialogPolyfill.registerDialog(dialog);
})
CONTENT
end
else
gem 'jquery-rails'
contents = "\n//\n// Required by Blacklight\n"
contents += "//= require popper\n"
contents += "// Twitter Typeahead for autocomplete\n"
contents += "//= require twitter/typeahead\n"
contents += "//= require bootstrap\n"
contents += "//= require blacklight/blacklight\n"

marker = if turbolinks?
'//= require turbolinks'
else
'//= require rails-ujs'
end

insert_into_file "app/assets/javascripts/application.js", after: marker do
contents
end

insert_into_file "app/assets/javascripts/application.js", before: '//= require rails-ujs' do
"//= require jquery3\n"
end
end
end
# rubocop:enable Metrics/MethodLength

private

Expand Down