Skip to content

Commit

Permalink
fix: improve "isInternaRequest" check (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Dec 20, 2022
1 parent 44581e2 commit 2bb9a39
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
59 changes: 31 additions & 28 deletions packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,38 @@ import { extractSourceMap } from './source-map'
const debugExecute = createDebug('vite-node:client:execute')
const debugNative = createDebug('vite-node:client:native')

export const DEFAULT_REQUEST_STUBS = {
'/@vite/client': {
injectQuery: (id: string) => id,
createHotContext() {
return {
accept: () => {},
prune: () => {},
dispose: () => {},
decline: () => {},
invalidate: () => {},
on: () => {},
}
},
updateStyle(id: string, css: string) {
if (typeof document === 'undefined')
return

const element = document.getElementById(id)
if (element)
element.remove()

const head = document.querySelector('head')
const style = document.createElement('style')
style.setAttribute('type', 'text/css')
style.id = id
style.innerHTML = css
head?.appendChild(style)
},
const clientStub = {
injectQuery: (id: string) => id,
createHotContext() {
return {
accept: () => {},
prune: () => {},
dispose: () => {},
decline: () => {},
invalidate: () => {},
on: () => {},
}
},
updateStyle(id: string, css: string) {
if (typeof document === 'undefined')
return

const element = document.getElementById(id)
if (element)
element.remove()

const head = document.querySelector('head')
const style = document.createElement('style')
style.setAttribute('type', 'text/css')
style.id = id
style.innerHTML = css
head?.appendChild(style)
},
}

export const DEFAULT_REQUEST_STUBS = {
'/@vite/client': clientStub,
'@vite/client': clientStub,
}

export class ModuleCacheMap extends Map<string, ModuleCache> {
Expand Down
9 changes: 8 additions & 1 deletion packages/vite-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ export const hashRE = /#.*$/s
export const cleanUrl = (url: string): string =>
url.replace(hashRE, '').replace(queryRE, '')

const internalRequests = [
'@vite/client',
'@vite/env',
]

const internalRequestRegexp = new RegExp(`^/?(${internalRequests.join('|')})$`)

export const isInternalRequest = (id: string): boolean => {
return id.startsWith('/@vite/')
return internalRequestRegexp.test(id)
}

export function normalizeModuleId(id: string) {
Expand Down
6 changes: 6 additions & 0 deletions test/core/test/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ test('dynamic import throws an error', async () => {
// @ts-expect-error path does not exist
await expect(() => import('./some-unknown-path')).rejects.toThrowError(/Failed to load/)
})

test('can import @vite/client', async () => {
const name = '@vite/client'
await expect(import(name)).resolves.not.toThrow()
await expect(import(`/${name}`)).resolves.not.toThrow()
})

0 comments on commit 2bb9a39

Please sign in to comment.