@@ -28,16 +28,28 @@ export const augmentWorkspace = (
2828) => {
2929 const all = compilerOptions ? [ ...pluginSourceMaps , tsconfigSourceMap ( dir , compilerOptions ) ] : pluginSourceMaps ;
3030 if ( all . length === 0 ) return ;
31- workspace . sourceMaps = all . sort ( ( a , b ) => b . outDir . length - a . outDir . length ) ;
31+ const seen = new Set < string > ( ) ;
32+ const unique : SourceMap [ ] = [ ] ;
33+ for ( const sm of all ) {
34+ const key = `${ sm . srcDir } \0${ sm . outDir } ` ;
35+ if ( seen . has ( key ) ) continue ;
36+ seen . add ( key ) ;
37+ unique . push ( sm ) ;
38+ }
39+ workspace . sourceMaps = unique . sort ( ( a , b ) => b . outDir . length - a . outDir . length ) ;
3240} ;
3341
3442const isUnderOutDir = ( absPath : string , outDir : string ) => absPath === outDir || absPath . startsWith ( `${ outDir } /` ) ;
3543
44+ const rewriteOne = ( pair : SourceMap , absSpecifier : string , extensions : string ) => {
45+ if ( pair . srcDir === pair . outDir || ! isUnderOutDir ( absSpecifier , pair . outDir ) ) return ;
46+ return pair . srcDir + absSpecifier . slice ( pair . outDir . length ) . replace ( matchExt , extensions ) ;
47+ } ;
48+
3649const rewritePattern = ( sourceMaps : SourceMap [ ] , absSpecifier : string , extensions : string ) => {
37- for ( const { srcDir, outDir } of sourceMaps ) {
38- if ( srcDir !== outDir && isUnderOutDir ( absSpecifier , outDir ) ) {
39- return srcDir + absSpecifier . slice ( outDir . length ) . replace ( matchExt , extensions ) ;
40- }
50+ for ( const sm of sourceMaps ) {
51+ const r = rewriteOne ( sm , absSpecifier , extensions ) ;
52+ if ( r ) return r ;
4153 }
4254} ;
4355
@@ -54,25 +66,27 @@ export const getWorkspaceManifestHandler = (chief: ConfigurationChief): Workspac
5466} ;
5567
5668export const getModuleSourcePathHandler = ( chief : ConfigurationChief ) => {
57- const toSourceMapCache = new Map < string , string > ( ) ;
69+ const toSourceMapCache = new Map < string , string | undefined > ( ) ;
5870
5971 return ( filePath : string ) => {
6072 if ( ! isInternal ( filePath ) || hasTSExt . test ( filePath ) ) return ;
6173 if ( toSourceMapCache . has ( filePath ) ) return toSourceMapCache . get ( filePath ) ;
6274 const workspace = chief . findWorkspaceByFilePath ( filePath ) ;
63- if ( ! workspace ?. sourceMaps ) return ;
64- for ( const { srcDir, outDir } of workspace . sourceMaps ) {
65- if ( ! ( isUnderOutDir ( filePath , outDir ) || srcDir === outDir ) ) continue ;
66- const basePath = ( srcDir + filePath . slice ( outDir . length ) ) . replace ( matchExt , '' ) ;
67- const srcFilePath = findFileWithExtensions ( basePath , sourceExtensions ) ;
68- if ( srcFilePath ) {
69- toSourceMapCache . set ( filePath , srcFilePath ) ;
70- if ( srcFilePath !== filePath ) {
75+ let result : string | undefined ;
76+ if ( workspace ?. sourceMaps ) {
77+ for ( const { srcDir, outDir } of workspace . sourceMaps ) {
78+ if ( ! ( isUnderOutDir ( filePath , outDir ) || srcDir === outDir ) ) continue ;
79+ const basePath = ( srcDir + filePath . slice ( outDir . length ) ) . replace ( matchExt , '' ) ;
80+ const srcFilePath = findFileWithExtensions ( basePath , sourceExtensions ) ;
81+ if ( srcFilePath && srcFilePath !== filePath ) {
7182 debugLog ( '*' , `Source mapping ${ toRelative ( filePath , chief . cwd ) } → ${ toRelative ( srcFilePath , chief . cwd ) } ` ) ;
72- return srcFilePath ;
83+ result = srcFilePath ;
84+ break ;
7385 }
7486 }
7587 }
88+ toSourceMapCache . set ( filePath , result ) ;
89+ return result ;
7690 } ;
7791} ;
7892
@@ -101,10 +115,10 @@ export const toSourceMappedSpecifiers = (
101115 extensions = defaultExtensions
102116) => {
103117 const out : string [ ] = [ ] ;
104- if ( ! ws ?. sourceMaps ) return out ;
105- for ( const { srcDir , outDir } of ws . sourceMaps ) {
106- if ( srcDir !== outDir && isUnderOutDir ( absSpecifier , outDir ) ) {
107- out . push ( srcDir + absSpecifier . slice ( outDir . length ) . replace ( matchExt , extensions ) ) ;
118+ if ( ws ?. sourceMaps ) {
119+ for ( const sm of ws . sourceMaps ) {
120+ const r = rewriteOne ( sm , absSpecifier , extensions ) ;
121+ if ( r ) out . push ( r ) ;
108122 }
109123 }
110124 return out ;
0 commit comments