-
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
Conversation
GJ! 👍 |
@@ -2709,6 +2709,17 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { | |||
} | |||
else { | |||
if (node.flags & NodeFlags.Default) { | |||
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) { | |||
write("Object.defineProperty(exports, \"__esModule\", {"); |
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.
Object.defineProperty
is NOT available in ES3.
If compile target is ES3, you may not use it.
Just exports.__esModule = true;
like babel's loose mode.
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.
If you want to emulate this automatically, putting the define property behind a languageVersion >= ScriptTarget.ES5
and adding a write("exports.__esModule = true");
for es3 would work.
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.
thanks! applied.
if (compilerOptions.module === ModuleKind.CommonJS || compilerOptions.module === ModuleKind.AMD || compilerOptions.module === ModuleKind.UMD) { | ||
if (languageVersion >= ScriptTarget.ES5) { | ||
write("Object.defineProperty(exports, \"__esModule\", {"); | ||
writeLine(); |
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.
nit: can we put it on one line. i.e. loose the indent and the new line.
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.
✅
how is it? |
Ping @mhegazy |
Sorry for the delay. i had a comment that i wanted to fix as i merge the review, |
@@ -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 comment
The 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:
- emitClassLikeDeclarationBelowES6 (this one was obviously below ES6)
- emitSignatureAndBody (this one was not obvious)
The call from emitSignatureAndBody is guarded by !isES6ExportedDeclaration
. For isES6ExportedDeclaration to be false, any of its 3 disjuncts could be false. We want the ES6 one to be false, so let's trying to prove the other 2 true.
!!(node.flags & NodeFlags.Export)
: This is true because of line 2707node.parent.kind === SyntaxKind.SourceFile
: This is true by the rules of our language. We only allow export default at a source file level, or an ambient external module. We don't emit javascript for an ambient external module, so in legal code, it must be in a source file.
I think the following would help with static reasoning. Take the above check node.parent === currentSourceFile
and guard both the System and the default cases with that. Possibly also change it to node.parent.kind === SyntaxKind.SourceFile
to make it even clearer.
@mhegazy @JsonFreeman thanks for reviewing. I make a new commit. is it reflect your opinions? |
👍 |
@JsonFreeman I want to assert calling context of emitEs6ExportDefaultCompat. |
Can you change the conditions to |
@JsonFreeman sure! |
@@ -3012,6 +3012,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi | |||
return result; | |||
} | |||
|
|||
function emitEs6ExportDefaultCompat(node: Node) { | |||
if (node.parent.kind === SyntaxKind.SourceFile) { |
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.
Should this be an assert too?
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.
@JsonFreeman I think this line is required. if this line changes into Debug.assert
, we got a assertion error in wrong context.
3 failing
1) compiler tests for tests/cases/compiler/moduleElementsInWrongContext.ts "before all" hook:
Error: Debug Failure. False expression:
at Object.assert (eval at <anonymous> (built/local/run.js:52497:13), <anonymous>:1533:23)
2) compiler tests for tests/cases/compiler/moduleElementsInWrongContext2.ts "before all" hook:
Error: Debug Failure. False expression:
at Object.assert (eval at <anonymous> (built/local/run.js:52497:13), <anonymous>:1533:23)
3) compiler tests for tests/cases/compiler/moduleElementsInWrongContext3.ts "before all" hook:
Error: Debug Failure. False expression:
at Object.assert (eval at <anonymous> (built/local/run.js:52497:13), <anonymous>:1533:23)
Sorry @vvakame, I did not see... This is good to go 👍 Thanks! |
improve module loading interoperability for babel
✨ 😸 ✨ |
thanks @vvakame ! |
implementation for #2719