1
1
import type { TransformResult } from 'vite'
2
2
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
3
3
import { install } from './source-map-handler'
4
+ import { toFilePath } from './utils'
4
5
5
6
interface InstallSourceMapSupportOptions {
6
7
getSourceMap : ( source : string ) => EncodedSourceMap | null | undefined
@@ -13,13 +14,24 @@ const VITE_NODE_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-node'
13
14
const VITE_NODE_SOURCEMAPPING_URL = `${ SOURCEMAPPING_URL } =data:application/json;charset=utf-8`
14
15
const VITE_NODE_SOURCEMAPPING_REGEXP = new RegExp ( `//# ${ VITE_NODE_SOURCEMAPPING_URL } ;base64,(.+)` )
15
16
16
- export function withInlineSourcemap ( result : TransformResult ) {
17
+ export function withInlineSourcemap ( result : TransformResult , options : {
18
+ root : string // project root path of this resource
19
+ } ) {
17
20
const map = result . map
18
21
let code = result . code
19
22
20
23
if ( ! map || code . includes ( VITE_NODE_SOURCEMAPPING_SOURCE ) )
21
24
return result
22
25
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
+
23
35
// to reduce the payload size, we only inline vite node source map, because it's also the only one we use
24
36
const OTHER_SOURCE_MAP_REGEXP = new RegExp ( `//# ${ SOURCEMAPPING_URL } =data:application/json[^,]+base64,(.+)` , 'g' )
25
37
while ( OTHER_SOURCE_MAP_REGEXP . test ( code ) )
0 commit comments