-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve module loading interoperability for babel #3586
Changes from 3 commits
54a4e9e
3aba5aa
e9d590f
75466e6
86d106a
a512e9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2687,6 +2687,22 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { | |
return result; | ||
} | ||
|
||
function emitEs6ExportDefaultCompat() { | ||
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) { | ||
if (!hasProperty(currentSourceFile.identifiers, "___esModule")) { | ||
if (languageVersion >= ScriptTarget.ES5) { | ||
// default value of configurable, enumerable, writable are `false`. | ||
write("Object.defineProperty(exports, \"__esModule\", { value: true });"); | ||
writeLine(); | ||
} | ||
else { | ||
write("exports.__esModule = true;"); | ||
writeLine(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move writeLine after the if-else so it only appears once. Not a big deal though. |
||
} | ||
} | ||
} | ||
} | ||
|
||
function emitExportMemberAssignment(node: FunctionLikeDeclaration | ClassDeclaration) { | ||
if (node.flags & NodeFlags.Export) { | ||
writeLine(); | ||
|
@@ -2709,9 +2725,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { | |
} | ||
else { | ||
if (node.flags & NodeFlags.Default) { | ||
emitEs6ExportDefaultCompat(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was tricky to convince myself that this would not get called in ES6. To do so, I had to look at the calls to emitExportMemberAssignment. I found two:
The call from emitSignatureAndBody is guarded by
I think the following would help with static reasoning. Take the above check |
||
if (languageVersion === ScriptTarget.ES3) { | ||
write("exports[\"default\"]"); | ||
} else { | ||
} | ||
else { | ||
write("exports.default"); | ||
} | ||
} | ||
|
@@ -4867,6 +4885,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { | |
write(")"); | ||
} | ||
else { | ||
emitEs6ExportDefaultCompat(); | ||
emitContainingModuleName(node); | ||
if (languageVersion === ScriptTarget.ES3) { | ||
write("[\"default\"] = "); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//// [es5-commonjs.ts] | ||
|
||
export default class A | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
{ | ||
return 42; | ||
} | ||
} | ||
|
||
|
||
//// [es5-commonjs.js] | ||
var A = (function () { | ||
function A() { | ||
} | ||
A.prototype.B = function () { | ||
return 42; | ||
}; | ||
return A; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = A; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
=== tests/cases/compiler/es5-commonjs.ts === | ||
|
||
export default class A | ||
>A : Symbol(A, Decl(es5-commonjs.ts, 0, 0)) | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
>B : Symbol(B, Decl(es5-commonjs.ts, 6, 5)) | ||
{ | ||
return 42; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
=== tests/cases/compiler/es5-commonjs.ts === | ||
|
||
export default class A | ||
>A : A | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
>B : () => number | ||
{ | ||
return 42; | ||
>42 : number | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//// [es5-commonjs2.ts] | ||
|
||
export default 1; | ||
|
||
|
||
//// [es5-commonjs2.js] | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = 1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
=== tests/cases/compiler/es5-commonjs2.ts === | ||
|
||
No type information for this code.export default 1; | ||
No type information for this code. | ||
No type information for this code. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
=== tests/cases/compiler/es5-commonjs2.ts === | ||
|
||
No type information for this code.export default 1; | ||
No type information for this code. | ||
No type information for this code. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//// [es5-commonjs3.ts] | ||
|
||
export default "test"; | ||
export var __esModule = 1; | ||
|
||
|
||
//// [es5-commonjs3.js] | ||
exports.default = "test"; | ||
exports.__esModule = 1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
=== tests/cases/compiler/es5-commonjs3.ts === | ||
|
||
export default "test"; | ||
export var __esModule = 1; | ||
>__esModule : Symbol(__esModule, Decl(es5-commonjs3.ts, 2, 10)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
=== tests/cases/compiler/es5-commonjs3.ts === | ||
|
||
export default "test"; | ||
export var __esModule = 1; | ||
>__esModule : number | ||
>1 : number | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//// [es5-commonjs4.ts] | ||
|
||
export default class A | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
{ | ||
return 42; | ||
} | ||
} | ||
export var __esModule = 1; | ||
|
||
|
||
//// [es5-commonjs4.js] | ||
var A = (function () { | ||
function A() { | ||
} | ||
A.prototype.B = function () { | ||
return 42; | ||
}; | ||
return A; | ||
})(); | ||
exports.default = A; | ||
exports.__esModule = 1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
=== tests/cases/compiler/es5-commonjs4.ts === | ||
|
||
export default class A | ||
>A : Symbol(A, Decl(es5-commonjs4.ts, 0, 0)) | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
>B : Symbol(B, Decl(es5-commonjs4.ts, 6, 5)) | ||
{ | ||
return 42; | ||
} | ||
} | ||
export var __esModule = 1; | ||
>__esModule : Symbol(__esModule, Decl(es5-commonjs4.ts, 13, 10)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
=== tests/cases/compiler/es5-commonjs4.ts === | ||
|
||
export default class A | ||
>A : A | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
>B : () => number | ||
{ | ||
return 42; | ||
>42 : number | ||
} | ||
} | ||
export var __esModule = 1; | ||
>__esModule : number | ||
>1 : number | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//// [es5-commonjs5.ts] | ||
|
||
export default function () { | ||
return "test"; | ||
} | ||
|
||
|
||
//// [es5-commonjs5.js] | ||
function default_1() { | ||
return "test"; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = default_1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
=== tests/cases/compiler/es5-commonjs5.ts === | ||
|
||
No type information for this code.export default function () { | ||
No type information for this code. return "test"; | ||
No type information for this code.} | ||
No type information for this code. | ||
No type information for this code. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
=== tests/cases/compiler/es5-commonjs5.ts === | ||
|
||
export default function () { | ||
return "test"; | ||
>"test" : string | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//// [es5-system.ts] | ||
|
||
export default class A | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
{ | ||
return 42; | ||
} | ||
} | ||
|
||
|
||
//// [es5-system.js] | ||
System.register([], function(exports_1) { | ||
var A; | ||
return { | ||
setters:[], | ||
execute: function() { | ||
A = (function () { | ||
function A() { | ||
} | ||
A.prototype.B = function () { | ||
return 42; | ||
}; | ||
return A; | ||
})(); | ||
exports_1("default", A); | ||
} | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
=== tests/cases/compiler/es5-system.ts === | ||
|
||
export default class A | ||
>A : Symbol(A, Decl(es5-system.ts, 0, 0)) | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
>B : Symbol(B, Decl(es5-system.ts, 6, 5)) | ||
{ | ||
return 42; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
=== tests/cases/compiler/es5-system.ts === | ||
|
||
export default class A | ||
>A : A | ||
{ | ||
constructor () | ||
{ | ||
|
||
} | ||
|
||
public B() | ||
>B : () => number | ||
{ | ||
return 42; | ||
>42 : number | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, this should check only the properties of the module object.