Skip to content

Commit 4e0acc9

Browse files
committedJul 21, 2015
Merge pull request microsoft#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;

0 commit comments

Comments
 (0)
Please sign in to comment.