Class: Geoblacklight::Assets::ImportmapGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/geoblacklight/assets/importmap_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_customizationsObject

Add the customization overrides and insert before bootstrap import



30
31
32
33
34
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 30

def add_customizations
  copy_file "assets/_customizations.scss", "app/assets/stylesheets/_customizations.scss"
  gsub_file "app/assets/stylesheets/_customizations.scss", "@geoblacklight/frontend/app/assets/", ""
  insert_into_file "app/assets/stylesheets/application.bootstrap.scss", "@import 'customizations';\n", before: "@import 'bootstrap/scss/bootstrap';"
end

#add_geoblacklight_jsObject

Import Geoblacklight’s JS using the name that importmap has pinned



60
61
62
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 60

def add_geoblacklight_js
  append_to_file "app/javascript/application.js", "import Geoblacklight from \"geoblacklight\";"
end

#add_geoblacklight_stylesObject

Add an import for Geoblacklight’s stylesheet



50
51
52
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 50

def add_geoblacklight_styles
  append_to_file "app/assets/stylesheets/application.bootstrap.scss", "@import '@geoblacklight/frontend/app/assets/stylesheets/geoblacklight/geoblacklight';"
end

#add_leaflet_ol_css_cdnObject

Add CDN imports for CSS files used by Geoblacklight (leaflet, openlayers)



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 37

def add_leaflet_ol_css_cdn
  insert_into_file "app/assets/stylesheets/application.bootstrap.scss", before: "@import 'customizations';\n" do
    "      /* GeoBlacklight dependencies CSS */\n      @import url(\"https://cdn.skypack.dev/[email protected]/dist/leaflet.css\");\n      @import url(\"https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.fullscreen.css\");\n      @import url(\"https://cdn.skypack.dev/[email protected]/ol.css\");\n\n    SCSS\n  end\nend\n"

#build_stylesObject

Run the build so styles are available for the first load of the app



65
66
67
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 65

def build_styles
  run "yarn build:css"
end

#update_blacklight_importObject

Ensure import of Blacklight’s JS uses the name that importmap has pinned



55
56
57
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 55

def update_blacklight_import
  gsub_file "app/javascript/application.js", "blacklight-frontend", "blacklight"
end

#use_bootstrap_esmObject

Switch bootstrap import to ESM version so we can import individual parts



25
26
27
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 25

def use_bootstrap_esm
  gsub_file "config/importmap.rb", /dist\/js\/bootstrap\.js/, "dist/js/bootstrap.esm.js"
end