-
-
Notifications
You must be signed in to change notification settings - Fork 601
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
feat(commonjs): inject __esModule marker into ES namespaces #552
Conversation
At the moment, I wonder how this should affect external dependencies and interact with |
15328cf
to
09cc21a
Compare
@pachuka Could you check of this PR would solve your issue? I know it is not trivial to test a plugin PR right now, but what works for me is
|
@lukastaegert - I can try it tomorrow, thanks for the instructions! |
I updated the PR to integrate with the |
@lukastaegert 👏 - just tested it out, seems to work great!! My diff of my Then in my consuming application, I was able to use my library successfully and the SvgIcons that I was importing from the default namespace from @material-ui/icons in my library components loaded properly. Thank you for taking a look into this! |
I'm converting this one back to draft because with rollup/rollup#3760 (comment), I'm actively considering adding the marker in Rollup core because it might solve a few more issues. |
@pachuka Could you check if rollup/rollup#3764 would solve this issue as well if you use the currently published version of the commonjs plugin? |
Following the discussion in rollup/rollup#3764 I again removed the draft state here and hope this can be merged and released soon. |
Any reviewer comments? @shellscape @danielgindi @guybedford |
I've been following updates, I think it's good to go. |
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.
Looks good!
// output | ||
import * as dep$1 from 'dep'; | ||
|
||
function getAugmentedNamespace(n) { |
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.
Perhaps this function could have an explicit opt-out if __esModule
is already defined? Would that be a slight perf improvement in some cases?
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.
Definitely, added!
6a91d84
to
7a63115
Compare
hi, @lukastaegert I wonder whether we could have a helper function like this:
It mimic the helper function like babel/ts importHelper function. This 'namespace' helper func aims to solve the interop issue at import side, but solving esm + cjs interop issue at "export" side might be better IMHO. |
The helper is shared between importers as all of them are using the same proxy module. So in a way it is applied at export side, but only for those importers that are commonjs. That way, there is no interference with ES module importers. |
It looks very similar but it is not the same and it serves a very different purpose. It just looks similar methods that try to copy a potentially frozen object and add properties tend to look very similar. Here the issue is that I have a perfectly valid namespace because it came from an ES module, but I need to add the The issues that the helpers you mention solve are very different, they are about creating a proper namespace from an object where you do not know a priori if it is an ESM namespace or something else. Most notably, putting the whole object as the default if there is no default does not make sense to me in this situation. |
And mutating an existing default export is a nice way of breaking code. Just imagine the default export is an object, well, then it is a very different object after that. |
Thanks for your explanation. I can get a more clear idea to the problem this pr is solving |
I will take over publishing this one |
7a63115
to
06b4c1f
Compare
06b4c1f
to
848958b
Compare
Rollup Plugin Name:
commonjs
This PR contains:
Are tests included?
Breaking Changes?
If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.
List any relevant issue numbers:
#481 (comment)
Description
This is an attempt at fixing the issue in the linked comment. It also integrates into the
requireReturnsDefault
option. So the current solution is:requireReturnsDefault
isfalse
getAugmentedNamespace
that copied all property definitions from the namespace to a new objectThe helper looks like this:
We could have made this even simpler via the plural
Object.getOwnPropertyDescriptors
but to my understanding this is not IE11 compatible.This applies to external imports as well if the
esmExternals
option is set. If you do not want this helper injected, you can setrequireReturnsDefault: "namespace"
to get the old behaviour. Any value other thanfalse
will also not inject the helper.