Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace ts {
type: "boolean",
description: Diagnostics.Generates_corresponding_d_ts_file,
},
{
name: "declarationDir",
type: "string",
isFilePath: true,
paramType: Diagnostics.DIRECTORY,
},
{
name: "diagnostics",
type: "boolean",
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,15 @@ namespace ts {
}
}

if (options.declarationDir) {
if (!options.declaration) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "declarationDir", "declaration"));
}
if (options.out || options.outFile) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "declarationDir", options.out ? "out" : "outFile"));
}
}

const languageVersion = options.target || ScriptTarget.ES3;
const outFile = options.outFile || options.out;

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,7 @@ namespace ts {
allowNonTsExtensions?: boolean;
charset?: string;
declaration?: boolean;
declarationDir?: string;
diagnostics?: boolean;
emitBOM?: boolean;
help?: boolean;
Expand Down
28 changes: 18 additions & 10 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,18 @@ namespace ts {
return emitOutputFilePathWithoutExtension + extension;
}

export function getDeclarationEmitOutputFilePath(sourceFile: SourceFile, host: EmitHost) {
const options = host.getCompilerOptions();
const outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified

if (options.declaration) {
const path = outputDir
? getSourceFilePathInNewDir(sourceFile, host, outputDir)
: sourceFile.fileName;
return removeFileExtension(path) + ".d.ts";
}
}

export function getEmitScriptTarget(compilerOptions: CompilerOptions) {
return compilerOptions.target || ScriptTarget.ES3;
}
Expand Down Expand Up @@ -2065,23 +2077,23 @@ namespace ts {
const emitFileNames: EmitFileNames = {
jsFilePath,
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitFilePath(jsFilePath, options) : undefined
declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined
};
action(emitFileNames, [sourceFile], /*isBundledEmit*/false);
}

function onBundledEmit(host: EmitHost) {
// Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified
const bundledSources = filter(host.getSourceFiles(),
sourceFile => !isDeclarationFile(sourceFile) && // Not a declaration file
(!isExternalModule(sourceFile) || // non module file
(getEmitModuleKind(options) && isExternalModule(sourceFile)))); // module that can emit - note falsy value from getEmitModuleKind means the module kind that shouldn't be emitted
const bundledSources = filter(host.getSourceFiles(), sourceFile =>
!isDeclarationFile(sourceFile) // Not a declaration file
&& (!isExternalModule(sourceFile) || !!getEmitModuleKind(options))); // and not a module, unless module emit enabled

if (bundledSources.length) {
const jsFilePath = options.outFile || options.out;
const emitFileNames: EmitFileNames = {
jsFilePath,
sourceMapFilePath: getSourceMapFilePath(jsFilePath, options),
declarationFilePath: getDeclarationEmitFilePath(jsFilePath, options)
declarationFilePath: options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since --declarationDir is ignored here, it should be an error to specify --out/--outFile + --declarationDir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRosenwasser suggests that the declarationDir option be respected when specified alongside out/outFile. I think this means I should simply put the bundled declaration file in declarationDir.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do not think this is correct. declarationDir should behave like outDir, we can add in the future a declarationOutFile to behave like outFile

};
action(emitFileNames, bundledSources, /*isBundledEmit*/true);
}
Expand All @@ -2090,10 +2102,6 @@ namespace ts {
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
return options.sourceMap ? jsFilePath + ".map" : undefined;
}

function getDeclarationEmitFilePath(jsFilePath: string, options: CompilerOptions) {
return options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : undefined;
}
}

export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) {
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/project/declarationDir/amd/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports"], function (require, exports) {
"use strict";
var A = (function () {
function A() {
}
return A;
}());
exports.A = A;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"scenario": "declarationDir: specify declarationDir only",
"projectRoot": "tests/cases/projects/declarationDir",
"inputFiles": [
"a.ts",
"subfolder/b.ts",
"subfolder/c.ts"
],
"declaration": true,
"declarationDir": "declarations",
"baselineCheck": true,
"resolvedInputFiles": [
"lib.d.ts",
"subfolder/b.ts",
"a.ts",
"subfolder/c.ts"
],
"emittedFiles": [
"subfolder/b.js",
"declarations/subfolder/b.d.ts",
"a.js",
"declarations/a.d.ts",
"subfolder/c.js",
"declarations/subfolder/c.d.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { B } from './subfolder/b';
export declare class A {
b: B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare class B {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { A } from '../a';
export declare class C {
a: A;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports"], function (require, exports) {
"use strict";
var B = (function () {
function B() {
}
return B;
}());
exports.B = B;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports"], function (require, exports) {
"use strict";
var C = (function () {
function C() {
}
return C;
}());
exports.C = C;
});
7 changes: 7 additions & 0 deletions tests/baselines/reference/project/declarationDir/node/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
var A = (function () {
function A() {
}
return A;
}());
exports.A = A;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"scenario": "declarationDir: specify declarationDir only",
"projectRoot": "tests/cases/projects/declarationDir",
"inputFiles": [
"a.ts",
"subfolder/b.ts",
"subfolder/c.ts"
],
"declaration": true,
"declarationDir": "declarations",
"baselineCheck": true,
"resolvedInputFiles": [
"lib.d.ts",
"subfolder/b.ts",
"a.ts",
"subfolder/c.ts"
],
"emittedFiles": [
"subfolder/b.js",
"declarations/subfolder/b.d.ts",
"a.js",
"declarations/a.d.ts",
"subfolder/c.js",
"declarations/subfolder/c.d.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { B } from './subfolder/b';
export declare class A {
b: B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare class B {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { A } from '../a';
export declare class C {
a: A;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
var B = (function () {
function B() {
}
return B;
}());
exports.B = B;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
var C = (function () {
function C() {
}
return C;
}());
exports.C = C;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"scenario": "declarationDir: specify declarationDir with outDir",
"projectRoot": "tests/cases/projects/declarationDir",
"inputFiles": [
"a.ts",
"subfolder/b.ts",
"subfolder/c.ts"
],
"outDir": "out",
"declaration": true,
"declarationDir": "declarations",
"baselineCheck": true,
"resolvedInputFiles": [
"lib.d.ts",
"subfolder/b.ts",
"a.ts",
"subfolder/c.ts"
],
"emittedFiles": [
"out/subfolder/b.js",
"declarations/subfolder/b.d.ts",
"out/a.js",
"declarations/a.d.ts",
"out/subfolder/c.js",
"declarations/subfolder/c.d.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { B } from './subfolder/b';
export declare class A {
b: B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare class B {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { A } from '../a';
export declare class C {
a: A;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports"], function (require, exports) {
"use strict";
var A = (function () {
function A() {
}
return A;
}());
exports.A = A;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports"], function (require, exports) {
"use strict";
var B = (function () {
function B() {
}
return B;
}());
exports.B = B;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports"], function (require, exports) {
"use strict";
var C = (function () {
function C() {
}
return C;
}());
exports.C = C;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"scenario": "declarationDir: specify declarationDir with outDir",
"projectRoot": "tests/cases/projects/declarationDir",
"inputFiles": [
"a.ts",
"subfolder/b.ts",
"subfolder/c.ts"
],
"outDir": "out",
"declaration": true,
"declarationDir": "declarations",
"baselineCheck": true,
"resolvedInputFiles": [
"lib.d.ts",
"subfolder/b.ts",
"a.ts",
"subfolder/c.ts"
],
"emittedFiles": [
"out/subfolder/b.js",
"declarations/subfolder/b.d.ts",
"out/a.js",
"declarations/a.d.ts",
"out/subfolder/c.js",
"declarations/subfolder/c.d.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { B } from './subfolder/b';
export declare class A {
b: B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare class B {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { A } from '../a';
export declare class C {
a: A;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
var A = (function () {
function A() {
}
return A;
}());
exports.A = A;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
var B = (function () {
function B() {
}
return B;
}());
exports.B = B;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
var C = (function () {
function C() {
}
return C;
}());
exports.C = C;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error TS5053: Option 'declarationDir' cannot be specified with option 'out'.


!!! error TS5053: Option 'declarationDir' cannot be specified with option 'out'.
==== b.ts (0 errors) ====
export class B {

}
==== a.ts (0 errors) ====
import {B} from './subfolder/b';
export class A {
b: B;
}
==== subfolder/c.ts (0 errors) ====
import {A} from '../a';

export class C {
a: A;
}
Loading