diff --git a/README.md b/README.md index 2c17e6f..3f26d6f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CSS Bundling for Rails -Use [Tailwind CSS](https://tailwindcss.com), [PostCSS](https://postcss.org), or [Dart Sass](https://sass-lang.com/) to bundle and process your CSS, then deliver it via the asset pipeline in Rails. This gem provides installers to get you going with the bundler of your choice in a new Rails application, and a convention to use `app/assets/builds` to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to `.gitignore` by default). +Use [Tailwind CSS](https://tailwindcss.com), [PostCSS](https://postcss.org), [Dart Sass](https://sass-lang.com/), or [Bootstrap](https://getbootstrap.com/) to bundle and process your CSS, then deliver it via the asset pipeline in Rails. This gem provides installers to get you going with the bundler of your choice in a new Rails application, and a convention to use `app/assets/builds` to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to `.gitignore` by default). You develop using this approach by running the bundler in watch mode in a terminal with `yarn build:css --watch` (and your Rails server in another, if you're not using something like [puma-dev](https://github.com/puma/puma-dev)). Whenever the bundler detects changes to any of the stylesheet files in your project, it'll bundle `app/assets/stylesheets/application.[bundler].css` into `app/assets/builds/application.css`. This build output takes over from the regular asset pipeline default file. So you continue to refer to the build output in your layout using the standard asset pipeline approach with `<%= stylesheet_link_tag "application" %>`. @@ -21,9 +21,9 @@ You must already have node and yarn installed on your system. You will also need 1. Add `cssbundling-rails` to your Gemfile with `gem 'cssbundling-rails'` 2. Run `./bin/bundle install` -3. Run `./bin/rails css:install:[tailwind|postcss|sass]` +3. Run `./bin/rails css:install:[tailwind|postcss|sass|bootstrap]` -Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp --css [tailwind|postcss|sass]`. +Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp --css [tailwind|postcss|sass|bootstrap]`. ## License diff --git a/lib/install/bootstrap/application.bootstrap.scss b/lib/install/bootstrap/application.bootstrap.scss new file mode 100644 index 0000000..54adde9 --- /dev/null +++ b/lib/install/bootstrap/application.bootstrap.scss @@ -0,0 +1 @@ +@import 'bootstrap/scss/bootstrap'; diff --git a/lib/install/bootstrap/install.rb b/lib/install/bootstrap/install.rb new file mode 100644 index 0000000..f695e8a --- /dev/null +++ b/lib/install/bootstrap/install.rb @@ -0,0 +1,14 @@ +say "Install Bootstrap with Popperjs/core" +copy_file "#{__dir__}/application.bootstrap.scss", + "app/assets/stylesheets/application.bootstrap.scss" +run "yarn add sass bootstrap @popperjs/core" + +if Rails.root.join("app/javascript/application.js").exist? + say "Appending Bootstrap JavaScript import to default entry point" + append_to_file "app/javascript/application.js", %(import * as bootstrap from "bootstrap"\n) +else + say %(Add import * as bootstrap from "bootstrap" to your entry point JavaScript file), :red +end + +say "Add build:css script" +run %(npm set-script build:css "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules") diff --git a/lib/install/install.rb b/lib/install/install.rb index 50245fa..9567314 100644 --- a/lib/install/install.rb +++ b/lib/install/install.rb @@ -3,6 +3,9 @@ keep_file "app/assets/builds" append_to_file "app/assets/config/manifest.js", %(//= link_tree ../builds\n) +say "Stop linking stylesheets automatically" +gsub_file "app/assets/config/manifest.js", "//= link_directory ../stylesheets .css", "" + if Rails.root.join(".gitignore").exist? append_to_file(".gitignore", %(/app/assets/builds\n!/app/assets/builds/.keep\n)) end diff --git a/lib/tasks/cssbundling/install.rake b/lib/tasks/cssbundling/install.rake index 3031cc1..6a87cd3 100644 --- a/lib/tasks/cssbundling/install.rake +++ b/lib/tasks/cssbundling/install.rake @@ -19,5 +19,10 @@ namespace :css do task sass: "css:install:shared" do system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/sass/install.rb", __dir__)}" end + + desc "Install Bootstrap" + task bootstrap: "css:install:shared" do + system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/bootstrap/install.rb", __dir__)}" + end end end