@@ -1117,17 +1117,15 @@ async function patchVendorPaths() {
11171117async function patchVitestCoreResolver ( ) {
11181118 console . log ( '\nPatching VitestCoreResolver for CLI package alias...' ) ;
11191119
1120- const cliApiChunks = await fsGlob ( join ( distDir , 'chunks/cli-api.*.js' ) ) ;
1121- const cliApiChunkArr : string [ ] = [ ] ;
1122- for await ( const chunk of cliApiChunks ) {
1123- cliApiChunkArr . push ( chunk ) ;
1120+ let cliApiChunk : string | undefined ;
1121+ for await ( const chunk of fsGlob ( join ( distDir , 'chunks/cli-api.*.js' ) ) ) {
1122+ cliApiChunk = chunk ;
1123+ break ;
11241124 }
11251125
1126- if ( cliApiChunkArr . length === 0 ) {
1126+ if ( ! cliApiChunk ) {
11271127 throw new Error ( 'cli-api chunk not found' ) ;
11281128 }
1129-
1130- const cliApiChunk = cliApiChunkArr [ 0 ] ;
11311129 let content = await readFile ( cliApiChunk , 'utf8' ) ;
11321130
11331131 // Find the VitestCoreResolver resolveId function and add our package aliases
@@ -1352,20 +1350,36 @@ async function patchVitestBrowserPackage() {
13521350 // Pattern: const exclude = ["vitest", ...
13531351 const excludePattern = / ( c o n s t e x c l u d e = \[ ) ( \n ? \s * " v i t e s t " , ) / ;
13541352 // Exclude packages that:
1355- // - @vitest/browser: needs our resolveId plugin
1356- // - vite: Node.js only
1357- // - @voidzero-dev/vite-plus-core: our Node.js core package
1358- // - @voidzero-dev/vite-plus-core/module-runner: pulled by index.js -> evaluatedModules
1359- // - lightningcss: has native bindings
1360- // - @tailwindcss/oxide: has native bindings
1361- // - tailwindcss: pulls in @tailwindcss/oxide
1362- // Also exclude @vitest /ui (optional peer dependency) and its subpath
1363- // Also exclude @vitest /mocker/node which imports @voidzero -dev/vite-plus-core
1364- // Also exclude our package aliases to preserve module identity with init scripts
1365- // This prevents Vite from pre-bundling our browser context, ensuring both init scripts
1366- // (loaded via /@fs/) and tests use the same page singleton
1367- const excludeReplacement =
1368- '$1\n "@vitest/browser",\n "@vitest/ui",\n "@vitest/ui/reporter",\n "@vitest/mocker/node",\n "@voidzero-dev/vite-plus-test",\n "@voidzero-dev/vite-plus-test/browser",\n "@voidzero-dev/vite-plus-test/browser/context",\n "@voidzero-dev/vite-plus/test",\n "@voidzero-dev/vite-plus/test/browser",\n "@voidzero-dev/vite-plus/test/browser/context",\n "vite",\n "@voidzero-dev/vite-plus-core",\n "@voidzero-dev/vite-plus-core/module-runner",\n "lightningcss",\n "@tailwindcss/oxide",\n "tailwindcss",$2' ;
1353+ // Packages to exclude from Vite's dependency pre-bundling (optimizeDeps.exclude)
1354+ const packagesToExclude = [
1355+ // @vitest packages that need our resolveId plugin
1356+ '@vitest/browser' ,
1357+ '@vitest/ui' ,
1358+ '@vitest/ui/reporter' ,
1359+ '@vitest/mocker/node' , // imports @voidzero -dev/vite-plus-core
1360+
1361+ // Our package aliases - preserve module identity with init scripts
1362+ // This ensures both init scripts (loaded via /@fs/) and tests use the same page singleton
1363+ '@voidzero-dev/vite-plus-test' ,
1364+ '@voidzero-dev/vite-plus-test/browser' ,
1365+ '@voidzero-dev/vite-plus-test/browser/context' ,
1366+ '@voidzero-dev/vite-plus/test' ,
1367+ '@voidzero-dev/vite-plus/test/browser' ,
1368+ '@voidzero-dev/vite-plus/test/browser/context' ,
1369+
1370+ // Node.js only packages
1371+ 'vite' ,
1372+ '@voidzero-dev/vite-plus-core' ,
1373+ '@voidzero-dev/vite-plus-core/module-runner' ,
1374+
1375+ // Native bindings
1376+ 'lightningcss' ,
1377+ '@tailwindcss/oxide' ,
1378+ 'tailwindcss' , // pulls in @tailwindcss /oxide
1379+ ] ;
1380+
1381+ const excludeListStr = packagesToExclude . map ( ( pkg ) => `"${ pkg } "` ) . join ( ',\n ' ) ;
1382+ const excludeReplacement = `$1\n ${ excludeListStr } ,$2` ;
13691383 if ( excludePattern . test ( content ) ) {
13701384 content = content . replace ( excludePattern , excludeReplacement ) ;
13711385 console . log ( ' Patched exclude list with native deps' ) ;
0 commit comments