为什么Vite创建两个打字稿配置文件:tsconfig.json和tsconfig.node.json?
我正在使用vite来创建一个新的react + toxpimpt + tyspercript项目。
创建项目后,根文件夹上有两个打字稿配置文件:tsconfig.json
和tsconfig.node.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,
{
"compilerOptions": {
"composite": true,
"module": "esnext",
"moduleResolution": "node"
},
"include": ["vite.config.ts"]
}
为什么我们需要两个?
第二个人做什么?
我可以删除第二个吗?
I'm using Vite to create a new React + TypeScript project.
After creating the project, there are two TypeScript config files on the root folder: tsconfig.json
and tsconfig.node.json
. These are the contents of each one:
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
{
"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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
的TS配置
因此这些环境有两个独立的配置,以及两组由
定义的源文件集合> >和
排除这些配置的部分。然后有一个“主”配置
tsconfig.json
“规则所有人”,不,您可能不想删除
tsconfig.node.json
,但是您可以将其重命名为tsconfig.vite.json
之类的东西,以使其目的更加清晰。如果这样做,请确保相应地更新“ Main”配置You need two different TS configs because the project is using two different environments in which the TypeScript code is executed:
src
folder) is targeting (will be running) inside the browserThus there are two separate configs for those environments and two sets of source files that are defined by the
include
andexclude
sections of these configs. Then there is one "master" configtsconfig.json
which "rules them all"And no, you probably don't want to delete the
tsconfig.node.json
but you can probably rename it to something liketsconfig.vite.json
to make its purpose more clear. If you do, make sure to update "main" config accordingly正如MichalLevý提到的那样,这些是不同环境的不同配置。
您会注意到
tsconfig.json
包括一个“引用”键,该密钥指向一个包括tsconfig.node.json.json
文件的数组。如果您想更改Vite Tsconfig的文件名,请务必更新此参考。As mentioned by Michal Levý, these are different configs for different environments.
You will notice that
tsconfig.json
includes a "references" key which points to an array that includes thetsconfig.node.json
file. If you wish to change the filename of your vite tsconfig, be sure to update this reference.添加已经提供的答案之上:
我进行了一些挖掘,甚至检查了源代码。
vite.config.ts
似乎是使用每个构建/dev Server restart上的typeScript API直接transperp thrpspers ,因此从“ Project tsconfig”中引用了它的引用对我来说很有意义,然后我找到了以下内容:@type/node 到基于浏览器的环境) - vite github问题:tsconfig.node.node.json。 tsconfig.json参考- 问题是,这似乎不再起作用
- 使用
项目参考
或其他依赖于特定的打字稿设置时,这将引入其他问题。
Adding on top of the already provided answers:
I did some digging and even checked the source code. The
vite.config.ts
seems to be transpiled and loaded directly using typescript API on each build/dev server restart so the reference to it from the "Project tsconfig" did not make much sense to me and then I found the following:@types/node
to a browser based environment) - Vite GitHub issue: tsconfig.node.json should not be included in tsconfig.json referencesproject references
or other libraries relying on specific typescript setup