twitter-bootstrap-rails 5.5.1 → 5.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +51 -0
  3. data/README.md +84 -5
  4. data/app/assets/javascripts/twitter/bootstrap/bootstrap.min.js +3 -3
  5. data/app/assets/stylesheets/twitter-bootstrap-static/bootstrap.css +3 -3
  6. data/lib/generators/bootstrap/agent/agent_generator.rb +12 -0
  7. data/lib/generators/bootstrap/agent/templates/guide.md.erb +23 -3
  8. data/lib/generators/bootstrap/bootstrap5_markup.rb +22 -0
  9. data/lib/generators/bootstrap/cdn/cdn_generator.rb +32 -2
  10. data/lib/generators/bootstrap/cdn_tags.rb +11 -0
  11. data/lib/generators/bootstrap/install/install_generator.rb +77 -21
  12. data/lib/generators/bootstrap/layout/layout_generator.rb +15 -0
  13. data/lib/generators/bootstrap/layout/templates/layout.html.erb +4 -1
  14. data/lib/generators/bootstrap/layout/templates/layout.html.haml +4 -1
  15. data/lib/generators/bootstrap/layout/templates/layout.html.slim +4 -1
  16. data/lib/generators/bootstrap/nes/nes_generator.rb +101 -0
  17. data/lib/generators/bootstrap/snippets/snippet_base.rb +4 -0
  18. data/lib/generators/bootstrap/stimulus/stimulus_generator.rb +49 -0
  19. data/lib/generators/bootstrap/stimulus/templates/bootstrap_controller.js.tt +67 -0
  20. data/lib/generators/bootstrap/themed/themed_generator.rb +4 -0
  21. data/lib/twitter/bootstrap/rails/config.rb +17 -4
  22. data/lib/twitter/bootstrap/rails/doctor.rb +125 -6
  23. data/lib/twitter/bootstrap/rails/version.rb +37 -5
  24. data/spec/lib/generators/cdn_generator_spec.rb +31 -0
  25. data/spec/lib/generators/doctor_generator_spec.rb +52 -0
  26. data/spec/lib/generators/install_generator_spec.rb +51 -1
  27. data/spec/lib/generators/layout_generator_spec.rb +24 -0
  28. data/spec/lib/generators/nes_generator_spec.rb +164 -0
  29. data/spec/lib/generators/no_hardcoded_versions_spec.rb +3 -1
  30. data/spec/lib/generators/stimulus_generator_spec.rb +65 -0
  31. data/spec/support/generator_spec_helper.rb +2 -0
  32. data/vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js +3 -3
  33. data/vendor/assets/javascripts/twitter/bootstrap/bootstrap.esm.min.js +7 -0
  34. data/vendor/assets/javascripts/twitter/bootstrap/popper.esm.js +15 -0
  35. data/vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css +3 -3
  36. metadata +28 -10
@@ -0,0 +1,67 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ <% if import_bootstrap? -%>
3
+ import * as bootstrap from "bootstrap"
4
+ <% end -%>
5
+
6
+ // Generated by twitter-bootstrap-rails (rails g bootstrap:stimulus).
7
+ //
8
+ // Attach it once, to <body>: <body data-controller="bootstrap">
9
+ //
10
+ // Bootstrap's data attributes (data-bs-toggle="modal", "dropdown", "collapse")
11
+ // keep working on their own. Tooltips and popovers are opt-in, so this sets them
12
+ // up on every Turbo render and disposes of them afterwards. Before Turbo caches
13
+ // a page it also closes open modals, offcanvases and dropdowns, so going back
14
+ // doesn't restore a page with a backdrop stuck over it.
15
+ export default class extends Controller {
16
+ connect() {
17
+ this.beforeCache = this.beforeCache.bind(this)
18
+ document.addEventListener("turbo:before-cache", this.beforeCache)
19
+ this.setUp()
20
+ }
21
+
22
+ disconnect() {
23
+ document.removeEventListener("turbo:before-cache", this.beforeCache)
24
+ this.tearDown()
25
+ }
26
+
27
+ setUp() {
28
+ const bs = this.bootstrap
29
+ if (!bs) return
30
+
31
+ this.element.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el) => bs.Tooltip.getOrCreateInstance(el))
32
+ this.element.querySelectorAll('[data-bs-toggle="popover"]').forEach((el) => bs.Popover.getOrCreateInstance(el))
33
+ }
34
+
35
+ tearDown() {
36
+ const bs = this.bootstrap
37
+ if (!bs) return
38
+
39
+ this.element.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el) => bs.Tooltip.getInstance(el)?.dispose())
40
+ this.element.querySelectorAll('[data-bs-toggle="popover"]').forEach((el) => bs.Popover.getInstance(el)?.dispose())
41
+ }
42
+
43
+ beforeCache() {
44
+ const bs = this.bootstrap
45
+ if (bs) {
46
+ document.querySelectorAll(".modal.show").forEach((el) => bs.Modal.getInstance(el)?.dispose())
47
+ document.querySelectorAll(".offcanvas.show").forEach((el) => bs.Offcanvas.getInstance(el)?.dispose())
48
+ document.querySelectorAll('[data-bs-toggle="dropdown"]').forEach((el) => bs.Dropdown.getInstance(el)?.hide())
49
+ }
50
+ this.tearDown()
51
+
52
+ // dispose() skips the hide transition, so clear what it would have removed.
53
+ document.querySelectorAll(".modal.show, .offcanvas.show").forEach((el) => el.classList.remove("show"))
54
+ document.querySelectorAll(".modal-backdrop, .offcanvas-backdrop").forEach((el) => el.remove())
55
+ document.body.classList.remove("modal-open")
56
+ document.body.style.removeProperty("overflow")
57
+ document.body.style.removeProperty("padding-right")
58
+ }
59
+
60
+ get bootstrap() {
61
+ <% if import_bootstrap? -%>
62
+ return bootstrap
63
+ <% else -%>
64
+ return window.bootstrap
65
+ <% end -%>
66
+ }
67
+ }
@@ -1,9 +1,12 @@
1
1
  require 'rails/generators'
2
2
  require 'rails/generators/generated_attribute'
3
+ require 'generators/bootstrap/bootstrap5_markup'
3
4
 
4
5
  module Bootstrap
5
6
  module Generators
6
7
  class ThemedGenerator < ::Rails::Generators::Base
8
+ include Bootstrap5Markup
9
+
7
10
  source_root File.expand_path('../templates', __FILE__)
8
11
  argument :controller_path, :type => :string
9
12
  argument :model_name, :type => :string, :required => false
@@ -18,6 +21,7 @@ module Bootstrap
18
21
  end
19
22
 
20
23
  def copy_views
24
+ warn_if_nes
21
25
  generate_views
22
26
  end
23
27
 
@@ -4,10 +4,15 @@ module Twitter
4
4
  module Bootstrap
5
5
  module Rails
6
6
  # How the host app loads Bootstrap's assets. `rails g bootstrap:install cdn`
7
- # writes an initializer setting this to :cdn; `bootstrap:layout` reads it to
8
- # pick its default. Defaults to :static so the layout generator works whether
9
- # or not that initializer exists.
10
- VALID_ASSET_MODES = [:static, :cdn].freeze
7
+ # (or importmap) and `rails g bootstrap:nes` write an initializer setting
8
+ # this; `bootstrap:layout` reads it to pick its default. Defaults to :static
9
+ # so the layout generator works whether or not that initializer exists.
10
+ #
11
+ # :static vendored files, linked through the asset pipeline
12
+ # :cdn jsDelivr tags with Subresource Integrity
13
+ # :importmap vendored stylesheet, ES modules pinned in config/importmap.rb
14
+ # :nes HeroDevs Bootstrap NES 4.6 from its private npm registry
15
+ VALID_ASSET_MODES = [:static, :cdn, :importmap, :nes].freeze
11
16
 
12
17
  @asset_mode = :static
13
18
 
@@ -27,6 +32,14 @@ module Twitter
27
32
  asset_mode == :cdn
28
33
  end
29
34
 
35
+ def importmap?
36
+ asset_mode == :importmap
37
+ end
38
+
39
+ def nes?
40
+ asset_mode == :nes
41
+ end
42
+
30
43
  # Which asset pipeline the host app runs. Propshaft (the Rails 8 default)
31
44
  # has no Sprockets directives: a `//= require` line in a manifest is an
32
45
  # inert comment there, so assets have to be linked explicitly instead.
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  require 'twitter/bootstrap/rails/version'
2
3
  require 'twitter/bootstrap/rails/config'
3
4
  require 'twitter/bootstrap/rails/legacy_markup'
@@ -15,6 +16,8 @@ module Twitter
15
16
  SCANNED_EXTENSIONS = %w[.erb .haml .slim .rb].freeze
16
17
  PLACEHOLDER_IMAGES = ["bootstrap-docs.png", "bootstrap-themes.png", "/docs/5.3/assets/", "unsplash-photo-"].freeze
17
18
  SEVERITY = %w[ok warn error].freeze
19
+ IMPORTMAP = "config/importmap.rb".freeze
20
+ APPLICATION_JS = "app/javascript/application.js".freeze
18
21
  # What `--v6` scans on top of views and helpers. The gem's own vendored
19
22
  # Bootstrap files are skipped: replacing them is the first migration step.
20
23
  V6_EXTRA_FILES = "app/{javascript,assets/javascripts,assets/stylesheets}/**/*".freeze
@@ -27,7 +30,12 @@ module Twitter
27
30
 
28
31
  # v6: true adds a v6_migration check and list for apps moving to Bootstrap 6.
29
32
  def report(v6: false)
30
- checks = [css_check, js_check, version_check, sprite_check, placeholder_check, legacy_check]
33
+ checks = if nes?
34
+ [nes_package_check, nes_registry_check, importmap_tags_check]
35
+ else
36
+ [css_check, js_check, importmap_tags_check, version_check]
37
+ end
38
+ checks += [sprite_check, placeholder_check, legacy_check]
31
39
  checks << v6_check if v6
32
40
 
33
41
  result = facts.merge(
@@ -40,7 +48,8 @@ module Twitter
40
48
 
41
49
  def facts
42
50
  {
43
- :bootstrap_version => BOOTSTRAP_VERSION,
51
+ :bootstrap_version => nes? ? BOOTSTRAP_NES[:bootstrap_version] : BOOTSTRAP_VERSION,
52
+ :bootstrap_distribution => nes? ? BOOTSTRAP_NES[:name] : "Bootstrap",
44
53
  :gem_version => VERSION,
45
54
  :rails_version => ::Rails.version,
46
55
  :asset_pipeline => pipeline.to_s,
@@ -51,14 +60,19 @@ module Twitter
51
60
 
52
61
  private
53
62
 
63
+ def nes?
64
+ asset_mode == "nes"
65
+ end
66
+
54
67
  def pipeline
55
68
  Twitter::Bootstrap::Rails.asset_pipeline
56
69
  end
57
70
 
58
71
  def asset_mode
59
- initializer = read("config/initializers/bootstrap.rb")
60
- cdn = initializer.match?(/asset_mode\s*=\s*:cdn/) || layouts.match?(%r{cdn\.jsdelivr\.net/npm/bootstrap@})
61
- cdn ? "cdn" : "static"
72
+ @asset_mode ||= begin
73
+ recorded = read("config/initializers/bootstrap.rb")[/asset_mode\s*=\s*:(\w+)/, 1]
74
+ recorded || (layouts.match?(%r{cdn\.jsdelivr\.net/npm/bootstrap@}) ? "cdn" : "static")
75
+ end
62
76
  end
63
77
 
64
78
  def template_engine
@@ -77,6 +91,8 @@ module Twitter
77
91
  end
78
92
 
79
93
  def js_check
94
+ return importmap_js_check if asset_mode == "importmap"
95
+
80
96
  asset_check("bootstrap_js", "JavaScript",
81
97
  :cdn => %r{bootstrap@[\d.]+/dist/js/bootstrap},
82
98
  :vendored_name => "twitter/bootstrap/bootstrap.bundle.min",
@@ -105,6 +121,96 @@ module Twitter
105
121
  "or link it from your own layout (`rails g bootstrap:cdn` prints the tags).")
106
122
  end
107
123
 
124
+ def importmap_js_check
125
+ pinned = read(IMPORTMAP).match?(/^\s*pin\s+["']bootstrap["']/)
126
+ unless pinned
127
+ return check("bootstrap_js", "error", "#{IMPORTMAP} does not pin \"bootstrap\", so `import \"bootstrap\"` fails in the browser.",
128
+ "Run `rails g bootstrap:install importmap`.")
129
+ end
130
+
131
+ vendored = read(IMPORTMAP)[/^\s*pin\s+["']bootstrap["']\s*,\s*to:\s*["']([^"']+)["']/, 1]
132
+ if vendored && !vendored.start_with?("http") && !exist?(File.join("vendor/javascript", vendored))
133
+ return check("bootstrap_js", "error", "#{IMPORTMAP} pins bootstrap to vendor/javascript/#{vendored}, which does not exist.",
134
+ "Run `rails g bootstrap:install importmap`.")
135
+ end
136
+
137
+ unless javascript_sources.match?(/^\s*import\s+(?:\*\s+as\s+\w+\s+from\s+|\{[^}]*\}\s+from\s+)?["']bootstrap["']/)
138
+ return check("bootstrap_js", "error", "Bootstrap is pinned, but nothing in app/javascript imports it, so it never runs.",
139
+ "Add `import \"bootstrap\"` to #{APPLICATION_JS}.")
140
+ end
141
+
142
+ check("bootstrap_js", "ok", "Bootstrap JavaScript is pinned in the importmap and imported.")
143
+ end
144
+
145
+ def nes_package_check
146
+ json = package_json
147
+ spec = json.dig("dependencies", "bootstrap").to_s
148
+ prefix = "npm:#{BOOTSTRAP_NES[:package]}@"
149
+
150
+ unless spec.start_with?(prefix)
151
+ return check("nes_package", "error",
152
+ "This app is set to NES mode, but package.json's bootstrap dependency is #{spec.empty? ? 'missing' : spec.inspect}, not the NES package.",
153
+ "Run `rails g bootstrap:nes`.")
154
+ end
155
+
156
+ version = spec.delete_prefix(prefix)
157
+ if version != BOOTSTRAP_NES[:version]
158
+ return check("nes_package", "warn",
159
+ "package.json uses #{BOOTSTRAP_NES[:name]} #{version}; the latest this gem knows is #{BOOTSTRAP_NES[:version]}.",
160
+ "Run `rails g bootstrap:nes`, then `npm install`.")
161
+ end
162
+
163
+ unless json.dig("overrides", "bootstrap")
164
+ return check("nes_package", "warn", "package.json has no override for bootstrap, so a dependency could still pull in an unpatched Bootstrap.",
165
+ "Run `rails g bootstrap:nes`.")
166
+ end
167
+
168
+ check("nes_package", "ok", "package.json uses #{BOOTSTRAP_NES[:name]} #{version}, with an override.")
169
+ end
170
+
171
+ def nes_registry_check
172
+ npmrc = read(".npmrc")
173
+ registry = BOOTSTRAP_NES[:registry]
174
+ unless npmrc.include?("@neverendingsupport:registry=#{registry}")
175
+ return check("nes_registry", "error", ".npmrc doesn't point @neverendingsupport at #{registry}, so npm can't find the NES package.",
176
+ "Run `rails g bootstrap:nes`.")
177
+ end
178
+
179
+ auth = npmrc[/^#{Regexp.escape(registry.sub(/\Ahttps?:/, ''))}:_authToken=(.*)$/, 1]
180
+ if auth.nil?
181
+ return check("nes_registry", "error", ".npmrc has no access token line for the NES registry.",
182
+ "Run `rails g bootstrap:nes`, then export #{BOOTSTRAP_NES[:token_env]}.")
183
+ end
184
+
185
+ unless auth.strip.match?(/\A\$\{\w+\}\z/)
186
+ # Never echo the value: it is a credential.
187
+ return check("nes_registry", "warn", ".npmrc contains the NES access token itself, which leaks it if the file is committed.",
188
+ "Replace it with ${#{BOOTSTRAP_NES[:token_env]}} and set that environment variable instead.")
189
+ end
190
+
191
+ check("nes_registry", "ok", ".npmrc reads the NES access token from #{auth.strip}.")
192
+ end
193
+
194
+ def package_json
195
+ JSON.parse(read("package.json"))
196
+ rescue JSON::ParserError
197
+ {}
198
+ end
199
+
200
+ def importmap_tags_check
201
+ return check("importmap_tags", "ok", "The app does not use importmap.") unless exist?(IMPORTMAP)
202
+ return check("importmap_tags", "ok", "A layout loads the importmap.") if layouts.include?("javascript_importmap_tags")
203
+
204
+ check("importmap_tags", "warn",
205
+ "#{IMPORTMAP} exists, but no layout calls javascript_importmap_tags, so the app's own JavaScript " \
206
+ "(Turbo, Stimulus, an imported Bootstrap) never loads.",
207
+ "Add <%= javascript_importmap_tags %> to the <head> of your layout, or re-run `rails g bootstrap:layout`.")
208
+ end
209
+
210
+ def javascript_sources
211
+ relative_files("app/javascript/**/*", %w[.js .mjs]).map { |file| read(file) }.join("\n")
212
+ end
213
+
108
214
  def linked_through_pipeline?(source)
109
215
  propshaft_link = layouts.include?(source[:vendored_name])
110
216
  sprockets_link = layouts.match?(source[:layout_tag]) && read(source[:manifest]).match?(source[:manifest_entry])
@@ -121,7 +227,7 @@ module Twitter
121
227
  end
122
228
 
123
229
  def version_check
124
- found = layouts.scan(/bootstrap@(\d+\.\d+\.\d+)/).flatten
230
+ found = layouts.scan(/bootstrap@(\d+\.\d+\.\d+(?:-[\w.]+)?)/).flatten
125
231
  [VENDORED_CSS, VENDORED_JS].each do |file|
126
232
  found.concat(read(file)[0, 400].to_s.scan(/Bootstrap\s+v(\d+\.\d+\.\d+)/).flatten)
127
233
  end
@@ -129,6 +235,12 @@ module Twitter
129
235
 
130
236
  return check("bootstrap_version", "ok", "No Bootstrap version other than #{BOOTSTRAP_VERSION} found.") if stale.empty?
131
237
 
238
+ if stale.any? { |version| version.start_with?("6.") }
239
+ return check("bootstrap_version", "warn",
240
+ "Found Bootstrap #{stale.join(', ')}. This gem ships #{BOOTSTRAP_VERSION}, and its generators and helpers write Bootstrap 5 markup.",
241
+ "Run `rails g bootstrap:doctor --v6` for the Bootstrap 5 markup to migrate.")
242
+ end
243
+
132
244
  check("bootstrap_version", "warn",
133
245
  "Found Bootstrap #{stale.join(', ')}, but this gem ships #{BOOTSTRAP_VERSION}.",
134
246
  "Run `rails g bootstrap:install static --force` or update the CDN tags with `rails g bootstrap:cdn`.")
@@ -165,6 +277,11 @@ module Twitter
165
277
  end
166
278
 
167
279
  def legacy_check
280
+ if nes?
281
+ return check("legacy_markup", "ok",
282
+ "Skipped: this app runs Bootstrap #{BOOTSTRAP_NES[:bootstrap_version]} (NES), where Bootstrap 4 markup is correct.")
283
+ end
284
+
168
285
  count = legacy_markup.size
169
286
  return check("legacy_markup", "ok", "No older Bootstrap markup found.") if count.zero?
170
287
 
@@ -173,6 +290,8 @@ module Twitter
173
290
  end
174
291
 
175
292
  def legacy_markup
293
+ return [] if nes?
294
+
176
295
  @legacy_markup ||= scanned_files.flat_map do |file|
177
296
  LegacyMarkup.scan(read(file), File.extname(file)).map { |finding| { :file => file }.merge(finding) }
178
297
  end
@@ -1,11 +1,11 @@
1
1
  module Twitter
2
2
  module Bootstrap
3
3
  module Rails
4
- VERSION = "5.5.1"
4
+ VERSION = "5.6.1"
5
5
 
6
6
  # The Bootstrap release this gem vendors and links to. The gem version no
7
7
  # longer tracks it; this constant is the authoritative statement.
8
- BOOTSTRAP_VERSION = "5.3.8"
8
+ BOOTSTRAP_VERSION = "5.3.9"
9
9
  POPPER_VERSION = "2.11.8"
10
10
 
11
11
  # URLs and Subresource Integrity hashes exactly as published on
@@ -14,21 +14,53 @@ module Twitter
14
14
  BOOTSTRAP_CDN = {
15
15
  :css => {
16
16
  :url => "https://cdn.jsdelivr.net/npm/bootstrap@#{BOOTSTRAP_VERSION}/dist/css/bootstrap.min.css",
17
- :integrity => "sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB"
17
+ :integrity => "sha384-yEPZ7jP9S0QKiUQ0m3jjsO4ProIW7wvR9gXkI7JOGa2XVsw53Yke2bsdkBbjmmiX"
18
18
  },
19
19
  :bundle => {
20
20
  :url => "https://cdn.jsdelivr.net/npm/bootstrap@#{BOOTSTRAP_VERSION}/dist/js/bootstrap.bundle.min.js",
21
- :integrity => "sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
21
+ :integrity => "sha384-WyOxWvTyr1QaMPfiPJSv76iReVthorl5uWlFtZPyK0IEB0hGzo9y5Bp9mYanr8Bu"
22
22
  },
23
23
  :js => {
24
24
  :url => "https://cdn.jsdelivr.net/npm/bootstrap@#{BOOTSTRAP_VERSION}/dist/js/bootstrap.min.js",
25
- :integrity => "sha384-G/EV+4j2dNv+tEPo3++6LCgdCROaejBqfUeNjuKAiuXbjrxilcCdDz6ZAVfHWe1Y"
25
+ :integrity => "sha384-89aa4glufciPv46aKltztWQEmk+Tu9W2RhzXr6VcnUUEub7F8+06RcL2RYLCOHUq"
26
26
  },
27
27
  :popper => {
28
28
  :url => "https://cdn.jsdelivr.net/npm/@popperjs/core@#{POPPER_VERSION}/dist/umd/popper.min.js",
29
29
  :integrity => "sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
30
30
  }
31
31
  }.freeze
32
+
33
+ # The Bootstrap 6 prerelease that `rails g bootstrap:cdn --bootstrap=6` prints.
34
+ # The gem's generators and helpers still write Bootstrap 5 markup, so this is
35
+ # only for trying 6 out or migrating to it. Its bundle is an ES module and
36
+ # includes Floating UI, so there is no separate Popper pair. The integrity
37
+ # hashes are sha384 of the files jsDelivr serves for this version.
38
+ BOOTSTRAP_NEXT_VERSION = "6.0.0-alpha.1"
39
+ BOOTSTRAP_NEXT_CDN = {
40
+ :css => {
41
+ :url => "https://cdn.jsdelivr.net/npm/bootstrap@#{BOOTSTRAP_NEXT_VERSION}/dist/css/bootstrap.min.css",
42
+ :integrity => "sha384-B/GM4XqrwHnWXNOWMbloTmrYXZg10cakYGmpfsR/bbzQ6JAJI4ihuyADKLnBgrCe"
43
+ },
44
+ :bundle => {
45
+ :url => "https://cdn.jsdelivr.net/npm/bootstrap@#{BOOTSTRAP_NEXT_VERSION}/dist/js/bootstrap.bundle.min.js",
46
+ :integrity => "sha384-1a/pXj49ZQ1aHEmrJ+gMw1otqoVsYwlEnlD8mIfY2TV03r20Y0CN7uqx1tQogjPL"
47
+ }
48
+ }.freeze
49
+
50
+ # HeroDevs Bootstrap NES (Never-Ending Support): security-patched Bootstrap 4.6
51
+ # for apps that can't move to 5 yet. It is a paid product served only from
52
+ # HeroDevs' private npm registry, so the gem can't vendor it; `rails g
53
+ # bootstrap:nes` points an app's package.json and .npmrc at it instead.
54
+ # Source: https://docs.herodevs.com/bootstrap/bootstrap-4-6 and its release notes.
55
+ BOOTSTRAP_NES = {
56
+ :name => "HeroDevs Bootstrap NES",
57
+ :package => "@neverendingsupport/bootstrap",
58
+ :version => "4.6.2-bootstrap-4.6.5",
59
+ :bootstrap_version => "4.6.5",
60
+ :registry => "https://registry.nes.herodevs.com/npm/pkg/",
61
+ :token_env => "HERODEVS_NES_TOKEN",
62
+ :docs => "https://docs.herodevs.com/bootstrap/bootstrap-4-6"
63
+ }.freeze
32
64
  end
33
65
  end
34
66
  end
@@ -39,4 +39,35 @@ describe Bootstrap::Generators::CdnGenerator do
39
39
 
40
40
  expect(Dir.children(destination_root)).to be_empty
41
41
  end
42
+ describe "--bootstrap=6" do
43
+ let(:next_cdn) { Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_CDN }
44
+
45
+ it "prints the Bootstrap 6 prerelease tags, with the bundle as an ES module" do
46
+ output = run_generator(described_class, %w[--bootstrap=6])
47
+
48
+ expect(output).to include(next_cdn[:css][:url], next_cdn[:css][:integrity])
49
+ expect(output).to include(%{<script type="module" src="#{next_cdn[:bundle][:url]}" integrity="#{next_cdn[:bundle][:integrity]}"})
50
+ expect(output).not_to include(cdn[:css][:url])
51
+ end
52
+
53
+ it "warns that it is a prerelease the gem's markup does not match" do
54
+ output = run_generator(described_class, %w[--bootstrap=6])
55
+
56
+ expect(output).to include(Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION).and include("bootstrap:doctor --v6")
57
+ end
58
+
59
+ it "points at Bootstrap 6 files" do
60
+ expect(next_cdn.values.map { |entry| entry[:url] }).to all(include("bootstrap@#{Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION}/"))
61
+ end
62
+
63
+ it "has no separate Popper pair" do
64
+ expect { run_generator(described_class, %w[--bootstrap=6 --separate-popper]) }
65
+ .to raise_error(Rails::Generators::Error, /separate-popper/)
66
+ end
67
+
68
+ it "rejects other versions" do
69
+ expect { run_generator(described_class, %w[--bootstrap=4]) }
70
+ .to raise_error(Rails::Generators::Error, /bootstrap:nes/)
71
+ end
72
+ end
42
73
  end
@@ -74,6 +74,50 @@ describe Bootstrap::Generators::DoctorGenerator do
74
74
  expect(check("bootstrap_js")[:status]).to eq("warn")
75
75
  end
76
76
 
77
+ describe "on importmap" do
78
+ def install_importmap
79
+ rails_mod.asset_pipeline = :propshaft
80
+ write_destination("config/importmap.rb", %{pin "application"\n})
81
+ write_destination("app/javascript/application.js", %{import "@hotwired/turbo-rails"\n})
82
+ run_generator(Bootstrap::Generators::InstallGenerator, %w[importmap])
83
+ rails_mod.asset_mode = :importmap
84
+ run_generator(Bootstrap::Generators::LayoutGenerator)
85
+ end
86
+
87
+ it "is happy with an app set up by the generators" do
88
+ install_importmap
89
+
90
+ expect(report).to include(:status => "ok", :asset_mode => "importmap")
91
+ expect(check("bootstrap_js")[:message]).to include("importmap")
92
+ end
93
+
94
+ it "reports an error when Bootstrap is not pinned" do
95
+ install_importmap
96
+ write_destination("config/importmap.rb", %{pin "application"\n})
97
+
98
+ expect(check("bootstrap_js")).to include(:status => "error", :fix => a_string_including("bootstrap:install importmap"))
99
+ end
100
+
101
+ it "reports an error when nothing imports Bootstrap" do
102
+ install_importmap
103
+ write_destination("app/javascript/application.js", %{import "@hotwired/turbo-rails"\n})
104
+
105
+ expect(check("bootstrap_js")).to include(:status => "error", :message => a_string_including("imports"))
106
+ end
107
+
108
+ it "warns when no layout loads the importmap, which drops the app's own JavaScript" do
109
+ install_importmap
110
+ write_destination("app/views/layouts/application.html.erb",
111
+ %{<%= stylesheet_link_tag "twitter/bootstrap/bootstrap.min" %>})
112
+
113
+ expect(check("importmap_tags")).to include(:status => "warn", :fix => a_string_including("javascript_importmap_tags"))
114
+ end
115
+ end
116
+
117
+ it "has nothing to say about importmap tags in an app without importmap" do
118
+ expect(check("importmap_tags")[:status]).to eq("ok")
119
+ end
120
+
77
121
  it "reads the mode from the layout if the initializer is missing" do
78
122
  write_destination("app/views/layouts/application.html.erb",
79
123
  %{<link href="https://cdn.jsdelivr.net/npm/bootstrap@#{version}/dist/css/bootstrap.min.css">})
@@ -99,6 +143,14 @@ describe Bootstrap::Generators::DoctorGenerator do
99
143
  expect(check("bootstrap_version")).to include(:status => "warn", :message => a_string_including("5.3.3"))
100
144
  end
101
145
 
146
+ it "names a Bootstrap 6 prerelease and points at the migration worklist" do
147
+ write_destination("app/views/layouts/application.html.erb",
148
+ %{<link href="https://cdn.jsdelivr.net/npm/bootstrap@6.0.0-alpha.1/dist/css/bootstrap.min.css">})
149
+
150
+ expect(check("bootstrap_version")).to include(:status => "warn", :message => a_string_including("6.0.0-alpha.1"),
151
+ :fix => a_string_including("--v6"))
152
+ end
153
+
102
154
  it "accepts what the generators installed" do
103
155
  install_static_on_propshaft
104
156
 
@@ -140,9 +140,59 @@ describe Bootstrap::Generators::InstallGenerator do
140
140
  end
141
141
  end
142
142
 
143
+ describe "importmap" do
144
+ let(:esm) { "vendor/javascript/twitter/bootstrap/bootstrap.esm.min.js" }
145
+ let(:popper) { "vendor/javascript/twitter/bootstrap/popper.esm.js" }
146
+
147
+ before do
148
+ Twitter::Bootstrap::Rails.asset_pipeline = :propshaft
149
+ write_destination("config/importmap.rb", %{pin "application"\n})
150
+ write_destination("app/javascript/application.js", %{import "@hotwired/turbo-rails"\n})
151
+ end
152
+
153
+ it "vendors the ES module builds of Bootstrap and Popper, and the stylesheet" do
154
+ run_generator(described_class, %w[importmap])
155
+
156
+ expect(read_destination(esm)).to start_with("/*!\n * Bootstrap v#{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION} ")
157
+ expect(read_destination(popper)).to include("@popperjs/core v#{Twitter::Bootstrap::Rails::POPPER_VERSION}")
158
+ expect(destination_exist?("app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css")).to be true
159
+ expect(destination_exist?("app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js")).to be false
160
+ end
161
+
162
+ it "pins both modules and imports Bootstrap from application.js" do
163
+ run_generator(described_class, %w[importmap])
164
+
165
+ expect(read_destination("config/importmap.rb"))
166
+ .to include(%{pin "bootstrap", to: "twitter/bootstrap/bootstrap.esm.min.js"})
167
+ .and include(%{pin "@popperjs/core", to: "twitter/bootstrap/popper.esm.js"})
168
+ expect(read_destination("app/javascript/application.js")).to include(%{import "bootstrap"})
169
+ end
170
+
171
+ it "does not pin or import twice when run again" do
172
+ run_generator(described_class, %w[importmap])
173
+ run_generator(described_class, %w[importmap --force])
174
+
175
+ expect(read_destination("config/importmap.rb").scan(/pin "bootstrap"/).size).to eq(1)
176
+ expect(read_destination("app/javascript/application.js").scan(/import "bootstrap"/).size).to eq(1)
177
+ end
178
+
179
+ it "writes an initializer selecting importmap mode" do
180
+ run_generator(described_class, %w[importmap])
181
+
182
+ expect(read_destination("config/initializers/bootstrap.rb")).to include("asset_mode = :importmap")
183
+ end
184
+
185
+ it "refuses an app without importmap" do
186
+ FileUtils.rm(destination_path("config/importmap.rb"))
187
+
188
+ expect { run_generator(described_class, %w[importmap]) }
189
+ .to raise_error(Rails::Generators::Error, /config\/importmap\.rb/)
190
+ end
191
+ end
192
+
143
193
  it "rejects an unknown mode" do
144
194
  expect {
145
195
  run_generator(described_class, %w[less])
146
- }.to raise_error(Rails::Generators::Error, /Expected 'static' or 'cdn'/)
196
+ }.to raise_error(Rails::Generators::Error, /Expected one of static, cdn, importmap/)
147
197
  end
148
198
  end
@@ -121,4 +121,28 @@ describe Bootstrap::Generators::LayoutGenerator do
121
121
  expect(layout).to include('stylesheet_link_tag "application"')
122
122
  expect(layout).not_to include("cdn.jsdelivr.net")
123
123
  end
124
+ describe "on an importmap app" do
125
+ before do
126
+ Twitter::Bootstrap::Rails.asset_pipeline = :propshaft
127
+ write_destination("config/importmap.rb", %{pin "application"\n})
128
+ end
129
+
130
+ [:erb, :haml, :slim].each do |engine|
131
+ it "keeps javascript_importmap_tags in #{engine}, so the app's own JavaScript still loads" do
132
+ expect(generate_layout([], :engine => engine)).to include("javascript_importmap_tags")
133
+ end
134
+
135
+ it "leaves Bootstrap's JavaScript to the importmap in importmap mode, in #{engine}" do
136
+ Twitter::Bootstrap::Rails.asset_mode = :importmap
137
+ layout = generate_layout([], :engine => engine)
138
+
139
+ expect(layout).to include('stylesheet_link_tag "twitter/bootstrap/bootstrap.min", "application"')
140
+ expect(layout).not_to include("bootstrap.bundle")
141
+ end
142
+ end
143
+ end
144
+
145
+ it "adds no importmap tags to an app without importmap" do
146
+ expect(generate_layout).not_to include("javascript_importmap_tags")
147
+ end
124
148
  end