Skip to content

Ships wrong types #962

Closed
Closed

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.

Activity

  1. antongolub commented on Dec 9, 2024

    @antongolub
    Collaborator

    Keep in mind, zx relies on external deps, some of them are distributed as esm modules only. These libdefs cannot be used in .d.cts without additional modifications.

  2. locked and limited conversation to collaborators on Dec 9, 2024
  3. converted this issue into a discussion #963 on Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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