From 5428e4bbda1469ec6a9e7d09e70b26524cb7e93b Mon Sep 17 00:00:00 2001 From: Micah Zoltu Date: Wed, 15 Jan 2025 13:40:33 +0800 Subject: [PATCH] Fixes `_types` generation and modernizes module resolution. Currently, the `_types` is built using `node` module resolution, which results in this project generating invalid types. Details about the TypeScript bug can be found at https://github.com/microsoft/TypeScript/issues/60930#issue-2773343774 and there are no plans to fix it since `node` (which is equivalent to `node10`) is being deprecated in TypeScript soon. The way this bug manifests is in files like `src/_types/core/P256.d.ts` where it would previously generate code like: ```ts create: (hash: import("@noble/curves/abstract/utils.js").CHash) => import("@noble/curves/abstract/weierstrass.js").CurveFn; ``` when it should have generated code like this (which is what is generated after this change): ```ts create: (hash: import("@noble/curves/abstract/utils").CHash) => import("@noble/curves/abstract/weierstrass").CurveFn; ``` For the cjs build command, I changed from `node` to `node10` to be more explicit/clear, since `node` was just an alias for `node10`. For the esm build command, I changed it to use the tsconfig so it is less likely to get out of sync with the generated types. Also updated .gitignore to ignore `.pnpm-store` which had 30,000 files it wanted to commit after I did `pnpm install` using the recommended version of `pnpm`. --- .gitignore | 1 + package.json | 6 +++--- tsconfig.base.json | 4 ++-- tsconfig.build.json | 1 - 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8053dd48..740bd089 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .attest .env .local +.pnpm-store _esm _cjs _types diff --git a/package.json b/package.json index 55ea5e57..580b4d9b 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,9 @@ "bench": "vitest -c ./test/vitest.config.ts bench", "bench:types": "TYPES=true vitest -c ./test/vitest.config.ts src/**/*.bench-d.ts", "build": "pnpm clean && pnpm build:cjs && pnpm build:esm && pnpm build:types", - "build:esm": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./src/_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./src/_esm/package.json", - "build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./src/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./src/_cjs/package.json", - "build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./src/_types --emitDeclarationOnly --declaration --declarationMap", + "build:esm": "tsc --project ./tsconfig.build.json --outDir ./src/_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./src/_esm/package.json", + "build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --moduleResolution node10 --outDir ./src/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./src/_cjs/package.json", + "build:types": "tsc --project ./tsconfig.build.json --declarationDir ./src/_types --emitDeclarationOnly --declaration --declarationMap", "changeset:prepublish": "pnpm version:update && pnpm build && tsx scripts/prepublish.ts", "changeset:publish": "pnpm changeset:prepublish && changeset publish", "changeset:version": "changeset version && pnpm version:update && pnpm format", diff --git a/tsconfig.base.json b/tsconfig.base.json index d3cda538..091cb5ed 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -30,8 +30,8 @@ "importHelpers": true, // This is only used for build validation. Since we do not have `tslib` installed, this will fail if we accidentally make use of anything that'd require injection of helpers. // Language and environment - "moduleResolution": "NodeNext", - "module": "NodeNext", + "moduleResolution": "nodenext", + "module": "nodenext", "target": "ES2021", // Setting this to `ES2021` enables native support for `Node v16+`: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping. "lib": [ "ES2022", // By using ES2022 we get access to the `.cause` property on `Error` instances. diff --git a/tsconfig.build.json b/tsconfig.build.json index 362badcb..d3c59392 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -10,7 +10,6 @@ "src/**/*.test-d.ts" ], "compilerOptions": { - "moduleResolution": "node", "sourceMap": true, "rootDir": "./src" }