Skip to content

Commit

Permalink
JS: Define components in the bootstrap namespace
Browse files Browse the repository at this point in the history
They were previously defined directly on `window` when using
`//=require bootstrap-sprockets`.
  • Loading branch information
glebm committed Feb 26, 2021
1 parent 083f8d2 commit db61cec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Then, remove all the `*= require` and `*= require_tree` statements from the Sass

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

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

```ruby
gem 'jquery-rails'
Expand Down
6 changes: 6 additions & 0 deletions assets/javascripts/bootstrap-global-this-define.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Set a `globalThis` so that bootstrap components are defined on window.bootstrap instead of window.
window['bootstrap'] = {
Popper: window.Popper,
_originalGlobalThis: window['globalThis']
};
window['globalThis'] = window['bootstrap'];
2 changes: 2 additions & 0 deletions assets/javascripts/bootstrap-global-this-undefine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window['globalThis'] = window['bootstrap']._originalGlobalThis;
window['bootstrap']._originalGlobalThis = null;
2 changes: 2 additions & 0 deletions assets/javascripts/bootstrap-sprockets.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//= require ./bootstrap-global-this-define
//= require ./bootstrap/dom/data
//= require ./bootstrap/base-component
//= require ./bootstrap/dom/event-handler
Expand All @@ -14,3 +15,4 @@
//= require ./bootstrap/scrollspy
//= require ./bootstrap/tab
//= require ./bootstrap/toast
//= require ./bootstrap-global-this-undefine
15 changes: 14 additions & 1 deletion tasks/updater/js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ def update_javascript_assets
log_processed "#{bootstrap_js_files * ' '}"

log_status 'Updating javascript manifest'
manifest = ''
manifest = "//= require ./bootstrap-global-this-define\n"
bootstrap_js_files.each do |name|
name = name.gsub(/\.js$/, '')
manifest << "//= require ./bootstrap/#{name}\n"
end
manifest << "//= require ./bootstrap-global-this-undefine\n"
dist_js = read_files('dist/js', %w(bootstrap.js bootstrap.min.js))
{
'assets/javascripts/bootstrap-global-this-define.js' => <<~JS,
// Set a `globalThis` so that bootstrap components are defined on window.bootstrap instead of window.
window['bootstrap'] = {
Popper: window.Popper,
_originalGlobalThis: window['globalThis']
};
window['globalThis'] = window['bootstrap'];
JS
'assets/javascripts/bootstrap-global-this-undefine.js' => <<~JS,
window['globalThis'] = window['bootstrap']._originalGlobalThis;
window['bootstrap']._originalGlobalThis = null;
JS
'assets/javascripts/bootstrap-sprockets.js' => manifest,
'assets/javascripts/bootstrap.js' => dist_js['bootstrap.js'],
'assets/javascripts/bootstrap.min.js' => dist_js['bootstrap.min.js'],
Expand Down

0 comments on commit db61cec

Please sign in to comment.