Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 12, 2023
1 parent c706caa commit a48f743
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/vite/src/modes/global/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,23 @@ export function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedMo

// inject css modules to send callback on css load
if (layer && code.includes('import.meta.hot')) {
const immediateFunction = (code: string) => `;(async function() {\n${code}\n})()`
const importMetaHot = `if (import.meta.hot) {
try { await import.meta.hot.send('${WS_EVENT_PREFIX}', ['${layer}', __vite__css.slice(2,${2 + HASH_LENGTH})]); }
catch (e) { console.warn('[unocss-hmr]', e) }
if (!import.meta.url.includes('?'))
await new Promise(resolve => setTimeout(resolve, 100))
}`
const { hmrTopLevelAwait } = await getConfig() as VitePluginConfig

return `${code}\n${
hmrTopLevelAwait !== false
? importMetaHot
: immediateFunction(importMetaHot)
}`
let hmr = `
try {
await import.meta.hot.send('${WS_EVENT_PREFIX}', ['${layer}', __vite__css.slice(2,${2 + HASH_LENGTH})]);
} catch (e) {
console.warn('[unocss-hmr]', e)
}
if (!import.meta.url.includes('?'))
await new Promise(resolve => setTimeout(resolve, 100))`

const config = await getConfig() as VitePluginConfig

if (config.hmrTopLevelAwait === false)
hmr = `;(async function() {${hmr}\n})()`

hmr = `\nif (import.meta.hot) {${hmr}}`

return code + hmr
}
},
},
Expand Down

0 comments on commit a48f743

Please sign in to comment.