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

fix: improve "isInternalRequest" check #2541

Merged
merged 1 commit into from Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 31 additions & 28 deletions packages/vite-node/src/client.ts
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
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
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()
})