Skip to content

Commit ba63a48

Browse files
committed
Add declarationDir option
1 parent 2bf6456 commit ba63a48

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ namespace ts {
1717
type: "boolean",
1818
description: Diagnostics.Generates_corresponding_d_ts_file,
1919
},
20+
{
21+
name: "declarationDir",
22+
type: "string",
23+
isFilePath: true,
24+
paramType: Diagnostics.DIRECTORY,
25+
},
2026
{
2127
name: "diagnostics",
2228
type: "boolean",

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,7 @@ namespace ts {
23712371
allowNonTsExtensions?: boolean;
23722372
charset?: string;
23732373
declaration?: boolean;
2374+
declarationDir?: string;
23742375
diagnostics?: boolean;
23752376
emitBOM?: boolean;
23762377
help?: boolean;

src/compiler/utilities.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,16 @@ namespace ts {
20122012
return emitOutputFilePathWithoutExtension + extension;
20132013
}
20142014

2015+
export function getDeclarationEmitOutputFilePath(sourceFile: SourceFile, host: EmitHost) {
2016+
const options = host.getCompilerOptions();
2017+
const outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified
2018+
return options.declaration ? removeFileExtension(
2019+
outputDir
2020+
? getSourceFilePathInNewDir(sourceFile, host, outputDir)
2021+
: sourceFile.fileName
2022+
) + ".d.ts" : undefined;
2023+
}
2024+
20152025
export function getEmitScriptTarget(compilerOptions: CompilerOptions) {
20162026
return compilerOptions.target || ScriptTarget.ES3;
20172027
}
@@ -2065,23 +2075,23 @@ namespace ts {
20652075
const emitFileNames: EmitFileNames = {
20662076
jsFilePath,
20672077
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
2068-
declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitFilePath(jsFilePath, options) : undefined
2078+
declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined
20692079
};
20702080
action(emitFileNames, [sourceFile], /*isBundledEmit*/false);
20712081
}
20722082

20732083
function onBundledEmit(host: EmitHost) {
20742084
// Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified
2075-
const bundledSources = filter(host.getSourceFiles(),
2076-
sourceFile => !isDeclarationFile(sourceFile) && // Not a declaration file
2077-
(!isExternalModule(sourceFile) || // non module file
2078-
(getEmitModuleKind(options) && isExternalModule(sourceFile)))); // module that can emit - note falsy value from getEmitModuleKind means the module kind that shouldn't be emitted
2085+
const bundledSources = filter(host.getSourceFiles(), sourceFile =>
2086+
!isDeclarationFile(sourceFile) // Not a declaration file
2087+
&& (!isExternalModule(sourceFile) || !!getEmitModuleKind(options))); // and not a module, unless module emit enabled
2088+
20792089
if (bundledSources.length) {
20802090
const jsFilePath = options.outFile || options.out;
20812091
const emitFileNames: EmitFileNames = {
20822092
jsFilePath,
20832093
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
2084-
declarationFilePath: getDeclarationEmitFilePath(jsFilePath, options)
2094+
declarationFilePath: options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : undefined
20852095
};
20862096
action(emitFileNames, bundledSources, /*isBundledEmit*/true);
20872097
}
@@ -2090,10 +2100,6 @@ namespace ts {
20902100
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
20912101
return options.sourceMap ? jsFilePath + ".map" : undefined;
20922102
}
2093-
2094-
function getDeclarationEmitFilePath(jsFilePath: string, options: CompilerOptions) {
2095-
return options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : undefined;
2096-
}
20972103
}
20982104

20992105
export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) {

0 commit comments

Comments
 (0)