11import fs from 'node:fs'
22import path from 'node:path'
33import { parseErrorStacktrace } from '@vitest/utils/source-map'
4+ import convertSourceMap from 'convert-source-map'
45import c from 'picocolors'
56import type { ForwardConsolePayload } from '#types/customEvent'
67import type { DevEnvironment , Plugin } from '..'
78import { normalizePath } from '..'
9+ import { cleanUrl } from '../../shared/utils'
10+ import type { MemoryFiles } from '../server/bundledDev'
811import { extractSourcemapFromFile } from '../server/sourcemap'
912import { generateCodeFrame } from '../utils'
1013
@@ -19,12 +22,18 @@ export function forwardConsolePlugin(pluginOpts: {
1922 configureServer ( server ) {
2023 for ( const name of pluginOpts . environments ) {
2124 const environment = server . environments [ name ]
25+ const bundledDevOutDir = getBundledDevOutDir ( environment )
2226 environment . hot . on ( 'vite:forward-console' , ( payload ) => {
2327 if (
2428 payload . type === 'error' ||
2529 payload . type === 'unhandled-rejection'
2630 ) {
27- const output = formatError ( payload , environment , sourceMapCache )
31+ const output = formatError (
32+ payload ,
33+ environment ,
34+ sourceMapCache ,
35+ bundledDevOutDir ,
36+ )
2837 environment . config . logger . error ( output , {
2938 timestamp : true ,
3039 } )
@@ -58,10 +67,22 @@ function formatError(
5867 > ,
5968 environment : DevEnvironment ,
6069 sourceMapCache : Map < string , any > ,
70+ bundledDevOutDir : string ,
6171) {
6272 const error = payload . data
73+ const memoryFiles = environment . bundledDev ?. memoryFiles
6374 const stacks = parseErrorStacktrace ( error , {
6475 getUrlId ( id ) {
76+ if ( memoryFiles ) {
77+ return (
78+ getBundledDevChunkPath (
79+ environment ,
80+ memoryFiles ,
81+ bundledDevOutDir ,
82+ id ,
83+ ) ?? id
84+ )
85+ }
6586 const moduleGraph = environment . moduleGraph
6687 const mod = moduleGraph . getModuleById ( id )
6788 if ( mod ) {
@@ -83,6 +104,9 @@ function formatError(
83104 return id
84105 } ,
85106 getSourceMap ( id ) {
107+ if ( memoryFiles ) {
108+ return getBundledDevSourceMap ( memoryFiles , bundledDevOutDir , id )
109+ }
86110 if ( sourceMapCache . has ( id ) ) {
87111 return sourceMapCache . get ( id )
88112 }
@@ -114,6 +138,14 @@ function formatError(
114138 } )
115139
116140 const nearest = stacks . find ( ( stack ) => {
141+ if ( memoryFiles ) {
142+ return (
143+ ! environment . config . dev . sourcemapIgnoreList (
144+ stack . file ,
145+ `${ stack . file } .map` ,
146+ ) && fs . existsSync ( stack . file )
147+ )
148+ }
117149 const modules = environment . moduleGraph . getModulesByFile ( stack . file )
118150 return (
119151 [ ...( modules || [ ] ) ] . some ( ( m ) => m . transformResult ) &&
@@ -142,3 +174,62 @@ function formatError(
142174 }
143175 return output
144176}
177+
178+ // Under bundled dev, the client module graph is empty and the browser runs
179+ // bundle chunks served from `memoryFiles`. Their source maps list the sources
180+ // relative to the output dir, so chunk URLs are turned into paths inside it.
181+ function getBundledDevOutDir ( environment : DevEnvironment ) : string {
182+ return normalizePath (
183+ path . resolve ( environment . config . root , environment . config . build . outDir ) ,
184+ )
185+ }
186+
187+ function getBundledDevChunkPath (
188+ environment : DevEnvironment ,
189+ memoryFiles : MemoryFiles ,
190+ outDir : string ,
191+ url : string ,
192+ ) : string | undefined {
193+ const { decodedBase } = environment . config
194+ url = cleanUrl ( url )
195+ if ( ! url . startsWith ( decodedBase ) ) return
196+ const fileName = url . slice ( decodedBase . length )
197+ if ( memoryFiles . has ( fileName ) ) {
198+ return `${ outDir } /${ fileName } `
199+ }
200+ }
201+
202+ function getBundledDevSourceMap (
203+ memoryFiles : MemoryFiles ,
204+ outDir : string ,
205+ chunkPath : string ,
206+ ) {
207+ if ( ! chunkPath . startsWith ( `${ outDir } /` ) ) return null
208+ const chunkFileName = chunkPath . slice ( outDir . length + 1 )
209+ const readFile = ( fileName : string ) => {
210+ const source = memoryFiles . get ( fileName ) ?. source
211+ return typeof source === 'string'
212+ ? source
213+ : source && Buffer . from ( source ) . toString ( 'utf-8' )
214+ }
215+ const code = readFile ( chunkFileName )
216+ if ( code === undefined ) return null
217+ try {
218+ const converter =
219+ convertSourceMap . fromSource ( code ) ||
220+ convertSourceMap . fromMapFileSource ( code , ( mapFileName : string ) => {
221+ const mapFilePath = path . posix . join (
222+ path . posix . dirname ( chunkFileName ) ,
223+ mapFileName ,
224+ )
225+ const map = readFile ( mapFilePath )
226+ if ( map === undefined ) {
227+ throw new Error ( `${ mapFilePath } is not in memoryFiles` )
228+ }
229+ return map
230+ } )
231+ return converter ?. toObject ( ) ?? null
232+ } catch {
233+ return null
234+ }
235+ }
0 commit comments