Skip to content

Commit c941c5a

Browse files
author
David Heinemeier Hansson
committed
Use Sprockets compressor for purging
1 parent 5401a44 commit c941c5a

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

lib/install/tailwindcss.rb

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
say "Default application.html.erb is missing!", :red
88
say %( Add <%= stylesheet_link_tag "tailwind" %> within the <head> tag in your custom layout.)
99
end
10+
11+
say "Turn on purging of unused css classes in production"
12+
append_to_file Rails.root.join("app/config/environments/production.rb"), %( config.assets.css_compressor = :purger)

lib/tailwindcss-rails.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ module Tailwindcss
33

44
require "tailwindcss/version"
55
require "tailwindcss/engine"
6+
require "tailwindcss/compressor"

lib/tailwindcss/compressor.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "tailwindcss/purger"
2+
3+
class Tailwindcss::Compressor
4+
def self.instance
5+
@instance ||= new
6+
end
7+
8+
def self.call(input)
9+
instance.call(input)
10+
end
11+
12+
def initialize(options = {})
13+
@options = { paths_with_css_class_names: [ "app/views/**/*.*", "app/helpers/**/*.rb" ] }.merge(options).freeze
14+
@purger = Tailwindcss::Purger.new(@options)
15+
end
16+
17+
def call(input)
18+
{ data: @purger.purge(input[:data]) }
19+
end
20+
end

lib/tailwindcss/engine.rb

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
module Tailwindcss
44
class Engine < ::Rails::Engine
5+
initializer "tailwindcss.compressor" do
6+
Sprockets.register_compressor "text/css", :purger, Tailwindcss::Compressor
7+
end
8+
59
initializer "tailwindcss.assets" do
610
Rails.application.config.assets.precompile += %w( tailwind )
711
end

lib/tailwindcss/purger.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Tailwindcss::Purger
2-
attr_reader :stylesheet_path, :paths_with_css_class_names
2+
attr_reader :paths_with_css_class_names
33

4-
def initialize(stylesheet_path:, paths_with_css_class_names:)
5-
@stylesheet_path, @paths_with_css_class_names = stylesheet_path, paths_with_css_class_names
4+
def initialize(paths_with_css_class_names:)
5+
@paths_with_css_class_names = paths_with_css_class_names
66
end
77

8-
def purge
8+
def purge(input)
99
inside_valid_selector = inside_invalid_selector = false
1010
output = []
1111

12-
stylesheet_path.readlines.each do |line|
12+
input.split("\n").each do |line|
1313
case
1414
when inside_valid_selector
1515
output << line

lib/tasks/tailwindcss_tasks.rake

-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,4 @@ namespace :tailwindcss do
33
task :install do
44
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../install/tailwindcss.rb", __dir__)}"
55
end
6-
7-
task :purge do
8-
Tailwindcss::Purger.new(
9-
stylesheet_path: Rails.root.join("public/assets").glob("tailwindcss-*.css").first,
10-
paths_with_css_class_names: [ "app/views/**/*.html*", "app/helpers/**/*.rb" ]
11-
).purge
12-
end
136
end

0 commit comments

Comments
 (0)