Skip to content

Commit 199c1ba

Browse files
authored
fix(vite): encode virtual IDs in global HMR updates (#5298)
1 parent 2de0b99 commit 199c1ba

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

packages-integrations/vite/src/modes/global/dev.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { LAYER_MARK_ALL } from '#integration/constants'
88
import { getHash } from '#integration/hash'
99
import { resolveId, resolveLayer } from '#integration/layers'
1010
import { getPath } from '#integration/utils'
11-
import { toViteVirtualId } from '../../virtual'
11+
import { toViteClientPath, toViteVirtualId } from '../../virtual'
1212
import { MESSAGE_UNOCSS_ENTRY_NOT_FOUND } from './shared'
1313

1414
const WARN_TIMEOUT = 20000
@@ -75,9 +75,10 @@ export function GlobalModeDevPlugin(ctx: UnocssPluginContext): Plugin[] {
7575
const mod = server.moduleGraph.getModuleById(id)
7676
if (!mod)
7777
return null
78+
const path = toViteClientPath(mod.url)
7879
return {
79-
acceptedPath: mod.url,
80-
path: mod.url,
80+
acceptedPath: path,
81+
path,
8182
timestamp: lastServedTime,
8283
type: 'js-update',
8384
} as Update
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
const VITE_VIRTUAL_ID_PREFIX = '\0'
2+
const VITE_CLIENT_ID_PREFIX = '/@id/'
3+
const VITE_NULL_BYTE_PLACEHOLDER = '__x00__'
24

35
export function toViteVirtualId(id: string) {
46
return id.startsWith(VITE_VIRTUAL_ID_PREFIX)
57
? id
68
: `${VITE_VIRTUAL_ID_PREFIX}${id}`
79
}
10+
11+
export function toViteClientPath(id: string) {
12+
return id.startsWith(VITE_VIRTUAL_ID_PREFIX)
13+
? `${VITE_CLIENT_ID_PREFIX}${id.replace(VITE_VIRTUAL_ID_PREFIX, VITE_NULL_BYTE_PLACEHOLDER)}`
14+
: id
15+
}

test/virtual-modules.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'node:path'
22
import * as vite from 'vite'
33
import { describe, expect, it } from 'vitest'
44
import UnoCSS from '../packages-integrations/vite/src/index'
5+
import { toViteClientPath } from '../packages-integrations/vite/src/virtual'
56

67
describe('vite virtual module ids', () => {
78
async function createServer(mode: 'global' | 'per-module') {
@@ -28,6 +29,13 @@ describe('vite virtual module ids', () => {
2829
}
2930
})
3031

32+
it('converts internal ids to public client paths', () => {
33+
expect(toViteClientPath('\0/__uno.css'))
34+
.toBe('/@id/__x00__/__uno.css')
35+
expect(toViteClientPath('/src/main.ts'))
36+
.toBe('/src/main.ts')
37+
})
38+
3139
it('uses internal ids for per-module CSS', async () => {
3240
const server = await createServer('per-module')
3341

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo