11import fs from 'node:fs'
22import path from 'node:path'
33import { spawn } from 'node:child_process'
4- import type { FullConfig } from '@playwright/test'
4+ import { chromium , type FullConfig } from '@playwright/test'
55import { getTestServerPort } from '@tanstack/router-e2e-utils'
66import packageJson from '../package.json' with { type : 'json' }
77
@@ -77,96 +77,11 @@ const routes = [
7777 '/client-only-jsx' ,
7878]
7979
80- const extraModulePaths = [
81- '/src/routes/leaky-server-import.tsx' ,
82- '/src/violations/leaky-server-import.ts' ,
83- '/src/routes/client-only-violations.tsx' ,
84- '/src/routes/client-only-jsx.tsx' ,
85- '/src/violations/window-size.client.tsx' ,
86- // Index route violation modules: triggers client-env violations for
87- // file-based (edge-a, edge-3, compiler-leak → secret.server) and
88- // marker-based (marked-server-only) denials.
89- '/src/routes/index.tsx' ,
90- '/src/violations/edge-a.ts' ,
91- '/src/violations/edge-1.ts' ,
92- '/src/violations/edge-2.ts' ,
93- '/src/violations/edge-3.ts' ,
94- '/src/violations/compiler-leak.ts' ,
95- '/src/violations/marked-server-only-edge.ts' ,
96- '/src/violations/marked-server-only.ts' ,
97- ]
98-
99- /**
100- * Warmup pass: start a throwaway Vite dev server, hit all routes and module
101- * URLs so that Vite's dep optimization runs and populates the
102- * `node_modules/.vite` cache, then kill the server.
103- *
104- * On a cold start (no `.vite` cache), Vite's dep optimization runs lazily on
105- * first request. During that burst, `resolveId` can fire before the
106- * transform-cache plugin has stored `{code, map}` for importers, which means
107- * violations logged in this window may lack snippets.
108- *
109- * By running the warmup in a separate server instance we ensure the `.vite`
110- * dep cache exists before the real capture pass starts. The second server
111- * boots with a warm dep cache so all transforms are populated before
112- * `resolveId` fires for violation edges.
113- */
114- async function warmupDepOptimization (
115- cwd : string ,
116- port : number ,
117- baseURL : string ,
118- ) : Promise < void > {
119- const child = startDevServer ( cwd , port )
120-
121- // Drain output to /dev/null — we don't need warmup logs.
122- child . stdout ?. resume ( )
123- child . stderr ?. resume ( )
124-
125- try {
126- await waitForHttpOk ( baseURL , 30_000 )
127-
128- // Fetch HTML pages to trigger SSR transforms + dep discovery.
129- for ( const route of routes ) {
130- try {
131- await fetch ( `${ baseURL } ${ route } ` , {
132- signal : AbortSignal . timeout ( 10_000 ) ,
133- } )
134- } catch {
135- // ignore
136- }
137- }
138-
139- // Fetch module URLs to trigger client-env transforms and ensure all
140- // external deps are discovered and optimized.
141- const moduleUrls = [ '/@vite/client' , ...extraModulePaths ] . map ( ( u ) =>
142- u . startsWith ( 'http' ) ? u : `${ baseURL } ${ u } ` ,
143- )
144- for ( const url of moduleUrls ) {
145- try {
146- await fetch ( url , { signal : AbortSignal . timeout ( 10_000 ) } )
147- } catch {
148- // ignore
149- }
150- }
151-
152- // Wait for dep optimization to finish writing the .vite cache.
153- await new Promise ( ( r ) => setTimeout ( r , 2000 ) )
154- } finally {
155- await killChild ( child )
156- }
157- }
158-
15980async function captureDevViolations ( cwd : string ) : Promise < void > {
16081 const port = await getTestServerPort ( `${ packageJson . name } _dev` )
16182 const baseURL = `http://localhost:${ port } `
16283 const logFile = path . resolve ( cwd , 'webserver-dev.log' )
16384
164- // --- Warmup pass ----------------------------------------------------------
165- // Run a throwaway dev server to populate the .vite dep cache. This is a
166- // no-op when the cache already exists (warm start).
167- await warmupDepOptimization ( cwd , port , baseURL )
168-
169- // --- Capture pass (warm dep cache) ----------------------------------------
17085 const out = fs . createWriteStream ( logFile )
17186 const child = startDevServer ( cwd , port )
17287
@@ -176,46 +91,30 @@ async function captureDevViolations(cwd: string): Promise<void> {
17691 try {
17792 await waitForHttpOk ( baseURL , 30_000 )
17893
179- const htmlPages : Array < string > = [ ]
180- for ( const route of routes ) {
181- try {
182- const htmlRes = await fetch ( `${ baseURL } ${ route } ` , {
183- signal : AbortSignal . timeout ( 5000 ) ,
184- } )
185- htmlPages . push ( await htmlRes . text ( ) )
186- } catch {
187- // ignore
94+ // Use a real browser to navigate to every route. This triggers SSR
95+ // (server-env transforms + compiler cross-module resolution) AND client
96+ // module loading (client-env transforms), exactly mirroring real usage.
97+ // Direct HTTP fetches of module URLs do NOT trigger the compiler's
98+ // cross-module resolution path that surfaces certain violations.
99+ const browser = await chromium . launch ( )
100+ try {
101+ const context = await browser . newContext ( )
102+ const page = await context . newPage ( )
103+
104+ for ( const route of routes ) {
105+ try {
106+ await page . goto ( `${ baseURL } ${ route } ` , {
107+ waitUntil : 'networkidle' ,
108+ timeout : 15_000 ,
109+ } )
110+ } catch {
111+ // ignore navigation errors — we only care about server logs
112+ }
188113 }
189- }
190-
191- const scriptSrcs = htmlPages
192- . flatMap ( ( html ) =>
193- Array . from (
194- html . matchAll ( / < s c r i p t [ ^ > ] * t y p e = " m o d u l e " [ ^ > ] * s r c = " ( [ ^ " ] + ) " / g) ,
195- ) . map ( ( m ) => m [ 1 ] ) ,
196- )
197- . filter ( Boolean )
198- . slice ( 0 , 10 )
199-
200- // Trigger module transforms by fetching module scripts referenced from
201- // HTML, plus known module paths to ensure code-split routes are
202- // transformed. Fetching these via HTTP triggers Vite's client-environment
203- // transform pipeline, which fires resolveId for each import and surfaces
204- // violations.
205- const moduleUrls = Array . from (
206- new Set ( [ '/@vite/client' , ...scriptSrcs , ...extraModulePaths ] ) ,
207- )
208- . map ( ( u ) => ( u . startsWith ( 'http' ) ? u : `${ baseURL } ${ u } ` ) )
209- . slice ( 0 , 30 )
210114
211- for ( const url of moduleUrls ) {
212- try {
213- await fetch ( url , {
214- signal : AbortSignal . timeout ( 10_000 ) ,
215- } )
216- } catch {
217- // ignore
218- }
115+ await context . close ( )
116+ } finally {
117+ await browser . close ( )
219118 }
220119
221120 // Give the server a moment to flush logs.
0 commit comments