-
Notifications
You must be signed in to change notification settings - Fork 252
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
Nested default
exports with using dynamic import
#813
Comments
Please have a look at #757 (comment) |
This is pure ESM Node without any transpilers: https://stackblitz.com/edit/node-mucfca?file=index.js I'm not sure if I think bundlers tried to support this way of exporting default, but official implementation doesn't. Babel has a lot of experimental things that didn't end up in Node/browsers and the whole point of using babel is to make it run everywhere 😄 |
You could set ESM differentiates This is how Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function () {
return // ... value ...
}
});
Object.defineProperty(exports, "someNamedExport", {
enumerable: true,
get: function () {
return // ... value ...
}
}); It looks like the consuming environment is lacking function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } I'm not sure about the best export moving forward. |
I don't think Node should watch at the I would prefer shipping both CJS and ESM and specifying Also what you provided does not look like what user-event is shipping (https://unpkg.com/browse/@testing-library/[email protected]/dist/index.js) By the way I should point out that named exports are working fine, as you can see in @antfu issue. Only default one is kinda fishy |
I know, but it's equivalent here.
There is no default export in CJS. That's what
Sadly it seems not to be that simple. There is no way CJS and ESM can be consumed the same way without some magic like |
What do you mean? If you bundle cjs/esm in different files, you can set something like this in
Why do you need to use Maybe there are other ways. I know @antfu helped other packages fix similar issues while working on Vite. Maybe he has something to say? |
If you bothered to read my posts and followed the links...
Yes, a bundler is an option as mentioned before. |
I did. And they never explain why it can't be done. I see why you can't put them in one file, but there is nothing that stops you provide different files for different formats. I even gave you a link to Nodejs documentation that tells you how it can be done and provided a way to bundle it. Also you are being rude. My only goal is to help you be compatible with native Node ESM implementation and not to fight. |
I'm sorry if I came off as rude. It's just that this isn't that simple as you're making it out to be. I guess otherwise there would be a solution that everybody would be using. I've read quite a few posts and asked a few people about this when updating the script used to build the package with dual export mentioned above. CJS and ESM are not interchangeable. At least not for the default export which does not exist in CJS. Therefore Also there is a very long discussion in TypeScript repo over multiple issues why they won't manipulate any import path. I tend to disagree regarding their stance that it also should not be possible per a compiler flag, but they do have a point in the possible downfalls of it which also apply here. Also I know a dual export can be done for some environments. But this is exactly the problem: No solution so far seems to be working for all environments. This is due to the fact that how imports are resolved is subject to the environment. There are mechanisms described e.g. in the NodeJS docs how to make this work e.g. per exports map, but they are not supported by all environments. |
What environments are you refering to? Where having two files with exports like this won't work? And how does it affect userbase?
If you Also all bundlers support That's what unplugin-auto-import does, for example |
I think the ideal way is indeed to ship dual formats (CJS & ESM), so that Node can understand and import it correctly based on the user environment. Without an additional layer from other tools to do the interop. It's the standard, and I believe it will benefit in the long run. While in our particular cases on Vitest, interpretation is acceptable and it's also what we are already doing. The only blocker is the nesting. I would expect the correct format to be: {
__esModule: true,
default: {
click: [Function: click],
dblClick: [Function: dblClick],
type: [Function: type],
clear: [Function: clear],
tab: [Function: tab],
hover: [Function: hover],
unhover: [Function: unhover],
upload: [Function: upload],
selectOptions: [Function: bound selectOptionsBase],
deselectOptions: [Function: bound selectOptionsBase],
}
} |
I'm not sure if there are environments out there that would already break on main imports. I'm not against a change here. A well written and researched PR would be welcome. It doesn't help if you throw in hunches how this might be improved if you leave it to me to research how these might affect some of our users. |
Agree.
The problem seems to be that the library was designed to be consumed per default export while being exported as CJS which has no differentiation for default and named exports. Changing this would be a big breaking change.
That is what is seen in other environments that do support |
The problem is that we are not talking about "some environments", it's native Node. I would be surprised to see a node package that does not work on the node directly but require external tools to interop every single file to work (this make thing a lot less efficient). Interoperation for top-level import is understandable since the different expressive between CJS and ESM, but for nested modules, there is nothing users could do to interop them without hacks or bundling. Thanks for following up on this thread. I am not forcing you to make changes. But as a maintainer of many packages, I would think is even breaking change is worth it to make things correct and future-proofing. |
@antfu How would you address the following concerns?
If we can find a solution that really solves/eliminates a problem, that would be great. |
Resolved in v14.0.0-beta.4. |
Sadly the solution does not "just work". preset: 'ts-jest/presets/default-esm',
globals: {
'ts-jest': {
useESM: true,
},
}, |
🎉 This issue has been resolved in version 14.0.0-beta.5 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@testing-library/user-event
version: 13.5.0Relevant code or config
When using Node's native dynamic import, nested default is exported, causing the resolving to fail.
Related vitest-dev/vitest#243
The text was updated successfully, but these errors were encountered: