@@ -57,6 +57,7 @@ import {
57
57
ConstructSignatureDeclaration ,
58
58
contains ,
59
59
ContinueStatement ,
60
+ CoreEmitHost ,
60
61
createBinaryExpressionTrampoline ,
61
62
createDiagnosticCollection ,
62
63
createGetCanonicalFileName ,
@@ -402,6 +403,7 @@ import {
402
403
SourceFilePrologueInfo ,
403
404
SourceMapEmitResult ,
404
405
SourceMapGenerator ,
406
+ SourceMapOptions ,
405
407
SourceMapSource ,
406
408
SpreadAssignment ,
407
409
SpreadElement ,
@@ -721,6 +723,62 @@ export function getOutputFileNames(commandLine: ParsedCommandLine, inputFileName
721
723
return getOutputs ( ) ;
722
724
}
723
725
726
+ /** @internal */
727
+ export function getSourceMapDirectory ( host : CoreEmitHost , mapOptions : SourceMapOptions , filePath : string , sourceFile : SourceFile | undefined ) {
728
+ if ( mapOptions . sourceRoot ) return host . getCommonSourceDirectory ( ) ;
729
+ if ( mapOptions . mapRoot ) {
730
+ let sourceMapDir = normalizeSlashes ( mapOptions . mapRoot ) ;
731
+ if ( sourceFile ) {
732
+ // For modules or multiple emit files the mapRoot will have directory structure like the sources
733
+ // 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
734
+ sourceMapDir = getDirectoryPath ( getSourceFilePathInNewDir ( sourceFile . fileName , host , sourceMapDir ) ) ;
735
+ }
736
+ if ( getRootLength ( sourceMapDir ) === 0 ) {
737
+ // The relative paths are relative to the common directory
738
+ sourceMapDir = combinePaths ( host . getCommonSourceDirectory ( ) , sourceMapDir ) ;
739
+ }
740
+ return sourceMapDir ;
741
+ }
742
+ return getDirectoryPath ( normalizePath ( filePath ) ) ;
743
+ }
744
+
745
+ /** @internal */
746
+ export function getSourceMappingURL ( host : CoreEmitHost , mapOptions : SourceMapOptions , sourceMapGenerator : SourceMapGenerator , filePath : string , sourceMapFilePath : string | undefined , sourceFile : SourceFile | undefined ) {
747
+ if ( mapOptions . inlineSourceMap ) {
748
+ // Encode the sourceMap into the sourceMap url
749
+ const sourceMapText = sourceMapGenerator . toString ( ) ;
750
+ const base64SourceMapText = base64encode ( sys , sourceMapText ) ;
751
+ return `data:application/json;base64,${ base64SourceMapText } ` ;
752
+ }
753
+
754
+ const sourceMapFile = getBaseFileName ( normalizeSlashes ( Debug . checkDefined ( sourceMapFilePath ) ) ) ;
755
+ if ( mapOptions . mapRoot ) {
756
+ let sourceMapDir = normalizeSlashes ( mapOptions . mapRoot ) ;
757
+ if ( sourceFile ) {
758
+ // For modules or multiple emit files the mapRoot will have directory structure like the sources
759
+ // 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
760
+ sourceMapDir = getDirectoryPath ( getSourceFilePathInNewDir ( sourceFile . fileName , host , sourceMapDir ) ) ;
761
+ }
762
+ if ( getRootLength ( sourceMapDir ) === 0 ) {
763
+ // The relative paths are relative to the common directory
764
+ sourceMapDir = combinePaths ( host . getCommonSourceDirectory ( ) , sourceMapDir ) ;
765
+ return encodeURI (
766
+ getRelativePathToDirectoryOrUrl (
767
+ getDirectoryPath ( normalizePath ( filePath ) ) , // get the relative sourceMapDir path based on jsFilePath
768
+ combinePaths ( sourceMapDir , sourceMapFile ) , // this is where user expects to see sourceMap
769
+ host . getCurrentDirectory ( ) ,
770
+ host . getCanonicalFileName ,
771
+ /*isAbsolutePathAnUrl*/ true ,
772
+ ) ,
773
+ ) ;
774
+ }
775
+ else {
776
+ return encodeURI ( combinePaths ( sourceMapDir , sourceMapFile ) ) ;
777
+ }
778
+ }
779
+ return encodeURI ( sourceMapFile ) ;
780
+ }
781
+
724
782
/** @internal */
725
783
export function getFirstProjectOutput ( configFile : ParsedCommandLine , ignoreCase : boolean ) : string {
726
784
if ( outFile ( configFile . options ) ) {
@@ -982,7 +1040,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
982
1040
host ,
983
1041
getBaseFileName ( normalizeSlashes ( jsFilePath ) ) ,
984
1042
getSourceRoot ( mapOptions ) ,
985
- getSourceMapDirectory ( mapOptions , jsFilePath , sourceFile ) ,
1043
+ getSourceMapDirectory ( host , mapOptions , jsFilePath , sourceFile ) ,
986
1044
mapOptions ,
987
1045
) ;
988
1046
}
@@ -1004,6 +1062,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
1004
1062
}
1005
1063
1006
1064
const sourceMappingURL = getSourceMappingURL (
1065
+ host ,
1007
1066
mapOptions ,
1008
1067
sourceMapGenerator ,
1009
1068
jsFilePath ,
@@ -1039,15 +1098,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
1039
1098
writer . clear ( ) ;
1040
1099
}
1041
1100
1042
- interface SourceMapOptions {
1043
- sourceMap ?: boolean ;
1044
- inlineSourceMap ?: boolean ;
1045
- inlineSources ?: boolean ;
1046
- sourceRoot ?: string ;
1047
- mapRoot ?: string ;
1048
- extendedDiagnostics ?: boolean ;
1049
- }
1050
-
1051
1101
function shouldEmitSourceMaps ( mapOptions : SourceMapOptions , sourceFileOrBundle : SourceFile | Bundle ) {
1052
1102
return ( mapOptions . sourceMap || mapOptions . inlineSourceMap )
1053
1103
&& ( sourceFileOrBundle . kind !== SyntaxKind . SourceFile || ! fileExtensionIs ( sourceFileOrBundle . fileName , Extension . Json ) ) ;
@@ -1059,60 +1109,6 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
1059
1109
const sourceRoot = normalizeSlashes ( mapOptions . sourceRoot || "" ) ;
1060
1110
return sourceRoot ? ensureTrailingDirectorySeparator ( sourceRoot ) : sourceRoot ;
1061
1111
}
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
1112
}
1117
1113
1118
1114
/** @internal */
0 commit comments