Skip to content

Commit 2bb9a39

Browse files
authored
fix: improve "isInternaRequest" check (#2541)
1 parent 44581e2 commit 2bb9a39

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

packages/vite-node/src/client.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,38 @@ import { extractSourceMap } from './source-map'
1313
const debugExecute = createDebug('vite-node:client:execute')
1414
const debugNative = createDebug('vite-node:client:native')
1515

16-
export const DEFAULT_REQUEST_STUBS = {
17-
'/@vite/client': {
18-
injectQuery: (id: string) => id,
19-
createHotContext() {
20-
return {
21-
accept: () => {},
22-
prune: () => {},
23-
dispose: () => {},
24-
decline: () => {},
25-
invalidate: () => {},
26-
on: () => {},
27-
}
28-
},
29-
updateStyle(id: string, css: string) {
30-
if (typeof document === 'undefined')
31-
return
32-
33-
const element = document.getElementById(id)
34-
if (element)
35-
element.remove()
36-
37-
const head = document.querySelector('head')
38-
const style = document.createElement('style')
39-
style.setAttribute('type', 'text/css')
40-
style.id = id
41-
style.innerHTML = css
42-
head?.appendChild(style)
43-
},
16+
const clientStub = {
17+
injectQuery: (id: string) => id,
18+
createHotContext() {
19+
return {
20+
accept: () => {},
21+
prune: () => {},
22+
dispose: () => {},
23+
decline: () => {},
24+
invalidate: () => {},
25+
on: () => {},
26+
}
4427
},
28+
updateStyle(id: string, css: string) {
29+
if (typeof document === 'undefined')
30+
return
31+
32+
const element = document.getElementById(id)
33+
if (element)
34+
element.remove()
35+
36+
const head = document.querySelector('head')
37+
const style = document.createElement('style')
38+
style.setAttribute('type', 'text/css')
39+
style.id = id
40+
style.innerHTML = css
41+
head?.appendChild(style)
42+
},
43+
}
44+
45+
export const DEFAULT_REQUEST_STUBS = {
46+
'/@vite/client': clientStub,
47+
'@vite/client': clientStub,
4548
}
4649

4750
export class ModuleCacheMap extends Map<string, ModuleCache> {

packages/vite-node/src/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ export const hashRE = /#.*$/s
4141
export const cleanUrl = (url: string): string =>
4242
url.replace(hashRE, '').replace(queryRE, '')
4343

44+
const internalRequests = [
45+
'@vite/client',
46+
'@vite/env',
47+
]
48+
49+
const internalRequestRegexp = new RegExp(`^/?(${internalRequests.join('|')})$`)
50+
4451
export const isInternalRequest = (id: string): boolean => {
45-
return id.startsWith('/@vite/')
52+
return internalRequestRegexp.test(id)
4653
}
4754

4855
export function normalizeModuleId(id: string) {

test/core/test/imports.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,9 @@ test('dynamic import throws an error', async () => {
6161
// @ts-expect-error path does not exist
6262
await expect(() => import('./some-unknown-path')).rejects.toThrowError(/Failed to load/)
6363
})
64+
65+
test('can import @vite/client', async () => {
66+
const name = '@vite/client'
67+
await expect(import(name)).resolves.not.toThrow()
68+
await expect(import(`/${name}`)).resolves.not.toThrow()
69+
})

0 commit comments

Comments
 (0)