002 vue3-admin项目的目录及文件说明之tsconfig.node.json文件

说明

tsconfig.node.json 是现代前端项目中常见的 TypeScript 配置文件,专门用于 Node.js 环境的代码配置。

{
  "compilerOptions": {
    "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
    "target": "ES2023",
    "lib": ["ES2023"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "moduleDetection": "force",
    "noEmit": true,

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "erasableSyntaxOnly": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedSideEffectImports": true
  },
  "include": ["vite.config.ts"]
}

核心配置

1 compilerOptions 属性

项目引用相关配置

"compilerOptions": {
  "composite": true,
  "skipLibCheck": true
}
  • composite:启用项目引用功能
    • 必须设置为 true 才能被其他项目引用
    • 会生成 .tsbuildinfo 文件用于增量编译
  • skipLibCheck:跳过库文件的类型检查
    • 提高编译性能
    • 避免第三方库的类型错误影响项目

模块配置

"compilerOptions": {
  "module": "ESNext",
  "moduleResolution": "bundler",
  "allowSyntheticDefaultImports": true
}
  • module:指定生成的模块代码类型
    • ESNext:使用最新的 ECMAScript 模块语法
  • moduleResolution:指定模块解析策略
    • bundler:适合现代构建工具(Vite、Webpack 等)
  • allowSyntheticDefaultImports:允许从没有默认导出的模块中默认导入
    • 提高兼容性和开发体验

类型检查配置

"compilerOptions": {
  "strict": true
}
  • strict:启用所有严格类型检查选项
    • 提高代码质量和类型安全性

2 include 属性

"include": ["vite.config.ts"]
  • 作用:指定要编译的文件
  • 典型包含:构建配置文件(如 vite.config.ts
  • 其他可能包含:
    • webpack.config.ts
    • rollup.config.ts
    • 构建脚本、工具脚本等

完整示例分析

典型的 tsconfig.node.json

{
  "compilerOptions": {
    "composite": true,
    "skipLibCheck": true,
    "module": "ESNext",
    "moduleResolution": "bundler",
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "noEmit": true,
    "resolveJsonModule": true,
    "isolatedModules": true
  },
  "include": ["vite.config.ts", "env.d.ts", "build/**/*.ts"]
}

配置项说明

  • noEmit:不生成输出文件(因为这些文件通常由构建工具处理)
  • resolveJsonModule:允许导入 JSON 文件
  • isolatedModules:确保每个文件都可以独立编译
  • include:包含构建配置和构建脚本

与项目引用的关系

项目引用结构

project/
├── tsconfig.json          # 主配置文件
│   ├── "references": [
│   ├──   { "path": "./tsconfig.app.json" },
│   ├──   { "path": "./tsconfig.node.json" }
│   ├── ]
├── tsconfig.app.json      # 应用代码配置
├── tsconfig.node.json     # Node.js 代码配置
└── vite.config.ts         # Vite 配置文件

工作原理

1 主配置文件:引用 tsconfig.node.json 和其他子项目2 编译流程:
  • TypeScript 编译器首先编译 tsconfig.node.json
  • 然后编译其他依赖于它的项目
     
    3 增量编译:只重新编译发生变化的部分

 

posted @ 2025-11-16 10:24  Allen_Hao  阅读(11)  评论(0)    收藏  举报