1- import { existsSync , readdirSync } from 'node:fs'
1+ import { existsSync , readFileSync , readdirSync } from 'node:fs'
22import { builtinModules } from 'node:module'
33import { basename , dirname , extname , isAbsolute , join , resolve } from 'pathe'
44import type { PartialResolvedId } from 'rollup'
55import type { ResolvedConfig } from 'vitest'
6+ import type { ResolvedConfig as ViteConfig } from 'vite'
67import type { WorkspaceProject } from 'vitest/node'
78
89export async function resolveMock (
@@ -14,7 +15,9 @@ export async function resolveMock(
1415 const { id, fsPath, external } = await resolveId ( project , rawId , importer )
1516
1617 if ( hasFactory ) {
17- return { type : 'factory' as const , resolvedId : id }
18+ const needsInteropMap = viteDepsInteropMap ( project . browser ! . vite . config )
19+ const needsInterop = needsInteropMap ?. get ( fsPath ) ?? false
20+ return { type : 'factory' as const , resolvedId : id , needsInterop }
1821 }
1922
2023 const mockPath = resolveMockPath ( project . config . root , fsPath , external )
@@ -126,3 +129,29 @@ const postfixRE = /[?#].*$/
126129export function cleanUrl ( url : string ) : string {
127130 return url . replace ( postfixRE , '' )
128131}
132+
133+ const metadata = new WeakMap < ViteConfig , Map < string , boolean > > ( )
134+
135+ function viteDepsInteropMap ( config : ViteConfig ) {
136+ if ( metadata . has ( config ) ) {
137+ return metadata . get ( config ) !
138+ }
139+ const cacheDirPath = getDepsCacheDir ( config )
140+ const metadataPath = resolve ( cacheDirPath , '_metadata.json' )
141+ if ( ! existsSync ( metadataPath ) ) {
142+ return null
143+ }
144+ const { optimized } = JSON . parse ( readFileSync ( metadataPath , 'utf-8' ) )
145+ const needsInteropMap = new Map ( )
146+ for ( const name in optimized ) {
147+ const dep = optimized [ name ]
148+ const file = resolve ( cacheDirPath , dep . file )
149+ needsInteropMap . set ( file , dep . needsInterop )
150+ }
151+ metadata . set ( config , needsInteropMap )
152+ return needsInteropMap
153+ }
154+
155+ function getDepsCacheDir ( config : ViteConfig ) : string {
156+ return resolve ( config . cacheDir , 'deps' )
157+ }
0 commit comments