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

Pass filename to importInterop method #14456

Merged

Conversation

NickHeiner
Copy link
Contributor

@NickHeiner NickHeiner commented Apr 11, 2022

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix? No
Major: Breaking Change? No
Minor: New Feature? Yes
Tests Added + Pass? Yes
Documentation PR Link babel/website#2639
Any Dependency Changes? No
License MIT

babel/plugin-transform-modules-commonjs's importInterop function signature is currently: (specifier: string) =>. This allows the function to implement heuristic-based decisions, like "if it starts with ./, then use strategy babel, otherwise node.".

I would like to implement an importInterop function in my project where the build process actually inspects the file being imported, then picks the right strategy based on that file's contents. I can't do this from the specifier alone, because I need to know where to resolve from. For that, we must also pass the filename of the file doing the import.

Slack context.

@babel-bot
Copy link
Collaborator

babel-bot commented Apr 11, 2022

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51671/

@nicolo-ribaudo nicolo-ribaudo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Apr 11, 2022
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thank you! I admit I completely forgot about our slack discussion, but still as soon as I saw this PR I thought "this is a good idea!".

Can you also add a test for what happens when no filename is passed to Babel, and if we need to update the type annotation to string | undefined?

@@ -211,6 +211,7 @@ export default declare((api, options) => {
? mjsStrictNamespace
: strictNamespace,
noIncompleteNsImportDetection,
filename: this.file.opts.filename,
Copy link
Member

Choose a reason for hiding this comment

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

We also use rewriteModuleStatementsAndPrepareHeader in the amd and umd plugins. I don't remember if they all support the importInterop option, but if they do they should have the same behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's covered by what I already have in the diff, right? Or am I missing something?

@NickHeiner
Copy link
Contributor Author

Can you also add a test for what happens when no filename is passed to Babel, and if we need to update the type annotation to string | undefined?

Ah yes, good point!

@NickHeiner
Copy link
Contributor Author

@nicolo-ribaudo thanks for your review so far! What are the next steps on this?

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Can you open a docs PR to babel/website?

https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs#importinterop and the amd/umd accordingly.

@nicolo-ribaudo nicolo-ribaudo added this to the v7.18.0 milestone Apr 13, 2022
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks! We will merge this PR when we are ready for the next minor.

@nicolo-ribaudo nicolo-ribaudo added PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release PR: Needs Docs labels Apr 13, 2022
@NickHeiner
Copy link
Contributor Author

Woooo thanks for adding more tests and finding a bug. 😄

@NickHeiner
Copy link
Contributor Author

Can you open a docs PR to babel/website?

https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs#importinterop and the amd/umd accordingly.

Done! babel/website#2639.

@nicolo-ribaudo nicolo-ribaudo merged commit 92ffff4 into babel:main May 17, 2022
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 17, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories PR: Ready to be Merged A pull request with already two approvals, but waiting for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants