Skip to content

Commit b77bc39

Browse files
joshkeladuth
andauthored
Fix types for .cjs build (#16)
* Fix types for .cjs build [Are the Types Wrong](https://arethetypeswrong.github.io/?p=memize%402.1.0) is currently reporting errors for memize: > Problems > > * ❌ No types: Import resolved to JavaScript files, but no type declarations were found. > > > ||"memize" > |---|---| > |node10 |✅| > |node16 (from CJS) |❌ No types| > |node16 (from ESM) |✅ (ESM)| > |bundler |✅| For more information, see Are the Types Wrong's [UntypedResolution.md](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UntypedResolution.md). Note that the solution is not to point both the CJS and ESM builds at the same `.d.ts` file, for reasons explained in Are the Types Wrong's [FalseESM.md](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md). Quoting that page: > A golden rule of declaration files is that if they represent a module—that is, if they use import or export at the top level—they must represent exactly one JavaScript file. They _especially_ cannot represent JavaScript files of two different module formats. The easiest solution I found was to run `tsc` a second time to build a `.d.cts` file. For this second invocation, I pointed it at the Rollup-generated `.cjs` file. (This is because tsc and Rollup by default handle default exports differently for CJS files: tsc translates `export default foo` to a `default` attribute on an object with `__esModule: true` and requires that you instead use `export =` for a "true" CommonJS export, while Rollup [by default](https://rollupjs.org/configuration-options/#output-exports) creates a "true" default export. By pointing tsc at the Rollup-generated `.cjs` file, tsc sees Rollup's `export =`.) * Remove no longer needed `types` Co-authored-by: Andrew Duthie <[email protected]> * Remove more redundant `types` entries Since we follow the default extensions, we shouldn't need to specify `types`. --------- Co-authored-by: Andrew Duthie <[email protected]>
1 parent d461490 commit b77bc39

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
"import": "./dist/index.js",
99
"require": "./dist/index.cjs"
1010
},
11-
"types": "dist/index.d.ts",
1211
"scripts": {
1312
"build": "npm run build:bundle && npm run build:types",
1413
"build:bundle": "rollup -c rollup.config.js",
15-
"build:types": "tsc -b tsconfig.decl.json",
14+
"build:types": "tsc -b tsconfig.decl.json && tsc -b tsconfig.decl.cts.json",
1615
"test:unit": "NODE_ENV=test mocha",
1716
"test:lint": "eslint .",
1817
"test:types": "tsc -b",

tsconfig.decl.cts.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"noEmit": false,
6+
"declaration": true,
7+
"emitDeclarationOnly": true
8+
},
9+
"files": [
10+
"dist/index.cjs"
11+
]
12+
}

0 commit comments

Comments
 (0)