Skip to content

Commit

Permalink
fix(vite-plugin-windicss): fix vite5 hmr (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Dec 25, 2023
1 parent a0fcf18 commit 29cbb86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/shared/virtual-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const MODULE_ID_VIRTUAL_MODULES = [
`${MODULE_ID_VIRTUAL_PREFIX}-components.css`,
]

export function createVirtualModuleLoader(ctx: { utils: WindiPluginUtils }): Pick<Plugin, 'resolveId' | 'load' | 'watchChange'> {
export function createVirtualModuleLoader(ctx: { utils: WindiPluginUtils; inHmr?: boolean }): Pick<Plugin, 'resolveId' | 'load' | 'watchChange'> {
return {
resolveId(id) {
if (id.startsWith(MODULE_ID_VIRTUAL_PREFIX))
Expand Down Expand Up @@ -40,6 +40,10 @@ export function createVirtualModuleLoader(ctx: { utils: WindiPluginUtils }): Pic
},

async watchChange(id, change) {
// In vite5, watchChange is triggered before hmr even if the devServer is started, which causes the hot update to fail, add the inHmr flag to skip the repeat trigger (as in vite4)
if (ctx.inHmr)
return

if (change.event === 'delete' || !existsSync(id))
return
if (!ctx.utils.isDetectTarget(id))
Expand Down
7 changes: 6 additions & 1 deletion packages/vite-plugin-windicss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ function VitePluginWindicss(userOptions: UserOptions = {}, utilsOptions: WindiPl
await utils.ensureInit()
},

...createVirtualModuleLoader({ get utils() { return utils } }) as Omit<Plugin, 'name'>,
...createVirtualModuleLoader({
get utils() { return utils },
get inHmr() {
return Boolean(server)
},
}) as Omit<Plugin, 'name'>,
})

let _cssReloadTask: any
Expand Down

0 comments on commit 29cbb86

Please sign in to comment.