Class: BlacklightRangeLimit::JsbundlingBl7FixupGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/blacklight_range_limit/jsbundling_bl7_fixup_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_blacklight7_esm_importsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/generators/blacklight_range_limit/jsbundling_bl7_fixup_generator.rb', line 44

def add_blacklight7_esm_imports
  js_dir = "app/javascript"
  app_js_file = js_dir + "/application.js"

  unless Pathname(app_js_file).exist?
    raise "Cannot find file to set up at #{app_js_file}"
  end

  # Need to setup some things BEFORE actual blacklight imports, to work right
  create_file (js_dir + "/blacklight_dependency_setup.js") do
    <<~EOS
      // Making JQuery from ESM available to Blacklight 7 and Bootstrap 4 that want
      // it in window globals.
      import $ from 'jquery'
      window.jQuery = window.$ = $

      // Bootstrap 4 also needs Popper, and needs it installed in window.Popper
      import Popper from 'popper.js';
      window.Popper = Popper;
    EOS
  end

  append_to_file app_js_file do
    <<~EOS
      import "bootstrap";
      import "./blacklight_dependency_setup.js"

      //import 'blacklight-frontend/app/assets/javascripts/blacklight/blacklight';
      // for some reason we need these all like this to work, can we figure out why?

      import 'blacklight-frontend/app/javascript/blacklight/core';
      import 'blacklight-frontend/app/javascript/blacklight/bookmark_toggle';
      import 'blacklight-frontend/app/javascript/blacklight/button_focus';
      import 'blacklight-frontend/app/javascript/blacklight/checkbox_submit';
      import 'blacklight-frontend/app/javascript/blacklight/facet_load';
      import 'blacklight-frontend/app/javascript/blacklight/modal';
      import 'blacklight-frontend/app/javascript/blacklight/search_context';
    EOS
  end
end

#add_blacklight7_sass_esm_importObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/blacklight_range_limit/jsbundling_bl7_fixup_generator.rb', line 85

def add_blacklight7_sass_esm_import
  # only if we're using propshaft and not sprockets: We are using cssbundling-rails with
  # sass, and we need to add a sass import from blacklight npm package -- that BL7 geenrator
  # didn't know how to do.  (BL8 generator prob does!)
  if !defined?(Sprockets) && defined?(Propshaft)
    append_to_file 'app/assets/stylesheets/application.bootstrap.scss' do
      <<~CONTENT
        @import "blacklight-frontend/app/assets/stylesheets/blacklight/blacklight";
      CONTENT
    end
  end
end

#add_blacklight_dependencies_to_package_jsonObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/generators/blacklight_range_limit/jsbundling_bl7_fixup_generator.rb', line 20

def add_blacklight_dependencies_to_package_json
  # there is no blacklight 7.39.0 or 7.40.0 makes it hard for us to know what version to
  # generate here, I guess we'll try generating an open range like "^7.0.0"?

  # while blacklight7 may work with bootstrap 5, we'll test with 4 for now, and popper 1.x that goes with it
  run %{yarn add --non-interactive
          blacklight-frontend@^#{Blacklight::VERSION.split(".").first}.0.0
          bootstrap@^4.1.0
          popper.js@^1.16.0}.squish, abort_on_failure: true
end

#guard_test_app_onlyObject



14
15
16
17
18
# File 'lib/generators/blacklight_range_limit/jsbundling_bl7_fixup_generator.rb', line 14

def guard_test_app_only
  unless Rails.application.class.name == "Internal::Application"
    raise "For safety, this generator can only be run in a test app, with app name 'test', not '#{Rails.application.name}'"
  end
end

#remove_default_stimulus_codeObject

NOTE this is why you don’t want to run this in a real app!!!



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/blacklight_range_limit/jsbundling_bl7_fixup_generator.rb', line 32

def remove_default_stimulus_code
  # Due to a bug or something, import of stimulus will cause a problem with
  # esbuild, in the presence of Blacklight layout's default application.js
  # script tag lacking type=module
  #
  # generated BL app isn't using this stuff, we will just remove the include
  #
  # SEE: https://gist.github.com/pch/fe276b29ba037bdaeaa525932478ca18

  gsub_file("app/javascript/application.js", %r{^ *import +["']\./controllers.*$}, '')
end