@@ -402,6 +402,7 @@ import {
402
402
SourceFilePrologueInfo ,
403
403
SourceMapEmitResult ,
404
404
SourceMapGenerator ,
405
+ SourceMapOptions ,
405
406
SourceMapSource ,
406
407
SpreadAssignment ,
407
408
SpreadElement ,
@@ -721,6 +722,62 @@ export function getOutputFileNames(commandLine: ParsedCommandLine, inputFileName
721
722
return getOutputs ( ) ;
722
723
}
723
724
725
+ /** @internal */
726
+ export function getSourceMapDirectory ( host : EmitHost , mapOptions : SourceMapOptions , filePath : string , sourceFile : SourceFile | undefined ) {
727
+ if ( mapOptions . sourceRoot ) return host . getCommonSourceDirectory ( ) ;
728
+ if ( mapOptions . mapRoot ) {
729
+ let sourceMapDir = normalizeSlashes ( mapOptions . mapRoot ) ;
730
+ if ( sourceFile ) {
731
+ // For modules or multiple emit files the mapRoot will have directory structure like the sources
732
+ // So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
733
+ sourceMapDir = getDirectoryPath ( getSourceFilePathInNewDir ( sourceFile . fileName , host , sourceMapDir ) ) ;
734
+ }
735
+ if ( getRootLength ( sourceMapDir ) === 0 ) {
736
+ // The relative paths are relative to the common directory
737
+ sourceMapDir = combinePaths ( host . getCommonSourceDirectory ( ) , sourceMapDir ) ;
738
+ }
739
+ return sourceMapDir ;
740
+ }
741
+ return getDirectoryPath ( normalizePath ( filePath ) ) ;
742
+ }
743
+
744
+ /** @internal */
745
+ export function getSourceMappingURL ( host : EmitHost , mapOptions : SourceMapOptions , sourceMapGenerator : SourceMapGenerator , filePath : string , sourceMapFilePath : string | undefined , sourceFile : SourceFile | undefined ) {
746
+ if ( mapOptions . inlineSourceMap ) {
747
+ // Encode the sourceMap into the sourceMap url
748
+ const sourceMapText = sourceMapGenerator . toString ( ) ;
749
+ const base64SourceMapText = base64encode ( sys , sourceMapText ) ;
750
+ return `data:application/json;base64,${ base64SourceMapText } ` ;
751
+ }
752
+
753
+ const sourceMapFile = getBaseFileName ( normalizeSlashes ( Debug . checkDefined ( sourceMapFilePath ) ) ) ;
754
+ if ( mapOptions . mapRoot ) {
755
+ let sourceMapDir = normalizeSlashes ( mapOptions . mapRoot ) ;
756
+ if ( sourceFile ) {
757
+ // For modules or multiple emit files the mapRoot will have directory structure like the sources
758
+ // So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
759
+ sourceMapDir = getDirectoryPath ( getSourceFilePathInNewDir ( sourceFile . fileName , host , sourceMapDir ) ) ;
760
+ }
761
+ if ( getRootLength ( sourceMapDir ) === 0 ) {
762
+ // The relative paths are relative to the common directory
763
+ sourceMapDir = combinePaths ( host . getCommonSourceDirectory ( ) , sourceMapDir ) ;
764
+ return encodeURI (
765
+ getRelativePathToDirectoryOrUrl (
766
+ getDirectoryPath ( normalizePath ( filePath ) ) , // get the relative sourceMapDir path based on jsFilePath
767
+ combinePaths ( sourceMapDir , sourceMapFile ) , // this is where user expects to see sourceMap
768
+ host . getCurrentDirectory ( ) ,
769
+ host . getCanonicalFileName ,
770
+ /*isAbsolutePathAnUrl*/ true ,
771
+ ) ,
772
+ ) ;
773
+ }
774
+ else {
775
+ return encodeURI ( combinePaths ( sourceMapDir , sourceMapFile ) ) ;
776
+ }
777
+ }
778
+ return encodeURI ( sourceMapFile ) ;
779
+ }
780
+
724
781
/** @internal */
725
782
export function getFirstProjectOutput ( configFile : ParsedCommandLine , ignoreCase : boolean ) : string {
726
783
if ( outFile ( configFile . options ) ) {
@@ -982,7 +1039,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
982
1039
host ,
983
1040
getBaseFileName ( normalizeSlashes ( jsFilePath ) ) ,
984
1041
getSourceRoot ( mapOptions ) ,
985
- getSourceMapDirectory ( mapOptions , jsFilePath , sourceFile ) ,
1042
+ getSourceMapDirectory ( host , mapOptions , jsFilePath , sourceFile ) ,
986
1043
mapOptions ,
987
1044
) ;
988
1045
}
@@ -1004,6 +1061,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
1004
1061
}
1005
1062
1006
1063
const sourceMappingURL = getSourceMappingURL (
1064
+ host ,
1007
1065
mapOptions ,
1008
1066
sourceMapGenerator ,
1009
1067
jsFilePath ,
@@ -1039,15 +1097,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
1039
1097
writer . clear ( ) ;
1040
1098
}
1041
1099
1042
- interface SourceMapOptions {
1043
- sourceMap ?: boolean ;
1044
- inlineSourceMap ?: boolean ;
1045
- inlineSources ?: boolean ;
1046
- sourceRoot ?: string ;
1047
- mapRoot ?: string ;
1048
- extendedDiagnostics ?: boolean ;
1049
- }
1050
-
1051
1100
function shouldEmitSourceMaps ( mapOptions : SourceMapOptions , sourceFileOrBundle : SourceFile | Bundle ) {
1052
1101
return ( mapOptions . sourceMap || mapOptions . inlineSourceMap )
1053
1102
&& ( sourceFileOrBundle . kind !== SyntaxKind . SourceFile || ! fileExtensionIs ( sourceFileOrBundle . fileName , Extension . Json ) ) ;
@@ -1059,60 +1108,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
1059
1108
const sourceRoot = normalizeSlashes ( mapOptions . sourceRoot || "" ) ;
1060
1109
return sourceRoot ? ensureTrailingDirectorySeparator ( sourceRoot ) : sourceRoot ;
1061
1110
}
1062
-
1063
- function getSourceMapDirectory ( mapOptions : SourceMapOptions , filePath : string , sourceFile : SourceFile | undefined ) {
1064
- if ( mapOptions . sourceRoot ) return host . getCommonSourceDirectory ( ) ;
1065
- if ( mapOptions . mapRoot ) {
1066
- let sourceMapDir = normalizeSlashes ( mapOptions . mapRoot ) ;
1067
- if ( sourceFile ) {
1068
- // For modules or multiple emit files the mapRoot will have directory structure like the sources
1069
- // So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
1070
- sourceMapDir = getDirectoryPath ( getSourceFilePathInNewDir ( sourceFile . fileName , host , sourceMapDir ) ) ;
1071
- }
1072
- if ( getRootLength ( sourceMapDir ) === 0 ) {
1073
- // The relative paths are relative to the common directory
1074
- sourceMapDir = combinePaths ( host . getCommonSourceDirectory ( ) , sourceMapDir ) ;
1075
- }
1076
- return sourceMapDir ;
1077
- }
1078
- return getDirectoryPath ( normalizePath ( filePath ) ) ;
1079
- }
1080
-
1081
- function getSourceMappingURL ( mapOptions : SourceMapOptions , sourceMapGenerator : SourceMapGenerator , filePath : string , sourceMapFilePath : string | undefined , sourceFile : SourceFile | undefined ) {
1082
- if ( mapOptions . inlineSourceMap ) {
1083
- // Encode the sourceMap into the sourceMap url
1084
- const sourceMapText = sourceMapGenerator . toString ( ) ;
1085
- const base64SourceMapText = base64encode ( sys , sourceMapText ) ;
1086
- return `data:application/json;base64,${ base64SourceMapText } ` ;
1087
- }
1088
-
1089
- const sourceMapFile = getBaseFileName ( normalizeSlashes ( Debug . checkDefined ( sourceMapFilePath ) ) ) ;
1090
- if ( mapOptions . mapRoot ) {
1091
- let sourceMapDir = normalizeSlashes ( mapOptions . mapRoot ) ;
1092
- if ( sourceFile ) {
1093
- // For modules or multiple emit files the mapRoot will have directory structure like the sources
1094
- // So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
1095
- sourceMapDir = getDirectoryPath ( getSourceFilePathInNewDir ( sourceFile . fileName , host , sourceMapDir ) ) ;
1096
- }
1097
- if ( getRootLength ( sourceMapDir ) === 0 ) {
1098
- // The relative paths are relative to the common directory
1099
- sourceMapDir = combinePaths ( host . getCommonSourceDirectory ( ) , sourceMapDir ) ;
1100
- return encodeURI (
1101
- getRelativePathToDirectoryOrUrl (
1102
- getDirectoryPath ( normalizePath ( filePath ) ) , // get the relative sourceMapDir path based on jsFilePath
1103
- combinePaths ( sourceMapDir , sourceMapFile ) , // this is where user expects to see sourceMap
1104
- host . getCurrentDirectory ( ) ,
1105
- host . getCanonicalFileName ,
1106
- /*isAbsolutePathAnUrl*/ true ,
1107
- ) ,
1108
- ) ;
1109
- }
1110
- else {
1111
- return encodeURI ( combinePaths ( sourceMapDir , sourceMapFile ) ) ;
1112
- }
1113
- }
1114
- return encodeURI ( sourceMapFile ) ;
1115
- }
1116
1111
}
1117
1112
1118
1113
/** @internal */
0 commit comments