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

TypeScript library with "type": "module" generating cjs declarations with .d.ts suffix #208

Closed
NexZhu opened this issue Jun 8, 2023 · 6 comments

Comments

@NexZhu
Copy link

NexZhu commented Jun 8, 2023

I have a TypeScript library with "type": "module" , I want to build both ESM and CommonJS output.

package.json:

  "packemon": {
    "format": [
      "esm",
      "cjs"
    ],
    "platform": [
      "node",
      "browser"
    ],
    "api": "public"
  },

Generated esm output looks fine but the cjs output also has declarations with .d.ts suffixes instead of .d.cts.
image

This will cause problems in CommonJS project importing the library.
See:
microsoft/TypeScript#50466 (comment)
microsoft/TypeScript#50466 (comment)

@milesj
Copy link
Owner

milesj commented Jun 8, 2023

@NexZhu The .d.cts extension only works if your source files are .cts and not .ts. This is a TS thing, not a Packemon thing.

@NexZhu
Copy link
Author

NexZhu commented Jun 10, 2023

@milesj I understand TS doesn't handle that, but I think this tool can do it by renaming file extensions. Because but hybrid package with both ESM and CommonJS build output won't be useful without .cts extensions because with "type": "module" in package.json, even if you have:

  "exports": {
    "./some-path": {
      "import": {
        "default": "./dist/esm/some-path/index.js",
        "types": "./dist/esm/some-path/index.d.ts"
      },
      "require": {
        "default": "./dist/cjs/some-path/index.cjs", # or .js doesn't matter
        "types": "./dist/cjs/some-path/index.d.ts" # This will cause issue if used in CommonJS + TypeScript project
      }
    }
  },

If you try to import this library from a CommonJS + TypeScript project like:

import { xxx } from 'library-name/some-path'

You will get this error, making it not usable from CommonJS + TypeScript project:
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require' (ts1479)

The links I posted above explain the issue in detail.

So, what I'm thinking is, for this tool to be useful for building hybrid package with both ESM and CommonJS build output, it'd better take care of outputting the .cts extensions for CommonJS by renaming files maybe.

@NexZhu
Copy link
Author

NexZhu commented Jun 12, 2023

Some more discussion here FYI
arethetypeswrong/arethetypeswrong.github.io#21

@milesj
Copy link
Owner

milesj commented Jun 16, 2023

@NexZhu Not sure I feel about this since it feels like a "hack" but I'll support it anyways through a setting.

@milesj
Copy link
Owner

milesj commented Jun 21, 2023

@NexZhu Starting on this here: #213

Can you take a look and see if that solves your problem?

@NexZhu
Copy link
Author

NexZhu commented Jun 22, 2023

Yes. Thanks!

@NexZhu NexZhu closed this as completed Jun 22, 2023
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

2 participants