Skip to content

Commit 6365eb0

Browse files
authored
Merge pull request #27715 from Microsoft/handleEmitDeclarationOnly
Do not generate jsFile path if its emitOnlyDeclarations is set
2 parents d493c47 + 0d91838 commit 6365eb0

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

src/compiler/emitter.ts

+27-18
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,23 @@ namespace ts {
4141
export function getOutputPathsFor(sourceFile: SourceFile | Bundle, host: EmitHost, forceDtsPaths: boolean): EmitFileNames {
4242
const options = host.getCompilerOptions();
4343
if (sourceFile.kind === SyntaxKind.Bundle) {
44-
const jsFilePath = options.outFile || options.out!;
45-
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
46-
const declarationFilePath = (forceDtsPaths || getEmitDeclarations(options)) ? removeFileExtension(jsFilePath) + Extension.Dts : undefined;
47-
const declarationMapPath = getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
44+
const outPath = options.outFile || options.out!;
45+
const jsFilePath = options.emitDeclarationOnly ? undefined : outPath;
46+
const sourceMapFilePath = jsFilePath && getSourceMapFilePath(jsFilePath, options);
47+
const declarationFilePath = (forceDtsPaths || getEmitDeclarations(options)) ? removeFileExtension(outPath) + Extension.Dts : undefined;
48+
const declarationMapPath = declarationFilePath && getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
4849
const bundleInfoPath = options.references && jsFilePath ? (removeFileExtension(jsFilePath) + infoExtension) : undefined;
4950
return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath };
5051
}
5152
else {
52-
const jsFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options));
53-
const sourceMapFilePath = isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options);
53+
const ownOutputFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options));
54+
// If json file emits to the same location skip writing it, if emitDeclarationOnly skip writing it
55+
const jsFilePath = options.emitDeclarationOnly ? undefined : ownOutputFilePath;
56+
const sourceMapFilePath = !jsFilePath || isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options);
5457
// 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
5558
const isJs = isSourceFileJS(sourceFile);
5659
const declarationFilePath = ((forceDtsPaths || getEmitDeclarations(options)) && !isJs) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined;
57-
const declarationMapPath = getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
60+
const declarationMapPath = declarationFilePath && getAreDeclarationMapsEnabled(options) ? declarationFilePath + ".map" : undefined;
5861
return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath: undefined };
5962
}
6063
}
@@ -135,27 +138,33 @@ namespace ts {
135138

136139
if (!emitSkipped && emittedFilesList) {
137140
if (!emitOnlyDtsFiles) {
138-
emittedFilesList.push(jsFilePath);
139-
}
140-
if (sourceMapFilePath) {
141-
emittedFilesList.push(sourceMapFilePath);
141+
if (jsFilePath) {
142+
emittedFilesList.push(jsFilePath);
143+
}
144+
if (sourceMapFilePath) {
145+
emittedFilesList.push(sourceMapFilePath);
146+
}
147+
if (bundleInfoPath) {
148+
emittedFilesList.push(bundleInfoPath);
149+
}
142150
}
143151
if (declarationFilePath) {
144152
emittedFilesList.push(declarationFilePath);
145153
}
146-
if (bundleInfoPath) {
147-
emittedFilesList.push(bundleInfoPath);
154+
if (declarationMapPath) {
155+
emittedFilesList.push(declarationMapPath);
148156
}
149157
}
150158
}
151159

152-
function emitJsFileOrBundle(sourceFileOrBundle: SourceFile | Bundle, jsFilePath: string, sourceMapFilePath: string | undefined, bundleInfoPath: string | undefined) {
153-
// Make sure not to write js file and source map file if any of them cannot be written
154-
if (host.isEmitBlocked(jsFilePath) || compilerOptions.noEmit || compilerOptions.emitDeclarationOnly) {
155-
emitSkipped = true;
160+
function emitJsFileOrBundle(sourceFileOrBundle: SourceFile | Bundle, jsFilePath: string | undefined, sourceMapFilePath: string | undefined, bundleInfoPath: string | undefined) {
161+
if (emitOnlyDtsFiles || !jsFilePath) {
156162
return;
157163
}
158-
if (emitOnlyDtsFiles) {
164+
165+
// Make sure not to write js file and source map file if any of them cannot be written
166+
if ((jsFilePath && host.isEmitBlocked(jsFilePath)) || compilerOptions.noEmit) {
167+
emitSkipped = true;
159168
return;
160169
}
161170
// Transform the source files

src/compiler/transformers/declarations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ namespace ts {
275275
else {
276276
if (isBundledEmit && contains((node as Bundle).sourceFiles, file)) return; // Omit references to files which are being merged
277277
const paths = getOutputPathsFor(file, host, /*forceDtsPaths*/ true);
278-
declFileName = paths.declarationFilePath || paths.jsFilePath;
278+
declFileName = paths.declarationFilePath || paths.jsFilePath || file.fileName;
279279
}
280280

281281
if (declFileName) {

src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3245,7 +3245,7 @@ namespace ts {
32453245
}
32463246

32473247
export interface EmitFileNames {
3248-
jsFilePath: string;
3248+
jsFilePath: string | undefined;
32493249
sourceMapFilePath: string | undefined;
32503250
declarationFilePath: string | undefined;
32513251
declarationMapPath: string | undefined;

src/testRunner/unittests/tsbuild.ts

+2
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,10 @@ export class cNew {}`);
347347
assert.deepEqual(host.traces, [
348348
"TSFILE: /src/core/anotherModule.js",
349349
"TSFILE: /src/core/anotherModule.d.ts",
350+
"TSFILE: /src/core/anotherModule.d.ts.map",
350351
"TSFILE: /src/core/index.js",
351352
"TSFILE: /src/core/index.d.ts",
353+
"TSFILE: /src/core/index.d.ts.map",
352354
"TSFILE: /src/logic/index.js",
353355
"TSFILE: /src/logic/index.js.map",
354356
"TSFILE: /src/logic/index.d.ts",

0 commit comments

Comments
 (0)