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 SCSS customization overrides and insert before bootstrap import



32
33
34
35
36
37
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 32

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



63
64
65
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 63

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



53
54
55
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 53

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)



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 40

def add_leaflet_ol_css_cdn
  insert_into_file "app/assets/stylesheets/application.bootstrap.scss", before: "@import 'customizations';\n" do
    <<~SCSS
      /* GeoBlacklight dependencies CSS */
      @import url("https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css");
      @import url("https://cdn.jsdelivr.net/npm/leaflet.fullscreen@5.3.0/dist/Control.FullScreen.css");
      @import url("https://cdn.jsdelivr.net/npm/ol@8.1.0/ol.css");

    SCSS
  end
end

#build_stylesObject

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



68
69
70
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 68

def build_styles
  run "yarn build:css"
end

#update_blacklight_importObject

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



58
59
60
# File 'lib/generators/geoblacklight/assets/importmap_generator.rb', line 58

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

#use_bootstrap_esmObject

Switch Blacklight’s bootstrap and popper imports to ESM versions so we can import individual modules



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

def use_bootstrap_esm
  gsub_file "config/importmap.rb", %r{dist/js/bootstrap.js}, "dist/js/bootstrap.esm.js"
  gsub_file "config/importmap.rb", %r{dist/umd/popper.min.js}, "dist/esm/popper.js"
end