diff --git a/packages/vite-node/src/client.ts b/packages/vite-node/src/client.ts index 8d144b7b9e8a..29cb4288a5ab 100644 --- a/packages/vite-node/src/client.ts +++ b/packages/vite-node/src/client.ts @@ -3,7 +3,7 @@ import { fileURLToPath, pathToFileURL } from 'url' import vm from 'vm' import { dirname, extname, isAbsolute, resolve } from 'pathe' import { isNodeBuiltin } from 'mlly' -import { isPrimitive, normalizeId, slash, toFilePath } from './utils' +import { isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils' import type { ModuleCache, ViteNodeRunnerOptions } from './types' export const DEFAULT_REQUEST_STUBS = { @@ -25,11 +25,7 @@ export const DEFAULT_REQUEST_STUBS = { export class ModuleCacheMap extends Map { normalizePath(fsPath: string) { - return fsPath - .replace(/\\/g, '/') - .replace(/^\/@fs\//, '/') - .replace(/^file:\//, '/') - .replace(/^\/+/, '/') + return normalizeModuleId(fsPath) } set(fsPath: string, mod: Partial) { @@ -74,8 +70,9 @@ export class ViteNodeRunner { return await this.cachedRequest(id, []) } + /** @internal */ async cachedRequest(rawId: string, callstack: string[]) { - const id = normalizeId(rawId, this.options.base) + const id = normalizeRequestId(rawId, this.options.base) const fsPath = toFilePath(id, this.root) if (this.moduleCache.get(fsPath)?.promise) @@ -87,8 +84,9 @@ export class ViteNodeRunner { return await promise } + /** @internal */ async directRequest(id: string, fsPath: string, callstack: string[]) { - callstack = [...callstack, id] + callstack = [...callstack, normalizeModuleId(id)] const request = async(dep: string) => { // probably means it was passed as variable // and wasn't transformed by Vite @@ -97,10 +95,11 @@ export class ViteNodeRunner { dep = resolvedDep?.id?.replace(this.root, '') || dep } - if (callstack.includes(dep)) { - if (!this.moduleCache.get(dep)?.exports) - throw new Error(`[vite-node] Circular dependency detected\nStack:\n${[...callstack, dep].reverse().map(p => `- ${p}`).join('\n')}`) - return this.moduleCache.get(dep)!.exports + if (callstack.includes(normalizeModuleId(dep))) { + const depExports = this.moduleCache.get(dep)?.exports + if (depExports) + return depExports + throw new Error(`[vite-node] Circular dependency detected\nStack:\n${[...callstack, dep].reverse().map(p => `- ${p}`).join('\n')}`) } return this.cachedRequest(dep, callstack) } diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index 0c11a66a02e8..893ffcb1f3d4 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -8,7 +8,7 @@ export function slash(str: string) { return str.replace(/\\/g, '/') } -export function normalizeId(id: string, base?: string): string { +export function normalizeRequestId(id: string, base?: string): string { if (base && id.startsWith(base)) id = `/${id.slice(base.length)}` @@ -25,6 +25,14 @@ export function normalizeId(id: string, base?: string): string { .replace(/\?+$/, '') // remove end query mark } +export function normalizeModuleId(id: string) { + return id + .replace(/\\/g, '/') + .replace(/^\/@fs\//, '/') + .replace(/^file:\//, '/') + .replace(/^\/+/, '/') +} + export function isPrimitive(v: any) { return v !== Object(v) }