diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index 73e3c54b7..8efaf3d5d 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -53,7 +53,7 @@ export function guessCJSversion(id: string): string | undefined { } // The code from https://github.com/unjs/mlly/blob/c5bcca0cda175921344fd6de1bc0c499e73e5dac/src/syntax.ts#L51-L98 -async function isValidNodeImport(id: string, code?: string) { +async function isValidNodeImport(id: string) { const extension = extname(id) if (BUILTIN_EXTENSIONS.has(extension)) @@ -72,8 +72,7 @@ async function isValidNodeImport(id: string, code?: string) { if (package_.type === 'module') return true - if (typeof code === 'undefined') - code = await fsp.readFile(id.replace('file:///', ''), 'utf8').catch(() => '') + const code = await fsp.readFile(id.replace('file:///', ''), 'utf8').catch(() => '') return !ESM_SYNTAX_RE.test(code) } @@ -81,18 +80,18 @@ async function isValidNodeImport(id: string, code?: string) { const _defaultExternalizeCache = new Map>() export async function shouldExternalize( id: string, - code?: string, + processed: boolean, options?: DepsHandlingOptions, cache = _defaultExternalizeCache, ) { if (!cache.has(id)) - cache.set(id, _shouldExternalize(id, code, options)) + cache.set(id, _shouldExternalize(id, processed, options)) return cache.get(id)! } async function _shouldExternalize( id: string, - code?: string, + processed: boolean, options?: DepsHandlingOptions, ): Promise { if (isNodeBuiltin(id)) @@ -125,7 +124,7 @@ async function _shouldExternalize( if (matchExternalizePattern(id, moduleDirectories, depsExternal)) return id - if (isLibraryModule && await isValidNodeImport(id, code)) + if (isLibraryModule && (processed || await isValidNodeImport(id))) return id return false diff --git a/packages/vite-node/src/server.ts b/packages/vite-node/src/server.ts index 40f5c4b2d..d60938ccb 100644 --- a/packages/vite-node/src/server.ts +++ b/packages/vite-node/src/server.ts @@ -98,7 +98,7 @@ export class ViteNodeServer { } shouldExternalize(id: string) { - return shouldExternalize(id, this.fetchCache.get(id)?.result.code, this.options.deps, this.externalizeCache) + return shouldExternalize(id, this.fetchCache.has(id), this.options.deps, this.externalizeCache) } private async ensureExists(id: string): Promise {