11import type { TransformResult } from 'vite'
22import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
33import { install } from './source-map-handler'
4+ import { toFilePath } from './utils'
45
56interface InstallSourceMapSupportOptions {
67 getSourceMap : ( source : string ) => EncodedSourceMap | null | undefined
@@ -13,13 +14,24 @@ const VITE_NODE_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-node'
1314const VITE_NODE_SOURCEMAPPING_URL = `${ SOURCEMAPPING_URL } =data:application/json;charset=utf-8`
1415const VITE_NODE_SOURCEMAPPING_REGEXP = new RegExp ( `//# ${ VITE_NODE_SOURCEMAPPING_URL } ;base64,(.+)` )
1516
16- export function withInlineSourcemap ( result : TransformResult ) {
17+ export function withInlineSourcemap ( result : TransformResult , options : {
18+ root : string // project root path of this resource
19+ } ) {
1720 const map = result . map
1821 let code = result . code
1922
2023 if ( ! map || code . includes ( VITE_NODE_SOURCEMAPPING_SOURCE ) )
2124 return result
2225
26+ // sources path from `ViteDevServer` may be not a valid filesystem path (eg. /src/main.js),
27+ // so we try to convert them to valid filesystem path
28+ map . sources = map . sources . map ( ( source ) => {
29+ if ( ! source )
30+ return source
31+ const { exists, path } = toFilePath ( source , options . root )
32+ return exists ? path : source
33+ } )
34+
2335 // to reduce the payload size, we only inline vite node source map, because it's also the only one we use
2436 const OTHER_SOURCE_MAP_REGEXP = new RegExp ( `//# ${ SOURCEMAPPING_URL } =data:application/json[^,]+base64,(.+)` , 'g' )
2537 while ( OTHER_SOURCE_MAP_REGEXP . test ( code ) )
0 commit comments