Skip to content

Refactor the "es2015-modules-*" plugins to use a centralized helper module - #6230

Merged
loganfsmyth merged 4 commits into
babel:masterfrom
loganfsmyth:module-refactor
Sep 13, 2017
Merged

loganfsmyth merged 4 commits into
babel:masterfrom
loganfsmyth:module-refactor

Conversation

@loganfsmyth

@loganfsmyth loganfsmyth commented Sep 12, 2017 •

Copy link
Copy Markdown
Member
Q                       A
Fixed Issues Fixes #4356, Fixes #3947, Fixes #2536, Fixes #5654, Fixes #4154, Fixes #4026, Fixes #5630, Fixes #6234, Fixes #6039
Patch: Bug Fix? Y
Major: Breaking Change? Y, but only if you were using these plugins in way we never officially supported. Technically you could for instance use the AMD or UMD plugins to convert a subset of CommonJS to AMD/UMD too, but that was never a feature we officially supported or recommended. Also see #6230 (comment) for another case.
Minor: New Feature?
Tests Added/Pass?
Spec Compliancy?
License MIT
Doc PR
Any Dependency Changes?

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.

@loganfsmyth

Copy link
Copy Markdown
Member Author

Oh these test failures sounds like fun...

@Jessidhia Jessidhia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good start for future patches.

Just need to figure out the mystery null[0] stuff 😄

}

const headers = [];
if (hasExports(meta) && !strict) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isSideEffectImport better?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

items.push([source, metadata, isAnonymous, inAnonymousBlock]);

if (source === lastNonAnonymousBlock) inAnonymousBlock = true;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to assume that side-effect imports are only ever done as the last imports in the file 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wouldn't be able to tell export default foo from export { 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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes more sense to consider them reexports.

*/
function normalizeLocalExports(
programPath: NodePath,
): Map<string, LocalExportMetadata> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😞

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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", [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should throw (as well as other code paths that try to modify imports at runtime).

Probably only fixable with a spec mode.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@loganfsmyth

Copy link
Copy Markdown
Member Author

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.

@babel-bot

babel-bot commented Sep 12, 2017 •

Copy link
Copy Markdown
Collaborator

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is literally a wrapper for Map now haha (also nice that dynamicData thing has already been a bit 😱 to me)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 hzoo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazing work, haven't ever seen a pr close that many issues/prs (including my old one from a year ago)

@loganfsmyth
loganfsmyth merged commit 1e750a9 into babel:master Sep 13, 2017
@loganfsmyth
loganfsmyth deleted the module-refactor branch September 13, 2017 00:17
@loganfsmyth loganfsmyth added the PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release label Sep 13, 2017
@loganfsmyth

Copy link
Copy Markdown
Member Author

Flagging this as breaking because I also realized this will conflict if you used multiple module transforms, like if you did

presets: ['es2015'],
plugins: ['transform-es2015-modules-umd']

this will also cause you issues, since es2015 will convert it to CommonJS, but then UMD will wrap it and basically export nothing, since there are no ES6 imports/exports left in the file to process.

@loganfsmyth

Copy link
Copy Markdown
Member Author

@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.

@rwjblue

rwjblue commented Sep 13, 2017

Copy link
Copy Markdown
Member

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 require statements are not modified when using AMD transform (this was one of the larger issues in ember-cli babel@5 to babel@6 migration).

@loganfsmyth

Copy link
Copy Markdown
Member Author

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.

matiasfha added a commit to matiasfha/babel that referenced this pull request Sep 13, 2017
@lock lock Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: modules outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

6 participants