mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #7170 from masaeedu/addDeclarationDirOption
Add declarationDir option
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -2371,6 +2371,7 @@ namespace ts {
|
||||
allowNonTsExtensions?: boolean;
|
||||
charset?: string;
|
||||
declaration?: boolean;
|
||||
declarationDir?: string;
|
||||
diagnostics?: boolean;
|
||||
emitBOM?: boolean;
|
||||
help?: boolean;
|
||||
|
||||
+18
-10
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
action(emitFileNames, bundledSources, /*isBundledEmit*/true);
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var A = (function () {
|
||||
function A() {
|
||||
}
|
||||
return A;
|
||||
}());
|
||||
exports.A = A;
|
||||
});
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { B } from './subfolder/b';
|
||||
export declare class A {
|
||||
b: B;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export declare class B {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { A } from '../a';
|
||||
export declare class C {
|
||||
a: A;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var B = (function () {
|
||||
function B() {
|
||||
}
|
||||
return B;
|
||||
}());
|
||||
exports.B = B;
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var C = (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
}());
|
||||
exports.C = C;
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
var A = (function () {
|
||||
function A() {
|
||||
}
|
||||
return A;
|
||||
}());
|
||||
exports.A = A;
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { B } from './subfolder/b';
|
||||
export declare class A {
|
||||
b: B;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export declare class B {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { A } from '../a';
|
||||
export declare class C {
|
||||
a: A;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
var B = (function () {
|
||||
function B() {
|
||||
}
|
||||
return B;
|
||||
}());
|
||||
exports.B = B;
|
||||
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
var C = (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
}());
|
||||
exports.C = C;
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { B } from './subfolder/b';
|
||||
export declare class A {
|
||||
b: B;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export declare class B {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { A } from '../a';
|
||||
export declare class C {
|
||||
a: A;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var A = (function () {
|
||||
function A() {
|
||||
}
|
||||
return A;
|
||||
}());
|
||||
exports.A = A;
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var B = (function () {
|
||||
function B() {
|
||||
}
|
||||
return B;
|
||||
}());
|
||||
exports.B = B;
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
define(["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var C = (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
}());
|
||||
exports.C = C;
|
||||
});
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { B } from './subfolder/b';
|
||||
export declare class A {
|
||||
b: B;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export declare class B {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { A } from '../a';
|
||||
export declare class C {
|
||||
a: A;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
var A = (function () {
|
||||
function A() {
|
||||
}
|
||||
return A;
|
||||
}());
|
||||
exports.A = A;
|
||||
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
var B = (function () {
|
||||
function B() {
|
||||
}
|
||||
return B;
|
||||
}());
|
||||
exports.B = B;
|
||||
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
var C = (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
}());
|
||||
exports.C = C;
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"scenario": "declarationDir: specify declarationDir with bundled output file",
|
||||
"projectRoot": "tests/cases/projects/declarationDir",
|
||||
"inputFiles": [
|
||||
"a.ts",
|
||||
"subfolder/b.ts",
|
||||
"subfolder/c.ts"
|
||||
],
|
||||
"out": "out.js",
|
||||
"declaration": true,
|
||||
"declarationDir": "declarations",
|
||||
"baselineCheck": true,
|
||||
"resolvedInputFiles": [
|
||||
"lib.d.ts",
|
||||
"subfolder/b.ts",
|
||||
"a.ts",
|
||||
"subfolder/c.ts"
|
||||
],
|
||||
"emittedFiles": [
|
||||
"out.js",
|
||||
"out.d.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
declare module "subfolder/b" {
|
||||
export class B {
|
||||
}
|
||||
}
|
||||
declare module "a" {
|
||||
import { B } from "subfolder/b";
|
||||
export class A {
|
||||
b: B;
|
||||
}
|
||||
}
|
||||
declare module "subfolder/c" {
|
||||
import { A } from "a";
|
||||
export class C {
|
||||
a: A;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
define("subfolder/b", ["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var B = (function () {
|
||||
function B() {
|
||||
}
|
||||
return B;
|
||||
}());
|
||||
exports.B = B;
|
||||
});
|
||||
define("a", ["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var A = (function () {
|
||||
function A() {
|
||||
}
|
||||
return A;
|
||||
}());
|
||||
exports.A = A;
|
||||
});
|
||||
define("subfolder/c", ["require", "exports"], function (require, exports) {
|
||||
"use strict";
|
||||
var C = (function () {
|
||||
function C() {
|
||||
}
|
||||
return C;
|
||||
}());
|
||||
exports.C = C;
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
error TS5053: Option 'declarationDir' cannot be specified with option 'out'.
|
||||
error TS6082: Only 'amd' and 'system' modules are supported alongside --out.
|
||||
|
||||
|
||||
!!! error TS5053: Option 'declarationDir' cannot be specified with option 'out'.
|
||||
!!! error TS6082: Only 'amd' and 'system' modules are supported alongside --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;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"scenario": "declarationDir: specify declarationDir with bundled output file",
|
||||
"projectRoot": "tests/cases/projects/declarationDir",
|
||||
"inputFiles": [
|
||||
"a.ts",
|
||||
"subfolder/b.ts",
|
||||
"subfolder/c.ts"
|
||||
],
|
||||
"out": "out.js",
|
||||
"declaration": true,
|
||||
"declarationDir": "declarations",
|
||||
"baselineCheck": true,
|
||||
"resolvedInputFiles": [
|
||||
"lib.d.ts",
|
||||
"subfolder/b.ts",
|
||||
"a.ts",
|
||||
"subfolder/c.ts"
|
||||
],
|
||||
"emittedFiles": [
|
||||
"out.js",
|
||||
"out.d.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
declare module "subfolder/b" {
|
||||
export class B {
|
||||
}
|
||||
}
|
||||
declare module "a" {
|
||||
import { B } from "subfolder/b";
|
||||
export class A {
|
||||
b: B;
|
||||
}
|
||||
}
|
||||
declare module "subfolder/c" {
|
||||
import { A } from "a";
|
||||
export class C {
|
||||
a: A;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"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
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"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
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"scenario": "declarationDir: specify declarationDir with bundled output file",
|
||||
"projectRoot": "tests/cases/projects/declarationDir",
|
||||
"inputFiles": [
|
||||
"a.ts",
|
||||
"subfolder/b.ts",
|
||||
"subfolder/c.ts"
|
||||
],
|
||||
"out": "out.js",
|
||||
"declaration": true,
|
||||
"declarationDir": "declarations",
|
||||
"baselineCheck": true
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import {B} from './subfolder/b';
|
||||
export class A {
|
||||
b: B;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export class B {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import {A} from '../a';
|
||||
|
||||
export class C {
|
||||
a: A;
|
||||
}
|
||||
Reference in New Issue
Block a user