Skip to content

Commit db61cec

Browse files
committed
JS: Define components in the bootstrap namespace
They were previously defined directly on `window` when using `//=require bootstrap-sprockets`.
1 parent 083f8d2 commit db61cec

5 files changed

Lines changed: 26 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Then, remove all the `*= require` and `*= require_tree` statements from the Sass
4747

4848
Do not use `*= require` in Sass or your other stylesheets will not be able to access the Bootstrap mixins and variables.
4949

50-
Bootstrap JavaScript depends on jQuery.
51-
If you're using Rails 5.1+, add the `jquery-rails` gem to your Gemfile:
50+
Bootstrap JavaScript can optionally use jQuery.
51+
If you're using Rails 5.1+, you can add add the `jquery-rails` gem to your Gemfile:
5252

5353
```ruby
5454
gem 'jquery-rails'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Set a `globalThis` so that bootstrap components are defined on window.bootstrap instead of window.
2+
window['bootstrap'] = {
3+
Popper: window.Popper,
4+
_originalGlobalThis: window['globalThis']
5+
};
6+
window['globalThis'] = window['bootstrap'];
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
window['globalThis'] = window['bootstrap']._originalGlobalThis;
2+
window['bootstrap']._originalGlobalThis = null;

assets/javascripts/bootstrap-sprockets.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//= require ./bootstrap-global-this-define
12
//= require ./bootstrap/dom/data
23
//= require ./bootstrap/base-component
34
//= require ./bootstrap/dom/event-handler
@@ -14,3 +15,4 @@
1415
//= require ./bootstrap/scrollspy
1516
//= require ./bootstrap/tab
1617
//= require ./bootstrap/toast
18+
//= require ./bootstrap-global-this-undefine

tasks/updater/js.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@ def update_javascript_assets
1313
log_processed "#{bootstrap_js_files * ' '}"
1414

1515
log_status 'Updating javascript manifest'
16-
manifest = ''
16+
manifest = "//= require ./bootstrap-global-this-define\n"
1717
bootstrap_js_files.each do |name|
1818
name = name.gsub(/\.js$/, '')
1919
manifest << "//= require ./bootstrap/#{name}\n"
2020
end
21+
manifest << "//= require ./bootstrap-global-this-undefine\n"
2122
dist_js = read_files('dist/js', %w(bootstrap.js bootstrap.min.js))
2223
{
24+
'assets/javascripts/bootstrap-global-this-define.js' => <<~JS,
25+
// Set a `globalThis` so that bootstrap components are defined on window.bootstrap instead of window.
26+
window['bootstrap'] = {
27+
Popper: window.Popper,
28+
_originalGlobalThis: window['globalThis']
29+
};
30+
window['globalThis'] = window['bootstrap'];
31+
JS
32+
'assets/javascripts/bootstrap-global-this-undefine.js' => <<~JS,
33+
window['globalThis'] = window['bootstrap']._originalGlobalThis;
34+
window['bootstrap']._originalGlobalThis = null;
35+
JS
2336
'assets/javascripts/bootstrap-sprockets.js' => manifest,
2437
'assets/javascripts/bootstrap.js' => dist_js['bootstrap.js'],
2538
'assets/javascripts/bootstrap.min.js' => dist_js['bootstrap.min.js'],

0 commit comments

Comments
 (0)