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

feat(vite): introduce hmrTopLevelAwait option #2066

Merged
merged 8 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,6 @@ export function resolveConfig<Theme extends {} = {}>(
shortcuts: resolveShortcuts(mergePresets('shortcuts')),
extractors,
safelist: mergePresets('safelist'),
topLevelAwait: config.topLevelAwait ?? true,
}
}
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ export interface ConfigBase<Theme extends {} = {}> {
* @default false
*/
details?: boolean

/**
* Whether to use the top-level await
* @default true
*/
topLevelAwait?: boolean
zyyv marked this conversation as resolved.
Show resolved Hide resolved
}

export type AutoCompleteTemplate = string
Expand Down
10 changes: 8 additions & 2 deletions packages/vite/src/modes/global/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,19 @@ export function GlobalModeDevPlugin({ uno, tokens, tasks, flushTasks, affectedMo

// inject css modules to send callback on css load
if (layer && code.includes('import.meta.hot')) {
return `${code}
if (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))
}`

return `${code}\n${
uno.userConfig.topLevelAwait
? importMetaHot
: immediateFunction(importMetaHot)
}`
}
},
},
Expand Down