Skip to content

Commit 7ad6fe9

Browse files
author
David Heinemeier Hansson
committed
Follow the white rabbit
0 parents  commit 7ad6fe9

23 files changed

+403
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.byebug_history
2+
*.gem

Gemfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
source 'https://rubygems.org'
2+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3+
4+
# Specify your gem's dependencies in importmap-rails.gemspec.
5+
gemspec
6+
7+
rails_version = ENV["RAILS_VERSION"] || "6.1.0"
8+
gem "rails", "~> #{rails_version}"
9+
10+
gem "sqlite3"
11+
12+
group :test do
13+
gem "turbo-rails"
14+
gem "stimulus-rails"
15+
16+
gem "byebug"
17+
18+
gem "rexml"
19+
gem "capybara"
20+
gem "selenium-webdriver"
21+
gem "webdrivers"
22+
end

Gemfile.lock

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
PATH
2+
remote: .
3+
specs:
4+
cssbundling-rails (0.1.1)
5+
rails (>= 6.0.0)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
actioncable (6.1.3.1)
11+
actionpack (= 6.1.3.1)
12+
activesupport (= 6.1.3.1)
13+
nio4r (~> 2.0)
14+
websocket-driver (>= 0.6.1)
15+
actionmailbox (6.1.3.1)
16+
actionpack (= 6.1.3.1)
17+
activejob (= 6.1.3.1)
18+
activerecord (= 6.1.3.1)
19+
activestorage (= 6.1.3.1)
20+
activesupport (= 6.1.3.1)
21+
mail (>= 2.7.1)
22+
actionmailer (6.1.3.1)
23+
actionpack (= 6.1.3.1)
24+
actionview (= 6.1.3.1)
25+
activejob (= 6.1.3.1)
26+
activesupport (= 6.1.3.1)
27+
mail (~> 2.5, >= 2.5.4)
28+
rails-dom-testing (~> 2.0)
29+
actionpack (6.1.3.1)
30+
actionview (= 6.1.3.1)
31+
activesupport (= 6.1.3.1)
32+
rack (~> 2.0, >= 2.0.9)
33+
rack-test (>= 0.6.3)
34+
rails-dom-testing (~> 2.0)
35+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
36+
actiontext (6.1.3.1)
37+
actionpack (= 6.1.3.1)
38+
activerecord (= 6.1.3.1)
39+
activestorage (= 6.1.3.1)
40+
activesupport (= 6.1.3.1)
41+
nokogiri (>= 1.8.5)
42+
actionview (6.1.3.1)
43+
activesupport (= 6.1.3.1)
44+
builder (~> 3.1)
45+
erubi (~> 1.4)
46+
rails-dom-testing (~> 2.0)
47+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
48+
activejob (6.1.3.1)
49+
activesupport (= 6.1.3.1)
50+
globalid (>= 0.3.6)
51+
activemodel (6.1.3.1)
52+
activesupport (= 6.1.3.1)
53+
activerecord (6.1.3.1)
54+
activemodel (= 6.1.3.1)
55+
activesupport (= 6.1.3.1)
56+
activestorage (6.1.3.1)
57+
actionpack (= 6.1.3.1)
58+
activejob (= 6.1.3.1)
59+
activerecord (= 6.1.3.1)
60+
activesupport (= 6.1.3.1)
61+
marcel (~> 1.0.0)
62+
mini_mime (~> 1.0.2)
63+
activesupport (6.1.3.1)
64+
concurrent-ruby (~> 1.0, >= 1.0.2)
65+
i18n (>= 1.6, < 2)
66+
minitest (>= 5.1)
67+
tzinfo (~> 2.0)
68+
zeitwerk (~> 2.3)
69+
addressable (2.8.0)
70+
public_suffix (>= 2.0.2, < 5.0)
71+
builder (3.2.4)
72+
byebug (11.1.3)
73+
capybara (3.35.3)
74+
addressable
75+
mini_mime (>= 0.1.3)
76+
nokogiri (~> 1.8)
77+
rack (>= 1.6.0)
78+
rack-test (>= 0.6.3)
79+
regexp_parser (>= 1.5, < 3.0)
80+
xpath (~> 3.2)
81+
childprocess (3.0.0)
82+
concurrent-ruby (1.1.9)
83+
crass (1.0.6)
84+
erubi (1.10.0)
85+
globalid (0.5.1)
86+
activesupport (>= 5.0)
87+
i18n (1.8.10)
88+
concurrent-ruby (~> 1.0)
89+
loofah (2.10.0)
90+
crass (~> 1.0.2)
91+
nokogiri (>= 1.5.9)
92+
mail (2.7.1)
93+
mini_mime (>= 0.1.1)
94+
marcel (1.0.1)
95+
method_source (1.0.0)
96+
mini_mime (1.0.3)
97+
minitest (5.14.4)
98+
nio4r (2.5.7)
99+
nokogiri (1.11.7-arm64-darwin)
100+
racc (~> 1.4)
101+
nokogiri (1.11.7-x86_64-darwin)
102+
racc (~> 1.4)
103+
nokogiri (1.11.7-x86_64-linux)
104+
racc (~> 1.4)
105+
public_suffix (4.0.6)
106+
racc (1.5.2)
107+
rack (2.2.3)
108+
rack-test (1.1.0)
109+
rack (>= 1.0, < 3)
110+
rails (6.1.3.1)
111+
actioncable (= 6.1.3.1)
112+
actionmailbox (= 6.1.3.1)
113+
actionmailer (= 6.1.3.1)
114+
actionpack (= 6.1.3.1)
115+
actiontext (= 6.1.3.1)
116+
actionview (= 6.1.3.1)
117+
activejob (= 6.1.3.1)
118+
activemodel (= 6.1.3.1)
119+
activerecord (= 6.1.3.1)
120+
activestorage (= 6.1.3.1)
121+
activesupport (= 6.1.3.1)
122+
bundler (>= 1.15.0)
123+
railties (= 6.1.3.1)
124+
sprockets-rails (>= 2.0.0)
125+
rails-dom-testing (2.0.3)
126+
activesupport (>= 4.2.0)
127+
nokogiri (>= 1.6)
128+
rails-html-sanitizer (1.3.0)
129+
loofah (~> 2.3)
130+
railties (6.1.3.1)
131+
actionpack (= 6.1.3.1)
132+
activesupport (= 6.1.3.1)
133+
method_source
134+
rake (>= 0.8.7)
135+
thor (~> 1.0)
136+
rake (13.0.6)
137+
regexp_parser (2.1.1)
138+
rexml (3.2.5)
139+
rubyzip (2.3.2)
140+
selenium-webdriver (3.142.7)
141+
childprocess (>= 0.5, < 4.0)
142+
rubyzip (>= 1.2.2)
143+
sprockets (4.0.2)
144+
concurrent-ruby (~> 1.0)
145+
rack (> 1, < 3)
146+
sprockets-rails (3.2.2)
147+
actionpack (>= 4.0)
148+
activesupport (>= 4.0)
149+
sprockets (>= 3.0.0)
150+
sqlite3 (1.4.2)
151+
stimulus-rails (0.3.8)
152+
rails (>= 6.0.0)
153+
thor (1.1.0)
154+
turbo-rails (0.7.4)
155+
rails (>= 6.0.0)
156+
tzinfo (2.0.4)
157+
concurrent-ruby (~> 1.0)
158+
webdrivers (4.6.0)
159+
nokogiri (~> 1.6)
160+
rubyzip (>= 1.3.0)
161+
selenium-webdriver (>= 3.0, < 4.0)
162+
websocket-driver (0.7.5)
163+
websocket-extensions (>= 0.1.0)
164+
websocket-extensions (0.1.5)
165+
xpath (3.2.0)
166+
nokogiri (~> 1.8)
167+
zeitwerk (2.4.2)
168+
169+
PLATFORMS
170+
arm64-darwin-20
171+
x86_64-darwin-20
172+
x86_64-linux
173+
174+
DEPENDENCIES
175+
byebug
176+
capybara
177+
cssbundling-rails!
178+
rails (~> 6.1.0)
179+
rexml
180+
selenium-webdriver
181+
sqlite3
182+
stimulus-rails
183+
turbo-rails
184+
webdrivers
185+
186+
BUNDLED WITH
187+
2.2.24

MIT-LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2021 David Heinemeier Hansson
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# CSS Bundling for Rails
2+
3+
Use [Tailwind CSS](https://tailwindcss.com), [PostCSS](https://postcss.org), or [Dart Sass](https://sass-lang.com/) to bundle and process your JavaScript, 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).
4+
5+
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_include_tag "application" %>`.
6+
7+
When you deploy your application to production, the `css:build` task attaches to the `assets:precompile` task to ensure that all your package dependencies from `package.json` have been installed via yarn, and then runs `yarn build:css` to process your stylesheet entrypoint, as it would in development. This output is then picked up by the asset pipeline, digested, and copied into public/assets, as any other asset pipeline file.
8+
9+
This also happens in testing where the bundler attaches to the `test:prepare` task to ensure the stylesheets have been bundled before testing commences. (Note that this currently only applies to rails `test:*` tasks (like `test:all` or `test:controllers`), not "rails test", as that doesn't load `test:prepare`).
10+
11+
That's it!
12+
13+
You can configure your bundler options in the `build:css` script in `package.json` or via the installer-generated `tailwind.config.js` for Tailwind or `postcss.config.js` for PostCSS.
14+
15+
16+
## Installation
17+
18+
You must already have node and yarn installed on your system. Then:
19+
20+
1. Add `cssbundling-rails` to your Gemfile with `gem 'cssbundling-rails'`
21+
2. Run `./bin/bundle install`
22+
3. Run `./bin/rails css:install:[tailwind|postcss|sass]`
23+
24+
Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp --css [tailwind|postcss|sass]`.
25+
26+
27+
## License
28+
29+
CSS Bundling for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).

bin/release

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
VERSION=$1
4+
5+
printf "module Cssbundling\n VERSION = \"$VERSION\"\nend\n" > ./lib/cssbundling/version.rb
6+
bundle
7+
git add Gemfile.lock lib/cssbundling/version.rb
8+
git commit -m "Bump version for $VERSION"
9+
git push
10+
git tag v$VERSION
11+
git push --tags
12+
gem build cssbundling-rails.gemspec
13+
gem push "cssbundling-rails-$VERSION.gem" --host https://rubygems.org
14+
rm "cssbundling-rails-$VERSION.gem"

cssbundling-rails.gemspec

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require_relative "lib/cssbundling/version"
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = "cssbundling-rails"
5+
spec.version = Cssbundling::VERSION
6+
spec.authors = [ "David Heinemeier Hansson", "Dom Christie" ]
7+
spec.email = "[email protected]"
8+
spec.homepage = "https://github.com/rails/cssbundling-rails"
9+
spec.summary = "Bundle and process stylesheets in Rails with Tailwind, PostCSS, and Sass."
10+
spec.license = "MIT"
11+
12+
spec.files = Dir["lib/**/*", "MIT-LICENSE", "README.md"]
13+
14+
spec.add_dependency "rails", ">= 6.0.0"
15+
end

lib/cssbundling-rails.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Cssbundling
2+
end
3+
4+
require "cssbundling/version"
5+
require "cssbundling/engine"

lib/cssbundling/engine.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Cssbundling
2+
class Engine < ::Rails::Engine
3+
end
4+
end

lib/cssbundling/version.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Cssbundling
2+
VERSION = "0.1.0"
3+
end

lib/install/install.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
say "Build into app/assets/builds"
2+
empty_directory "app/assets/builds"
3+
keep_file "app/assets/builds"
4+
append_to_file "app/assets/config/manifest.js", %(//= link_tree ../builds\n)
5+
6+
if Rails.root.join(".gitignore").exist?
7+
append_to_file(".gitignore", %(/app/assets/builds\n))
8+
end
9+
10+
say "Remove app/assets/stylesheets/application.css so build output can take over"
11+
remove_file "app/assets/stylesheets/application.css"
12+
13+
unless Rails.root.join("package.json").exist?
14+
say "Add default package.json"
15+
copy_file "#{__dir__}/package.json", "package.json"
16+
end

lib/install/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "app",
3+
"private": "true"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Entry point for your PostCSS build */

lib/install/postcss/install.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
say "Install PostCSS w/ postcss-preset-env"
2+
copy_file "#{__dir__}/postcss.config.js", "postcss.config.js"
3+
copy_file "#{__dir__}/application.postcss.css", "app/assets/stylesheets/application.postcss.css"
4+
run "yarn add postcss postcss-cli postcss-preset-env"
5+
6+
say "Add build:css script"
7+
run %(npm set-script build:css "postcss ./app/assets/stylesheets/application.postcss.css -o ./app/assets/builds/application.css")

lib/install/postcss/postcss.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
plugins: [
3+
require('postcss-preset-env')({
4+
stage: 2,
5+
features: {
6+
'nesting-rules': true
7+
}
8+
})
9+
],
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Entry point for your Sass build

lib/install/sass/install.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
say "Install Sass"
2+
copy_file "#{__dir__}/application.sass.scss", "app/assets/stylesheets/application.sass.scss"
3+
run "yarn add sass"
4+
5+
say "Add build:css script"
6+
run %(npm set-script build:css "sass ./app/assets/stylesheets/application.sass.scss ./app/assets/builds/application.css --no-source-map")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;

lib/install/tailwind/install.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
say "Install Tailwind (+PostCSS w/ autoprefixer)"
2+
copy_file "#{__dir__}/tailwind.config.js", "tailwind.config.js"
3+
copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css"
4+
run "yarn add tailwindcss@latest postcss@latest autoprefixer@latest"
5+
6+
say "Add build:css script"
7+
run %(npm set-script build:css "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css")

lib/install/tailwind/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "app",
3+
"private": "true",
4+
"scripts": {
5+
"build:css": "tailwindcss -i ./app/assets/stylesheets/application.tw.css -o ./app/assets/builds/application.css"
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
mode: 'jit',
3+
purge: [
4+
'./app/views/**/*.html.erb',
5+
'./app/helpers/**/*.rb',
6+
'./app/javascript/**/*.js'
7+
]
8+
}

0 commit comments

Comments
 (0)