Skip to content

Commit 8380816

Browse files
author
David Heinemeier Hansson
authored
Add --css app generator option (#43177)
* Add --css to preconfigure a CSS bundler/processor * Simpler conditional * Add CSS gems * Test CSS options
1 parent 061bf31 commit 8380816

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ gem "rack-cache", "~> 1.2"
1616
gem "stimulus-rails"
1717
gem "turbo-rails"
1818
gem "jsbundling-rails"
19+
gem "cssbundling-rails"
1920
gem "importmap-rails"
21+
gem "tailwindcss-rails"
2022
# require: false so bcrypt is loaded only when has_secure_password is used.
2123
# This is to avoid Active Model (and by extension the entire framework)
2224
# being dependent on a binary library.

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ GEM
184184
crack (0.4.5)
185185
rexml
186186
crass (1.0.6)
187+
cssbundling-rails (0.1.0)
188+
rails (>= 6.0.0)
187189
curses (1.4.2)
188190
daemons (1.4.0)
189191
dalli (2.7.11)
@@ -470,6 +472,8 @@ GEM
470472
rails (>= 6.0.0)
471473
sucker_punch (3.0.1)
472474
concurrent-ruby (~> 1.0)
475+
tailwindcss-rails (0.4.3)
476+
rails (>= 6.0.0)
473477
terser (1.1.4)
474478
execjs (>= 0.3.0, < 3)
475479
thin (1.8.1)
@@ -530,6 +534,7 @@ DEPENDENCIES
530534
byebug
531535
capybara (>= 3.26)
532536
connection_pool
537+
cssbundling-rails
533538
dalli
534539
delayed_job
535540
delayed_job_active_record
@@ -578,6 +583,7 @@ DEPENDENCIES
578583
stackprof
579584
stimulus-rails
580585
sucker_punch
586+
tailwindcss-rails
581587
terser (>= 1.1.4)
582588
turbo-rails
583589
tzinfo-data

railties/lib/rails/generators/app_base.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def gemfile_entries # :doc:
110110
web_server_gemfile_entry,
111111
javascript_gemfile_entry,
112112
hotwire_gemfile_entry,
113+
css_gemfile_entry,
113114
jbuilder_gemfile_entry,
114115
psych_gemfile_entry,
115116
cable_gemfile_entry].flatten.find_all(&@gem_filter)
@@ -315,6 +316,20 @@ def hotwire_gemfile_entry
315316
[ turbo_rails_entry, stimulus_rails_entry ]
316317
end
317318

319+
def using_node?
320+
options[:javascript] && options[:javascript] != "importmap"
321+
end
322+
323+
def css_gemfile_entry
324+
return [] unless options[:css]
325+
326+
if !using_node? && options[:css] == "tailwind"
327+
GemfileEntry.version("tailwindcss-rails", ">= 0.4.3", "Use Tailwind CSS. See: https://github.com/rails/tailwindcss-rails")
328+
else
329+
GemfileEntry.version("cssbundling-rails", ">= 0.1.0", "Bundle and process CSS with Tailwind, PostCSS, or Sass. Read more: https://github.com/rails/cssbundling-rails")
330+
end
331+
end
332+
318333
def psych_gemfile_entry
319334
return [] unless defined?(Rubinius)
320335

@@ -388,6 +403,16 @@ def run_hotwire
388403
rails_command "turbo:install stimulus:install"
389404
end
390405

406+
def run_css
407+
return if !options[:css] || !bundle_install?
408+
409+
if !using_node? && options[:css] == "tailwind"
410+
rails_command "tailwindcss:install"
411+
else
412+
rails_command "css:install:#{options[:css]}"
413+
end
414+
end
415+
391416
def generate_bundler_binstub
392417
if bundle_install?
393418
bundle_command("binstubs bundler")

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class AppGenerator < AppBase
271271
class_option :api, type: :boolean, desc: "Preconfigure smaller stack for API only apps"
272272
class_option :minimal, type: :boolean, desc: "Preconfigure a minimal rails app"
273273
class_option :javascript, type: :string, aliases: "-j", default: "importmap", desc: "Choose JavaScript approach [options: importmap (default), webpack, esbuild, rollup]"
274+
class_option :css, type: :string, desc: "Choose CSS processor [options: tailwind, postcss, sass]"
274275
class_option :skip_bundle, type: :boolean, aliases: "-B", default: false, desc: "Don't run bundle install"
275276

276277
def initialize(*args)
@@ -512,6 +513,7 @@ def finish_template
512513
public_task :generate_bundler_binstub
513514
public_task :run_javascript
514515
public_task :run_hotwire
516+
public_task :run_css
515517

516518
def run_after_bundle_callbacks
517519
@after_bundle_callbacks.each(&:call)

railties/lib/rails/generators/rails/app/templates/Gemfile.tt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ ruby <%= "\"#{RUBY_VERSION}\"" -%>
2121

2222
# Use Sass to process CSS
2323
# gem "sassc-rails", "~> 2.1"
24-
25-
# Use Tailwind CSS. See: https://github.com/rails/tailwindcss-rails
26-
# gem "tailwindcss-rails", "~> 0.4.3"
2724
<% end -%>
2825

2926
# Use Active Model has_secure_password

railties/test/generators/app_generator_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,20 @@ def test_skip_hotwire
819819
assert_no_file "app/javascript/application.js"
820820
end
821821

822+
def test_css_option_with_asset_pipeline_tailwind
823+
run_generator [destination_root, "--dev", "--css", "tailwind"]
824+
assert_gem "tailwindcss-rails"
825+
assert_file "app/views/layouts/application.html.erb" do |content|
826+
assert_match(/tailwind/, content)
827+
end
828+
end
829+
830+
def test_css_option_with_cssbundling_gem
831+
run_generator [destination_root, "--dev", "--css", "postcss"]
832+
assert_gem "cssbundling-rails"
833+
assert_file "app/assets/stylesheets/application.postcss.css"
834+
end
835+
822836
def test_bootsnap
823837
run_generator [destination_root, "--no-skip-bootsnap"]
824838

0 commit comments

Comments
 (0)