Skip to content

Commit ef13af0

Browse files
committed
Uses tailwindcss cli to render minimal css
- update tailwind jit js pack to 3.1.4
1 parent caa9d44 commit ef13af0

File tree

7 files changed

+369
-68
lines changed

7 files changed

+369
-68
lines changed

Gemfile

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ gem 'redcarpet', '~> 3.5'
5454
gem 'simple_form'
5555
gem 'tinymce-rails'
5656
gem 'image_processing', '~> 1.2'
57-
gem 'puppeteer-ruby'
5857
gem 'react-rails'
5958
gem 'webpacker'
6059

Gemfile.lock

-5
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,6 @@ GEM
362362
nio4r (~> 2.0)
363363
pundit (2.1.1)
364364
activesupport (>= 3.0.0)
365-
puppeteer-ruby (0.40.7)
366-
concurrent-ruby (~> 1.1.0)
367-
mime-types (>= 3.0)
368-
websocket-driver (>= 0.6.0)
369365
racc (1.6.0)
370366
rack (2.2.3.1)
371367
rack-mini-profiler (2.3.3)
@@ -583,7 +579,6 @@ DEPENDENCIES
583579
pry-rescue
584580
puma
585581
pundit
586-
puppeteer-ruby
587582
rack-mini-profiler
588583
rack-timeout (~> 0.5)
589584
rails (~> 6.1.4)

app/controllers/staff/pages_controller.rb

+21-17
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,28 @@ def set_page
7171
end
7272

7373
def save_tailwind_page_content
74-
Puppeteer.launch(
75-
headless: true,
76-
args: ['--no-sandbox', '--disable-setuid-sandbox']
77-
) do |browser|
78-
page = browser.pages.first || browser.new_page
79-
page.goto(root_url)
80-
cookies.each do |name, value|
81-
page.set_cookie(name: name, value: value)
82-
end
83-
page.goto(event_staff_page_url(current_event, @page), wait_until: 'domcontentloaded')
84-
css = page.query_selector_all('style').map do |style|
85-
style.evaluate('(el) => el.textContent')
86-
end.detect { |text| text.match("tailwindcss") }
87-
html = "<style>#{css}</style>"
88-
89-
content = @page.contents.find_or_initialize_by(name: Page::TAILWIND)
90-
content.update!(placement: Website::Content::HEAD, html: html)
74+
@body = @page.unpublished_body
75+
content = render_to_string(template: 'pages/show', layout: "themes/#{current_website.theme}")
76+
command = ["yarn run tailwindcss --minify"]
77+
page_file = Tempfile.new(['page_content', '.html'], 'tmp')
78+
page_file.write(content)
79+
page_file.close
80+
command.push("--content", page_file.path)
81+
if tailwind_config = current_website.tailwind_config
82+
config_file = Tempfile.new(['config_file', '.js'], 'tmp')
83+
config_file.write(tailwind_config)
84+
config_file.close
85+
command.push("--config", config_file.path)
9186
end
87+
output = `#{command.join(' ')}`
88+
css = output.match(/(\/\*! tailwindcss .*)/m)
89+
html = "<style>#{css}</style>"
90+
91+
content = @page.contents.find_or_initialize_by(name: Page::TAILWIND)
92+
content.update!(placement: Website::Content::HEAD, html: html)
93+
ensure
94+
page_file&.unlink
95+
config_file&.unlink
9296
end
9397

9498
def build_page

app/javascript/packs/tailwind.js

+35-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/models/website.rb

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def self.domain_match(domain)
2929
where(arel_table[:domains].matches("%#{domain}%"))
3030
end
3131

32+
def tailwind_config
33+
config = contents
34+
.where(Content.arel_table[:html]
35+
.matches("%tailwind.config%"))
36+
.first
37+
if config
38+
config.html.gsub(%r{<script>|</script>}, "").gsub("tailwind.config", "module.exports")
39+
end
40+
end
41+
3242
def manual_purge
3343
purge_cache { save }
3444
end

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"react-dom": "^16.8.6",
1515
"react_ujs": "^2.5.0",
1616
"regenerator-runtime": "^0.13.7",
17-
"stimulus": "^3.0.1"
17+
"stimulus": "^3.0.1",
18+
"tailwindcss": "^3.1.5"
1819
},
1920
"devDependencies": {
2021
"webpack-dev-server": "^3.3.1"

0 commit comments

Comments
 (0)