Skip to content

Commit f153c33

Browse files
committed
Merge pull request #5090 from weswigham/out-module-concat
--outFile & --module concatenation
2 parents 4cba1b2 + 977c3ee commit f153c33

File tree

994 files changed

+12127
-24226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

994 files changed

+12127
-24226
lines changed

Jakefile.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ var builtLocalCompiler = path.join(builtLocalDirectory, compilerFilename);
228228
function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOutFile, generateDeclarations, outDir, preserveConstEnums, keepComments, noResolve, stripInternal, callback) {
229229
file(outFile, prereqs, function() {
230230
var compilerPath = useBuiltCompiler ? builtLocalCompiler : LKGCompiler;
231-
var options = "--module commonjs --noImplicitAny --noEmitOnError --pretty";
231+
var options = "--noImplicitAny --noEmitOnError --pretty";
232232

233233
// Keep comments when specifically requested
234234
// or when in debug mode.
@@ -251,6 +251,9 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu
251251
if (!noOutFile) {
252252
options += " --out " + outFile;
253253
}
254+
else {
255+
options += " --module commonjs"
256+
}
254257

255258
if(noResolve) {
256259
options += " --noResolve";

src/compiler/checker.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -14931,10 +14931,20 @@ namespace ts {
1493114931
getReferencedValueDeclaration,
1493214932
getTypeReferenceSerializationKind,
1493314933
isOptionalParameter,
14934-
isArgumentsLocalBinding
14934+
isArgumentsLocalBinding,
14935+
getExternalModuleFileFromDeclaration
1493514936
};
1493614937
}
1493714938

14939+
function getExternalModuleFileFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration): SourceFile {
14940+
const specifier = getExternalModuleName(declaration);
14941+
const moduleSymbol = getSymbolAtLocation(specifier);
14942+
if (!moduleSymbol) {
14943+
return undefined;
14944+
}
14945+
return getDeclarationOfKind(moduleSymbol, SyntaxKind.SourceFile) as SourceFile;
14946+
}
14947+
1493814948
function initializeTypeChecker() {
1493914949
// Bind all source files and propagate errors
1494014950
forEach(host.getSourceFiles(), file => {

src/compiler/declarationEmitter.ts

+56-7
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ namespace ts {
5858
let errorNameNode: DeclarationName;
5959
const emitJsDocComments = compilerOptions.removeComments ? function (declaration: Node) { } : writeJsDocComments;
6060
const emit = compilerOptions.stripInternal ? stripInternal : emitNode;
61+
let noDeclare = !root;
6162

62-
const moduleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[] = [];
63+
let moduleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[] = [];
6364
let asynchronousSubModuleDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[];
6465

6566
// Contains the reference paths that needs to go in the declaration file.
@@ -107,26 +108,60 @@ namespace ts {
107108
else {
108109
// Emit references corresponding to this file
109110
const emittedReferencedFiles: SourceFile[] = [];
111+
let prevModuleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[] = [];
110112
forEach(host.getSourceFiles(), sourceFile => {
111-
if (!isExternalModuleOrDeclarationFile(sourceFile)) {
113+
if (!isDeclarationFile(sourceFile)) {
112114
// Check what references need to be added
113115
if (!compilerOptions.noResolve) {
114116
forEach(sourceFile.referencedFiles, fileReference => {
115117
const referencedFile = tryResolveScriptReference(host, sourceFile, fileReference);
116118

117-
// If the reference file is a declaration file or an external module, emit that reference
118-
if (referencedFile && (isExternalModuleOrDeclarationFile(referencedFile) &&
119+
// If the reference file is a declaration file, emit that reference
120+
if (referencedFile && (isDeclarationFile(referencedFile) &&
119121
!contains(emittedReferencedFiles, referencedFile))) { // If the file reference was not already emitted
120122

121123
writeReferencePath(referencedFile);
122124
emittedReferencedFiles.push(referencedFile);
123125
}
124126
});
125127
}
128+
}
126129

130+
if (!isExternalModuleOrDeclarationFile(sourceFile)) {
131+
noDeclare = false;
132+
emitSourceFile(sourceFile);
133+
}
134+
else if (isExternalModule(sourceFile)) {
135+
noDeclare = true;
136+
write(`declare module "${getResolvedExternalModuleName(host, sourceFile)}" {`);
137+
writeLine();
138+
increaseIndent();
127139
emitSourceFile(sourceFile);
140+
decreaseIndent();
141+
write("}");
142+
writeLine();
143+
144+
// create asynchronous output for the importDeclarations
145+
if (moduleElementDeclarationEmitInfo.length) {
146+
const oldWriter = writer;
147+
forEach(moduleElementDeclarationEmitInfo, aliasEmitInfo => {
148+
if (aliasEmitInfo.isVisible && !aliasEmitInfo.asynchronousOutput) {
149+
Debug.assert(aliasEmitInfo.node.kind === SyntaxKind.ImportDeclaration);
150+
createAndSetNewTextWriterWithSymbolWriter();
151+
Debug.assert(aliasEmitInfo.indent === 1);
152+
increaseIndent();
153+
writeImportDeclaration(<ImportDeclaration>aliasEmitInfo.node);
154+
aliasEmitInfo.asynchronousOutput = writer.getText();
155+
decreaseIndent();
156+
}
157+
});
158+
setWriter(oldWriter);
159+
}
160+
prevModuleElementDeclarationEmitInfo = prevModuleElementDeclarationEmitInfo.concat(moduleElementDeclarationEmitInfo);
161+
moduleElementDeclarationEmitInfo = [];
128162
}
129163
});
164+
moduleElementDeclarationEmitInfo = moduleElementDeclarationEmitInfo.concat(prevModuleElementDeclarationEmitInfo);
130165
}
131166

132167
return {
@@ -607,7 +642,7 @@ namespace ts {
607642
if (node.flags & NodeFlags.Default) {
608643
write("default ");
609644
}
610-
else if (node.kind !== SyntaxKind.InterfaceDeclaration) {
645+
else if (node.kind !== SyntaxKind.InterfaceDeclaration && !noDeclare) {
611646
write("declare ");
612647
}
613648
}
@@ -702,11 +737,25 @@ namespace ts {
702737
}
703738
write(" from ");
704739
}
705-
writeTextOfNode(currentText, node.moduleSpecifier);
740+
emitExternalModuleSpecifier(node.moduleSpecifier);
706741
write(";");
707742
writer.writeLine();
708743
}
709744

745+
function emitExternalModuleSpecifier(moduleSpecifier: Expression) {
746+
if (moduleSpecifier.kind === SyntaxKind.StringLiteral && (!root) && (compilerOptions.out || compilerOptions.outFile)) {
747+
const moduleName = getExternalModuleNameFromDeclaration(host, resolver, moduleSpecifier.parent as (ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration));
748+
if (moduleName) {
749+
write("\"");
750+
write(moduleName);
751+
write("\"");
752+
return;
753+
}
754+
}
755+
756+
writeTextOfNode(currentText, moduleSpecifier);
757+
}
758+
710759
function emitImportOrExportSpecifier(node: ImportOrExportSpecifier) {
711760
if (node.propertyName) {
712761
writeTextOfNode(currentText, node.propertyName);
@@ -738,7 +787,7 @@ namespace ts {
738787
}
739788
if (node.moduleSpecifier) {
740789
write(" from ");
741-
writeTextOfNode(currentText, node.moduleSpecifier);
790+
emitExternalModuleSpecifier(node.moduleSpecifier);
742791
}
743792
write(";");
744793
writer.writeLine();

src/compiler/diagnosticMessages.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,6 @@
23102310
"category": "Message",
23112311
"code": 6078
23122312
},
2313-
23142313
"Specify JSX code generation: 'preserve' or 'react'": {
23152314
"category": "Message",
23162315
"code": 6080
@@ -2319,7 +2318,10 @@
23192318
"category": "Message",
23202319
"code": 6081
23212320
},
2322-
2321+
"Only 'amd' and 'system' modules are supported alongside --{0}.": {
2322+
"category": "Error",
2323+
"code": 6082
2324+
},
23232325
"Variable '{0}' implicitly has an '{1}' type.": {
23242326
"category": "Error",
23252327
"code": 7005

0 commit comments

Comments
 (0)