fix: _interopRequireWildcard should only cache objects#10574
fix: _interopRequireWildcard should only cache objects#10574nicolo-ribaudo merged 3 commits intobabel:masterfrom samMeow:fix/require_wildcard_string_10561
Conversation
| return obj; | ||
| } | ||
|
|
||
| if (!(obj instanceof Object)) { |
There was a problem hiding this comment.
This isn't safe, for example, if __proto__ is null. You should use typeof.
There was a problem hiding this comment.
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.
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.
| return obj; | ||
| } | ||
|
|
||
| if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) { |
There was a problem hiding this comment.
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.
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.