You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[@rollup/plugin-commonjs] does not provide an export named 'exports' #1306
I recently took a deep dive into some issues related to the @web/dev-server in conjunction with the commonjs plugin. You can read up on those related issues here. One remaining issue is with this plugin and the above error message. The commonjs-plugin injects an import statement using this logic here, but the generated output here does not provide the expected export-signature.
Apparently rollup itself does not show the same issue, which makes me wonder what I am missing in my analysis and if this is actually a bug in this plugin or if the @web/dev-server is actually at fault here. Any input is greatly appreciated!
Expected Behavior
web-dev-server should be able to transpile individual modules and pass module and import resolution over to the browser
Actual Behavior
The browser shows an error message, because the export signature of the ?commonjs-module stub does not match the signature of the generated import. Namely an export named exports is missing.
The commonjs-plugin injects an import statement using this logic here, but the generated output here does not provide the expected export-signature.
You probably mean this line instead https://github.com/rollup/plugins/blob/master/packages/commonjs/src/index.js#L248 which is for MODULE_SUFFIX, not EXPORTS_SUFFIX. This one defines the __module export, but not the exports export. Instead it declares syntheticNamedExports: '__module', which means that if a file attempts a named export of something other than __module, then instead provide a property on the __module export. I.e. Rollup internally resolves this as __module.exports, and most importantly this is a live-binding that reacts to changes in __module, something you cannot as easily do with traditional ES exports. Unless we use proxies or accessors I guess. If you issues can be traced back to problems here, maybe this is something we should look into.
Oh, I see! Apparently I did slip with the lines I linked to. Thank you so much for the insight and yes, the issues so seem to be that browsers do not resolve the synthetic exports. I assume that the commonjs-plugin uses syntheticNamedExports here to allow mutation of the __module object to propagate to the exports export? Like
import{__moduleas${moduleName},exportsas ${exportsName}}from'...';constcustomExports={a:1};${moduleName}.exports=customExports;// replace the instance${exportsName}.a=2;// access the new instance
if this is the case then yes, it matches the issues seen in @web/dev-server.
Yes, that is exactly what is happening and you gave a good example for what we try to handle with this. In that case, we might even get around it some other way. But we also use it to allow arbitrary named ESM imports from CommonJS, and here we cannot easily fake it. But if you have a solution for the other problem, or do not have this problem, we could fix the exports issue in some way.
Activity
lukastaegert commentedon Nov 27, 2022
Sorry for the late reply.
You probably mean this line instead https://github.com/rollup/plugins/blob/master/packages/commonjs/src/index.js#L248 which is for
MODULE_SUFFIX, notEXPORTS_SUFFIX. This one defines the__moduleexport, but not theexportsexport. Instead it declaressyntheticNamedExports: '__module', which means that if a file attempts a named export of something other than__module, then instead provide a property on the__moduleexport. I.e. Rollup internally resolves this as__module.exports, and most importantly this is a live-binding that reacts to changes in__module, something you cannot as easily do with traditional ES exports. Unless we use proxies or accessors I guess. If you issues can be traced back to problems here, maybe this is something we should look into.lucaelin commentedon Nov 27, 2022
Oh, I see! Apparently I did slip with the lines I linked to. Thank you so much for the insight and yes, the issues so seem to be that browsers do not resolve the synthetic exports. I assume that the commonjs-plugin uses
syntheticNamedExportshere to allow mutation of the__moduleobject to propagate to theexportsexport? Likeif this is the case then yes, it matches the issues seen in @web/dev-server.
lukastaegert commentedon Nov 28, 2022
Yes, that is exactly what is happening and you gave a good example for what we try to handle with this. In that case, we might even get around it some other way. But we also use it to allow arbitrary named ESM imports from CommonJS, and here we cannot easily fake it. But if you have a solution for the other problem, or do not have this problem, we could fix the
exportsissue in some way.lukastaegert commentedon Nov 28, 2022
It appears that this approach would also satisfy all our test cases, but I will need to look a little deeper if there are potential issues:
lucaelin commentedon Dec 3, 2022
I ran your code against some of the problematic modules we discovered on @web/dev-server and it seems to be working great!