From 3e768cdabf51bb5955388804a6a56ad26febfb5c Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Sun, 21 Jan 2024 14:26:16 -0500 Subject: [PATCH] Fix running "rails new --css bootstrap" on 7.1 (#147) 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@84458a8577e09621205f2ea86f570745033105f5 --- lib/install/bootstrap/install.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/install/bootstrap/install.rb b/lib/install/bootstrap/install.rb index f00991e..abf8e1d 100644 --- a/lib/install/bootstrap/install.rb +++ b/lib/install/bootstrap/install.rb @@ -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")