Skip to content

Commit

Permalink
feat(tsconfig): finish
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Sep 17, 2023
1 parent 8fa9b94 commit 53039fd
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-books-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@git-validator/tsconfig": minor
---

feat(tsconfig): finish
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
149 changes: 149 additions & 0 deletions packages/tsconfig/README.md
Original file line number Diff line number Diff line change
@@ -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 <path> directory path to generate file to (default: ".")
-n, --name <filename> tsconfig file name (default: "tsconfig.json")
-f, --force forcefully overwrite existing file
-h, --help display help for command
```

## License

MIT
48 changes: 48 additions & 0 deletions packages/tsconfig/cjs.json
Original file line number Diff line number Diff line change
@@ -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"],
}
}
48 changes: 48 additions & 0 deletions packages/tsconfig/esm.json
Original file line number Diff line number Diff line change
@@ -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"],
}
}
47 changes: 47 additions & 0 deletions packages/tsconfig/legacy.json
Original file line number Diff line number Diff line change
@@ -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"],
}
}
30 changes: 30 additions & 0 deletions packages/tsconfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@git-validator/tsconfig",
"type": "module",
"version": "0.0.0",
"description": "Strict shared tsconfig out-of-box.",
"author": "[email protected]",
"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"
}
}
42 changes: 42 additions & 0 deletions packages/tsconfig/src/cli.js
Original file line number Diff line number Diff line change
@@ -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 <path>", "directory path to generate file to", ".")
.option("-n, --name <filename>", "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();
3 changes: 3 additions & 0 deletions packages/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./esm.json"
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@zanminkian/tsconfig"
"extends": "@git-validator/tsconfig"
}

0 comments on commit 53039fd

Please sign in to comment.