Skip to content
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

Styles Generator: Drop support for Tailwind #1169

Merged
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
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,13 @@ via [better_html][], [erb_lint][] and [erblint-github][].

### Styles

Configures applications to use [PostCSS][] or [Tailwind][] via
[cssbundling-rails][]. Defaults to PostCSS with [modern-normalize][], with the
option to override via `--css=tailwind`.
Configures applications to use [PostCSS][] via [cssbundling-rails][].

Also creates additional stylesheets if using PostCSS.
Adds [modern-normalize][], and style sheet structure.

`bin/rails g suspenders:styles --css[postcss:tailwind]`
`bin/rails g suspenders:styles`

[PostCSS]: https://postcss.org
[Tailwind]: https://tailwindcss.com
[cssbundling-rails]: https://github.com/rails/cssbundling-rails
[modern-normalize]: https://github.com/sindresorhus/modern-normalize

Expand Down
6 changes: 1 addition & 5 deletions lib/generators/suspenders/lint_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ def install_gems
end

def configure_stylelint
if using_tailwind?
copy_file "tailwind.stylelintrc.json", ".stylelintrc.json"
else
copy_file "stylelintrc.json", ".stylelintrc.json"
end
copy_file "stylelintrc.json", ".stylelintrc.json"
end

def configure_eslint
Expand Down
26 changes: 3 additions & 23 deletions lib/generators/suspenders/styles_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,20 @@ module Generators
class StylesGenerator < Rails::Generators::Base
include Suspenders::Generators::APIAppUnsupported

CSS_OPTIONS = %w[tailwind postcss].freeze

class_option :css, enum: CSS_OPTIONS, default: "postcss"
desc <<~TEXT
Configures applications to use PostCSS or Tailwind via cssbundling-rails.
Defaults to PostCSS with modern-normalize, with the option to override via
--css=tailwind.
Configures application to use PostCSS via cssbundling-rails.

Also creates additional stylesheets if using PostCSS.
Adds modern-normalize, and style sheet structure.
TEXT

def add_cssbundling_rails_gem
gem "cssbundling-rails"

Bundler.with_unbundled_env { run "bundle install" }
run "bin/rails css:install:#{css}"
run "bin/rails css:install:postcss"
end

def build_directory_structure
return if is_tailwind?

create_file "app/assets/stylesheets/base.css"
append_to_file "app/assets/stylesheets/base.css", "/* Base Styles */"

Expand All @@ -34,10 +27,7 @@ def build_directory_structure
append_to_file "app/assets/stylesheets/utilities.css", "/* Utility Styles */"
end

# Modify if https://github.com/rails/cssbundling-rails/pull/139 is merged
def configure_application_stylesheet
return if is_tailwind?

run "yarn add modern-normalize"

append_to_file "app/assets/stylesheets/application.postcss.css" do
Expand All @@ -49,16 +39,6 @@ def configure_application_stylesheet
TEXT
end
end

private

def css
@css ||= options["css"]
end

def is_tailwind?
css == "tailwind"
end
end
end
end
11 changes: 0 additions & 11 deletions lib/generators/templates/lint/tailwind.stylelintrc.json

This file was deleted.

4 changes: 0 additions & 4 deletions lib/suspenders/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def default_test_helper_present?
def rspec_test_helper_present?
File.exist? Rails.root.join("spec/rails_helper.rb")
end

def using_tailwind?
File.exist? Rails.root.join("tailwind.config.js")
end
end

module APIAppUnsupported
Expand Down
24 changes: 0 additions & 24 deletions test/generators/suspenders/lint_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,6 @@ class LintGeneratorTest < Rails::Generators::TestCase
end
end

test "configures stylelint for tailwind" do
expected_content = <<~TEXT
{
"extends": "@thoughtbot/stylelint-config",
"rules": {
"scss/at-rule-no-unknown": [
true,
{
"ignoreAtRules": ["tailwind"]
}
]
}
}
TEXT

with_css_option :tailwind do
capture(:stderr) { run_generator }

assert_file app_root(".stylelintrc.json") do |file|
assert_equal expected_content, file
end
end
end

test "configures eslint" do
expected_content = <<~JSON
{
Expand Down
130 changes: 6 additions & 124 deletions test/generators/suspenders/styles_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Suspenders
module Generators
class StylesGenerator::DefaultTest < Rails::Generators::TestCase
class StylesGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::StylesGenerator
Expand Down Expand Up @@ -45,130 +45,8 @@ class StylesGenerator::DefaultTest < Rails::Generators::TestCase
assert_match(/bin\/rails css:install:postcss/, output)
end

test "generator has a description" do
description = <<~TEXT
Configures applications to use PostCSS or Tailwind via cssbundling-rails.
Defaults to PostCSS with modern-normalize, with the option to override via
--css=tailwind.

Also creates additional stylesheets if using PostCSS.
TEXT

assert_equal description, generator_class.desc
end

private

def prepare_destination
touch "Gemfile"
touch "app/assets/stylesheets/application.postcss.css"
end

def restore_destination
remove_file_if_exists "Gemfile"
remove_file_if_exists "package.json", root: true
remove_file_if_exists "yarn.lock", root: true
remove_file_if_exists "app/assets/stylesheets/application.postcss.css"
remove_file_if_exists "app/assets/stylesheets/base.css"
remove_file_if_exists "app/assets/stylesheets/components.css"
remove_file_if_exists "app/assets/stylesheets/utilities.css"
end
end

class StylesGenerator::ClassOptionTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::StylesGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "has a css option" do
option = generator_class.class_options[:css]

assert_equal :string, option.type
assert_not option.required
assert_equal %w[tailwind postcss], option.enum
assert_equal "postcss", option.default
end

test "raises if css option is unsupported" do
output = capture(:stderr) { run_generator %w[--css=unknown] }

assert_match(/Expected '--css' to be one of/, output)
end

private

def prepare_destination
touch "Gemfile"
end

def restore_destination
remove_file_if_exists "Gemfile"
remove_file_if_exists "package.json", root: true
remove_file_if_exists "yarn.lock", root: true
remove_file_if_exists "app/assets/stylesheets/application.postcss.css"
remove_file_if_exists "app/assets/stylesheets/base.css"
remove_file_if_exists "app/assets/stylesheets/components.css"
remove_file_if_exists "app/assets/stylesheets/utilities.css"
end
end

class StylesGenerator::TailwindTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::StylesGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "runs install script" do
output = run_generator %w[--css=tailwind]

assert_match(/bin\/rails css:install:tailwind/, output)
end

test "does not install modern-normalize" do
output = run_generator %w[--css=tailwind]

assert_no_match(/add.*modern-normalize/, output)
end

test "does not create stylesheets" do
run_generator %w[--css=tailwind]

assert_no_file app_root("app/assets/stylesheets/base.css")
assert_no_file app_root("app/assets/stylesheets/components.css")
assert_no_file app_root("app/assets/stylesheets/utilities.css")
end

private

def prepare_destination
touch "Gemfile"
end

def restore_destination
remove_file_if_exists "Gemfile"
remove_file_if_exists "package.json", root: true
remove_file_if_exists "yarn.lock", root: true
remove_file_if_exists "app/assets/stylesheets/base.css"
remove_file_if_exists "app/assets/stylesheets/components.css"
remove_file_if_exists "app/assets/stylesheets/utilities.css"
end
end

class StylesGenerator::PostCssTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::StylesGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "installs modern-normalize and imports stylesheets" do
output = run_generator %w[--css=postcss]
output = run_generator
application_stylesheet = <<~TEXT
@import "modern-normalize";
@import "base.css";
Expand Down Expand Up @@ -197,6 +75,10 @@ class StylesGenerator::PostCssTest < Rails::Generators::TestCase
end
end

test "generator has a custom description" do
assert_no_match(/Description/, generator_class.desc)
end

private

def prepare_destination
Expand Down
15 changes: 0 additions & 15 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,6 @@ def with_test_suite(test_suite, &block)
remove_dir_if_exists "spec"
end

def with_css_option(css, &block)
case css
when :postcss
touch "postcss.config.js"
when :tailwind
touch "tailwind.config.js"
else
raise ArgumentError, "unknown css option: #{css.inspect}"
end
yield
ensure
remove_file_if_exists "postcss.config.js"
remove_file_if_exists "tailwind.config.js"
end

def backup_file(file)
FileUtils.copy app_root(file), app_root("#{file}.bak")
end
Expand Down