-
Notifications
You must be signed in to change notification settings - Fork 42
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
Why doesn't rollup (and other tooling) insert the node16 compatibility layer during ESM->CJS transpilation? #109
Comments
Note that I think I’m going to silence this error in
So I might leave it as a problem for I don’t know the answer to your question. The compatibility layer does change the semantics of the code in a way that is observably different from real ESM imports. The docs page mentions that you’ll be able to do |
I think my practical advice would be
|
Thank you for the information. export = defaultExport;
declare namespace defaultExport {
export {
// Other exports
};
} ...along with the .cjs file containing both exports.default, and exports.__esModule (transpiled by rollup). It seems like removing the default export is the easiest option, but I am curious to know exactly how it works :) |
esbuild and Webpack both go out of their way to act more like Node.js when they see Node ESM cues like But I think that won’t affect you here, because Webpack and esbuild, like ESM files in Node.js, should resolve to the ESM entrypoint of your package. I think there’s no problem. I definitely plan to make some updates soon to arethetypeswrong to do a better job of analyzing and calling out exactly what the potential problem is here. Is the project you’re working on available as a test case, by any chance? |
Interesting. The only problematic tool I can think of is Jest, as ESM support just entered experimental stage, but I am not familiar in exactly how it's resolution works (but did encounter .default problem in various packages). The project I am working on is single-spa-react, specifically, this branch/PR is what I'm working on: single-spa/single-spa-react#197 . Feel free to use it. I was thinking of setting up, and open-sourcing a comprehensive integration test suite with common tooling, and resolutions. What do you think? |
Sounds great! |
My existing fixtures already had some coverage for the changes I made, so I decided not to try to turn your PR into a test. But you may find with the latest version of @arethetypeswrong/cli, you don’t need to add the manual compat code. Let me know how it works out 👍 |
I encountered a node16 (cjs) resolution mode problem after the transpilation of ESM module to CJS with rollup, and had to resort to inserting the compatibility layer in the code itself:
I am curious as to why some tools don't insert the compatibility layer, since they already set the exports.default and exports.__esModule?
I was thinking about building a rollup plugin to insert it, but would like to know if there are any caveats/downsides.
The text was updated successfully, but these errors were encountered: