Skip to content

Commit

Permalink
Uses tailwindcss cli to render minimal css
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsgreen committed Jul 12, 2022
1 parent 7db5dc0 commit 5fb5c74
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 32 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ gem 'redcarpet', '~> 3.5'
gem 'simple_form'
gem 'tinymce-rails'
gem 'image_processing', '~> 1.2'
gem 'puppeteer-ruby'
gem 'react-rails'
gem 'webpacker'

Expand Down
5 changes: 0 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,6 @@ GEM
nio4r (~> 2.0)
pundit (2.1.1)
activesupport (>= 3.0.0)
puppeteer-ruby (0.40.7)
concurrent-ruby (~> 1.1.0)
mime-types (>= 3.0)
websocket-driver (>= 0.6.0)
racc (1.6.0)
rack (2.2.3.1)
rack-mini-profiler (2.3.3)
Expand Down Expand Up @@ -572,7 +568,6 @@ DEPENDENCIES
pry-rescue
puma
pundit
puppeteer-ruby
rack-mini-profiler
rack-timeout (~> 0.5)
rails (~> 6.1.4)
Expand Down
38 changes: 21 additions & 17 deletions app/controllers/staff/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,28 @@ def set_page
end

def save_tailwind_page_content
Puppeteer.launch(
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
) do |browser|
page = browser.pages.first || browser.new_page
page.goto(root_url)
cookies.each do |name, value|
page.set_cookie(name: name, value: value)
end
page.goto(event_staff_page_url(current_event, @page), wait_until: 'domcontentloaded')
css = page.query_selector_all('style').map do |style|
style.evaluate('(el) => el.textContent')
end.detect { |text| text.match("tailwindcss") }
html = "<style>#{css}</style>"

content = @page.contents.find_or_initialize_by(name: Page::TAILWIND)
content.update!(placement: Website::Content::HEAD, html: html)
@body = @page.unpublished_body
content = render_to_string(template: 'pages/show', layout: "themes/#{current_website.theme}")
command = ["yarn run tailwindcss --minify"]
page_file = Tempfile.new(['page_content', '.html'], 'tmp')
page_file.write(content)
page_file.close
command.push("--content", page_file.path)
if tailwind_config = current_website.tailwind_config
config_file = Tempfile.new(['config_file', '.js'], 'tmp')
config_file.write(tailwind_config)
config_file.close
command.push("--config", config_file.path)
end
output = `#{command.join(' ')}`
css = output.match(/(\/\*! tailwindcss .*)/m)
html = "<style>#{css}</style>"

content = @page.contents.find_or_initialize_by(name: Page::TAILWIND)
content.update!(placement: Website::Content::HEAD, html: html)
ensure
page_file&.unlink
config_file&.unlink
end

def build_page
Expand Down
10 changes: 10 additions & 0 deletions app/models/website.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def self.domain_match(domain)
where(arel_table[:domains].matches("%#{domain}%"))
end

def tailwind_config
config = contents
.where(Content.arel_table[:html]
.matches("%tailwind.config%"))
.first
if config
config.html.gsub(%r{<script>|</script>}, "").gsub("tailwind.config", "module.exports")
end
end

def manual_purge
purge_cache { save }
end
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"react-dom": "^16.8.6",
"react_ujs": "^2.5.0",
"regenerator-runtime": "^0.13.7",
"stimulus": "^3.0.1"
"stimulus": "^3.0.1",
"tailwindcss": "^3.1.5"
},
"devDependencies": {
"webpack-dev-server": "^3.3.1"
Expand Down
Loading

0 comments on commit 5fb5c74

Please sign in to comment.