Skip to content

Ships wrong types #962

@lishaduck

Description

@lishaduck

I'm trying to import zx from a cjs file (sorry 😔), but it only ships ESM declarations files, so TypeScript errors upon import. Here's the attw report, which agrees that zx is masquerading as ESM-only instead of dual.
Unfortunately, the TS team feel's it's not tsc's place to rewrite module syntax anymore, and hasn't had plans for extending dual packaging for a while, although it might still happen someday (though with require(esm), it's probably a lesser concern). I'd happy to rewrite your build scripts to fix this, but my Google account doesn't let me use sign in with Google for arbitrary apps, so I'm unable to sign the CLA. I'd be happy to review a PR or give recommendations, I just can't hit the button.


Personally, I'd recommend tsdown as a good, modern bundler. It's new, so it's not perfect, but I've found it less buggy than what tsup outputs (both require node18, but can compile down). tsup and tsdown have similar configuration, here's what I've found works best for packages:
You'll want to ensure you have ["esm", "cjs"] specified for format (dual emit), target set to node12 (based on your engines), clean set to true, platform set to node, skipNodeModulesBundle set to true (very important!), shims set to true (optional, enable only if needed), dts set to true (for tsup) or { transformer: 'oxc', autoAddExts: true, sourceMap: true } (for tsdown). If you want sourcemaps, consider sourcemap: true. For tsdown, also consider enabling unused: { level: 'error' } to warn on extraneous dependencies not used in the build.


I take most of that back. Y'all try to keep the package size down by using ESM wrappers of a CJS build, which is commendable. However, ESM can import CJS. Is there a reason to do the "fake" dual-emit? I also know require(esm) has changed some of the recommendations around using these wrapper scripts, though I don't know what specifically.

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