Skip to content
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

RFC: visit exports during dependency tree walk #2

Conversation

gregmagolan
Copy link

I needed the following change for a build to work that contained exports such as:

export {x, y as z} from 'some-module';

Files referenced on lines like this were not being included in the bundle. I took a look at the Es6ModuleRewrite code and used that as an example to update FindModuleDependencies.java so that the files referenced in export lines like the one above are visited and end up included in the bundle.

I didn't handle the export * from 'some-module' case but that one should probably be handled as well.

Does this change make sense to you? Can something like this be included in PR google#2641?

@ChadKillingsworth
Copy link
Owner

I don't use that form much. Did another file consume the exports? Are you wanting it included just because it exports something?

ChadKillingsworth pushed a commit that referenced this pull request Oct 9, 2017
…in a tight loop.

NodeTraversal have significant initialization cost.
Also, avoid repetitive hash lookups when mapping variables to indexes.

For a sample build, this moves CoalesceVariableNames from #2 to being google#13 in the cost rank.  I'm sure this pass could be a lot faster but this is a easy win.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=170096543
Node importNode = IR.importNode(IR.empty(), IR.empty(), moduleIdentifier.cloneNode());
importNode.useSourceInfoFrom(n);
parent.addChildBefore(importNode, n);
visit(t, importNode, parent);
Copy link
Owner

Choose a reason for hiding this comment

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

Definitely the wrong place. FindModuleDependencies does not actually modify the AST. This needs to be somewhere in Es6RewriteModules.java

Copy link
Author

Choose a reason for hiding this comment

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

It seemed to when I tried it. Put together an example showing how the bundle changes with and without the patch is here: https://github.com/gregmagolan/closure-export-example.

From what I can tell reading the code, with this change the module in the export { x, y as z } from 'some-module' line gets added as an ordered require for the input:

t.getInput().addOrderedRequire(moduleName);

Without the change, this doesn't happen and the contents of the module don't end up in the bundle.

Copy link
Owner

Choose a reason for hiding this comment

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

Adding the ordered required seems correct here. Augmenting the AST though needs to be deferred to other passes.

@gregmagolan
Copy link
Author

Yes. Another file consumes the export.

I put together a trivial example of this issue here: https://github.com/gregmagolan/closure-export-example

README explains how to build with a PR2641 dist that ends up with a runtime error and how to build with PR2641 + the patch in the PR which ends up working.

@gregmagolan
Copy link
Author

gregmagolan commented Oct 10, 2017

Update the PR. Removed some lines that were copied from Es6RewriteModules.java. Only visit call is now made for the export { x, y as z } from 'module' case. Example at https://github.com/gregmagolan/closure-export-example builds and runs with the updated patch.

How does this look Chad? No more AST modification I'm guessing with those lines removed. I also tried handling the export * from 'module' case but the ES6 transpiler doesn't handle that yet. Compiler errors out with "ES6 transpilation 'Wildcard export' is not yet implemented." when code contains an export * from 'module' statement. Not an issue as that doesn't come up in my use case.

@ChadKillingsworth
Copy link
Owner

@gregmagolan Realized I never got back to you. Go ahead and PR this to the main repo and we'll get this in. For the export * from case, let's add that too to the dependencies graph. Transpilation may not handle it yet, but we should still add it to the dependency graph.

@gregmagolan
Copy link
Author

PR'd against the main repo: google#2726

ChadKillingsworth pushed a commit that referenced this pull request Apr 3, 2024
With the recent go/accurately-maintain-script-node-featureSet change, we can make the ASTValidator to confirm after each pass that:

1. Features encountered during AST traversal of a SCRIPT  <= compiler's allowable featureSet [Source] (becomes optional)
2. Features encountered during AST traversal of a SCRIPT <= that particular SCRIPT node’s FEATURE_SET (accurate, without overestimation during transpile)
3. every SCRIPT node’s FEATURE_SET <= compiler's allowable featureSet (possible to do)

#1 and #2 happened automatically as part of go/accurately-maintain-script-node-featureSet.

This CL adds #3 above.

PiperOrigin-RevId: 554614152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants