为什么Vite创建两个打字稿配置文件:tsconfig.json和tsconfig.node.json?

发布于 2025-01-24 10:13:49 字数 1295 浏览 1 评论 0原文

我正在使用vite来创建一个新的react + toxpimpt + tyspercript项目

创建项目后,根文件夹上有两个打字稿配置文件:tsconfig.jsontsconfig.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遇见了你 2025-01-31 10:13:49

的TS配置

  1. 您需要两个
  2. 不同 在您的计算机上运行节点,这是完全不同的环境(与浏览器相比)具有不同的API和约束,

因此这些环境有两个独立的配置,以及两组由定义的源文件集合> >和排除这些配置的部分。然后有一个“主”配置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:

  1. Your app (src folder) is targeting (will be running) inside the browser
  2. Vite itself including its config is running on your computer inside Node, which is totally different environment (compared with browser) with different API's and constraints

Thus there are two separate configs for those environments and two sets of source files that are defined by the include and exclude sections of these configs. Then there is one "master" config tsconfig.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 like tsconfig.vite.json to make its purpose more clear. If you do, make sure to update "main" config accordingly

ま柒月 2025-01-31 10:13:49

正如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 the tsconfig.node.json file. If you wish to change the filename of your vite tsconfig, be sure to update this reference.

平安喜乐 2025-01-31 10:13:49

添加已经提供的答案之上:

我进行了一些挖掘,甚至检查了源代码。 vite.config.ts似乎是使用每个构建/dev Server restart上的typeScript API直接transperp thrpspers ,因此从“ Project tsconfig”中引用了它的引用对我来说很有意义,然后我找到了以下内容:

  1. 显然,不包括特定类型,vite取决于您正在构建的实际项目(例如@type/node 到基于浏览器的环境) - vite github问题:tsconfig.node.node.json。 tsconfig.json参考
    • 问题是,这似乎不再起作用
    • 使用项目参考或其他依赖于特定的打字稿设置
    • 时,这将引入其他问题。

  2. 有效对于将来的不同用例中,如 vite github pull request:plual request:pluct request:pluct request:pluct request:plual request:pluct request:plual request:pull tup request:pull tssconfig:vite tsconfig .config.ts是类型检查
    • 一种可能性是编译时间类型检查如上所述

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:

  1. Apparently it was kind of a hack to not include specific types, that Vite is dependent on, to the actual Project you are building (for example @types/node to a browser based environment) - Vite GitHub issue: tsconfig.node.json should not be included in tsconfig.json references
    • Issue is, that this does not seem to be working any more
    • This introduces other issues when using project references or other libraries relying on specific typescript setup
  2. That said, it might be used for different use cases in the future as seen in Vite GitHub pull request: use "solution" tsconfig so that vite.config.ts is type checked
    • One possibility is for compile time type checking as seen in the above PR
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文