From 760a8983b9d780601e7c6e2cb2c867f45f55f25f Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Mon, 9 Dec 2024 22:58:10 +0100 Subject: [PATCH] feat: Improve TS configs and add library types --- .gitignore | 3 ++ docs/prettier.md | 13 +++++++ docs/typescript.md | 15 +++++++ package.json | 84 ++++++++++++++++++++++++++++++++-------- prettier/base.js | 5 ++- semantic-release/base.js | 5 ++- tsconfig.json | 2 +- typescript/base.json | 5 ++- typescript/js-lib.json | 9 +++++ typescript/lib.json | 11 ++++-- 10 files changed, 128 insertions(+), 24 deletions(-) create mode 100644 docs/prettier.md create mode 100644 docs/typescript.md create mode 100644 typescript/js-lib.json diff --git a/.gitignore b/.gitignore index 123ae94..f3ba8c8 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules + +# Generated by TypeScript +types \ No newline at end of file diff --git a/docs/prettier.md b/docs/prettier.md new file mode 100644 index 0000000..4dee6f5 --- /dev/null +++ b/docs/prettier.md @@ -0,0 +1,13 @@ +# Peerigon's Prettier config + +## Install + +```sh +npm install prettier@3 --save-dev +``` + +Add a `prettier.config.js` to your project: + +``` + +``` diff --git a/docs/typescript.md b/docs/typescript.md new file mode 100644 index 0000000..4ddbfa3 --- /dev/null +++ b/docs/typescript.md @@ -0,0 +1,15 @@ +## Reasoning + +### `noUncheckedIndexedAccess` + +- Mention tiny-invariant + +### `noPropertyAccessFromIndexSignature` + +### `exactOptionalPropertyTypes` + +### `verbatimModuleSyntax` + +### `allowImportingTsExtensions` + +### `rewriteRelativeImportExtensions` diff --git a/package.json b/package.json index 4af5b19..dde3967 100644 --- a/package.json +++ b/package.json @@ -21,26 +21,74 @@ "author": "Peerigon GmbH ", "type": "module", "exports": { - "./eslint/presets/javascript-browser": "./eslint/presets/javascript-browser.js", - "./eslint/presets/javascript-node": "./eslint/presets/javascript-node.js", - "./eslint/presets/javascript": "./eslint/presets/javascript.js", - "./eslint/presets/typescript-node": "./eslint/presets/typescript-node.js", - "./eslint/presets/typescript-react": "./eslint/presets/typescript-react.js", - "./eslint/presets/typescript": "./eslint/presets/typescript.js", - "./eslint/styles/jsx-no-literals": "./eslint/styles/jsx-no-literals.js", - "./eslint/styles/no-default-export": "./eslint/styles/no-default-export.js", - "./eslint/styles/no-null": "./eslint/styles/no-null.js", - "./eslint/styles/prefer-array-shorthand": "./eslint/styles/prefer-array-shorthand.js", - "./eslint/styles/prefer-interface": "./eslint/styles/prefer-interface.js", - "./prettier": "./prettier/base.js", - "./semantic-release": "./semantic-release/base.js", - "./typescript": "./typescript/base.json", - "./typescript/lib": "./typescript/lib.json" + "./eslint/presets/javascript-browser": { + "types": "./types/eslint/presets/javascript-browser.d.ts", + "default": "./eslint/presets/javascript-browser.js" + }, + "./eslint/presets/javascript-node": { + "types": "./types/eslint/presets/javascript-node.d.ts", + "default": "./eslint/presets/javascript-node.js" + }, + "./eslint/presets/javascript": { + "types": "./types/eslint/presets/javascript.d.ts", + "default": "./eslint/presets/javascript.js" + }, + "./eslint/presets/typescript-node": { + "types": "./types/eslint/presets/typescript-node.d.ts", + "default": "./eslint/presets/typescript-node.js" + }, + "./eslint/presets/typescript-react": { + "types": "./types/eslint/presets/typescript-react.d.ts", + "default": "./eslint/presets/typescript-react.js" + }, + "./eslint/presets/typescript": { + "types": "./types/eslint/presets/typescript.d.ts", + "default": "./eslint/presets/typescript.js" + }, + "./eslint/styles/jsx-no-literals": { + "types": "./types/eslint/styles/jsx-no-literals.d.ts", + "default": "./eslint/styles/jsx-no-literals.js" + }, + "./eslint/styles/no-default-export": { + "types": "./types/eslint/styles/no-default-export.d.ts", + "default": "./eslint/styles/no-default-export.js" + }, + "./eslint/styles/no-null": { + "types": "./types/eslint/styles/no-null.d.ts", + "default": "./eslint/styles/no-null.js" + }, + "./eslint/styles/prefer-array-shorthand": { + "types": "./types/eslint/styles/prefer-array-shorthand.d.ts", + "default": "./eslint/styles/prefer-array-shorthand.js" + }, + "./eslint/styles/prefer-interface": { + "types": "./types/eslint/styles/prefer-interface.d.ts", + "default": "./eslint/styles/prefer-interface.js" + }, + "./prettier": { + "types": "./types/prettier/base.d.ts", + "default": "./prettier/base.js" + }, + "./semantic-release": { + "types": "./types/semantic-release/base.d.ts", + "default": "./semantic-release/base.js" + }, + "./typescript": { + "default": "./typescript/base.json" + }, + "./typescript/lib": { + "default": "./typescript/lib.json" + }, + "./typescript/js-lib": { + "default": "./typescript/js-lib.json" + } }, "files": [ + "docs", "eslint", "prettier", "semantic-release", + "types", "typescript", "CHANGELOG.md", "README.md" @@ -55,8 +103,11 @@ "test:styles:no-null": "cd eslint/styles/no-null.test; eslint --max-warnings 0 .", "test:styles:prefer-array-shorthand": "cd eslint/styles/prefer-array-shorthand.test; eslint --max-warnings 0 .", "test:styles:prefer-interface": "cd eslint/styles/prefer-interface.test; eslint --max-warnings 0 .", - "test:types": "tsc", + "test:types": "tsc --noEmit", "test:prettier": "prettier --check .", + "build": "run-s build:*", + "build:types": "tsc", + "prepublishOnly": "npm run build", "prepare": "husky", "release": "semantic-release" }, @@ -107,6 +158,7 @@ }, "peerDependencies": { "eslint": "^9.0.0", + "prettier": "^3.3.3", "typescript": "^5.6.3" }, "publishConfig": { diff --git a/prettier/base.js b/prettier/base.js index 29e200e..2192f5f 100644 --- a/prettier/base.js +++ b/prettier/base.js @@ -18,7 +18,8 @@ const plugins = await Promise.all([ safeResolve("prettier-plugin-tailwindcss"), ]); -export default { +/** @type {import("prettier").Config} */ +export const config = { plugins, importOrderParserPlugins: [ "typescript", @@ -28,3 +29,5 @@ export default { ], importOrderTypeScriptVersion: "5.0.0", }; + +export default config; diff --git a/semantic-release/base.js b/semantic-release/base.js index 535a374..cb7ab35 100644 --- a/semantic-release/base.js +++ b/semantic-release/base.js @@ -1,4 +1,5 @@ -export default { +/** @type {import("semantic-release").Options} */ +export const config = { branches: ["main", { name: "beta", prerelease: true }], plugins: [ "@semantic-release/commit-analyzer", @@ -36,3 +37,5 @@ export default { ], ], }; + +export default config; diff --git a/tsconfig.json b/tsconfig.json index 75ccc97..60419d9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "./typescript/base.json", + "extends": "./typescript/js-lib.json", "include": ["eslint", "prettier", "typescript", "semantic-release"], "exclude": [ "eslint/presets/typescript.test", diff --git a/typescript/base.json b/typescript/base.json index 9842e5f..5120699 100644 --- a/typescript/base.json +++ b/typescript/base.json @@ -7,7 +7,6 @@ "isolatedModules": true, "lib": ["DOM", "ESNext"], "module": "NodeNext", - "noEmit": true, // If code should be emitted, it's recommended to create a separate tsconfig.build.json file "noImplicitOverride": true, "noImplicitReturns": true, "noPropertyAccessFromIndexSignature": true, @@ -16,6 +15,10 @@ "skipLibCheck": true, "strict": true, "target": "ESNext" + // TODO: Turn on the following settings once TS 5.7 is released, see also https://nodejs.org/docs/latest/api/typescript.html#typescript-features + // "allowImportingTsExtensions": true, + // "rewriteRelativeImportExtensions": true, + // "verbatimModuleSyntax": true }, "$schema": "https://json.schemastore.org/tsconfig" } diff --git a/typescript/js-lib.json b/typescript/js-lib.json new file mode 100644 index 0000000..3f96cbf --- /dev/null +++ b/typescript/js-lib.json @@ -0,0 +1,9 @@ +{ + "extends": "./lib.json", + "compilerOptions": { + "allowJs": true, + "emitDeclarationOnly": true, + "outDir": "${configDir}/types" + }, + "$schema": "https://json.schemastore.org/tsconfig" +} diff --git a/typescript/lib.json b/typescript/lib.json index e754d7b..51d6acb 100644 --- a/typescript/lib.json +++ b/typescript/lib.json @@ -1,7 +1,10 @@ { - "extends": "./typescript.json", + "extends": "./base.json", "compilerOptions": { - "sourceMap": true, - "declaration": true - } + "declaration": true, + "declarationMap": true, + "outDir": "${configDir}/dist", + "sourceMap": true + }, + "$schema": "https://json.schemastore.org/tsconfig" }