@secret-momo/toolchain-config 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,81 +1,84 @@
1
1
  # @secret-momo/toolchain-config
2
2
 
3
- 跨项目共享 TypeScript、ESLint、Prettier 和 Stylelint 配置。按需安装工具,再从项目现有配置引用相应预设。路径别名、输出目录、文件范围和项目特有规则在项目里设置。
3
+ 跨项目共享的 TypeScript 配置。需要 Node.js 26+ 和 TypeScript `~6.0.3`。
4
4
 
5
5
  ```bash
6
- pnpm add -D @secret-momo/toolchain-config
6
+ pnpm add -D @secret-momo/toolchain-config typescript@~6.0.3
7
7
  ```
8
8
 
9
- 需要 Node.js 26+。按使用的入口安装对应的工具。
9
+ 使用自动加载 Node 环境类型的预设时,再安装与目标 Node.js 主版本对应的类型。以 Node.js 26 为例。
10
10
 
11
- | 工具 | 版本范围 |
12
- | ---------- | ---------- |
13
- | TypeScript | `~6.0.3` |
14
- | ESLint | `^10.15.0` |
15
- | Prettier | `^3.10.1` |
16
- | Stylelint | `^18.0.0` |
11
+ ```bash
12
+ pnpm add -D @types/node@26
13
+ ```
14
+
15
+ ## TypeScript 预设
16
+
17
+ 在 `tsconfig.json` 的 `extends` 中使用 `@secret-momo/toolchain-config/` 加下表中的入口。
18
+
19
+ | 入口 | 适用场景 |
20
+ | ---------------------------- | ----------------------------------------------------------------------------------------- |
21
+ | `tsconfig/base.json` | 只复用通用严格检查和模块语法规则 |
22
+ | `tsconfig/node.json` | 只复用 NodeNext 模块与解析规则,自行配置 `lib`、`types` 和输出策略 |
23
+ | `tsconfig/node-check.json` | 仅检查 NodeNext 项目,加载 Node 环境类型 |
24
+ | `tsconfig/browser.json` | 使用 Bundler 模块解析的浏览器项目,提供 DOM 类型 |
25
+ | `tsconfig/react.json` | 使用 `react-jsx` 的浏览器项目 |
26
+ | `tsconfig/node-library.json` | 检查索引访问和成员覆盖,用 `tsc` 构建 NodeNext 库并输出 JavaScript 和类型声明 |
27
+ | `tsconfig/node-native.json` | 由 Node.js 直接运行 TypeScript,允许 `.ts` 扩展名导入并检查语法可擦除性 |
28
+ | `tsconfig/bundler.json` | 由打包器或其他工具处理输出,使用 Preserve/Bundler 并允许 `.ts` 扩展名导入 |
29
+ | `tsconfig/node-bundler.json` | 为 Node.js 项目提供 Preserve/Bundler 类型检查,加载 Node 环境类型并检查索引访问和成员覆盖 |
30
+ | `tsconfig/tool.json` | 检查 Node.js 运行的工具配置和脚本,允许 `.ts` 扩展名导入,只接受可擦除的 TypeScript 语法 |
31
+ | `tsconfig/typecheck.json` | 叠加在项目预设之后,只做类型检查 |
32
+ | `tsconfig/declarations.json` | 叠加在项目预设之后,只生成类型声明 |
33
+
34
+ Node、工具和打包场景统一使用 `ESNext` 的编译目标与标准库类型,浏览器和 React 预设额外加载 DOM 类型。`ESNext` 随项目使用的 TypeScript 版本更新。
17
35
 
18
- ## TypeScript
36
+ 基础预设启用 `noEmitOnError`,编译报错时跳过 JavaScript 和声明文件输出。
19
37
 
20
- 在项目的 `tsconfig.json` 中引用预设。以下是一个 Node.js 项目的例子,`types` 中的 `node` 需要项目安装 `@types/node`。
38
+ `node-check.json`、`node-native.json`、`bundler.json`、`node-bundler.json` 和 `tool.json` 默认只做类型检查。除通用的 `bundler.json` 外,这些入口和 `node-library.json` 都从消费项目加载 `@types/node`。
39
+
40
+ 单独继承 `node.json` 时,TypeScript 6 使用包含 DOM 的默认标准库,并由项目显式设置所需的 `types`。需要一套完整的 Node 类型检查配置时,使用 `node-check.json`。
41
+
42
+ ## 使用示例
43
+
44
+ 下面的 NodeNext 库示例用于 ESM。使用普通 `.ts` 文件编写 `import` 和 `export` 时,在消费项目的 `package.json` 中设置 `"type": "module"`。单个 ESM 文件也可以用 `.mts` 扩展名。项目的 `tsconfig.json` 负责指定源码范围和输出目录。
21
45
 
22
46
  ```json
23
47
  {
24
- "extends": "@secret-momo/toolchain-config/tsconfig/node.json",
48
+ "extends": "@secret-momo/toolchain-config/tsconfig/node-library.json",
25
49
  "compilerOptions": {
26
- "target": "ES2022",
27
- "lib": ["ES2022"],
28
- "types": ["node"],
29
- "noEmit": true
50
+ "rootDir": "src",
51
+ "outDir": "dist"
30
52
  },
31
53
  "include": ["src"]
32
54
  }
33
55
  ```
34
56
 
35
- | 入口 | 内容 |
36
- | ----------------------- | ------------------------------------------ |
37
- | `tsconfig/base.json` | 通用严格检查和模块语法 |
38
- | `tsconfig/node.json` | 基础配置加 NodeNext 模块及解析策略 |
39
- | `tsconfig/browser.json` | 基础配置加 ES2023、DOM 类型和 Bundler 解析 |
40
- | `tsconfig/react.json` | 浏览器配置加 `react-jsx` |
41
-
42
- ## ESLint
43
-
44
- 在 `eslint.config.mjs` 中组合规则和运行环境。将配置文件所在项目的绝对路径传给 `base`,让类型感知规则查找消费项目的 TSConfig。`internalImportPattern` 可用于识别项目内部导入。
45
-
46
- ```js
47
- import { defineConfig, globalIgnores } from 'eslint/config';
57
+ 用打包器构建 Node 库时,可继承 `node-bundler.json` 进行类型检查。由 `tsc` 生成声明时,在构建配置中把 `declarations.json` 放在最后。
48
58
 
49
- import base from '@secret-momo/toolchain-config/eslint/base';
50
- import browser from '@secret-momo/toolchain-config/eslint/browser';
51
- import node from '@secret-momo/toolchain-config/eslint/node';
52
- import react from '@secret-momo/toolchain-config/eslint/react';
53
-
54
- export default defineConfig(globalIgnores(['**/dist/**']), base({ tsconfigRootDir: import.meta.dirname, internalImportPattern: '^@example/' }), { files: ['apps/server/**', '**/*.config.mjs'], extends: [node] }, { files: ['apps/web/src/**'], extends: [browser, react] });
59
+ ```json
60
+ {
61
+ "extends": ["@secret-momo/toolchain-config/tsconfig/node-bundler.json", "@secret-momo/toolchain-config/tsconfig/declarations.json"],
62
+ "compilerOptions": {
63
+ "rootDir": "src",
64
+ "outDir": ".cache/declarations"
65
+ },
66
+ "include": ["src"],
67
+ "exclude": ["src/**/*.test.ts"]
68
+ }
55
69
  ```
56
70
 
57
- 纯 Node.js 项目可以直接把 `node` 传给 `defineConfig`。普通浏览器项目使用 `browser`,React 项目额外叠加 `react` 启用 JSX 和 React Hooks 规则。共享包提供预设所需的插件。
71
+ Bundler 解析允许省略相对导入的扩展名,生成的 `.d.ts` 可能保留这些导入。发布前用 NodeNext 消费项目检查最终声明。可在源码中使用带 `.js` 扩展名的相对导入,或用 `rollup-plugin-dts` 等工具合并声明并检查合并后的入口。
58
72
 
59
- ## Prettier
60
-
61
- 在项目的 `package.json` 中引用共享配置。
73
+ 工具配置可以直接继承 `tool.json`,并通过 `include` 指定文件范围。
62
74
 
63
75
  ```json
64
76
  {
65
- "prettier": "@secret-momo/toolchain-config/prettier"
77
+ "extends": "@secret-momo/toolchain-config/tsconfig/tool.json",
78
+ "include": ["*.config.mts", "scripts/**/*.ts"]
66
79
  }
67
80
  ```
68
81
 
69
- 需要覆盖时,可以在 `prettier.config.mjs` 中导入该入口,展开配置并追加本地选项。追加 `overrides` 时一并展开共享配置的 `overrides`,即可保留 JSONC 的尾逗号设置。
70
-
71
- ## Stylelint
72
-
73
- 在 `stylelint.config.mjs` 中继承 CSS 预设,可用于普通 CSS 和 `.module.css` 文件。
74
-
75
- ```js
76
- export default {
77
- extends: ['@secret-momo/toolchain-config/stylelint/css'],
78
- };
79
- ```
82
+ 单独使用 `browser.json` 或 `react.json` 时,`tsc` 默认会输出文件,且没有模块标记的普通 `.ts` 文件可能共享全局作用域。使用 Vite 等打包器并希望 `tsc` 只检查类型、把每个文件视为模块时,先继承 `bundler.json`,再继承 `browser.json` 或 `react.json`。
80
83
 
81
- 项目可以继续配置扫描范围、忽略路径和设计变量规则。
84
+ 在 monorepo 中,可以由仓库根目录的配置为各场景选用预设,再让子包继承相应的根文件。在消费项目中设置 `include`、`exclude`、`paths` 和预设之外的环境类型。React 项目需安装 `react` 和 `@types/react`,Vite 项目可按需在 `types` 中加入 `vite/client`。
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@secret-momo/toolchain-config",
3
- "version": "0.0.1",
4
- "description": "跨项目共享的 TypeScript、ESLint、Prettier 和 Stylelint 配置。",
3
+ "version": "1.0.0",
4
+ "description": "跨项目共享的 TypeScript 配置。",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -14,17 +14,16 @@
14
14
  "./tsconfig/node.json": "./tsconfig/node.json",
15
15
  "./tsconfig/browser.json": "./tsconfig/browser.json",
16
16
  "./tsconfig/react.json": "./tsconfig/react.json",
17
- "./eslint/base": "./eslint/base.mjs",
18
- "./eslint/node": "./eslint/node.mjs",
19
- "./eslint/browser": "./eslint/browser.mjs",
20
- "./eslint/react": "./eslint/react.mjs",
21
- "./prettier": "./prettier/index.mjs",
22
- "./stylelint/css": "./stylelint/css.mjs"
17
+ "./tsconfig/node-check.json": "./tsconfig/node-check.json",
18
+ "./tsconfig/node-library.json": "./tsconfig/node-library.json",
19
+ "./tsconfig/node-native.json": "./tsconfig/node-native.json",
20
+ "./tsconfig/bundler.json": "./tsconfig/bundler.json",
21
+ "./tsconfig/node-bundler.json": "./tsconfig/node-bundler.json",
22
+ "./tsconfig/tool.json": "./tsconfig/tool.json",
23
+ "./tsconfig/typecheck.json": "./tsconfig/typecheck.json",
24
+ "./tsconfig/declarations.json": "./tsconfig/declarations.json"
23
25
  },
24
26
  "files": [
25
- "eslint",
26
- "prettier",
27
- "stylelint",
28
27
  "tsconfig"
29
28
  ],
30
29
  "engines": {
@@ -34,44 +33,12 @@
34
33
  "access": "public",
35
34
  "registry": "https://registry.npmjs.org/"
36
35
  },
37
- "dependencies": {
38
- "@eslint/js": "10.0.1",
39
- "@typescript-eslint/eslint-plugin": "8.73.0",
40
- "eslint-config-prettier": "10.1.8",
41
- "eslint-plugin-import-x": "4.18.1",
42
- "eslint-plugin-react-hooks": "7.1.1",
43
- "eslint-plugin-unused-imports": "4.4.1",
44
- "globals": "17.14.0",
45
- "stylelint-config-standard": "41.0.0",
46
- "typescript-eslint": "8.73.0"
47
- },
48
36
  "peerDependencies": {
49
- "eslint": "^10.15.0",
50
- "prettier": "^3.10.1",
51
- "stylelint": "^18.0.0",
52
37
  "typescript": "~6.0.3"
53
38
  },
54
39
  "peerDependenciesMeta": {
55
- "eslint": {
56
- "optional": true
57
- },
58
- "prettier": {
59
- "optional": true
60
- },
61
- "stylelint": {
62
- "optional": true
63
- },
64
40
  "typescript": {
65
41
  "optional": true
66
42
  }
67
- },
68
- "devDependencies": {
69
- "eslint": "10.15.0",
70
- "prettier": "3.10.1",
71
- "stylelint": "18.0.0",
72
- "typescript": "6.0.3"
73
- },
74
- "scripts": {
75
- "typecheck": "tsc -p tsconfig.json"
76
43
  }
77
44
  }
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "strict": true,
4
+ "skipLibCheck": true,
5
+ "noEmitOnError": true,
4
6
  "noFallthroughCasesInSwitch": true,
5
7
  "noUnusedLocals": true,
6
8
  "noUnusedParameters": true,
7
- "verbatimModuleSyntax": true,
8
- "forceConsistentCasingInFileNames": true,
9
- "skipLibCheck": true
9
+ "verbatimModuleSyntax": true
10
10
  }
11
11
  }
@@ -1,7 +1,7 @@
1
1
  {
2
- "extends": "./base.json",
2
+ "extends": ["./base.json", "./internal/esnext.json"],
3
3
  "compilerOptions": {
4
- "lib": ["ES2023", "DOM", "DOM.Iterable"],
4
+ "lib": ["ESNext", "DOM"],
5
5
  "module": "ESNext",
6
6
  "moduleResolution": "Bundler"
7
7
  }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": ["./base.json", "./internal/esnext.json", "./typecheck.json"],
3
+ "compilerOptions": {
4
+ "module": "Preserve",
5
+ "moduleResolution": "Bundler",
6
+ "moduleDetection": "force",
7
+ "allowImportingTsExtensions": true
8
+ }
9
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "compilerOptions": {
3
+ "noEmit": false,
4
+ "declaration": true,
5
+ "emitDeclarationOnly": true,
6
+ "noEmitOnError": true
7
+ }
8
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "lib": ["ESNext"]
5
+ }
6
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./esnext.json",
3
+ "compilerOptions": {
4
+ "types": ["node"],
5
+ "noUncheckedIndexedAccess": true,
6
+ "noImplicitOverride": true
7
+ }
8
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["./bundler.json", "./internal/node-runtime.json"]
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["./node.json", "./internal/node-runtime.json", "./typecheck.json"]
3
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": ["./node.json", "./internal/node-runtime.json"],
3
+ "compilerOptions": {
4
+ "noEmit": false,
5
+ "declaration": true
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./node-check.json",
3
+ "compilerOptions": {
4
+ "allowImportingTsExtensions": true,
5
+ "erasableSyntaxOnly": true
6
+ }
7
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "./node-native.json",
3
+ "compilerOptions": {
4
+ "declaration": false
5
+ }
6
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "compilerOptions": {
3
+ "noEmit": true
4
+ }
5
+ }
package/eslint/base.d.mts DELETED
@@ -1,8 +0,0 @@
1
- import type { Linter } from 'eslint';
2
-
3
- export interface BaseOptions {
4
- tsconfigRootDir: string;
5
- internalImportPattern?: string;
6
- }
7
-
8
- export default function base(options: BaseOptions): Linter.Config[];
package/eslint/base.mjs DELETED
@@ -1,65 +0,0 @@
1
- import js from '@eslint/js';
2
- import { defineConfig } from 'eslint/config';
3
- import prettier from 'eslint-config-prettier/flat';
4
- import { importX } from 'eslint-plugin-import-x';
5
- import unusedImports from 'eslint-plugin-unused-imports';
6
- import tseslint from 'typescript-eslint';
7
-
8
- import { javascriptFiles, sourceFiles, typescriptFiles } from './files.mjs';
9
-
10
- /**
11
- * @param {{ tsconfigRootDir: string, internalImportPattern?: string }} options
12
- * @returns {import('eslint').Linter.Config[]}
13
- */
14
- export default function base({ tsconfigRootDir, internalImportPattern }) {
15
- return defineConfig(
16
- {
17
- files: javascriptFiles,
18
- extends: [js.configs.recommended],
19
- },
20
- {
21
- files: typescriptFiles,
22
- extends: [js.configs.recommended, tseslint.configs.recommended],
23
- languageOptions: { parserOptions: { projectService: true, tsconfigRootDir } },
24
- rules: {
25
- '@typescript-eslint/consistent-type-imports': [
26
- 'error',
27
- { fixStyle: 'inline-type-imports' },
28
- ],
29
- '@typescript-eslint/no-deprecated': 'error',
30
- '@typescript-eslint/no-floating-promises': 'error',
31
- '@typescript-eslint/no-misused-promises': 'error',
32
- },
33
- },
34
- {
35
- files: sourceFiles,
36
- plugins: { 'import-x': importX, 'unused-imports': unusedImports },
37
- settings:
38
- internalImportPattern === undefined
39
- ? {}
40
- : { 'import-x/internal-regex': internalImportPattern },
41
- rules: {
42
- 'no-unused-vars': 'off',
43
- '@typescript-eslint/no-unused-vars': 'off',
44
- 'unused-imports/no-unused-imports': 'error',
45
- 'unused-imports/no-unused-vars': [
46
- 'error',
47
- {
48
- args: 'after-used',
49
- argsIgnorePattern: '^_',
50
- varsIgnorePattern: '^_',
51
- caughtErrorsIgnorePattern: '^_',
52
- },
53
- ],
54
- 'import-x/order': [
55
- 'error',
56
- {
57
- groups: ['builtin', 'external', 'internal', ['parent', 'sibling', 'index']],
58
- 'newlines-between': 'always',
59
- },
60
- ],
61
- },
62
- },
63
- prettier,
64
- );
65
- }
@@ -1,4 +0,0 @@
1
- import type { Linter } from 'eslint';
2
-
3
- declare const config: Linter.Config;
4
- export default config;
@@ -1,11 +0,0 @@
1
- import globals from 'globals';
2
-
3
- import { sourceFiles } from './files.mjs';
4
-
5
- /** @type {import('eslint').Linter.Config} */
6
- const config = {
7
- files: sourceFiles,
8
- languageOptions: { globals: globals.browser },
9
- };
10
-
11
- export default config;
package/eslint/files.mjs DELETED
@@ -1,3 +0,0 @@
1
- export const javascriptFiles = ['**/*.{js,mjs,cjs,jsx}'];
2
- export const typescriptFiles = ['**/*.{ts,tsx,mts,cts}'];
3
- export const sourceFiles = ['**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}'];
package/eslint/node.d.mts DELETED
@@ -1,4 +0,0 @@
1
- import type { Linter } from 'eslint';
2
-
3
- declare const config: Linter.Config;
4
- export default config;
package/eslint/node.mjs DELETED
@@ -1,11 +0,0 @@
1
- import globals from 'globals';
2
-
3
- import { sourceFiles } from './files.mjs';
4
-
5
- /** @type {import('eslint').Linter.Config} */
6
- const config = {
7
- files: sourceFiles,
8
- languageOptions: { globals: globals.node },
9
- };
10
-
11
- export default config;
@@ -1,4 +0,0 @@
1
- import type { Linter } from 'eslint';
2
-
3
- declare const config: Linter.Config[];
4
- export default config;
package/eslint/react.mjs DELETED
@@ -1,19 +0,0 @@
1
- import reactHooks from 'eslint-plugin-react-hooks';
2
-
3
- import { javascriptFiles, sourceFiles } from './files.mjs';
4
-
5
- /** @type {import('eslint').Linter.Config[]} */
6
- const config = [
7
- {
8
- files: javascriptFiles,
9
- languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } },
10
- },
11
- {
12
- files: sourceFiles,
13
- // 这里只注册规则,保留调用方对 React 文件范围的控制。
14
- plugins: { 'react-hooks': { meta: reactHooks.meta, rules: reactHooks.rules } },
15
- rules: reactHooks.configs.recommended.rules,
16
- },
17
- ];
18
-
19
- export default config;
@@ -1,4 +0,0 @@
1
- import type { Config } from 'prettier';
2
-
3
- declare const config: Config;
4
- export default config;
@@ -1,9 +0,0 @@
1
- /** @type {import('prettier').Config} */
2
- const config = {
3
- singleQuote: true,
4
- trailingComma: 'all',
5
- printWidth: 100,
6
- overrides: [{ files: '*.jsonc', options: { trailingComma: 'none' } }],
7
- };
8
-
9
- export default config;
@@ -1,4 +0,0 @@
1
- import type { Config } from 'stylelint';
2
-
3
- declare const config: Config;
4
- export default config;
package/stylelint/css.mjs DELETED
@@ -1,6 +0,0 @@
1
- /** @type {import('stylelint').Config} */
2
- const config = {
3
- extends: ['stylelint-config-standard'],
4
- };
5
-
6
- export default config;