@@ -49,20 +49,22 @@ namespace ts {
49
49
const jsFilePath = options . outFile || options . out ;
50
50
const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
51
51
const declarationFilePath = ( forceDtsPaths || options . declaration ) ? removeFileExtension ( jsFilePath ) + Extension . Dts : undefined ;
52
- return { jsFilePath, sourceMapFilePath, declarationFilePath } ;
52
+ const declarationMapPath = getAreDeclarationMapsEnabled ( options ) ? declarationFilePath + ".map" : undefined ;
53
+ return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } ;
53
54
}
54
55
else {
55
56
const jsFilePath = getOwnEmitOutputFilePath ( sourceFile , host , getOutputExtension ( sourceFile , options ) ) ;
56
57
const sourceMapFilePath = getSourceMapFilePath ( jsFilePath , options ) ;
57
58
// For legacy reasons (ie, we have baselines capturing the behavior), js files don't report a .d.ts output path - this would only matter if `declaration` and `allowJs` were both on, which is currently an error
58
59
const isJs = isSourceFileJavaScript ( sourceFile ) ;
59
60
const declarationFilePath = ( ( forceDtsPaths || options . declaration ) && ! isJs ) ? getDeclarationEmitOutputFilePath ( sourceFile , host ) : undefined ;
60
- return { jsFilePath, sourceMapFilePath, declarationFilePath } ;
61
+ const declarationMapPath = getAreDeclarationMapsEnabled ( options ) ? declarationFilePath + ".map" : undefined ;
62
+ return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } ;
61
63
}
62
64
}
63
65
64
66
function getSourceMapFilePath ( jsFilePath : string , options : CompilerOptions ) {
65
- return options . sourceMap ? jsFilePath + ".map" : undefined ;
67
+ return ( options . sourceMap && ! options . inlineSourceMap ) ? jsFilePath + ".map" : undefined ;
66
68
}
67
69
68
70
// JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
@@ -88,12 +90,19 @@ namespace ts {
88
90
export function emitFiles ( resolver : EmitResolver , host : EmitHost , targetSourceFile : SourceFile , emitOnlyDtsFiles ?: boolean , transformers ?: TransformerFactory < SourceFile > [ ] ) : EmitResult {
89
91
const compilerOptions = host . getCompilerOptions ( ) ;
90
92
const moduleKind = getEmitModuleKind ( compilerOptions ) ;
91
- const sourceMapDataList : SourceMapData [ ] = compilerOptions . sourceMap || compilerOptions . inlineSourceMap ? [ ] : undefined ;
93
+ const sourceMapDataList : SourceMapData [ ] = ( compilerOptions . sourceMap || compilerOptions . inlineSourceMap || getAreDeclarationMapsEnabled ( compilerOptions ) ) ? [ ] : undefined ;
92
94
const emittedFilesList : string [ ] = compilerOptions . listEmittedFiles ? [ ] : undefined ;
93
95
const emitterDiagnostics = createDiagnosticCollection ( ) ;
94
96
const newLine = host . getNewLine ( ) ;
95
97
const writer = createTextWriter ( newLine ) ;
96
98
const sourceMap = createSourceMapWriter ( host , writer ) ;
99
+ const declarationSourceMap = createSourceMapWriter ( host , writer , {
100
+ sourceMap : compilerOptions . declarationMap ,
101
+ sourceRoot : compilerOptions . sourceRoot ,
102
+ mapRoot : compilerOptions . mapRoot ,
103
+ extendedDiagnostics : compilerOptions . extendedDiagnostics ,
104
+ // Explicitly do not passthru either `inline` option
105
+ } ) ;
97
106
98
107
let currentSourceFile : SourceFile ;
99
108
let bundledHelpers : Map < boolean > ;
@@ -113,9 +122,9 @@ namespace ts {
113
122
sourceMaps : sourceMapDataList
114
123
} ;
115
124
116
- function emitSourceFileOrBundle ( { jsFilePath, sourceMapFilePath, declarationFilePath } : EmitFileNames , sourceFileOrBundle : SourceFile | Bundle ) {
125
+ function emitSourceFileOrBundle ( { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } : EmitFileNames , sourceFileOrBundle : SourceFile | Bundle ) {
117
126
emitJsFileOrBundle ( sourceFileOrBundle , jsFilePath , sourceMapFilePath ) ;
118
- emitDeclarationFileOrBundle ( sourceFileOrBundle , declarationFilePath ) ;
127
+ emitDeclarationFileOrBundle ( sourceFileOrBundle , declarationFilePath , declarationMapPath ) ;
119
128
120
129
if ( ! emitSkipped && emittedFilesList ) {
121
130
if ( ! emitOnlyDtsFiles ) {
@@ -162,13 +171,13 @@ namespace ts {
162
171
onSetSourceFile : setSourceFile ,
163
172
} ) ;
164
173
165
- printSourceFileOrBundle ( jsFilePath , sourceMapFilePath , isSourceFile ( sourceFileOrBundle ) ? transform . transformed [ 0 ] : createBundle ( transform . transformed ) , printer ) ;
174
+ printSourceFileOrBundle ( jsFilePath , sourceMapFilePath , isSourceFile ( sourceFileOrBundle ) ? transform . transformed [ 0 ] : createBundle ( transform . transformed ) , printer , sourceMap ) ;
166
175
167
176
// Clean up emit nodes on parse tree
168
177
transform . dispose ( ) ;
169
178
}
170
179
171
- function emitDeclarationFileOrBundle ( sourceFileOrBundle : SourceFile | Bundle , declarationFilePath : string | undefined ) {
180
+ function emitDeclarationFileOrBundle ( sourceFileOrBundle : SourceFile | Bundle , declarationFilePath : string | undefined , declarationMapPath : string | undefined ) {
172
181
if ( ! ( declarationFilePath && ! isInJavaScriptFile ( sourceFileOrBundle ) ) ) {
173
182
return ;
174
183
}
@@ -186,25 +195,29 @@ namespace ts {
186
195
// resolver hooks
187
196
hasGlobalName : resolver . hasGlobalName ,
188
197
198
+ // sourcemap hooks
199
+ onEmitSourceMapOfNode : declarationSourceMap . emitNodeWithSourceMap ,
200
+ onEmitSourceMapOfToken : declarationSourceMap . emitTokenWithSourceMap ,
201
+ onEmitSourceMapOfPosition : declarationSourceMap . emitPos ,
202
+ onSetSourceFile : setSourceFileForDeclarationSourceMaps ,
203
+
189
204
// transform hooks
190
205
onEmitNode : declarationTransform . emitNodeWithNotification ,
191
206
substituteNode : declarationTransform . substituteNode ,
192
207
} ) ;
193
208
const declBlocked = ( ! ! declarationTransform . diagnostics && ! ! declarationTransform . diagnostics . length ) || ! ! host . isEmitBlocked ( declarationFilePath ) || ! ! compilerOptions . noEmit ;
194
209
emitSkipped = emitSkipped || declBlocked ;
195
210
if ( ! declBlocked || emitOnlyDtsFiles ) {
196
- const previousState = sourceMap . setState ( /*disabled*/ true ) ;
197
- printSourceFileOrBundle ( declarationFilePath , /*sourceMapFilePath*/ undefined , declarationTransform . transformed [ 0 ] , declarationPrinter , /*shouldSkipSourcemap*/ true ) ;
198
- sourceMap . setState ( previousState ) ;
211
+ printSourceFileOrBundle ( declarationFilePath , declarationMapPath , declarationTransform . transformed [ 0 ] , declarationPrinter , declarationSourceMap ) ;
199
212
}
200
213
declarationTransform . dispose ( ) ;
201
214
}
202
215
203
- function printSourceFileOrBundle ( jsFilePath : string , sourceMapFilePath : string , sourceFileOrBundle : SourceFile | Bundle , printer : Printer , shouldSkipSourcemap = false ) {
216
+ function printSourceFileOrBundle ( jsFilePath : string , sourceMapFilePath : string | undefined , sourceFileOrBundle : SourceFile | Bundle , printer : Printer , mapRecorder : SourceMapWriter ) {
204
217
const bundle = sourceFileOrBundle . kind === SyntaxKind . Bundle ? sourceFileOrBundle : undefined ;
205
218
const sourceFile = sourceFileOrBundle . kind === SyntaxKind . SourceFile ? sourceFileOrBundle : undefined ;
206
219
const sourceFiles = bundle ? bundle . sourceFiles : [ sourceFile ] ;
207
- sourceMap . initialize ( jsFilePath , sourceMapFilePath , sourceFileOrBundle ) ;
220
+ mapRecorder . initialize ( jsFilePath , sourceMapFilePath || "" , sourceFileOrBundle , sourceMapDataList ) ;
208
221
209
222
if ( bundle ) {
210
223
bundledHelpers = createMap < boolean > ( ) ;
@@ -218,26 +231,21 @@ namespace ts {
218
231
219
232
writer . writeLine ( ) ;
220
233
221
- const sourceMappingURL = sourceMap . getSourceMappingURL ( ) ;
222
- if ( ! shouldSkipSourcemap && sourceMappingURL ) {
234
+ const sourceMappingURL = mapRecorder . getSourceMappingURL ( ) ;
235
+ if ( sourceMappingURL ) {
223
236
writer . write ( `//# ${ "sourceMappingURL" } =${ sourceMappingURL } ` ) ; // Sometimes tools can sometimes see this line as a source mapping url comment
224
237
}
225
238
226
239
// Write the source map
227
- if ( ! shouldSkipSourcemap && compilerOptions . sourceMap && ! compilerOptions . inlineSourceMap ) {
228
- writeFile ( host , emitterDiagnostics , sourceMapFilePath , sourceMap . getText ( ) , /*writeByteOrderMark*/ false , sourceFiles ) ;
229
- }
230
-
231
- // Record source map data for the test harness.
232
- if ( ! shouldSkipSourcemap && sourceMapDataList ) {
233
- sourceMapDataList . push ( sourceMap . getSourceMapData ( ) ) ;
240
+ if ( sourceMapFilePath ) {
241
+ writeFile ( host , emitterDiagnostics , sourceMapFilePath , mapRecorder . getText ( ) , /*writeByteOrderMark*/ false , sourceFiles ) ;
234
242
}
235
243
236
244
// Write the output file
237
245
writeFile ( host , emitterDiagnostics , jsFilePath , writer . getText ( ) , compilerOptions . emitBOM , sourceFiles ) ;
238
246
239
247
// Reset state
240
- sourceMap . reset ( ) ;
248
+ mapRecorder . reset ( ) ;
241
249
writer . clear ( ) ;
242
250
243
251
currentSourceFile = undefined ;
@@ -250,6 +258,11 @@ namespace ts {
250
258
sourceMap . setSourceFile ( node ) ;
251
259
}
252
260
261
+ function setSourceFileForDeclarationSourceMaps ( node : SourceFile ) {
262
+ currentSourceFile = node ;
263
+ declarationSourceMap . setSourceFile ( node ) ;
264
+ }
265
+
253
266
function emitHelpers ( node : Node , writeLines : ( text : string ) => void ) {
254
267
let helpersEmitted = false ;
255
268
const bundle = node . kind === SyntaxKind . Bundle ? < Bundle > node : undefined ;
0 commit comments