Skip to content

Commit 0018b8f

Browse files
authored
Preserve module marker in es2015+ module emit for tool compatability (microsoft#38712)
1 parent 62675a5 commit 0018b8f

36 files changed

+67
-13
lines changed

src/compiler/transformers/module/esnextAnd2015.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,36 @@ namespace ts {
1818
}
1919

2020
if (isExternalModule(node) || compilerOptions.isolatedModules) {
21-
const externalHelpersImportDeclaration = createExternalHelpersImportDeclarationIfNeeded(node, compilerOptions);
22-
if (externalHelpersImportDeclaration) {
23-
const statements: Statement[] = [];
24-
const statementOffset = addPrologue(statements, node.statements);
25-
append(statements, externalHelpersImportDeclaration);
26-
27-
addRange(statements, visitNodes(node.statements, visitor, isStatement, statementOffset));
28-
return updateSourceFileNode(
29-
node,
30-
setTextRange(createNodeArray(statements), node.statements));
31-
}
32-
else {
33-
return visitEachChild(node, visitor, context);
21+
const result = updateExternalModule(node);
22+
if (!isExternalModule(node) || some(result.statements, isExternalModuleIndicator)) {
23+
return result;
3424
}
25+
return updateSourceFileNode(
26+
result,
27+
setTextRange(createNodeArray([...result.statements, createEmptyExports()]), result.statements),
28+
);
3529
}
3630

3731
return node;
3832
}
3933

34+
function updateExternalModule(node: SourceFile) {
35+
const externalHelpersImportDeclaration = createExternalHelpersImportDeclarationIfNeeded(node, compilerOptions);
36+
if (externalHelpersImportDeclaration) {
37+
const statements: Statement[] = [];
38+
const statementOffset = addPrologue(statements, node.statements);
39+
append(statements, externalHelpersImportDeclaration);
40+
41+
addRange(statements, visitNodes(node.statements, visitor, isStatement, statementOffset));
42+
return updateSourceFileNode(
43+
node,
44+
setTextRange(createNodeArray(statements), node.statements));
45+
}
46+
else {
47+
return visitEachChild(node, visitor, context);
48+
}
49+
}
50+
4051
function visitor(node: Node): VisitResult<Node> {
4152
switch (node.kind) {
4253
case SyntaxKind.ImportEqualsDeclaration:

tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ var M;
7171
async function f1() { }
7272
M.f1 = f1;
7373
})(M || (M = {}));
74+
export {};

tests/baselines/reference/asyncAwaitIsolatedModules_es6.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,4 @@ var M;
112112
}
113113
M.f1 = f1;
114114
})(M || (M = {}));
115+
export {};

tests/baselines/reference/computedPropertyName.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ class F {
6565
}
6666
class G {
6767
}
68+
export {};

tests/baselines/reference/decoratedClassFromExternalModule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ let Decorated = /** @class */ (() => {
2727
})();
2828
export default Decorated;
2929
//// [undecorated.js]
30+
export {};

tests/baselines/reference/es6ExportAssignment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export = a;
44

55
//// [es6ExportAssignment.js]
66
var a = 10;
7+
export {};

tests/baselines/reference/es6ExportAssignment2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ import * as a from "a";
1010

1111
//// [a.js]
1212
var a = 10;
13+
export {};
1314
//// [b.js]
15+
export {};

tests/baselines/reference/es6ExportAssignment3.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ import * as a from "a";
99

1010

1111
//// [b.js]
12+
export {};

tests/baselines/reference/es6ExportAssignment4.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ import * as a from "a";
1111

1212

1313
//// [b.js]
14+
export {};

tests/baselines/reference/es6ImportEqualsDeclaration.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ import a = require("server");
99

1010
//// [server.js]
1111
var a = 10;
12+
export {};
1213
//// [client.js]
14+
export {};

0 commit comments

Comments
 (0)