Skip to content

Commit

Permalink
Remove Vite plugin delay in dev mode
Browse files Browse the repository at this point in the history
We were using setTimeout to wait for the initial scan to complete before generating Tailwind CSS. Now that full builds wait until the bundle stage, dev builds can simply run immediately. This might generate Tailwind CSS twice, generating a FOUC, but will be faster than waiting for the timeout.

A [proposed Vite change](vitejs/vite#16135) could address the FOUC and extra build.
  • Loading branch information
KrisBraun committed Mar 12, 2024
1 parent ededb6d commit a9f1bc7
Showing 1 changed file with 0 additions and 34 deletions.
34 changes: 0 additions & 34 deletions packages/@tailwindcss-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,6 @@ export default function tailwindcss(): Plugin[] {
return css
}

// In dev mode, there isn't a hook to signal that we've seen all files. We use
// a timer, resetting it on each file seen, and trigger CSS generation when we
// haven't seen any new files after a timeout. If this triggers too early,
// there will be a FOOC and but CSS will regenerate after we've seen more files.
let initialScan = (() => {
// If too short, we're more likely to trigger a FOOC and generate CSS
// multiple times. If too long, we delay dev builds.
let delayInMs = 50

let timer: ReturnType<typeof setTimeout>
let resolve: () => void
let resolved = false

return {
tick() {
if (resolved) return
timer && clearTimeout(timer)
timer = setTimeout(resolve, delayInMs)
},

complete: new Promise<void>((_resolve) => {
resolve = () => {
resolved = true
_resolve()
}
}),
}
})()

return [
{
// Step 1: Scan source files for candidates
Expand All @@ -161,7 +132,6 @@ export default function tailwindcss(): Plugin[] {

// Scan index.html for candidates
transformIndexHtml(html) {
initialScan.tick()
let updated = scan(html, 'html')

// In dev mode, if the generated CSS contains a URL that causes the
Expand All @@ -175,7 +145,6 @@ export default function tailwindcss(): Plugin[] {

// Scan all other files for candidates
transform(src, id) {
initialScan.tick()
if (id.includes('/.vite/')) return
let [filename] = id.split('?', 2)
let extension = path.extname(filename).slice(1)
Expand All @@ -198,9 +167,6 @@ export default function tailwindcss(): Plugin[] {

cssModules.add(id)

// For the initial load we must wait for all source files to be scanned
await initialScan.complete

let css = generateCss(src)
css = await transformWithPlugins(this, css)
return { code: css }
Expand Down

0 comments on commit a9f1bc7

Please sign in to comment.