1+ import type { SourceMap } from '../types/config.ts' ;
12import type { CompilerOptions } from '../types/project.ts' ;
23import type { ConfigurationChief , Workspace } from '../ConfigurationChief.ts' ;
34import { DEFAULT_EXTENSIONS } from '../constants.ts' ;
@@ -12,11 +13,34 @@ const matchExt = /(\.d)?\.(m|c)?(j|t)s$/;
1213
1314const sourceExtensions = [ ...DEFAULT_EXTENSIONS ] ;
1415
15- export const augmentWorkspace = ( workspace : Workspace , dir : string , compilerOptions : CompilerOptions ) => {
16+ const tsconfigSourceMap = ( dir : string , compilerOptions : CompilerOptions ) : SourceMap => {
1617 const srcDir = join ( dir , 'src' ) ;
1718 const outDirHasSrc = compilerOptions . outDir && isDirectory ( compilerOptions . outDir , 'src' ) ;
18- workspace . srcDir = compilerOptions . rootDir ?? ( outDirHasSrc ? dir : isDirectory ( srcDir ) ? srcDir : dir ) ;
19- workspace . outDir = compilerOptions . outDir || workspace . srcDir ;
19+ const resolvedSrc = compilerOptions . rootDir ?? ( outDirHasSrc ? dir : isDirectory ( srcDir ) ? srcDir : dir ) ;
20+ return { srcDir : resolvedSrc , outDir : compilerOptions . outDir || resolvedSrc } ;
21+ } ;
22+
23+ export const augmentWorkspace = (
24+ workspace : Workspace ,
25+ dir : string ,
26+ compilerOptions : CompilerOptions | undefined ,
27+ pluginSourceMaps : SourceMap [ ] = [ ]
28+ ) => {
29+ const all = compilerOptions ? [ ...pluginSourceMaps , tsconfigSourceMap ( dir , compilerOptions ) ] : pluginSourceMaps ;
30+ if ( all . length === 0 ) return ;
31+ workspace . sourceMaps = all . sort ( ( a , b ) => b . outDir . length - a . outDir . length ) ;
32+ } ;
33+
34+ const isUnderOutDir = ( absPath : string , outDir : string ) => absPath === outDir || absPath . startsWith ( `${ outDir } /` ) ;
35+
36+ const isUnderSrcDir = ( absPath : string , srcDir : string ) => absPath === srcDir || absPath . startsWith ( `${ srcDir } /` ) ;
37+
38+ const rewritePattern = ( sourceMaps : SourceMap [ ] , absSpecifier : string , extensions : string ) => {
39+ for ( const { srcDir, outDir } of sourceMaps ) {
40+ if ( ! isUnderSrcDir ( absSpecifier , srcDir ) && isUnderOutDir ( absSpecifier , outDir ) ) {
41+ return srcDir + absSpecifier . slice ( outDir . length ) . replace ( matchExt , extensions ) ;
42+ }
43+ }
2044} ;
2145
2246export const getModuleSourcePathHandler = ( chief : ConfigurationChief ) => {
@@ -26,16 +50,16 @@ export const getModuleSourcePathHandler = (chief: ConfigurationChief) => {
2650 if ( ! isInternal ( filePath ) || hasTSExt . test ( filePath ) ) return ;
2751 if ( toSourceMapCache . has ( filePath ) ) return toSourceMapCache . get ( filePath ) ;
2852 const workspace = chief . findWorkspaceByFilePath ( filePath ) ;
29- if ( workspace ?. srcDir && workspace . outDir ) {
30- if ( filePath . startsWith ( workspace . outDir ) || workspace . srcDir === workspace . outDir ) {
31- const basePath = filePath . replace ( workspace . outDir , workspace . srcDir ) . replace ( matchExt , '' ) ;
32- const srcFilePath = findFileWithExtensions ( basePath , sourceExtensions ) ;
33- if ( srcFilePath ) {
34- toSourceMapCache . set ( filePath , srcFilePath ) ;
35- if ( srcFilePath !== filePath ) {
36- debugLog ( '*' , `Source mapping ${ toRelative ( filePath , chief . cwd ) } → ${ toRelative ( srcFilePath , chief . cwd ) } ` ) ;
37- return srcFilePath ;
38- }
53+ if ( ! workspace ?. sourceMaps ) return ;
54+ for ( const { srcDir, outDir } of workspace . sourceMaps ) {
55+ if ( ! ( isUnderOutDir ( filePath , outDir ) || srcDir === outDir ) ) continue ;
56+ const basePath = ( srcDir + filePath . slice ( outDir . length ) ) . replace ( matchExt , '' ) ;
57+ const srcFilePath = findFileWithExtensions ( basePath , sourceExtensions ) ;
58+ if ( srcFilePath ) {
59+ toSourceMapCache . set ( filePath , srcFilePath ) ;
60+ if ( srcFilePath !== filePath ) {
61+ debugLog ( '*' , `Source mapping ${ toRelative ( filePath , chief . cwd ) } → ${ toRelative ( srcFilePath , chief . cwd ) } ` ) ;
62+ return srcFilePath ;
3963 }
4064 }
4165 }
@@ -49,12 +73,8 @@ export const getToSourcePathsHandler = (chief: ConfigurationChief) => {
4973 for ( const specifier of specifiers ) {
5074 const absSpecifier = isAbsolute ( specifier ) ? specifier : prependDirToPattern ( dir , specifier ) ;
5175 const ws = chief . findWorkspaceByFilePath ( absSpecifier ) ;
52- if ( ws ?. srcDir && ws . outDir && ! absSpecifier . startsWith ( ws . srcDir ) && absSpecifier . startsWith ( ws . outDir ) ) {
53- const pattern = absSpecifier . replace ( ws . outDir , ws . srcDir ) . replace ( matchExt , extensions ) ;
54- patterns . add ( pattern ) ;
55- } else {
56- patterns . add ( absSpecifier ) ;
57- }
76+ const mapped = ws ?. sourceMaps && rewritePattern ( ws . sourceMaps , absSpecifier , extensions ) ;
77+ patterns . add ( mapped ?? absSpecifier ) ;
5878 }
5979
6080 const filePaths = await _glob ( { patterns : Array . from ( patterns ) , cwd : dir , label } ) ;
@@ -65,4 +85,19 @@ export const getToSourcePathsHandler = (chief: ConfigurationChief) => {
6585 } ;
6686} ;
6787
88+ export const toSourceMappedSpecifiers = (
89+ ws : Workspace | undefined ,
90+ absSpecifier : string ,
91+ extensions = defaultExtensions
92+ ) => {
93+ const out : string [ ] = [ ] ;
94+ if ( ! ws ?. sourceMaps ) return out ;
95+ for ( const { srcDir, outDir } of ws . sourceMaps ) {
96+ if ( ! isUnderSrcDir ( absSpecifier , srcDir ) && isUnderOutDir ( absSpecifier , outDir ) ) {
97+ out . push ( srcDir + absSpecifier . slice ( outDir . length ) . replace ( matchExt , extensions ) ) ;
98+ }
99+ }
100+ return out ;
101+ } ;
102+
68103export type ToSourceFilePath = ReturnType < typeof getModuleSourcePathHandler > ;
0 commit comments