Skip to content

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

@hazzo

Description

@hazzo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions