Skip to content

Commit 85c3b2b

Browse files
authored
Use Vite waitForRequestsIdle (#13291)
1 parent 89a0b86 commit 85c3b2b

File tree

4 files changed

+398
-64
lines changed

4 files changed

+398
-64
lines changed

packages/@tailwindcss-vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
},
3535
"devDependencies": {
3636
"@types/node": "^20.11.17",
37-
"vite": "^5.0.11"
37+
"vite": "^5.2.0"
3838
}
3939
}

packages/@tailwindcss-vite/src/index.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,35 +71,6 @@ export default function tailwindcss(): Plugin[] {
7171
return optimizeCss(generateCss(css), { minify })
7272
}
7373

74-
// In dev mode, there isn't a hook to signal that we've seen all files. We use
75-
// a timer, resetting it on each file seen, and trigger CSS generation when we
76-
// haven't seen any new files after a timeout. If this triggers too early,
77-
// there will be a FOOC and but CSS will regenerate after we've seen more files.
78-
let initialScan = (() => {
79-
// If too short, we're more likely to trigger a FOOC and generate CSS
80-
// multiple times. If too long, we delay dev builds.
81-
let delayInMs = 50
82-
83-
let timer: ReturnType<typeof setTimeout>
84-
let resolve: () => void
85-
let resolved = false
86-
87-
return {
88-
tick() {
89-
if (resolved) return
90-
timer && clearTimeout(timer)
91-
timer = setTimeout(resolve, delayInMs)
92-
},
93-
94-
complete: new Promise<void>((_resolve) => {
95-
resolve = () => {
96-
resolved = true
97-
_resolve()
98-
}
99-
}),
100-
}
101-
})()
102-
10374
return [
10475
{
10576
// Step 1: Scan source files for candidates
@@ -116,7 +87,6 @@ export default function tailwindcss(): Plugin[] {
11687

11788
// Scan index.html for candidates
11889
transformIndexHtml(html) {
119-
initialScan.tick()
12090
let updated = scan(html, 'html')
12191

12292
// In dev mode, if the generated CSS contains a URL that causes the
@@ -130,7 +100,6 @@ export default function tailwindcss(): Plugin[] {
130100

131101
// Scan all other files for candidates
132102
transform(src, id) {
133-
initialScan.tick()
134103
if (id.includes('/.vite/')) return
135104
let [filename] = id.split('?', 2)
136105
let extension = path.extname(filename).slice(1)
@@ -148,13 +117,15 @@ export default function tailwindcss(): Plugin[] {
148117
// Step 2 (dev mode): Generate CSS
149118
name: '@tailwindcss/vite:generate:serve',
150119
apply: 'serve',
120+
151121
async transform(src, id) {
152122
if (!isCssFile(id) || !src.includes('@tailwind')) return
153123

154124
cssModules.add(id)
155125

156-
// For the initial load we must wait for all source files to be scanned
157-
await initialScan.complete
126+
// Wait until all other files have been processed, so we can extract all
127+
// candidates before generating CSS.
128+
await server?.waitForRequestsIdle(id)
158129

159130
return { code: generateCss(src) }
160131
},

playgrounds/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@types/react": "^18.2.48",
1919
"@types/react-dom": "^18.2.18",
2020
"bun": "^1.0.26",
21-
"vite": "^5.0.11",
21+
"vite": "^5.2.0",
2222
"vite-plugin-handlebars": "^2.0.0"
2323
}
2424
}

0 commit comments

Comments
 (0)