Skip to content

Commit

Permalink
fix: ESM and CJS type definitions conflict
Browse files Browse the repository at this point in the history
The recent lingui#1742 explicitly exports TypeScript type definitions
from package.json:

```json
  "exports": {
     ".": {
       "require": {
         "types": "./dist/index.d.ts",
         "default": "./dist/index.cjs"
       },
       "import": {
         "types": "./dist/index.d.ts",
         "default": "./dist/index.mjs"
       }
     },
     "./package.json": "./package.json"
   },
```

Unfortunately, [as documented in the TypeScript Handbook][handbook],
CommonJS (`require`) and ESM (`import`) module versions
cannot share the same index.d.ts file:

> It’s important to note that the CommonJS entrypoint and the ES module entrypoint each needs its own declaration file, even if the contents are the same between them. Every declaration file is interpreted either as a CommonJS module or as an ES module, based on its file extension and the "type" field of the package.json, and this detected module kind must match the module kind that Node will detect for the corresponding JavaScript file for type checking to be correct. Attempting to use a single .d.ts file to type both an ES module entrypoint and a CommonJS entrypoint will cause TypeScript to think only one of those entrypoints exists, causing compiler errors for users of the package.

I am seeing the compiler errors that this documentation warns of.

This issue appears to have been recently fixed in
the unbuild build tool that js-lingui depends upon
(see issue unjs/unbuild#238
and fix unjs/unbuild#273),
and that fix was released in [unbuild 2.0.0-rc0].

This fix for detect-locale, therefore, is to upgrade to unbuild 2.0.0,
and use the separate .d.cts and .d.mts type declaration files
it outputs.

**Note:** This moves detect-locale onto a newer major version of unbuild
than the other packages in this monorepo.
It may be preferred to upgrade them all,
but I am not familiar enough with the other packages to do this quickly.

[handbook]: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing
[unbuild 2.0.0-rc0]: https://github.com/unjs/unbuild/blob/main/CHANGELOG.md#v200-rc0
  • Loading branch information
sentience committed Aug 24, 2023
1 parent 47d4fdb commit e73b59a
Show file tree
Hide file tree
Showing 2 changed files with 1,014 additions and 12 deletions.
6 changes: 3 additions & 3 deletions packages/detect-locale/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"exports": {
".": {
"require": {
"types": "./dist/index.d.ts",
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.ts",
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
},
Expand All @@ -54,6 +54,6 @@
],
"devDependencies": {
"jsdom": "^16.4.0",
"unbuild": "^1.1.2"
"unbuild": "^2.0.0"
}
}
Loading

0 comments on commit e73b59a

Please sign in to comment.