Skip to content

Added Bootstrap option #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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" %>`.

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/install/bootstrap/application.bootstrap.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'bootstrap/scss/bootstrap';
14 changes: 14 additions & 0 deletions lib/install/bootstrap/install.rb
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 3 additions & 0 deletions lib/install/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/cssbundling/install.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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