Skip to content

Commit 4e0acc9

Browse files
committed
Merge pull request #3586 from vvakame/addEsModule
improve module loading interoperability for babel
2 parents 2da47e8 + a512e9e commit 4e0acc9

File tree

59 files changed

+425
-97
lines changed

Some content is hidden

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

59 files changed

+425
-97
lines changed

src/compiler/emitter.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -3012,6 +3012,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30123012
return result;
30133013
}
30143014

3015+
function emitEs6ExportDefaultCompat(node: Node) {
3016+
if (node.parent.kind === SyntaxKind.SourceFile) {
3017+
Debug.assert(!!(node.flags & NodeFlags.Default) || node.kind === SyntaxKind.ExportAssignment);
3018+
// only allow export default at a source file level
3019+
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) {
3020+
if (!currentSourceFile.symbol.exports["___esModule"]) {
3021+
if (languageVersion === ScriptTarget.ES5) {
3022+
// default value of configurable, enumerable, writable are `false`.
3023+
write("Object.defineProperty(exports, \"__esModule\", { value: true });");
3024+
writeLine();
3025+
}
3026+
else if (languageVersion === ScriptTarget.ES3) {
3027+
write("exports.__esModule = true;");
3028+
writeLine();
3029+
}
3030+
}
3031+
}
3032+
}
3033+
}
3034+
30153035
function emitExportMemberAssignment(node: FunctionLikeDeclaration | ClassDeclaration) {
30163036
if (node.flags & NodeFlags.Export) {
30173037
writeLine();
@@ -3034,9 +3054,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
30343054
}
30353055
else {
30363056
if (node.flags & NodeFlags.Default) {
3057+
emitEs6ExportDefaultCompat(node);
30373058
if (languageVersion === ScriptTarget.ES3) {
30383059
write("exports[\"default\"]");
3039-
} else {
3060+
}
3061+
else {
30403062
write("exports.default");
30413063
}
30423064
}
@@ -5538,6 +5560,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
55385560
write(")");
55395561
}
55405562
else {
5563+
emitEs6ExportDefaultCompat(node);
55415564
emitContainingModuleName(node);
55425565
if (languageVersion === ScriptTarget.ES3) {
55435566
write("[\"default\"] = ");
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [es5-commonjs.ts]
2+
3+
export default class A
4+
{
5+
constructor ()
6+
{
7+
8+
}
9+
10+
public B()
11+
{
12+
return 42;
13+
}
14+
}
15+
16+
17+
//// [es5-commonjs.js]
18+
var A = (function () {
19+
function A() {
20+
}
21+
A.prototype.B = function () {
22+
return 42;
23+
};
24+
return A;
25+
})();
26+
Object.defineProperty(exports, "__esModule", { value: true });
27+
exports.default = A;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/es5-commonjs.ts ===
2+
3+
export default class A
4+
>A : Symbol(A, Decl(es5-commonjs.ts, 0, 0))
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : Symbol(B, Decl(es5-commonjs.ts, 6, 5))
13+
{
14+
return 42;
15+
}
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/es5-commonjs.ts ===
2+
3+
export default class A
4+
>A : A
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : () => number
13+
{
14+
return 42;
15+
>42 : number
16+
}
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [es5-commonjs2.ts]
2+
3+
export default 1;
4+
5+
6+
//// [es5-commonjs2.js]
7+
Object.defineProperty(exports, "__esModule", { value: true });
8+
exports.default = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/es5-commonjs2.ts ===
2+
3+
No type information for this code.export default 1;
4+
No type information for this code.
5+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/es5-commonjs2.ts ===
2+
3+
No type information for this code.export default 1;
4+
No type information for this code.
5+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [es5-commonjs3.ts]
2+
3+
export default "test";
4+
export var __esModule = 1;
5+
6+
7+
//// [es5-commonjs3.js]
8+
exports.default = "test";
9+
exports.__esModule = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/es5-commonjs3.ts ===
2+
3+
export default "test";
4+
export var __esModule = 1;
5+
>__esModule : Symbol(__esModule, Decl(es5-commonjs3.ts, 2, 10))
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/es5-commonjs3.ts ===
2+
3+
export default "test";
4+
export var __esModule = 1;
5+
>__esModule : number
6+
>1 : number
7+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [es5-commonjs4.ts]
2+
3+
export default class A
4+
{
5+
constructor ()
6+
{
7+
8+
}
9+
10+
public B()
11+
{
12+
return 42;
13+
}
14+
}
15+
export var __esModule = 1;
16+
17+
18+
//// [es5-commonjs4.js]
19+
var A = (function () {
20+
function A() {
21+
}
22+
A.prototype.B = function () {
23+
return 42;
24+
};
25+
return A;
26+
})();
27+
exports.default = A;
28+
exports.__esModule = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/es5-commonjs4.ts ===
2+
3+
export default class A
4+
>A : Symbol(A, Decl(es5-commonjs4.ts, 0, 0))
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : Symbol(B, Decl(es5-commonjs4.ts, 6, 5))
13+
{
14+
return 42;
15+
}
16+
}
17+
export var __esModule = 1;
18+
>__esModule : Symbol(__esModule, Decl(es5-commonjs4.ts, 13, 10))
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/es5-commonjs4.ts ===
2+
3+
export default class A
4+
>A : A
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : () => number
13+
{
14+
return 42;
15+
>42 : number
16+
}
17+
}
18+
export var __esModule = 1;
19+
>__esModule : number
20+
>1 : number
21+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [es5-commonjs5.ts]
2+
3+
export default function () {
4+
return "test";
5+
}
6+
7+
8+
//// [es5-commonjs5.js]
9+
function default_1() {
10+
return "test";
11+
}
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.default = default_1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/es5-commonjs5.ts ===
2+
3+
No type information for this code.export default function () {
4+
No type information for this code. return "test";
5+
No type information for this code.}
6+
No type information for this code.
7+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/es5-commonjs5.ts ===
2+
3+
export default function () {
4+
return "test";
5+
>"test" : string
6+
}
7+
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [es5-commonjs6.ts]
2+
3+
export default "test";
4+
var __esModule = 1;
5+
6+
7+
//// [es5-commonjs6.js]
8+
Object.defineProperty(exports, "__esModule", { value: true });
9+
exports.default = "test";
10+
var __esModule = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/es5-commonjs6.ts ===
2+
3+
export default "test";
4+
var __esModule = 1;
5+
>__esModule : Symbol(__esModule, Decl(es5-commonjs6.ts, 2, 3))
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/es5-commonjs6.ts ===
2+
3+
export default "test";
4+
var __esModule = 1;
5+
>__esModule : number
6+
>1 : number
7+
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [es5-system.ts]
2+
3+
export default class A
4+
{
5+
constructor ()
6+
{
7+
8+
}
9+
10+
public B()
11+
{
12+
return 42;
13+
}
14+
}
15+
16+
17+
//// [es5-system.js]
18+
System.register([], function(exports_1) {
19+
var A;
20+
return {
21+
setters:[],
22+
execute: function() {
23+
A = (function () {
24+
function A() {
25+
}
26+
A.prototype.B = function () {
27+
return 42;
28+
};
29+
return A;
30+
})();
31+
exports_1("default", A);
32+
}
33+
}
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/es5-system.ts ===
2+
3+
export default class A
4+
>A : Symbol(A, Decl(es5-system.ts, 0, 0))
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : Symbol(B, Decl(es5-system.ts, 6, 5))
13+
{
14+
return 42;
15+
}
16+
}
17+
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/es5-system.ts ===
2+
3+
export default class A
4+
>A : A
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : () => number
13+
{
14+
return 42;
15+
>42 : number
16+
}
17+
}
18+

tests/baselines/reference/es5-umd3.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ export default class A
3131
};
3232
return A;
3333
})();
34+
Object.defineProperty(exports, "__esModule", { value: true });
3435
exports.default = A;
3536
});

tests/baselines/reference/es5ExportDefaultClassDeclaration.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var C = (function () {
1212
C.prototype.method = function () { };
1313
return C;
1414
})();
15+
Object.defineProperty(exports, "__esModule", { value: true });
1516
exports.default = C;
1617

1718

tests/baselines/reference/es5ExportDefaultClassDeclaration2.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var default_1 = (function () {
1212
default_1.prototype.method = function () { };
1313
return default_1;
1414
})();
15+
Object.defineProperty(exports, "__esModule", { value: true });
1516
exports.default = default_1;
1617

1718

tests/baselines/reference/es5ExportDefaultClassDeclaration3.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var C = (function () {
2424
};
2525
return C;
2626
})();
27+
Object.defineProperty(exports, "__esModule", { value: true });
2728
exports.default = C;
2829
var after = new C();
2930
var t = C;

tests/baselines/reference/es5ExportDefaultExpression.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default (1 + 2);
44

55

66
//// [es5ExportDefaultExpression.js]
7+
Object.defineProperty(exports, "__esModule", { value: true });
78
exports.default = (1 + 2);
89

910

0 commit comments

Comments
 (0)