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

Conditional import file if output is cjs or esm? #579

Open
hazzo opened this issue Sep 21, 2023 · 0 comments
Open

Conditional import file if output is cjs or esm? #579

hazzo opened this issue Sep 21, 2023 · 0 comments

Comments

@hazzo
Copy link

hazzo commented Sep 21, 2023

Is there a way of conditional importing different kind of files depending on the output bundle, if esm or cjs?

For example, I've done a package that uses class-transformer as peer dependency that uses an unexposed API by importing it directly from dist directory, which is a cjs bundle. The issue arises because class-transformer has both cjs and esm builds, so when you use both class-transformer and my package in a esm bundle, my package will always import class-transformer cjs file but class-transformer will uses internally the esm one when is been used. So we can't share the singleton instance that's created inside of the unexposed API.

It can be easily fixed in the webpack config that imports both packages by resolving aliases to a specific class-transformer bundle, the cjs one. But my idea was if maybe in my package I could import the file from dist from a bundle or the other depending on what build target I will be bundling, either esm or cjs.

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

1 participant