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
Pass filename to importInterop
method
#14456
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51671/ |
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.
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, |
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.
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.
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.
Yeah, that's covered by what I already have in the diff, right? Or am I missing something?
Ah yes, good point! |
@nicolo-ribaudo thanks for your review so far! What are the next steps on this? |
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.
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.
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! We will merge this PR when we are ready for the next minor.
Woooo thanks for adding more tests and finding a bug. 😄 |
Done! babel/website#2639. |
Fixes #1, Fixes #2
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 strategybabel
, otherwisenode
.".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 thespecifier
alone, because I need to know where to resolve from. For that, we must also pass thefilename
of the file doing the import.Slack context.