Skip to content

Commit

Permalink
Fix running "rails new --css bootstrap" on 7.1 (#147)
Browse files Browse the repository at this point in the history
Rails 7.1 included a [change][1] to allow using importmaps along with
all cssbundling options. However, the Bootstrap installer was never
updated to take this new default into effect (and is currently broken
because of this).

This commit adds the additional configuration required to use the
Bootstrap npm package with importmaps so that "rails new" generates an
application that runs without errors.

[1]: rails/rails@84458a8
  • Loading branch information
skipkayhil committed Jan 21, 2024
1 parent 9075819 commit 3e768cd
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/install/bootstrap/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
say %(Add import * as bootstrap from "bootstrap" to your entry point JavaScript file), :red
end

if Rails.root.join("config/importmap.rb").exist?
say "Pin Bootstrap"
append_to_file "config/importmap.rb", %(pin "bootstrap", to: "bootstrap.min.js"\n)

inject_into_file "config/initializers/assets.rb", after: /.*Rails.application.config.assets.paths.*\n/ do
<<~RUBY
Rails.application.config.assets.paths << Rails.root.join("node_modules/bootstrap/dist/js")
RUBY
end

append_to_file "config/initializers/assets.rb", %(Rails.application.config.assets.precompile << "bootstrap.min.js")
end

add_package_json_script("build:css:compile", "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules")
add_package_json_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css")
add_package_json_script("build:css", "#{bundler_run_cmd} build:css:compile && #{bundler_run_cmd} build:css:prefix")
Expand Down

0 comments on commit 3e768cd

Please sign in to comment.