Skip to content

Make files with class names configurable #74

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

Closed
wants to merge 6 commits into from
Closed
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
5 changes: 2 additions & 3 deletions lib/tailwindcss/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ def call(input)
end

private

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation

private
  def files_with_class_names

def files_with_class_names
Rails.root.glob("app/views/**/*.*") +
Rails.root.glob("app/helpers/**/*.rb") +
Rails.root.glob("app/javascript/**/*.js")
Rails.application.config.tailwind.files_with_class_names
end
end
7 changes: 7 additions & 0 deletions lib/tailwindcss/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

module Tailwindcss
class Engine < ::Rails::Engine
config.tailwind = ActiveSupport::OrderedOptions.new
config.tailwind.files_with_class_names = []

initializer "set files with class names" do
config.tailwind.files_with_class_names += Rails.root.glob("app/views/**/*.*") + Rails.root.glob("app/helpers/**/*.rb") + Rails.root.glob("app/javascript/**/*.js")
end

initializer "tailwindcss.compressor" do
Sprockets.register_compressor "text/css", :purger, Tailwindcss::Compressor
end
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/tailwindcss_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace :tailwindcss do
end

desc "Show the list of class names being kept in Tailwind CSS"
task :keeping_class_names do
task keeping_class_names: :environment do
puts Tailwindcss::Purger.extract_class_names_from(default_files_with_class_names)
end

Expand All @@ -16,7 +16,7 @@ namespace :tailwindcss do
end

def default_files_with_class_names
Rails.root.glob("app/views/**/*.*") + Rails.root.glob("app/helpers/**/*.rb")
Rails.application.config.tailwind.files_with_class_names
end

def tailwind_css
Expand Down
16 changes: 16 additions & 0 deletions test/compressor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "test_helper"

class Tailwindcss::CompressorTest < ActiveSupport::TestCase
test "files_with_class_names on default configuration" do
default_files_with_class_names = Rails.root.glob("app/views/**/*.*") + Rails.root.glob("app/helpers/**/*.rb") + Rails.root.glob("app/javascript/**/*.js")

assert_equal default_files_with_class_names, Tailwindcss::Compressor.new.instance_variable_get(:@options)[:files_with_class_names]
end

test "files_with_class_names on custom configuration" do
files_with_class_names = Rails.application.config.tailwind.files_with_class_names + Rails.root.glob("app/custom_views/**/*.*")
with_files_with_class_names_configuration(files_with_class_names) do
assert_equal files_with_class_names, Tailwindcss::Compressor.new.instance_variable_get(:@options)[:files_with_class_names]
end
end
end
3 changes: 3 additions & 0 deletions test/dummy/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "config/application"

Rails.application.load_tasks
2 changes: 2 additions & 0 deletions test/dummy/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//= link_tree ../images
//= link_directory ../stylesheets .css
Empty file.
4 changes: 4 additions & 0 deletions test/dummy/app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
*= require_tree .
*= require_self
*/
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationController < ActionController::Base
end
Empty file.
3 changes: 3 additions & 0 deletions test/dummy/app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class PostsController < ApplicationController
def index; end
end
1 change: 1 addition & 0 deletions test/dummy/app/custom_views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 class="font-bold text-red-500">Posts</h1>
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ApplicationHelper
end
3 changes: 3 additions & 0 deletions test/dummy/app/javascript/packs/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//= require rails-ujs
//= require activestorage
//= require_tree .
3 changes: 3 additions & 0 deletions test/dummy/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Empty file.
17 changes: 17 additions & 0 deletions test/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Dummy</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "inter-font", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>

<%= stylesheet_link_tag 'application', media: 'all' %>
</head>

<body>
<%= yield %>
</body>
</html>
4 changes: 4 additions & 0 deletions test/dummy/bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative "../config/boot"
require "rails/commands"
4 changes: 4 additions & 0 deletions test/dummy/bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require_relative "../config/boot"
require "rake"
Rake.application.run
23 changes: 23 additions & 0 deletions test/dummy/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
require "fileutils"

APP_ROOT = File.expand_path('..', __dir__)

def system!(*args)
system(*args) || abort("\n== Command #{args} failed ==")
end

FileUtils.chdir APP_ROOT do
puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')

puts "\n== Preparing database =="
system! 'bin/rails db:prepare'

puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'

puts "\n== Restarting application server =="
system! 'bin/rails restart'
end
4 changes: 4 additions & 0 deletions test/dummy/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require_relative "config/environment"

run Rails.application
Rails.application.load_server
20 changes: 20 additions & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require_relative "boot"

require "rails"
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_view/railtie"
require "sprockets/railtie"

Bundler.require(*Rails.groups)
require "tailwindcss-rails"

module Dummy
class Application < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f

config.paths["app/views"].unshift("#{Rails.root}/app/custom_views")
end
end
4 changes: 4 additions & 0 deletions test/dummy/config/boot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)

require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
$LOAD_PATH.unshift File.expand_path('../../../lib', __dir__)
16 changes: 16 additions & 0 deletions test/dummy/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
<<: *default
database: db/development.sqlite3

test:
<<: *default
database: db/test.sqlite3

production:
<<: *default
database: db/production.sqlite3
3 changes: 3 additions & 0 deletions test/dummy/config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "application"

Rails.application.initialize!
37 changes: 37 additions & 0 deletions test/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "active_support/core_ext/integer/time"

Rails.application.configure do
config.cache_classes = false

config.eager_load = false

config.consider_all_requests_local = true

if Rails.root.join('tmp', 'caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true

config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false

config.cache_store = :null_store
end

config.active_support.deprecation = :log

config.active_support.disallowed_deprecation = :raise

config.active_support.disallowed_deprecation_warnings = []

config.active_record.migration_error = :page_load

config.active_record.verbose_query_logs = true

config.assets.debug = true

config.assets.quiet = true
end
38 changes: 38 additions & 0 deletions test/dummy/config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "active_support/core_ext/integer/time"

Rails.application.configure do
config.cache_classes = true

config.eager_load = true

config.consider_all_requests_local = false
config.action_controller.perform_caching = true

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

config.assets.css_compressor = :purger

config.assets.compile = false

config.log_level = :info

config.log_tags = [ :request_id ]

config.i18n.fallbacks = true

config.active_support.deprecation = :notify

config.active_support.disallowed_deprecation = :log

config.active_support.disallowed_deprecation_warnings = []

config.log_formatter = ::Logger::Formatter.new

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end

config.active_record.dump_schema_after_migration = false
end
26 changes: 26 additions & 0 deletions test/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "active_support/core_ext/integer/time"

Rails.application.configure do
config.cache_classes = true

config.eager_load = false

config.public_file_server.enabled = true
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
}

config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store

config.action_dispatch.show_exceptions = false

config.action_controller.allow_forgery_protection = false

config.active_support.deprecation = :stderr

config.active_support.disallowed_deprecation = :raise

config.active_support.disallowed_deprecation_warnings = []
end
1 change: 1 addition & 0 deletions test/dummy/config/initializers/assets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rails.application.config.assets.version = '1.0'
1 change: 1 addition & 0 deletions test/dummy/config/initializers/backtrace_silencers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rails.backtrace_cleaner.remove_silencers! if ENV["BACKTRACE"]
1 change: 1 addition & 0 deletions test/dummy/config/initializers/cookies_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rails.application.config.action_dispatch.cookies_serializer = :json
3 changes: 3 additions & 0 deletions test/dummy/config/initializers/filter_parameter_logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.config.filter_parameters += [
:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]
3 changes: 3 additions & 0 deletions test/dummy/config/initializers/wrap_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: [:json]
end
13 changes: 13 additions & 0 deletions test/dummy/config/puma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

port ENV.fetch("PORT") { 3000 }

environment ENV.fetch("RAILS_ENV") { "development" }

pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

plugin :tmp_restart
3 changes: 3 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.routes.draw do
get 'posts', to: 'posts#index'
end
7 changes: 7 additions & 0 deletions test/dummy/config/storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

local:
service: Disk
root: <%= Rails.root.join("storage") %>
Empty file added test/dummy/lib/assets/.keep
Empty file.
Empty file added test/dummy/log/.keep
Empty file.
8 changes: 8 additions & 0 deletions test/dummy/public/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>404</title>
</head>
<body>
</body>
</html>
8 changes: 8 additions & 0 deletions test/dummy/public/422.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>422</title>
</head>
<body>
</body>
</html>
8 changes: 8 additions & 0 deletions test/dummy/public/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>500</title>
</head>
<body>
</body>
</html>
11 changes: 10 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

require_relative "dummy/config/environment"
ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)]

require "rails"
require "rails/test_help"
require "debug"
Expand All @@ -12,3 +14,10 @@
class ActiveSupport::TestCase
self.file_fixture_path = File.expand_path("fixtures/files", __dir__)
end

def with_files_with_class_names_configuration(new_value)
old_value = Rails.application.config.tailwind.files_with_class_names
Rails.application.config.tailwind.files_with_class_names = new_value
yield
Rails.application.config.tailwind.files_with_class_names = old_value
end