简体   繁体   English

为什么 Vite 会创建两个 TypeScript 配置文件:tsconfig.json 和 tsconfig.node.json?

[英]Why does Vite create two TypeScript config files: tsconfig.json and tsconfig.node.json?

I'm using Vite to create a new React + TypeScript project .我正在使用 Vite 创建 一个新的 React + TypeScript 项目

After creating the project, there are two TypeScript config files on the root folder: tsconfig.json and tsconfig.node.json .创建项目后,根文件夹中有两个 TypeScript 配置文件: tsconfig.jsontsconfig.node.json These are the contents of each one:这些是每一个的内容:

tsconfig.json tsconfig.json

{
    "compilerOptions": {
        "target": "ESNext",
        "useDefineForClassFields": true,
        "lib": ["DOM", "DOM.Iterable", "ESNext"],
        "allowJs": false,
        "skipLibCheck": false,
        "esModuleInterop": false,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "ESNext",
        "moduleResolution": "Node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx"
    },
    "include": ["src"],
    "references": [{ "path": "./tsconfig.node.json" }]
}

tsconfig.node.json tsconfig.node.json

{
    "compilerOptions": {
        "composite": true,
        "module": "esnext",
        "moduleResolution": "node"
    },
    "include": ["vite.config.ts"]
}

Why do we need two?为什么我们需要两个?

What does the second one do?第二个是做什么的?

Can I remove the second one?我可以删除第二个吗?

You need two different TS configs because the project is using two different environments in which the TypeScript code is executed:您需要两个不同的 TS 配置,因为该项目使用两个不同的执行 TypeScript 代码的环境:

  1. Your app ( src folder) is targeting (will be running) inside the browser您的应用程序( src文件夹)在浏览器内定位(将运行)
  2. Vite itself including it's config is running on your computer inside Node, which is totally different environment (compared with browser) with different API's and constraints Vite 本身(包括它的配置)在您的计算机上运行在 Node 内,这是完全不同的环境(与浏览器相比),具有不同的 API 和约束

So there are two different configs for those two environments and two distinct sets of source files...所以这两个环境有两种不同的配置和两组不同的源文件......

And no, you probably don't want to delete the tsconfig.node.json but you can probably rename it to something like tsconfig.vite.json to make it's purpose more clear不,您可能不想删除tsconfig.node.json但您可能可以将其重命名为tsconfig.vite.json以使其目的更明确

As mentioned by Michal Levý, these are different configs for different environments.正如 Michal Levý 所提到的,这些是针对不同环境的不同配置。

You will notice that tsconfig.json includes a "references" key which points to an array that includes the tsconfig.node.json file.您会注意到tsconfig.json包含一个“references”键,它指向一个包含tsconfig.node.json文件的数组。 If you wish to change the filename of your vite tsconfig, be sure to update this reference.如果您希望更改 vite tsconfig 的文件名,请务必更新此参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将tsconfig.json放置在Symfony Encore中以定义TypeScript配置? - Where to place tsconfig.json in Symfony Encore to define TypeScript config? 启用tsconfig.json以为.js文件compileOnSave - Enable tsconfig.json to compileOnSave for .js files TypeScript编译器忽略tsconfig.json中的声明文件 - TypeScript compiler ignores declaration file in tsconfig.json Typescript 编译器在 tsconfig.json 上包含文件时找不到绝对路径 - Typescript compiler not finding absolute paths when including files on tsconfig.json 为什么普通的 json 文件不允许评论,而 tsconfig.json 允许评论? - Why normal json files don't allow comments where as tsconfig.json allow comment? 在没有 tsconfig.json 全局项目级别的情况下,打字稿文件在 vs2017 中自动创建 Javascript - Typescript file auto create Javascript in vs2017 without tsconfig.json globally project level 为什么打字稿编译器会在服务器启动时更改我的 tsconfig.json 文件? - Why is typescript compiler changing my tsconfig.json file on server start up? ts-node应该检查baseUrl tsconfig.json属性吗? - Should ts-node examine the baseUrl tsconfig.json property? 如何在 tsconfig.json 中排除以“.spec.ts”结尾的文件 - How to exclude files ending in '.spec.ts' in tsconfig.json tsconfig.json 中的 **/node_modules/* 是什么意思? - What it means **/node_modules/* in tsconfig.json?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM