From 53039fdd91210bf7e99fdff8e0003b8e57938ef7 Mon Sep 17 00:00:00 2001 From: zanminkian Date: Sun, 17 Sep 2023 18:49:29 +0000 Subject: [PATCH] feat(tsconfig): finish --- .changeset/good-books-taste.md | 5 ++ package.json | 2 +- packages/tsconfig/README.md | 149 ++++++++++++++++++++++++++++++++ packages/tsconfig/cjs.json | 48 ++++++++++ packages/tsconfig/esm.json | 48 ++++++++++ packages/tsconfig/legacy.json | 47 ++++++++++ packages/tsconfig/package.json | 30 +++++++ packages/tsconfig/src/cli.js | 42 +++++++++ packages/tsconfig/tsconfig.json | 3 + tsconfig.json | 2 +- 10 files changed, 374 insertions(+), 2 deletions(-) create mode 100644 .changeset/good-books-taste.md create mode 100644 packages/tsconfig/README.md create mode 100644 packages/tsconfig/cjs.json create mode 100644 packages/tsconfig/esm.json create mode 100644 packages/tsconfig/legacy.json create mode 100644 packages/tsconfig/package.json create mode 100755 packages/tsconfig/src/cli.js create mode 100644 packages/tsconfig/tsconfig.json diff --git a/.changeset/good-books-taste.md b/.changeset/good-books-taste.md new file mode 100644 index 00000000..1f09fd6b --- /dev/null +++ b/.changeset/good-books-taste.md @@ -0,0 +1,5 @@ +--- +"@git-validator/tsconfig": minor +--- + +feat(tsconfig): finish diff --git a/package.json b/package.json index 8939a278..ddacd668 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "devDependencies": { "@changesets/cli": "^2.26.2", "@types/node": "^20.6.2", - "@zanminkian/tsconfig": "^0.9.0", + "@git-validator/tsconfig": "workspace:^", "git-validator": "workspace:^", "typescript": "^5.2.2" } diff --git a/packages/tsconfig/README.md b/packages/tsconfig/README.md new file mode 100644 index 00000000..949e3bd4 --- /dev/null +++ b/packages/tsconfig/README.md @@ -0,0 +1,149 @@ +# @git-validator/tsconfig + +Strict shared tsconfig out-of-box + +[![](https://img.shields.io/npm/l/@git-validator/tsconfig.svg)](https://github.com/zanminkian/git-validator/blob/main/LICENSE) +[![](https://img.shields.io/npm/v/@git-validator/tsconfig.svg)](https://www.npmjs.com/package/@git-validator/tsconfig) +[![](https://img.shields.io/npm/dm/@git-validator/tsconfig.svg)](https://www.npmjs.com/package/@git-validator/tsconfig) +[![](https://img.shields.io/librariesio/release/npm/@git-validator/tsconfig)](https://www.npmjs.com/package/@git-validator/tsconfig) +[![](https://packagephobia.com/badge?p=@git-validator/tsconfig)](https://packagephobia.com/result?p=@git-validator/tsconfig) + +## Feature + +- Strictest configs with best practices. +- One-line of tsconfig. +- Support `ESM` and `CommonJS` by `type` field in `package.json`. +- Support FE (eg: [React](https://github.com/facebook/react)) & BE (eg: [Nest](https://github.com/nestjs/nest)) project. + +## Requirement + +- Typescript 5.0+. +- Node 16+. + +## Usage + +### Install + +```sh +npm i @git-validator/tsconfig -D +``` + +For node project, you may need to install `@types/node` additionally. + +```sh +npm i @types/node -D +``` + +For frontend project (like React), you may need to install `@types/web` additionally. + +```sh +npm i @types/web -D +``` + +### Config `tsconfig.json` + +```json +{ + "extends": "@git-validator/tsconfig" +} +``` + +## Best Practices + +Here are the best practices if you are using this package. + +### For polyrepo + +``` +├── src +│ └── index.ts +├── test +│ └── index.spec.ts +├── package.json +├── tsconfig.build.json +└── tsconfig.json +``` + +#### tsconfig.json + +```json +{ + "extends": "@git-validator/tsconfig" +} +``` + +#### tsconfig.build.json + +```json +{ + "extends": "./tsconfig", + "include": ["src"], + "exclude": ["**/*.spec.ts"], + "compilerOptions": { + "outDir": "dist" + } +} +``` + +### For monorepo + +``` +├── apps +│ ├── app1 +│ │ ├── src +│ │ │ └── main.ts +│ │ ├── test +│ │ │ └── main.spec.ts +│ │ ├── package.json +│ │ └── tsconfig.build.json +│ └── app2 +│ ├── src +│ │ └── main.ts +│ ├── test +│ │ └── main.spec.ts +│ ├── package.json +│ └── tsconfig.build.json +├── package.json +└── tsconfig.json +``` + +#### tsconfig.json in the root of project + +```json +{ + "extends": "@git-validator/tsconfig" +} +``` + +#### tsconfig.build.json in each app + +```json +{ + "extends": "../../tsconfig", + "include": ["src"], + "exclude": ["**/*.spec.ts"], + "compilerOptions": { + "outDir": "dist" + } +} +``` + +## Commands + +After installing `@git-validator/tsconfig`, you can run `npx tsconfig init` command to generate a `tsconfig.json` file. Run `npx tsconfig init -h` for more detail of the command: + +```txt +Usage: tsconfig init [options] + +init a tsconfig file + +Options: + -p, --path directory path to generate file to (default: ".") + -n, --name tsconfig file name (default: "tsconfig.json") + -f, --force forcefully overwrite existing file + -h, --help display help for command +``` + +## License + +MIT diff --git a/packages/tsconfig/cjs.json b/packages/tsconfig/cjs.json new file mode 100644 index 00000000..89dea58f --- /dev/null +++ b/packages/tsconfig/cjs.json @@ -0,0 +1,48 @@ +{ + "compilerOptions": { + // copied from https://github.com/tsconfig/bases/blob/main/bases/strictest.json + "strict": true, + "allowUnusedLabels": false, + "allowUnreachableCode": false, + "exactOptionalPropertyTypes": true, + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noPropertyAccessFromIndexSignature": true, + "noUncheckedIndexedAccess": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "checkJs": false, // Don't check js for better experience. User can add `// @ts-check` on the top of js file to check it manually. + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + + "module": "Node16", + "target": "ES2022", + "moduleResolution": "node16", + "allowJs": true, + "outDir": "dist", // Enable it to prevent to override js file. And solve the error warning in vscode. Ref: https://github.com/Microsoft/TypeScript/issues/29172 + // "verbatimModuleSyntax": true, + "allowArbitraryExtensions": true, + "allowSyntheticDefaultImports": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "resolveJsonModule": true, + "stripInternal": true, + "declaration": true, + "declarationMap": true, + "inlineSourceMap": true, + "isolatedModules": true, + "jsx": "preserve" + /** + * 1. Typescript will include APIs for newer JS features matching the `target`. See https://www.typescriptlang.org/tsconfig#lib. Therefore, there is no need to add "ESNext" to lib. + * 2. In ts 4.5, lib files can be overrode by npm modules. See https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#supporting-lib-from-node_modules. Therefore, libs like "DOM" can be included by installing `@types/web`. No need to add "DOM" to lib. + */ + // "lib": ["ESNext", "DOM"], + /** + * Ts will load all the `node_modules/@types/*` declaration files when `types` is removed. + * Remove it will improve the extensibility. + */ + // "types": ["node", "jest"], + } +} diff --git a/packages/tsconfig/esm.json b/packages/tsconfig/esm.json new file mode 100644 index 00000000..3aeecb87 --- /dev/null +++ b/packages/tsconfig/esm.json @@ -0,0 +1,48 @@ +{ + "compilerOptions": { + // copied from https://github.com/tsconfig/bases/blob/main/bases/strictest.json + "strict": true, + "allowUnusedLabels": false, + "allowUnreachableCode": false, + "exactOptionalPropertyTypes": true, + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noPropertyAccessFromIndexSignature": true, + "noUncheckedIndexedAccess": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "checkJs": false, // Don't check js for better experience. User can add `// @ts-check` on the top of js file to check it manually. + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + + "module": "Node16", + "target": "ES2022", + "moduleResolution": "node16", + "allowJs": true, + "outDir": "dist", // Enable it to prevent to override js file. And solve the error warning in vscode. Ref: https://github.com/Microsoft/TypeScript/issues/29172 + "verbatimModuleSyntax": true, + "allowArbitraryExtensions": true, + "allowSyntheticDefaultImports": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "resolveJsonModule": true, + "stripInternal": true, + "declaration": true, + "declarationMap": true, + "inlineSourceMap": true, + "isolatedModules": true, + "jsx": "preserve" + /** + * 1. Typescript will include APIs for newer JS features matching the `target`. See https://www.typescriptlang.org/tsconfig#lib. Therefore, there is no need to add "ESNext" to lib. + * 2. In ts 4.5, lib files can be overrode by npm modules. See https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#supporting-lib-from-node_modules. Therefore, libs like "DOM" can be included by installing `@types/web`. No need to add "DOM" to lib. + */ + // "lib": ["ESNext", "DOM"], + /** + * Ts will load all the `node_modules/@types/*` declaration files when `types` is removed. + * Remove it will improve the extensibility. + */ + // "types": ["node", "jest"], + } +} diff --git a/packages/tsconfig/legacy.json b/packages/tsconfig/legacy.json new file mode 100644 index 00000000..690dfc17 --- /dev/null +++ b/packages/tsconfig/legacy.json @@ -0,0 +1,47 @@ +{ + "compilerOptions": { + // copied from https://github.com/tsconfig/bases/blob/main/bases/strictest.json + "strict": true, + "allowUnusedLabels": false, + "allowUnreachableCode": false, + "exactOptionalPropertyTypes": true, + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noPropertyAccessFromIndexSignature": true, + "noUncheckedIndexedAccess": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "checkJs": false, // Don't check js for better experience. User can add `// @ts-check` on the top of js file to check it manually. + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + + "module": "Node16", + "target": "ES2022", + "moduleResolution": "node16", + "allowJs": true, + "outDir": "dist", // Enable it to prevent to override js file. And solve the error warning in vscode. Ref: https://github.com/Microsoft/TypeScript/issues/29172 + // "verbatimModuleSyntax": true, + "allowSyntheticDefaultImports": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "resolveJsonModule": true, + "stripInternal": true, + "declaration": true, + "declarationMap": true, + "inlineSourceMap": true, + "isolatedModules": true, + "jsx": "preserve" + /** + * 1. Typescript will include APIs for newer JS features matching the `target`. See https://www.typescriptlang.org/tsconfig#lib. Therefore, there is no need to add "ESNext" to lib. + * 2. In ts 4.5, lib files can be overrode by npm modules. See https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#supporting-lib-from-node_modules. Therefore, libs like "DOM" can be included by installing `@types/web`. No need to add "DOM" to lib. + */ + // "lib": ["ESNext", "DOM"], + /** + * Ts will load all the `node_modules/@types/*` declaration files when `types` is removed. + * Remove it will improve the extensibility. + */ + // "types": ["node", "jest"], + } +} diff --git a/packages/tsconfig/package.json b/packages/tsconfig/package.json new file mode 100644 index 00000000..39dccb6d --- /dev/null +++ b/packages/tsconfig/package.json @@ -0,0 +1,30 @@ +{ + "name": "@git-validator/tsconfig", + "type": "module", + "version": "0.0.0", + "description": "Strict shared tsconfig out-of-box.", + "author": "hellozmj@qq.com", + "license": "MIT", + "homepage": "https://github.com/zanminkian/git-validator/tree/main/packages/tsconfig", + "keywords": [ + "tsconfig", + "strict", + "out-of-box", + "typescript", + "ts", + "config", + "configuration" + ], + "bin": { + "tsconfig": "./src/cli.js" + }, + "scripts": { + "build": "tsc --noEmit -p ../../tsconfig.json" + }, + "dependencies": { + "commander": "^11.0.0" + }, + "devDependencies": { + "@types/node": "^20.6.2" + } +} diff --git a/packages/tsconfig/src/cli.js b/packages/tsconfig/src/cli.js new file mode 100755 index 00000000..6cc1ca62 --- /dev/null +++ b/packages/tsconfig/src/cli.js @@ -0,0 +1,42 @@ +// @ts-check +import fs from "node:fs/promises"; +import { resolve } from "node:path"; +import process from "node:process"; +import { Command } from "commander"; + +const generatingTsconfigContent = `{ + "extends": "@zanminkian/tsconfig", + "include": ["src"], + "exclude": ["**/*.spec.?s", "**/*.test.?s"], + "compilerOptions": { + "outDir": "dist" + } +} +`; + +const program = new Command(); +program.name("tsconfig"); +program + .command("init") + .description("init a tsconfig file") + .option("-p, --path ", "directory path to generate file to", ".") + .option("-n, --name ", "tsconfig file name", "tsconfig.json") + .option("-f, --force", "forcefully overwrite existing file") + .action(async ({ path, name, force }) => { + const fullName = resolve(process.cwd(), path, name); + if ( + !(await fs + .access(fullName) + .then(() => true) + .catch(() => false)) || + force + ) { + await fs.writeFile(fullName, generatingTsconfigContent); + } else { + throw new Error( + `${fullName} is already existing! You can apply --force option to overwrite it.`, + ); + } + }); + +program.parse(); diff --git a/packages/tsconfig/tsconfig.json b/packages/tsconfig/tsconfig.json new file mode 100644 index 00000000..820fc1d3 --- /dev/null +++ b/packages/tsconfig/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./esm.json" +} diff --git a/tsconfig.json b/tsconfig.json index f81a6989..c7f1d30f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,3 @@ { - "extends": "@zanminkian/tsconfig" + "extends": "@git-validator/tsconfig" }