From dd97d32a56f29ac81e36b6ca66e0dbbf92bfbd55 Mon Sep 17 00:00:00 2001 From: Osama Ansar Date: Wed, 16 Sep 2026 12:54:13 +0500 Subject: [PATCH] fix: nest types under import/require in exports maps (fixes #182) TypeScript's Node16/NodeNext resolver matches exports conditions by JSON key order against the active condition set. With a top-level `types` key sitting as a sibling of `import`/`require`, `types` always won regardless of which module system was actually being resolved - so a CJS consumer's `require` would still get pointed at the ESM `.d.ts`, which then recursively resolves its own imports in ESM mode and produces TS1479 ("this file is CommonJS ... but the referenced file is an ECMAScript module"). Fixes it in both @libsql/client and @libsql/core (the bug affects @libsql/core's own exports map too, and @libsql/client's CJS entry imports from it) by nesting `types` inside each `import`/`require` block instead, and flipping `declaration: false` -> `true` in each package's tsconfig.build-cjs.json so lib-cjs actually has .d.ts files for the `require` condition to point at - the previous shape assumed CJS types could just reuse the ESM ones, which is exactly what broke CJS resolution. Verified against the issue's own reproduction (a Node16-resolution CJS+TS consumer importing `@libsql/client`): `tsc --noEmit` failed with TS1479 before this change and passes after it. A parallel ESM consumer was checked too, to confirm no regression there. The full jest suite is unaffected: same 183 passing / 14 pre-existing failures before and after (those 14 are unrelated Windows temp-dir EPERM issues in the test harness, reproduced identically on unmodified main). --- packages/libsql-client/package.json | 62 ++++++++++++++----- .../libsql-client/tsconfig.build-cjs.json | 2 +- packages/libsql-core/package.json | 44 +++++++++---- packages/libsql-core/tsconfig.build-cjs.json | 2 +- 4 files changed, 79 insertions(+), 31 deletions(-) diff --git a/packages/libsql-client/package.json b/packages/libsql-client/package.json index 4265f721..0e39ca76 100644 --- a/packages/libsql-client/package.json +++ b/packages/libsql-client/package.json @@ -27,8 +27,8 @@ "types": "lib-esm/node.d.ts", "exports": { ".": { - "types": "./lib-esm/node.d.ts", "import": { + "types": "./lib-esm/node.d.ts", "workerd": "./lib-esm/web.js", "deno": "./lib-esm/node.js", "edge-light": "./lib-esm/web.js", @@ -37,32 +37,60 @@ "browser": "./lib-esm/web.js", "default": "./lib-esm/node.js" }, - "require": "./lib-cjs/node.js" + "require": { + "types": "./lib-cjs/node.d.ts", + "default": "./lib-cjs/node.js" + } }, "./node": { - "types": "./lib-esm/node.d.ts", - "import": "./lib-esm/node.js", - "require": "./lib-cjs/node.js" + "import": { + "types": "./lib-esm/node.d.ts", + "default": "./lib-esm/node.js" + }, + "require": { + "types": "./lib-cjs/node.d.ts", + "default": "./lib-cjs/node.js" + } }, "./http": { - "types": "./lib-esm/http.d.ts", - "import": "./lib-esm/http.js", - "require": "./lib-cjs/http.js" + "import": { + "types": "./lib-esm/http.d.ts", + "default": "./lib-esm/http.js" + }, + "require": { + "types": "./lib-cjs/http.d.ts", + "default": "./lib-cjs/http.js" + } }, "./ws": { - "types": "./lib-esm/ws.d.ts", - "import": "./lib-esm/ws.js", - "require": "./lib-cjs/ws.js" + "import": { + "types": "./lib-esm/ws.d.ts", + "default": "./lib-esm/ws.js" + }, + "require": { + "types": "./lib-cjs/ws.d.ts", + "default": "./lib-cjs/ws.js" + } }, "./sqlite3": { - "types": "./lib-esm/sqlite3.d.ts", - "import": "./lib-esm/sqlite3.js", - "require": "./lib-cjs/sqlite3.js" + "import": { + "types": "./lib-esm/sqlite3.d.ts", + "default": "./lib-esm/sqlite3.js" + }, + "require": { + "types": "./lib-cjs/sqlite3.d.ts", + "default": "./lib-cjs/sqlite3.js" + } }, "./web": { - "types": "./lib-esm/web.d.ts", - "import": "./lib-esm/web.js", - "require": "./lib-cjs/web.js" + "import": { + "types": "./lib-esm/web.d.ts", + "default": "./lib-esm/web.js" + }, + "require": { + "types": "./lib-cjs/web.d.ts", + "default": "./lib-cjs/web.js" + } } }, "typesVersions": { diff --git a/packages/libsql-client/tsconfig.build-cjs.json b/packages/libsql-client/tsconfig.build-cjs.json index 8feb91c5..9f694bcf 100644 --- a/packages/libsql-client/tsconfig.build-cjs.json +++ b/packages/libsql-client/tsconfig.build-cjs.json @@ -2,7 +2,7 @@ "extends": "./tsconfig.base.json", "compilerOptions": { "module": "commonjs", - "declaration": false, + "declaration": true, "outDir": "./lib-cjs/" } } diff --git a/packages/libsql-core/package.json b/packages/libsql-core/package.json index f4d10443..64970b64 100644 --- a/packages/libsql-core/package.json +++ b/packages/libsql-core/package.json @@ -25,24 +25,44 @@ "type": "module", "exports": { "./api": { - "types": "./lib-esm/api.d.ts", - "import": "./lib-esm/api.js", - "require": "./lib-cjs/api.js" + "import": { + "types": "./lib-esm/api.d.ts", + "default": "./lib-esm/api.js" + }, + "require": { + "types": "./lib-cjs/api.d.ts", + "default": "./lib-cjs/api.js" + } }, "./config": { - "types": "./lib-esm/config.d.ts", - "import": "./lib-esm/config.js", - "require": "./lib-cjs/config.js" + "import": { + "types": "./lib-esm/config.d.ts", + "default": "./lib-esm/config.js" + }, + "require": { + "types": "./lib-cjs/config.d.ts", + "default": "./lib-cjs/config.js" + } }, "./uri": { - "types": "./lib-esm/uri.d.ts", - "import": "./lib-esm/uri.js", - "require": "./lib-cjs/uri.js" + "import": { + "types": "./lib-esm/uri.d.ts", + "default": "./lib-esm/uri.js" + }, + "require": { + "types": "./lib-cjs/uri.d.ts", + "default": "./lib-cjs/uri.js" + } }, "./util": { - "types": "./lib-esm/util.d.ts", - "import": "./lib-esm/util.js", - "require": "./lib-cjs/util.js" + "import": { + "types": "./lib-esm/util.d.ts", + "default": "./lib-esm/util.js" + }, + "require": { + "types": "./lib-cjs/util.d.ts", + "default": "./lib-cjs/util.js" + } } }, "typesVersions": { diff --git a/packages/libsql-core/tsconfig.build-cjs.json b/packages/libsql-core/tsconfig.build-cjs.json index 8feb91c5..9f694bcf 100644 --- a/packages/libsql-core/tsconfig.build-cjs.json +++ b/packages/libsql-core/tsconfig.build-cjs.json @@ -2,7 +2,7 @@ "extends": "./tsconfig.base.json", "compilerOptions": { "module": "commonjs", - "declaration": false, + "declaration": true, "outDir": "./lib-cjs/" } }