Skip to content

Commit

Permalink
perf(vite): improve FOUC
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 4, 2022
1 parent ae64288 commit 17cc664
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/vite/src/modes/global/dev.ts
Expand Up @@ -97,8 +97,9 @@ export function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate
invalidate(0)
})
},
async buildStart() {
await uno.generate('', { preflights: true })
buildStart() {
// warm up for preflights
uno.generate('', { preflights: true })
},
transform(code, id) {
if (filter(code, id))
Expand Down Expand Up @@ -144,8 +145,13 @@ export function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate
transform(code, id) {
// inject css modules to send callback on css load
if (entries.has(getPath(id)) && code.includes('import.meta.hot')) {
const snippet = `\nif (import.meta.hot) { try { import.meta.hot.send('${WS_EVENT_PREFIX}', ${lastServed}) } catch (e) { console.warn('[unocss-hmr]', e) } }`
return code + snippet
return `${code}
if (import.meta.hot) {
try { await import.meta.hot.send('${WS_EVENT_PREFIX}', ${lastServed}); }
catch (e) { console.warn('[unocss-hmr]', e) }
if (!import.meta.url.includes('?'))
await new Promise(resolve => setTimeout(resolve, 100))
}`
}
},
},
Expand Down

0 comments on commit 17cc664

Please sign in to comment.