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
fix: _interopRequireWildcard should only cache objects #10574
fix: _interopRequireWildcard should only cache objects #10574
Conversation
@@ -620,6 +620,10 @@ helpers.interopRequireWildcard = helper("7.0.0-beta.0")` | |||
return obj; | |||
} | |||
|
|||
if (!(obj instanceof Object)) { |
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.
This isn't safe, for example, if __proto__
is null. You should use typeof.
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.
I wish we can use _.isObject
in the helpers. I copied from there
typeof obj === "object" || typeof obj === "function"
because (new WeakMap).set(new Function, 42)
is valid.
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 for the advices. Inspiring from _.isObject
. We should also add null check, because typeof null === 'object'
and (new WeakMap).set(null, { default: null })
throws error.
@@ -620,6 +620,10 @@ helpers.interopRequireWildcard = helper("7.0.0-beta.0")` | |||
return obj; | |||
} | |||
|
|||
if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) { |
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.
What about adding tests for this? It would be easier to understand how this function should work, and prevent possible issues in the future.
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.
For test case, you may refer to packages/babel-plugin-proposal-dynamic-import/test/fixtures/commonjs/*
. You will find an example for function module exec-interop
and newly added exec-interop-string
, exec-interop-null
.
Sorry for the offtopic, but are you sure it's still working correctly? For example, lets double check construction like classNames.default = classNames;
module.exports = classNames; So let's imagine -
So, question - what As far as I understand - the inner loop creates an object clone, plus stores the original object as So:
Right now compiling the same file using webpack or parcel(babel) produces not only different code - it's not working in the second case. |
In your example, |
Is it a part of ESM specification? As long as that would be TypeScript bug in this case, and all the projects using TypeScript and wildcard imports to read exports as is from commonjs module, and exposing ESM modules as the output... are broken. |
@theKashey Yes, the specification defines imported namespaces (with Also note that Babel aligns with Node's implementation in this case: the default export of a module is it's |
There’s a second non‑nullish check a few lines after this, so I’m removing it in #10585, since it’s no longer necessary. |
_interopRequireWildcard (used for dynamic import) wrongly cached string modules (e.g. base64 image) to Weakmap throws error. This fix is to cache object only for the function.