Repository navigation
Refactor the "es2015-modules-*" plugins to use a centralized helper module - #6230
Conversation
|
Oh these test failures sounds like fun... |
Jessidhia
left a comment
There was a problem hiding this comment.
Looks like a good start for future patches.
Just need to figure out the mystery null[0] stuff 😄
| } | ||
|
|
||
| const headers = []; | ||
| if (hasExports(meta) && !strict) { |
There was a problem hiding this comment.
(breaking if fixed) This should actually always run (unless !strict); at least when parsing module or ambiguous (when any module syntax at all is present). Not doing this could allow importing a module with no exports to result in an import of a synthetic default empty object.
// a.js
// look 'ma, no exports
// b.js
import a from './a.js'
typeof a === 'object'There was a problem hiding this comment.
I 100% agree. I was trying to keep the breaking changes in this PR to things that were hard to replicate, but definitely in the future.
| ).reduceRight((acc, [source, metadata]) => { | ||
| if (acc !== null) return acc; | ||
|
|
||
| if (isAnonymousImport(metadata)) return null; |
| items.push([source, metadata, isAnonymous, inAnonymousBlock]); | ||
|
|
||
| if (source === lastNonAnonymousBlock) inAnonymousBlock = true; | ||
| } |
There was a problem hiding this comment.
This seems to assume that side-effect imports are only ever done as the last imports in the file 🤔
There was a problem hiding this comment.
I'm replicating the existing logic from
but yeah it's weird.Someone actually filed #3802 so I'm tempted to remove this behavior, I just opted to leave it as something to do later.
| data.imports.set(localName, "default"); | ||
|
|
||
| const reexport = localData.get(localName); | ||
| if (reexport) { |
There was a problem hiding this comment.
IIUC this changes the internal state such that the foo export in import foo from 'bar'; export { foo } stays a live binding. However, it is not correct if it's reexported by using export default foo (should evaluate it as an expression so it stops being live).
Not sure if it's an existing or a new bug, but probably better fixed later.
There was a problem hiding this comment.
@Kovensky Could you clarify what you mean? import foo from 'bar'; export { foo } should be live, but isn't currently, which is a bug: #4154 This if doesn't handle export default foo; though, so that should still be non-live as expected.
There was a problem hiding this comment.
I'm probably misunderstanding, but I think this changes the internal state to remove the export { foo } and add a metadata equivalent of export { default as foo } from 'bar'. If this does what I think it does it fixes 4154, but it would break export default foo, since it wouldn't be able to tell export default foo from export { foo as default } 🤔
Unless the localData name is different
There was a problem hiding this comment.
it wouldn't be able to tell
export default foofromexport { foo as default }
Those two are managed differently. export default foo; Isn't part of the reexport dataset because it isn't live because it's split up in https://github.com/babel/babel/pull/6230/files#diff-d53cda1dedf7b332fe72eabbf998ac88R269 and treated like
var _tmp = foo;
export {_tmp as default};
so it would be _tmp in the localData object, not foo.
|
|
||
| data.importsNamespace.add(localName); | ||
| const reexport = localData.get(localName); | ||
| if (reexport) { |
There was a problem hiding this comment.
This OTOH doesn't run in the same corner case since the namespace reference itself (supposedly) never changes, but also might be better to fix the same way as the previous case.
There was a problem hiding this comment.
You're right that it isn't necessary to consider namespace exports "reexports", but I did it because it simplified things and I couldn't think of anything that would break from this. Thoughts?
There was a problem hiding this comment.
It makes more sense to consider them reexports.
| */ | ||
| function normalizeLocalExports( | ||
| programPath: NodePath, | ||
| ): Map<string, LocalExportMetadata> { |
There was a problem hiding this comment.
This function is a bit confusing as it has side-effects and returns a value.
Doing the data collection in a separate pass (getNormalizedLocalExports?) would be nicer but probably makes for a slower transform 😞
There was a problem hiding this comment.
Yeah I was on the fence. Since the default export of this module does return metadata and mutate, I didn't worry about it too much since we won't be making a breaking change if we come refactor it later. That said, I'm happy to change that now if you want.
There was a problem hiding this comment.
Ideally we'd have a method to collect metadata without mutations, and a method to apply the mutations as directed by the metadata, for each of the cases; but I understand if they have to be kept "dirty" due to the performance impact of multiple traversals.
| ]); | ||
| namedDecl._blockHoist = child.node._blockHoist; | ||
|
|
||
| const varDecl = t.variableDeclaration("var", [ |
There was a problem hiding this comment.
Ideally it should be let, but because of how things run right now it makes no difference at runtime (if the let is still in TDZ the export itself doesn't exist yet anyway).
There was a problem hiding this comment.
Even better it should be const, but yeah I wasn't confident that the block-scope plugin would get to it properly. I do want to revisit some stuff like this eventually though.
| if (exportedNames.length > 0 || imported.has(localName)) { | ||
| const assignment = path.node; | ||
|
|
||
| if (imported.has(localName)) { |
There was a problem hiding this comment.
This should throw (as well as other code paths that try to modify imports at runtime).
Probably only fixable with a spec mode.
There was a problem hiding this comment.
Could you clarify? This logic does throw, it just injects logic that throws at runtime (buildImportThrow) instead of compile time, since that's how it'd work in a real environment
There was a problem hiding this comment.
Oh, I missed the buildImportThrow near the end; had looked inside the inner if instead
| * Flag a set of statements as hoisted above all else so that module init | ||
| * statements all run before user code. | ||
| */ | ||
| export function ensureStatementsHoisted(statements) { |
There was a problem hiding this comment.
One day I'd like to be able to put all the init statements in a single BlockStatement and only hoist that, but the problem is how to expose the imports since they'd create let bindings in spec mode 🤔
There was a problem hiding this comment.
I'm on the fence. That's one option, but another would be to just put them all at the top of the file, and wrap everything that needs to be hoisted in a function declaration that will lazy-init the value to be hoisted. I haven't really settled on a solution either.
|
Updated, let's see if the tests pass now. I pushed some commits with ugly names, when I actually merge this I'm going to rebase and merge those commits with the ones with those names earlier in the commit list. |
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/4885/ |
| @@ -1,11 +1,6 @@ | |||
| export default class Store { | |||
There was a problem hiding this comment.
So this is literally a wrapper for Map now haha (also nice that dynamicData thing has already been a bit 😱 to me)
There was a problem hiding this comment.
Yeah we should probably just kill this file and copy this logic to the two classes where it's actually used, but I opted to leave that for a later PR.
hzoo
left a comment
There was a problem hiding this comment.
amazing work, haven't ever seen a pr close that many issues/prs (including my old one from a year ago)
182e5c8 to
a189549
Compare
|
Flagging this as breaking because I also realized this will conflict if you used multiple module transforms, like if you did this will also cause you issues, since |
|
@rwjblue I realize you'd be a good person to ping for this. This PR makes these transforms stricter in how they work, compared to the old behavior, as I mentioned in the original post. Is there any risk here for your usecases? e.g. Do you ever try to convert CommonJS-like files (rather than ES6 module syntax) to AMD/UMD using Babel's transforms? It wasn't officially supported, but it did kind of work. |
|
No, I think the refactor is great. Almost of the troubles that I have ran into actually stemmed from the things fixed here. We could add more tests to confirm global |
|
If you have time to test, it'd be awesome. If there are going to be issues, definitely best if we can catch them early. Maybe we can continue this on Slack if you find anything. |
…d add the test for loose mode
This doesn't do much on its own other that make the transforms independent of eachother, but it opens us up to handle their transformation better in the future.
The big thing this PR does do is make sure we properly handle exports that reference local bindings, and ensure that things are loaded in the correct order.