Skip to content
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

Allow CJS to expose the default export with require(...) instead of require(...).default #239

Open
bgrins opened this issue Dec 20, 2022 · 4 comments

Comments

@bgrins
Copy link

bgrins commented Dec 20, 2022

Hi, I've been trying to migrate the TinyColor library to use Deno and cross compile to Node (CJS and ESM). It's all working fine (bgrins/TinyColor#241), except for one problem:

The generated CJS exports the object on default when requiring (https://github.com/bgrins/TinyColor/blob/bcfcd1d49181a45fab2dd1858ef22890adba1809/npm/script/mod.js#L50) such that to access it I need to do var tinycolor = require("tinycolor2").default whereas the current behavior and external API works like var tinycolor = require("tinycolor2");. I don't want to change the existing API as the library is used by a lot of people - is there a way to change the output from dnt to match the current behavior?

My best guess was that it's the esModuleInterop option at https://github.com/denoland/dnt/blob/main/mod.ts#L348 which is causing this behavior, but the generated test output also expects to pull the object out of .default so maybe there would need to be more changes as well.

@bgrins
Copy link
Author

bgrins commented Dec 20, 2022

I checked with unsetting esModuleInterop but no luck. I guess as per the UMD at https://github.com/bgrins/TinyColor/blob/e3b7bdde11e05fe520f0ba63059d4b929a227279/npm/script/mod.js#L6 if the factory function returns an object it will set module.exports to that, which would work. Maybe there's a way to make that happen based on how things get exported in the module or some configuration in the build?

@uncenter
Copy link

I'm wondering why this is still an issue a year later! Forcing CJS imports to use the default property is a major drawback and unexpected for users.

@dsherret
Copy link
Member

dsherret commented Sep 16, 2023

I'm wondering why this is still an issue a year later!

@uncenter it's a much harder problem than you might expect. TypeScript does not provide a way (to my knowledge) to transform an esm default export to a module.exports = ... so that people can do const mod = require(...). Please open a PR if you know a solution and have the time. Thanks!

@uncenter
Copy link

I'm wondering why this is still an issue a year later!

@uncenter it's a much harder problem than you might expect. TypeScript does not provide a way (to my knowledge) to transform an esm default export to a module.exports = ... so that people can do const mod = require(...). Please open a PR if you know a solution and have the time. Thanks!

Sorry I didn't mean to come off rudely. I was just surprised that there wasn't any activity on the issue :)

I don't know an exact solution but this isn't a problem in every bundler so I'm guessing there must be a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants