twitter-bootstrap-rails 5.5.1 → 5.6.0

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 (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -0
  3. data/README.md +83 -4
  4. data/lib/generators/bootstrap/agent/agent_generator.rb +12 -0
  5. data/lib/generators/bootstrap/agent/templates/guide.md.erb +23 -3
  6. data/lib/generators/bootstrap/bootstrap5_markup.rb +22 -0
  7. data/lib/generators/bootstrap/cdn/cdn_generator.rb +32 -2
  8. data/lib/generators/bootstrap/cdn_tags.rb +11 -0
  9. data/lib/generators/bootstrap/install/install_generator.rb +77 -21
  10. data/lib/generators/bootstrap/layout/layout_generator.rb +15 -0
  11. data/lib/generators/bootstrap/layout/templates/layout.html.erb +4 -1
  12. data/lib/generators/bootstrap/layout/templates/layout.html.haml +4 -1
  13. data/lib/generators/bootstrap/layout/templates/layout.html.slim +4 -1
  14. data/lib/generators/bootstrap/nes/nes_generator.rb +101 -0
  15. data/lib/generators/bootstrap/snippets/snippet_base.rb +4 -0
  16. data/lib/generators/bootstrap/stimulus/stimulus_generator.rb +49 -0
  17. data/lib/generators/bootstrap/stimulus/templates/bootstrap_controller.js.tt +67 -0
  18. data/lib/generators/bootstrap/themed/themed_generator.rb +4 -0
  19. data/lib/twitter/bootstrap/rails/config.rb +17 -4
  20. data/lib/twitter/bootstrap/rails/doctor.rb +125 -6
  21. data/lib/twitter/bootstrap/rails/version.rb +33 -1
  22. data/spec/lib/generators/cdn_generator_spec.rb +31 -0
  23. data/spec/lib/generators/doctor_generator_spec.rb +52 -0
  24. data/spec/lib/generators/install_generator_spec.rb +51 -1
  25. data/spec/lib/generators/layout_generator_spec.rb +24 -0
  26. data/spec/lib/generators/nes_generator_spec.rb +164 -0
  27. data/spec/lib/generators/no_hardcoded_versions_spec.rb +3 -1
  28. data/spec/lib/generators/stimulus_generator_spec.rb +65 -0
  29. data/spec/support/generator_spec_helper.rb +2 -0
  30. data/vendor/assets/javascripts/twitter/bootstrap/bootstrap.esm.min.js +7 -0
  31. data/vendor/assets/javascripts/twitter/bootstrap/popper.esm.js +15 -0
  32. metadata +28 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 875edfcbadfdb5b579da81a68c3a5dc44d7b14d208bcf7de6dc89a8dabe969d3
4
- data.tar.gz: bc3f64fe1f6c45217d043887a9fbcab629e7161738b2c47c3ea46a0a830b1cb1
3
+ metadata.gz: 34e03d74439b02f3ce1da3ea93b4a9298978a32af11e82b1a3dd48526c1d0c1f
4
+ data.tar.gz: ddd677d336c9b7ae98479f85f95949c8530d69cf12a1d7865427de880f850abe
5
5
  SHA512:
6
- metadata.gz: 18bd87131c776f3da869462fb6af504614dd708a91434881a9e46955a3e7817cdbcd8071e3d490a5bd18841b3601016e632e73163071786c026b5a2fb52cfc74
7
- data.tar.gz: 75630ca82664a7956d088fbc90b48d2eb1f11e89087488dcb7a5b209221aff58ccabc2296546a4be9fc83595109b6b31fc41db9be8e931a23818ac8f6dea8841
6
+ metadata.gz: 5939e73a695d58c36e2717857949625f67ae53d0632954d075a2d5d0c5b6465a2e43eae1b14285dc1cf03ac792ca3907737f8b4ed9f74c5a38a89690ae324381
7
+ data.tar.gz: 6c841f7baf8afc2bc4b83210d370c27ef0cd519c48b782f018e1c37b524e114fc7e1929cfdb429c73eae5c7fb023462d4074bd15e3cfa7a2745e1d753e319f3f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,44 @@
1
+ ## 5.6.0
2
+
3
+ ### Added
4
+
5
+ - **`rails g bootstrap:install importmap`.** Vendors the stylesheet and Bootstrap's ES
6
+ module build, pins `bootstrap` and `@popperjs/core` in `config/importmap.rb`, and
7
+ imports Bootstrap from `app/javascript/application.js`. Popper's official UMD build
8
+ is wrapped with ES module exports, because Popper ships no single-file ES module.
9
+ - **`rails g bootstrap:stimulus`** writes a Stimulus controller that sets up tooltips
10
+ and popovers on each Turbo render, disposes of them afterwards, and closes modals,
11
+ offcanvases and dropdowns before Turbo caches a page.
12
+ - **`rails g bootstrap:nes`** sets up HeroDevs Bootstrap NES 4.6.5 (paid,
13
+ security-patched Bootstrap 4.6) through npm. It adds the aliased package and an
14
+ override to `package.json`, and the registry to `.npmrc` with the token read from
15
+ `${HERODEVS_NES_TOKEN}`. The doctor gets `nes_package` and `nes_registry` checks
16
+ and skips the legacy-markup scan. The agent guide tells agents to write Bootstrap 4
17
+ markup, and the Bootstrap 5 generators warn.
18
+ - **`rails g bootstrap:cdn --bootstrap=6`** prints the Bootstrap 6.0.0-alpha.1 tags,
19
+ with the bundle as an ES module. The doctor names a Bootstrap 6 prerelease and
20
+ points at `--v6`.
21
+ - The doctor has an `importmap_tags` check.
22
+ - The upstream `bootstrap-v4-v6-migration` skill is installed in this repository
23
+ next to `bootstrap-v5-v6-migration`.
24
+ - Gemspec metadata (changelog, source, bug tracker, documentation, funding), and
25
+ `rubygems_mfa_required`.
26
+ - A feature-request issue template and Dependabot for Bundler.
27
+
28
+ ### Changed
29
+
30
+ - **Requires Rails 7.1 or later** (was 5.0). Older lines get no security fixes and
31
+ weren't tested.
32
+ - `bootstrap:layout` keeps `javascript_importmap_tags` on apps with
33
+ `config/importmap.rb`. Regenerating the application layout used to drop it, which
34
+ turned off Turbo and Stimulus.
35
+ - `CONTRIBUTERS.md` is now `CONTRIBUTORS.md`.
36
+
37
+ ### Removed
38
+
39
+ - `WARP.md` and `.travis.yml`, which described the pre-5.4 toolchain.
40
+ - The unused `minitest`, `mocha`, `turn` and `coveralls` development gems.
41
+
1
42
  ## 5.5.1
2
43
 
3
44
  ### Added
data/README.md CHANGED
@@ -6,13 +6,15 @@
6
6
  [![GitHub issues](https://img.shields.io/github/issues/seyhunak/twitter-bootstrap-rails.svg)](https://github.com/seyhunak/twitter-bootstrap-rails/issues)
7
7
  [![Built with Claude Code](https://img.shields.io/badge/Built%20with-Claude%20Code-D97757?logo=claude&logoColor=white)](https://claude.com/claude-code)
8
8
 
9
- Integrates Bootstrap 5.3.8 into Rails Asset Pipeline. Supports Rails 8, 7, 6, 5.
10
- Tested against Rails 8.1.4.1 and 8.0.5.2.
9
+ Integrates Bootstrap 5.3.8 into Rails on Propshaft, Sprockets, importmap or the CDN.
10
+ Supports Rails 8 and 7.1+. Tested against Rails 8.1.4.1 and 8.0.5.2.
11
+ Apps still on Bootstrap 4 can use [HeroDevs Bootstrap NES 4.6](#bootstrap-nes-46-herodevs).
11
12
 
12
13
  > **Upgrade Rails.** [Rails 8.1.4.1 and 8.0.5.2](https://rubyonrails.org/2026/11/4/Rails-8.0.5.2-and-8.1.4.1-have-been-released)
13
14
  > fix 21 security issues, and the Rails team strongly recommends upgrading as soon
14
- > as possible. This gem's dependency range still allows older Rails versions, so
15
- > installing it won't upgrade Rails for you. Run `bundle update rails` in your app.
15
+ > as possible. This gem requires Rails 7.1 or later but allows unpatched releases
16
+ > within that range, so installing it won't upgrade Rails for you. Run
17
+ > `bundle update rails` in your app.
16
18
  > Security support for Rails 8.0 ends on 7 November 2026, and older series are
17
19
  > already unsupported.
18
20
 
@@ -40,6 +42,7 @@ is reproducible from a command:
40
42
  | CDN CSS `<link>` | `rails g bootstrap:cdn --css` |
41
43
  | CDN JS bundle `<script>` | `rails g bootstrap:cdn --js` |
42
44
  | Popper + `bootstrap.js` separately | `rails g bootstrap:cdn --separate-popper` |
45
+ | Bootstrap 6 prerelease tags | `rails g bootstrap:cdn --bootstrap=6` |
43
46
  | Important globals (doctype, `lang`, viewport) | baked into every generated layout and the starter template |
44
47
 
45
48
  ```bash
@@ -49,6 +52,9 @@ rails g bootstrap:install static
49
52
  # ...or load Bootstrap from the jsDelivr CDN instead
50
53
  rails g bootstrap:install cdn
51
54
 
55
+ # ...or pin Bootstrap as an ES module in config/importmap.rb
56
+ rails g bootstrap:install importmap
57
+
52
58
  # Generate a Bootstrap layout
53
59
  rails g bootstrap:layout application
54
60
 
@@ -69,6 +75,9 @@ rails g bootstrap:cdn
69
75
  rails g scaffold Task title:string done:boolean
70
76
  rails db:migrate
71
77
  rails g bootstrap:themed Tasks
78
+
79
+ # Keep tooltips, popovers and modals working with Turbo (needs Stimulus)
80
+ rails g bootstrap:stimulus
72
81
  ```
73
82
 
74
83
  `bootstrap:install cdn` records the choice in `config/initializers/bootstrap.rb`,
@@ -83,6 +92,64 @@ run `rails g bootstrap:layout` after `bootstrap:install static` either way.
83
92
  The Bootstrap version and its CDN URLs and integrity hashes live in one place,
84
93
  `Twitter::Bootstrap::Rails::BOOTSTRAP_CDN`. No template hardcodes them.
85
94
 
95
+ ### Importmap
96
+
97
+ `rails g bootstrap:install importmap` is for apps on importmap-rails (the Rails 7 and
98
+ 8 default). It vendors the stylesheet, copies Bootstrap's ES module build and Popper
99
+ into `vendor/javascript/twitter/bootstrap/`, pins both in `config/importmap.rb`, and
100
+ adds `import "bootstrap"` to `app/javascript/application.js`. Your own modules can
101
+ then `import { Tooltip } from "bootstrap"`. Nothing is fetched from a CDN at runtime.
102
+
103
+ Popper ships no single-file ES module build, so the gem wraps Popper's official
104
+ UMD build with ES module exports.
105
+
106
+ `bootstrap:layout` now keeps `<%= javascript_importmap_tags %>` in the layout on any
107
+ app with `config/importmap.rb`. Earlier versions dropped it, so regenerating
108
+ `application` silently turned off Turbo and Stimulus. The doctor warns
109
+ (`importmap_tags`) if no layout loads the importmap.
110
+
111
+ ### Turbo and Stimulus
112
+
113
+ Bootstrap's data attributes (`data-bs-toggle="modal"`, `"dropdown"`, `"collapse"`)
114
+ work with Turbo on their own. Tooltips and popovers are opt-in JavaScript, and an
115
+ open modal can be cached by Turbo and come back with its backdrop stuck. Run
116
+ `rails g bootstrap:stimulus` and add `data-controller="bootstrap"` to `<body>`. The
117
+ controller sets up tooltips and popovers on every render, disposes of them
118
+ afterwards, and closes modals, offcanvases and dropdowns before Turbo caches a page.
119
+ On importmap apps it imports Bootstrap, and elsewhere it uses the bundle's global.
120
+
121
+ ### Bootstrap NES 4.6 (HeroDevs)
122
+
123
+ Apps that can't leave Bootstrap 4 yet can run
124
+ [HeroDevs Bootstrap NES 4.6](https://docs.herodevs.com/bootstrap/bootstrap-4-6), a
125
+ security-patched Bootstrap 4.6 (currently 4.6.5). It is a **paid** product served
126
+ from HeroDevs' private npm registry, so the gem can't vendor it. It sets your app
127
+ up to install it:
128
+
129
+ ```bash
130
+ rails g bootstrap:nes # package.json + .npmrc + initializer
131
+ export HERODEVS_NES_TOKEN=... # your HeroDevs access token
132
+ npm install
133
+ rails g bootstrap:doctor # checks the NES setup
134
+ ```
135
+
136
+ - It needs npm, so use jsbundling-rails or cssbundling-rails. importmap can't
137
+ install from an authenticated registry.
138
+ - `package.json` gets
139
+ `"bootstrap": "npm:@neverendingsupport/bootstrap@4.6.2-bootstrap-4.6.5"` plus a
140
+ matching `overrides` entry, so packages that depend on Bootstrap get NES too.
141
+ - `.npmrc` gets the registry and
142
+ `_authToken=${HERODEVS_NES_TOKEN}`, so the token stays in the environment and out
143
+ of git. Use `--token-env=NAME` for another variable name.
144
+ - Bootstrap 4's JavaScript needs jQuery.
145
+ - The doctor checks the package, the override, the registry and the token line. It
146
+ warns if a token is written into `.npmrc` or the NES release is older than the one
147
+ the gem knows, and it doesn't flag Bootstrap 4 markup as legacy.
148
+ - The gem's layout, themed and snippet generators and its view helpers write
149
+ Bootstrap 5 markup, so they warn on an NES app. `bootstrap:agent` tells coding
150
+ agents to write Bootstrap 4 markup and points them at the upstream
151
+ `bootstrap-v4-v6-migration` skill.
152
+
86
153
  ## Snippets
87
154
 
88
155
  Every snippet published at
@@ -189,6 +256,7 @@ into your app alongside the gem's own guide:
189
256
  ```bash
190
257
  npx skills add twbs/bootstrap --skill bootstrap-v5-v6-migration
191
258
  rails g bootstrap:agent # the guide tells agents to stay on Bootstrap 5 markup until you migrate
259
+ rails g bootstrap:cdn --bootstrap=6 # prerelease CDN tags, to try Bootstrap 6 out
192
260
  rails g bootstrap:doctor --v6 # the migration worklist
193
261
  rails g bootstrap:doctor --v6 --json
194
262
  ```
@@ -229,6 +297,17 @@ rails server
229
297
  Run `bootstrap:layout` even if you already have a layout — that step is what puts
230
298
  Bootstrap's CSS and JS on the page.
231
299
 
300
+ ## Upgrading to 5.6.0
301
+
302
+ 5.6.0 requires **Rails 7.1 or later**. Apps on older Rails should pin
303
+ `gem "twitter-bootstrap-rails", "~> 5.5.1"`.
304
+
305
+ If your app uses importmap and you generated its layout with an earlier version,
306
+ re-run `rails g bootstrap:layout` or add `<%= javascript_importmap_tags %>` to its
307
+ `<head>`: earlier layouts left it out. `rails g bootstrap:doctor` reports this as
308
+ `importmap_tags`. To load Bootstrap's JavaScript through the importmap instead of the
309
+ bundle, run `rails g bootstrap:install importmap`.
310
+
232
311
  ## Upgrading to 5.5.0
233
312
 
234
313
  5.5.0 changes the HTML that the view helpers produce. They were still
@@ -71,6 +71,18 @@ module Bootstrap
71
71
  Twitter::Bootstrap::Rails::V6Migration
72
72
  end
73
73
 
74
+ def nes?
75
+ facts[:asset_mode] == "nes"
76
+ end
77
+
78
+ def nes
79
+ Twitter::Bootstrap::Rails::BOOTSTRAP_NES
80
+ end
81
+
82
+ def v4_skill_command
83
+ "npx skills add twbs/bootstrap --skill bootstrap-v4-v6-migration"
84
+ end
85
+
74
86
  def pipeline_note
75
87
  case facts[:asset_pipeline]
76
88
  when "propshaft"
@@ -1,6 +1,12 @@
1
1
  # Bootstrap in this app
2
2
 
3
+ <% if nes? -%>
4
+ This app uses Bootstrap <%= facts[:bootstrap_version] %> from <%= nes[:name] %>: security-patched Bootstrap 4.6, installed with npm from HeroDevs' private registry and set up by the twitter-bootstrap-rails gem (<%= facts[:gem_version] %>).
5
+
6
+ **Write Bootstrap 4 markup.** Bootstrap 4 uses <%= legacy::DATA_ATTRIBUTES.first(3).map { |name| "`data-#{name}`" }.join(", ") %> and so on, not `data-bs-*`, and Bootstrap 4 classes. The gem's layout, themed and snippet generators and its view helpers write Bootstrap 5 markup, which doesn't work here, so don't use them in this app.
7
+ <% else -%>
3
8
  This app uses Bootstrap <%= facts[:bootstrap_version] %> through the twitter-bootstrap-rails gem (<%= facts[:gem_version] %>). Prefer the gem's generators and helpers over writing Bootstrap markup from memory, which is often out of date.
9
+ <% end -%>
4
10
 
5
11
  This guide is generated. Run `rails g bootstrap:agent` again after upgrading the gem or changing the asset setup.
6
12
 
@@ -9,6 +15,18 @@ This guide is generated. Run `rails g bootstrap:agent` again after upgrading the
9
15
  - Asset pipeline: <%= pipeline_note %>
10
16
  - Bootstrap assets: <%= facts[:asset_mode] %>
11
17
  - Template engine: <%= facts[:template_engine] %>
18
+ <% if nes? -%>
19
+
20
+ ## Workflow
21
+
22
+ 1. Run `rails g bootstrap:doctor --json` to check the NES setup: the package in `package.json`, and the registry and token in `.npmrc`. It writes nothing, and it exits 1 if a check reports an error.
23
+ 2. npm reads the access token from the `<%= nes[:token_env] %>` environment variable (or the name in `.npmrc`). Never write the token into a file.
24
+ 3. Bootstrap 4's JavaScript needs jQuery, imported before Bootstrap.
25
+
26
+ ## Moving off Bootstrap 4
27
+
28
+ NES keeps Bootstrap 4.6 patched, but the gem's tooling targets Bootstrap 5. To migrate, install the upstream skill with `<%= v4_skill_command %>` and follow it. To stop at Bootstrap 5, switch `package.json` back to the public `bootstrap` package, run `rails g bootstrap:install`, then use `rails g bootstrap:doctor` to find the Bootstrap 4 markup that needs changing.
29
+ <% else -%>
12
30
 
13
31
  ## Workflow
14
32
 
@@ -33,9 +51,10 @@ Snippets are always written as ERB partials. They render from any template engin
33
51
 
34
52
  ## Other generators
35
53
 
36
- - `bootstrap:install static` or `bootstrap:install cdn`: sets up Bootstrap's assets.
37
- - `bootstrap:layout [NAME]`: writes a layout that loads Bootstrap. Don't overwrite an existing layout without checking its contents first.
54
+ - `bootstrap:install static`, `bootstrap:install cdn` or `bootstrap:install importmap`: sets up Bootstrap's assets. `importmap` pins Bootstrap as an ES module, so `import { Tooltip } from "bootstrap"` works.
55
+ - `bootstrap:layout [NAME]`: writes a layout that loads Bootstrap, and keeps `javascript_importmap_tags` on importmap apps. Don't overwrite an existing layout without checking its contents first.
38
56
  - `bootstrap:themed RESOURCE`: writes Bootstrap views for a scaffold.
57
+ - `bootstrap:stimulus`: writes a Stimulus controller that sets up tooltips and popovers on each Turbo render and closes modals before Turbo caches a page. Attach it with `data-controller="bootstrap"` on `<body>`.
39
58
  - `bootstrap:cdn`: prints the CDN tags and writes nothing.
40
59
 
41
60
  ## View helpers
@@ -53,7 +72,7 @@ To move the app to Bootstrap 6:
53
72
 
54
73
  1. Install the upstream migration skill: `<%= v6::SKILL_COMMAND %>`. Follow it alongside this guide.
55
74
  2. Run `rails g bootstrap:doctor --v6 --json`. It lists every Bootstrap 5 class, data attribute, Sass import and JavaScript call that changes, with file, line and replacement, under `v6_migration`.
56
- 3. Switch the Bootstrap assets before changing markup, because the vendored files and `bootstrap:install` are Bootstrap 5. Re-running `bootstrap:layout`, `bootstrap:themed` or a snippet generator writes Bootstrap 5 markup again.
75
+ 3. Switch the Bootstrap assets before changing markup, because the vendored files and `bootstrap:install` are Bootstrap 5. `rails g bootstrap:cdn --bootstrap=6` prints the prerelease CDN tags. Re-running `bootstrap:layout`, `bootstrap:themed` or a snippet generator writes Bootstrap 5 markup again.
57
76
  4. The helpers in "View helpers" render Bootstrap 5 markup at runtime, so the scan can't see their output. Replace them with Bootstrap 6 markup in the views that use them.
58
77
  5. v6 changes the spacing scale (`p-3` is smaller), so check spacing utilities by eye; the scan doesn't flag them.
59
78
 
@@ -71,3 +90,4 @@ Data attributes: <%= legacy::DATA_ATTRIBUTES.map { |name| "`data-#{name}`" }.joi
71
90
  <% legacy::CLASS_PATTERNS.each do |_pattern, _replacement, example| -%>
72
91
  | <%= example.split(" become").first %> | <%= example.split(/ becomes? /).last %> |
73
92
  <% end -%>
93
+ <% end -%>
@@ -0,0 +1,22 @@
1
+ require 'twitter/bootstrap/rails/config'
2
+ require 'twitter/bootstrap/rails/version'
3
+
4
+ module Bootstrap
5
+ module Generators
6
+ # For generators that write Bootstrap 5 markup. On an app running HeroDevs
7
+ # Bootstrap NES 4.6 that markup doesn't work, so say so before writing it.
8
+ module Bootstrap5Markup
9
+ private
10
+
11
+ def warn_if_nes
12
+ return unless Twitter::Bootstrap::Rails.nes?
13
+
14
+ say_status :warn,
15
+ "This app runs Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_NES[:bootstrap_version]} (NES), but this " \
16
+ "generator writes Bootstrap 5 markup, which needs data-bs-* attributes and Bootstrap 5 classes. " \
17
+ "Convert it to Bootstrap 4 markup, or migrate the app first.",
18
+ :yellow
19
+ end
20
+ end
21
+ end
22
+ end
@@ -17,21 +17,51 @@ module Bootstrap
17
17
  :desc => 'Print only the JavaScript bundle script'
18
18
  class_option :'separate-popper', :type => :boolean, :default => false,
19
19
  :desc => 'Print Popper and bootstrap.js as separate tags instead of the bundle'
20
+ class_option :bootstrap, :type => :string, :default => "5",
21
+ :desc => "Bootstrap major version: 5 (#{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION}) or 6 " \
22
+ "(the #{Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION} prerelease)"
23
+
24
+ def validate_version
25
+ unless %w[5 6].include?(options[:bootstrap])
26
+ raise ::Rails::Generators::Error,
27
+ "--bootstrap must be 5 or 6. For HeroDevs Bootstrap NES 4.6, run `rails g bootstrap:nes`."
28
+ end
29
+ return unless next_version? && options[:'separate-popper']
30
+
31
+ raise ::Rails::Generators::Error,
32
+ "--separate-popper does not apply to Bootstrap 6, whose bundle includes Floating UI."
33
+ end
20
34
 
21
35
  def print_tags
22
36
  say tags.join("\n")
23
37
  end
24
38
 
39
+ def warn_about_prerelease
40
+ return unless next_version?
41
+
42
+ say_status :warn,
43
+ "Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_VERSION} is a prerelease. This gem's generators and helpers " \
44
+ "write Bootstrap 5 markup, which needs migrating: run `rails g bootstrap:doctor --v6` for the list.",
45
+ :yellow
46
+ end
47
+
25
48
  private
26
49
 
50
+ def next_version?
51
+ options[:bootstrap] == "6"
52
+ end
53
+
27
54
  def tags
28
- return [css_tag] if options[:css] && !options[:js]
55
+ css = next_version? ? next_css_tag : css_tag
56
+ return [css] if options[:css] && !options[:js]
29
57
  return [javascript_tags] if options[:js] && !options[:css]
30
58
 
31
- [css_tag, javascript_tags]
59
+ [css, javascript_tags]
32
60
  end
33
61
 
34
62
  def javascript_tags
63
+ return next_bundle_tag if next_version?
64
+
35
65
  options[:'separate-popper'] ? separate_popper_tags : bundle_tag
36
66
  end
37
67
  end
@@ -23,6 +23,17 @@ module Bootstrap
23
23
  script_tag(:popper)
24
24
  end
25
25
 
26
+ # Bootstrap 6 ships only ES modules, so its bundle needs type="module".
27
+ def next_css_tag
28
+ entry = Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_CDN.fetch(:css)
29
+ %{<link href="#{entry[:url]}" rel="stylesheet" integrity="#{entry[:integrity]}" crossorigin="anonymous">}
30
+ end
31
+
32
+ def next_bundle_tag
33
+ entry = Twitter::Bootstrap::Rails::BOOTSTRAP_NEXT_CDN.fetch(:bundle)
34
+ %{<script type="module" src="#{entry[:url]}" integrity="#{entry[:integrity]}" crossorigin="anonymous"></script>}
35
+ end
36
+
26
37
  # Popper and bootstrap.js as the separate pair, in the docs' order.
27
38
  def separate_popper_tags
28
39
  [popper_tag, js_tag].join("\n")
@@ -12,22 +12,43 @@ module Bootstrap
12
12
 
13
13
  source_root File.expand_path("../templates", __FILE__)
14
14
  desc "This generator installs Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION} to the Asset Pipeline"
15
+ MODES = %w[static cdn importmap].freeze
16
+
15
17
  argument :asset_mode, :type => :string, :default => 'static',
16
- :banner => 'static or cdn',
17
- :desc => 'static vendors Bootstrap into the app; cdn links to jsDelivr instead'
18
-
19
- VENDORED_ASSETS = [
20
- ['vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css',
21
- 'app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css'],
22
- ['vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js',
23
- 'app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js']
18
+ :banner => MODES.join(' | '),
19
+ :desc => 'static vendors Bootstrap into the asset pipeline; cdn links to jsDelivr; ' \
20
+ 'importmap vendors the stylesheet and pins Bootstrap as an ES module'
21
+
22
+ STYLESHEET = ['vendor/assets/stylesheets/twitter/bootstrap/bootstrap.min.css',
23
+ 'app/assets/stylesheets/twitter/bootstrap/bootstrap.min.css'].freeze
24
+ BUNDLE = ['vendor/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js',
25
+ 'app/assets/javascripts/twitter/bootstrap/bootstrap.bundle.min.js'].freeze
26
+ # importmap-rails serves vendor/javascript, and these paths are what the pins name.
27
+ ES_MODULES = [
28
+ ['vendor/assets/javascripts/twitter/bootstrap/bootstrap.esm.min.js',
29
+ 'vendor/javascript/twitter/bootstrap/bootstrap.esm.min.js'],
30
+ ['vendor/assets/javascripts/twitter/bootstrap/popper.esm.js',
31
+ 'vendor/javascript/twitter/bootstrap/popper.esm.js']
24
32
  ].freeze
33
+ PINS = {
34
+ "bootstrap" => ["twitter/bootstrap/bootstrap.esm.min.js", "Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION}"],
35
+ "@popperjs/core" => ["twitter/bootstrap/popper.esm.js", "Popper #{Twitter::Bootstrap::Rails::POPPER_VERSION}"]
36
+ }.freeze
37
+ IMPORTMAP = "config/importmap.rb".freeze
38
+ APPLICATION_JS = "app/javascript/application.js".freeze
25
39
 
26
40
  def validate_asset_mode
27
- return if %w[static cdn].include?(asset_mode)
41
+ unless MODES.include?(asset_mode)
42
+ raise ::Rails::Generators::Error,
43
+ "Unknown asset mode #{asset_mode.inspect}. Expected one of #{MODES.join(', ')}. " \
44
+ "For HeroDevs Bootstrap NES 4.6, run `rails g bootstrap:nes`."
45
+ end
46
+
47
+ return unless importmap? && !exists_in_app?(IMPORTMAP)
28
48
 
29
49
  raise ::Rails::Generators::Error,
30
- "Unknown asset mode #{asset_mode.inspect}. Expected 'static' or 'cdn'."
50
+ "importmap mode needs importmap-rails, but #{IMPORTMAP} does not exist. " \
51
+ "Run `bin/rails importmap:install` first, or use `bootstrap:install static`."
31
52
  end
32
53
 
33
54
  def add_assets
@@ -38,7 +59,9 @@ module Bootstrap
38
59
 
39
60
  js_manifest = 'app/assets/javascripts/application.js'
40
61
 
41
- if exists_in_app?(js_manifest)
62
+ if importmap?
63
+ # Bootstrap's JavaScript comes from the importmap, not the manifest.
64
+ elsif exists_in_app?(js_manifest)
42
65
  add_require(js_manifest,
43
66
  "//= require twitter/bootstrap/bootstrap.bundle.min\n",
44
67
  "application\n",
@@ -62,7 +85,8 @@ module Bootstrap
62
85
  def copy_bootstrap_assets
63
86
  return if cdn?
64
87
 
65
- VENDORED_ASSETS.each do |relative_source, destination|
88
+ vendored = [STYLESHEET] + (importmap? ? ES_MODULES : [BUNDLE])
89
+ vendored.each do |relative_source, destination|
66
90
  source = File.join(gem_root, relative_source)
67
91
 
68
92
  unless File.file?(source)
@@ -75,22 +99,46 @@ module Bootstrap
75
99
  end
76
100
  end
77
101
 
102
+ def pin_modules
103
+ return unless importmap?
104
+
105
+ existing = read_from_app(IMPORTMAP)
106
+ pins = PINS.reject { |name, _| existing.match?(/^\s*pin\s+["']#{Regexp.escape(name)}["']/) }
107
+ .map { |name, (path, label)| %{pin "#{name}", to: "#{path}" # #{label} via twitter-bootstrap-rails\n} }
108
+ append_to_file IMPORTMAP, "\n" + pins.join unless pins.empty?
109
+
110
+ if exists_in_app?(APPLICATION_JS)
111
+ unless read_from_app(APPLICATION_JS).match?(/^\s*import\s+(?:\*\s+as\s+\w+\s+from\s+)?["']bootstrap["']/)
112
+ append_to_file APPLICATION_JS, %{import "bootstrap"\n}
113
+ end
114
+ else
115
+ say_status :warn, "#{APPLICATION_JS} not found. Add `import \"bootstrap\"` to your JavaScript entry point.", :yellow
116
+ end
117
+ end
118
+
78
119
  def add_bootstrap
79
120
  return if cdn?
80
121
  return if propshaft?
81
122
 
82
- copy_file "bootstrap.js", "app/assets/javascripts/bootstrap.js"
123
+ copy_file "bootstrap.js", "app/assets/javascripts/bootstrap.js" unless importmap?
83
124
  copy_file "bootstrap_and_overrides.css", "app/assets/stylesheets/bootstrap_and_overrides.css"
84
125
  end
85
126
 
86
- def add_cdn_initializer
87
- return unless cdn?
88
-
89
- create_file "config/initializers/bootstrap.rb", <<~RUBY
90
- # Bootstrap's CSS and JS are loaded from jsDelivr rather than vendored into
91
- # this app. `rails g bootstrap:layout` reads this to emit CDN tags by default.
92
- Twitter::Bootstrap::Rails.asset_mode = :cdn
93
- RUBY
127
+ def add_mode_initializer
128
+ if cdn?
129
+ create_file "config/initializers/bootstrap.rb", <<~RUBY
130
+ # Bootstrap's CSS and JS are loaded from jsDelivr rather than vendored into
131
+ # this app. `rails g bootstrap:layout` reads this to emit CDN tags by default.
132
+ Twitter::Bootstrap::Rails.asset_mode = :cdn
133
+ RUBY
134
+ elsif importmap?
135
+ create_file "config/initializers/bootstrap.rb", <<~RUBY
136
+ # Bootstrap's stylesheet is vendored into the asset pipeline and its JavaScript
137
+ # is pinned in config/importmap.rb. `rails g bootstrap:layout` reads this to
138
+ # leave Bootstrap's JavaScript to the importmap.
139
+ Twitter::Bootstrap::Rails.asset_mode = :importmap
140
+ RUBY
141
+ end
94
142
  end
95
143
 
96
144
  def add_locale
@@ -111,6 +159,10 @@ module Bootstrap
111
159
  if cdn?
112
160
  say "Bootstrap #{version} will be loaded from jsDelivr. " \
113
161
  "Run `rails g bootstrap:layout` to generate a layout with the CDN tags."
162
+ elsif importmap?
163
+ say "Bootstrap #{version} pinned in #{IMPORTMAP} and imported from #{APPLICATION_JS}. " \
164
+ "Use `import { Tooltip } from \"bootstrap\"` in your own modules. " \
165
+ "Run `rails g bootstrap:layout` if your layout doesn't link the stylesheet yet."
114
166
  elsif propshaft?
115
167
  say "Bootstrap #{version} vendored for Propshaft. " \
116
168
  "Run `rails g bootstrap:layout` to generate a layout that links it."
@@ -125,6 +177,10 @@ module Bootstrap
125
177
  asset_mode == 'cdn'
126
178
  end
127
179
 
180
+ def importmap?
181
+ asset_mode == 'importmap'
182
+ end
183
+
128
184
  def propshaft?
129
185
  Twitter::Bootstrap::Rails.propshaft?
130
186
  end
@@ -2,11 +2,13 @@ require 'rails/generators'
2
2
  require 'twitter/bootstrap/rails/version'
3
3
  require 'twitter/bootstrap/rails/config'
4
4
  require 'generators/bootstrap/cdn_tags'
5
+ require 'generators/bootstrap/bootstrap5_markup'
5
6
 
6
7
  module Bootstrap
7
8
  module Generators
8
9
  class LayoutGenerator < ::Rails::Generators::Base
9
10
  include CdnTags
11
+ include Bootstrap5Markup
10
12
 
11
13
  source_root File.expand_path("../templates", __FILE__)
12
14
  desc "This generator generates a Bootstrap #{Twitter::Bootstrap::Rails::BOOTSTRAP_VERSION} layout file with navigation."
@@ -29,6 +31,7 @@ module Bootstrap
29
31
  end
30
32
 
31
33
  def generate_layout
34
+ warn_if_nes
32
35
  app = ::Rails.application
33
36
  @app_name = app.class.to_s.split("::").first
34
37
  ext = app.config.generators.options[:rails][:template_engine] || :erb
@@ -43,6 +46,18 @@ module Bootstrap
43
46
  Twitter::Bootstrap::Rails.cdn?
44
47
  end
45
48
 
49
+ # Rails 7+ apps load their own JavaScript (Turbo, Stimulus) through the
50
+ # importmap. A layout without these tags silently drops all of it.
51
+ def importmap_app?
52
+ File.exist?(File.join(destination_root, "config/importmap.rb"))
53
+ end
54
+
55
+ # In importmap mode Bootstrap's JavaScript is imported from application.js,
56
+ # so the layout links no script of its own for it.
57
+ def bootstrap_script?
58
+ cdn? || !Twitter::Bootstrap::Rails.importmap?
59
+ end
60
+
46
61
  def separate_popper?
47
62
  options[:'separate-popper'] && cdn?
48
63
  end
@@ -11,6 +11,9 @@
11
11
  <% else -%>
12
12
 
13
13
  <%%= stylesheet_link_tag <%= stylesheet_arguments %>, :media => "all" %>
14
+ <% end -%>
15
+ <% if importmap_app? -%>
16
+ <%%= javascript_importmap_tags %>
14
17
  <% end -%>
15
18
  </head>
16
19
  <body>
@@ -40,7 +43,7 @@
40
43
 
41
44
  <% if cdn? -%>
42
45
  <%= indent_tags(script_tags, " ") %>
43
- <% else -%>
46
+ <% elsif bootstrap_script? -%>
44
47
  <%%= javascript_include_tag <%= javascript_arguments %> %>
45
48
  <% end -%>
46
49
  </body>
@@ -10,6 +10,9 @@
10
10
  <% else -%>
11
11
  = stylesheet_link_tag <%= stylesheet_arguments %>, :media => "all"
12
12
  <% end -%>
13
+ <% if importmap_app? -%>
14
+ = javascript_importmap_tags
15
+ <% end -%>
13
16
 
14
17
  %body
15
18
  %nav.navbar.navbar-expand-lg.navbar-dark.bg-primary
@@ -30,6 +33,6 @@
30
33
 
31
34
  <% if cdn? -%>
32
35
  <%= indent_tags(script_tags, " ") %>
33
- <% else -%>
36
+ <% elsif bootstrap_script? -%>
34
37
  = javascript_include_tag <%= javascript_arguments %>
35
38
  <% end -%>
@@ -10,6 +10,9 @@ html lang="en"
10
10
  <% else -%>
11
11
  = stylesheet_link_tag <%= stylesheet_arguments %>, :media => "all"
12
12
  <% end -%>
13
+ <% if importmap_app? -%>
14
+ = javascript_importmap_tags
15
+ <% end -%>
13
16
 
14
17
  body
15
18
  nav.navbar.navbar-expand-lg.navbar-dark.bg-primary
@@ -30,6 +33,6 @@ html lang="en"
30
33
 
31
34
  <% if cdn? -%>
32
35
  <%= slim_tags(script_tags, " ") %>
33
- <% else -%>
36
+ <% elsif bootstrap_script? -%>
34
37
  = javascript_include_tag <%= javascript_arguments %>
35
38
  <% end -%>