Skip to content

Commit 09309d8

Browse files
committed
Merge pull request #4695 from Microsoft/definitionsAsExternalModule
Change typescript.d.ts to be an external module instead of an ambient external module declaration
2 parents 50e122f + 8a38a1e commit 09309d8

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

Jakefile.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ var servicesFile = path.join(builtLocalDirectory, "typescriptServices.js");
394394
var standaloneDefinitionsFile = path.join(builtLocalDirectory, "typescriptServices.d.ts");
395395
var nodePackageFile = path.join(builtLocalDirectory, "typescript.js");
396396
var nodeDefinitionsFile = path.join(builtLocalDirectory, "typescript.d.ts");
397+
var nodeStandaloneDefinitionsFile = path.join(builtLocalDirectory, "typescript_standalone.d.ts");
397398

398399
compileFile(servicesFile, servicesSources,[builtLocalDirectory, copyright].concat(servicesSources),
399400
/*prefixes*/ [copyright],
@@ -410,11 +411,19 @@ compileFile(servicesFile, servicesSources,[builtLocalDirectory, copyright].conca
410411

411412
prependFile(copyright, standaloneDefinitionsFile);
412413

413-
// Create the node definition file by replacing 'ts' module with '"typescript"' as a module.
414+
// Stanalone/web definition file using global 'ts' namespace
414415
jake.cpR(standaloneDefinitionsFile, nodeDefinitionsFile, {silent: true});
415416
var definitionFileContents = fs.readFileSync(nodeDefinitionsFile).toString();
416-
definitionFileContents = definitionFileContents.replace(/declare (namespace|module) ts/g, 'declare module "typescript"');
417-
fs.writeFileSync(nodeDefinitionsFile, definitionFileContents);
417+
418+
// Official node package definition file, pointed to by 'typings' in package.json
419+
// Created by appending 'export = ts;' at the end of the standalone file to turn it into an external module
420+
var nodeDefinitionsFileContents = definitionFileContents + "\r\nexport = ts;";
421+
fs.writeFileSync(nodeDefinitionsFile, nodeDefinitionsFileContents);
422+
423+
// Node package definition file to be distributed without the package. Created by replacing
424+
// 'ts' namespace with '"typescript"' as a module.
425+
var nodeStandaloneDefinitionsFileContents = definitionFileContents.replace(/declare (namespace|module) ts/g, 'declare module "typescript"');
426+
fs.writeFileSync(nodeStandaloneDefinitionsFile, nodeStandaloneDefinitionsFileContents);
418427
});
419428

420429

lib/typescript.d.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ See the Apache Version 2.0 License for specific language governing permissions
1313
and limitations under the License.
1414
***************************************************************************** */
1515

16-
declare module "typescript" {
16+
declare namespace ts {
1717
interface Map<T> {
1818
[index: string]: T;
1919
}
@@ -1405,7 +1405,7 @@ declare module "typescript" {
14051405
newLength: number;
14061406
}
14071407
}
1408-
declare module "typescript" {
1408+
declare namespace ts {
14091409
interface System {
14101410
args: string[];
14111411
newLine: string;
@@ -1429,7 +1429,7 @@ declare module "typescript" {
14291429
}
14301430
var sys: System;
14311431
}
1432-
declare module "typescript" {
1432+
declare namespace ts {
14331433
interface ErrorCallback {
14341434
(message: DiagnosticMessage, length: number): void;
14351435
}
@@ -1474,7 +1474,7 @@ declare module "typescript" {
14741474
function isIdentifierPart(ch: number, languageVersion: ScriptTarget): boolean;
14751475
function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, languageVariant?: LanguageVariant, text?: string, onError?: ErrorCallback, start?: number, length?: number): Scanner;
14761476
}
1477-
declare module "typescript" {
1477+
declare namespace ts {
14781478
function getDefaultLibFileName(options: CompilerOptions): string;
14791479
function textSpanEnd(span: TextSpan): number;
14801480
function textSpanIsEmpty(span: TextSpan): boolean;
@@ -1504,14 +1504,14 @@ declare module "typescript" {
15041504
function collapseTextChangeRangesAcrossMultipleVersions(changes: TextChangeRange[]): TextChangeRange;
15051505
function getTypeParameterOwner(d: Declaration): Declaration;
15061506
}
1507-
declare module "typescript" {
1507+
declare namespace ts {
15081508
function getNodeConstructor(kind: SyntaxKind): new () => Node;
15091509
function createNode(kind: SyntaxKind): Node;
15101510
function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T;
15111511
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile;
15121512
function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile;
15131513
}
1514-
declare module "typescript" {
1514+
declare namespace ts {
15151515
const version: string;
15161516
function findConfigFile(searchPath: string): string;
15171517
function resolveTripleslashReference(moduleName: string, containingFile: string): string;
@@ -1524,7 +1524,7 @@ declare module "typescript" {
15241524
function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string;
15251525
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program;
15261526
}
1527-
declare module "typescript" {
1527+
declare namespace ts {
15281528
function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine;
15291529
/**
15301530
* Read tsconfig.json file
@@ -1551,7 +1551,7 @@ declare module "typescript" {
15511551
*/
15521552
function parseConfigFile(json: any, host: ParseConfigHost, basePath: string): ParsedCommandLine;
15531553
}
1554-
declare module "typescript" {
1554+
declare namespace ts {
15551555
/** The version of the language service API */
15561556
let servicesVersion: string;
15571557
interface Node {
@@ -2139,3 +2139,5 @@ declare module "typescript" {
21392139
*/
21402140
function getDefaultLibFilePath(options: CompilerOptions): string;
21412141
}
2142+
2143+
export = ts;

tests/cases/compiler/APISample_compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @module: commonjs
2-
// @includebuiltfile: typescript.d.ts
2+
// @includebuiltfile: typescript_standalone.d.ts
33
// @stripInternal:true
44

55
/*

tests/cases/compiler/APISample_linter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @module: commonjs
2-
// @includebuiltfile: typescript.d.ts
2+
// @includebuiltfile: typescript_standalone.d.ts
33
// @stripInternal:true
44

55
/*

tests/cases/compiler/APISample_transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @module: commonjs
2-
// @includebuiltfile: typescript.d.ts
2+
// @includebuiltfile: typescript_standalone.d.ts
33
// @stripInternal:true
44

55
/*

tests/cases/compiler/APISample_watcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @module: commonjs
2-
// @includebuiltfile: typescript.d.ts
2+
// @includebuiltfile: typescript_standalone.d.ts
33
// @stripInternal:true
44

55
/*

0 commit comments

Comments
 (0)