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

Revamp of the jit_builder.rb to support Fast Refresh in Bridgetown 2.0 #4

Open
jaredcwhite opened this issue Dec 7, 2024 · 0 comments

Comments

@jaredcwhite
Copy link
Member

jaredcwhite commented Dec 7, 2024

I spent a bit of time on a new site running the latest Bridgetown 2.0 beta and Tailwind 4.0 beta 1. In order to support the new fast refresh feature, we need a way to hook into a site's post_write event and, if on the fast refresh path, trigger the Tailwind JIT refresh.

This is best I could get it for now. Maybe folks can play around with the concept some more. Basically fast refresh happens first and you get a browser reload right away, which is good for normal content updates, and then there's a full refresh shortly thereafter. It does mean there can be a "flash of not yet Tailwind-generated styling" as you're playing around with element classes though. I'm not really sure how to make it smoother, but at least it works. I also thought of the idea of having a --skip-tw-jit CLI option, so when you know you're just working on content and not styling per se, you can skip the extra refresh entirely.

class Builders::TailwindJit < SiteBuilder
  def build
    return if ARGV.include?("--skip-tw-jit")

    fast_refreshing = false

    hook :site, :fast_refresh do
      fast_refreshing = true
    end

    hook :site, :post_write do
      if fast_refreshing
        fast_refreshing = false
        Thread.new do
          sleep 0.75
          refresh_file = site.in_root_dir("frontend", "styles", "jit-refresh.css")
          File.write refresh_file, "/* #{Time.now.to_i} */"
        end
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant