Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions guide/api-hmr.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ if (import.meta.hot) {
```

## TypeScript 的智能提示 {#intellisense-for-typescript}
Vite 在 [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts) 中为 `import.meta.hot` 提供了类型定义。你可以在 `src` 目录中创建一个 `vite-env.d.ts`,以便 TypeScript 获取类型定义
Vite 在 [`vite/client.d.ts`](https://github.com/vitejs/vite/blob/main/packages/vite/client.d.ts) 中为 `import.meta.hot` 提供了类型定义。你可以在 `tsconfig.json` 中添加 "vite/client",以便 TypeScript 获取这些类型定义

```ts [vite-env.d.ts]
/// <reference types="vite/client" />
```json [tsconfig.json]
{
"compilerOptions": {
"types": ["vite/client"]
}
}
```

## `hot.accept(cb)` {#hot-accept-cb}
Expand Down
13 changes: 11 additions & 2 deletions guide/env-and-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

Vite 在特殊的 `import.meta.env` 对象下暴露了一些常量。这些常量在开发阶段被定义为全局变量,并在构建阶段被静态替换,以使树摇(tree-shaking)更有效。

:::details Example

```js
if (import.meta.env.DEV) {
// 这里的代码在生产构建中会被 tree-shaking 优化掉
console.log('Dev mode')
}
```

:::

## 内置常量 {#built-in-constants}

一些内置常量在所有情况下都可用:
Expand Down Expand Up @@ -106,8 +117,6 @@ VITE_BAR=bar
要想做到这一点,你可以在 `src` 目录下创建一个 `vite-env.d.ts` 文件,接着按下面这样增加 `ImportMetaEnv` 的定义:

```typescript [vite-env.d.ts]
/// <reference types="vite/client" />

interface ViteTypeOptions {
// 添加这行代码,你就可以将 ImportMetaEnv 的类型设为严格模式,
// 这样就不允许有未知的键值了。
Expand Down
28 changes: 17 additions & 11 deletions guide/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,7 @@ Vite 启动模板默认情况下会设置 `"skipLibCheck": "true"`,以避免

### 客户端类型 {#client-types}

Vite 默认的类型定义是写给它的 Node.js API 的。要将其补充到一个 Vite 应用的客户端代码环境中,请添加一个 `d.ts` 声明文件:

```typescript
/// <reference types="vite/client" />
```

::: details 使用 `compilerOptions.types`

或者,你也可以将 `vite/client` 添加到 `tsconfig.json` 中的 `compilerOptions.types` 下:
Vite 默认的类型定义是写给它的 Node.js API 的。要将其补充到一个 Vite 应用的客户端代码环境中,你可以在 `tsconfig.json` 中的 `compilerOptions.types` 添加 `vite/client`:

```json [tsconfig.json]
{
Expand All @@ -131,7 +123,15 @@ Vite 默认的类型定义是写给它的 Node.js API 的。要将其补充到
}
```

需要注意的是,如果指定了 [`compilerOptions.types`](https://www.typescriptlang.org/tsconfig#types),则只有这些包会被包含在全局作用域内(而不是所有的“@types”包)。
需要注意的是,如果指定了 [`compilerOptions.types`](https://www.typescriptlang.org/tsconfig#types),则只有这些包会被包含在全局作用域内(而不是所有的“@types”包)。这是自 TypeScript 5.9 以来推荐的做法。

::: details Using triple-slash directive

或者,你可以添加一个 `d.ts` 声明文件:

```typescript [vite-env.d.ts]
/// <reference types="vite/client" />
```

:::

Expand All @@ -153,7 +153,13 @@ Vite 默认的类型定义是写给它的 Node.js API 的。要将其补充到
export default content
}
```
- The file containing the reference to `vite/client` (normally `vite-env.d.ts`):
- 如果你正在使用 `compilerOptions.types`,请确保该文件已包含在 `tsconfig.json` 中:
```json [tsconfig.json]
{
"include": ["src", "./vite-env-override.d.ts"]
}
```
- 如果你正在使用三斜线指令,请更新包含对 `vite/client` 引用的文件(通常是 `vite-env.d.ts`):
```ts
/// <reference types="./vite-env-override.d.ts" />
/// <reference types="vite/client" />
Expand Down
2 changes: 2 additions & 0 deletions guide/rolldown.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Rolldown 专注于三个主要原则:

在添加 overrides 之后,重新安装你的依赖并像往常一样启动你的开发服务器或构建你的项目即可,无需进一步的配置更改。

如果你正在启动一个新项目,你也可以像平常一样使用 `create-vite` 来创建 rolldown-vite 项目。最新版本会询问你是否要使用 `rolldown-vite`。

## 已知限制 {#known-limitations}

虽然 Rolldown 的目标是成为 Rollup 的替代品,但还有一些特性正在实现中,以及一些小的有意的行为差异。需要查看完整的列表,请参考 [这个 GitHub PR](https://github.com/vitejs/rolldown-vite/pull/84#issue-2903144667),它会定期更新。
Expand Down
2 changes: 1 addition & 1 deletion guide/static-deploy-github-pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v5
- name: Set up Node
uses: actions/setup-node@v4
uses: actions/setup-node@v5
with:
node-version: lts/*
cache: 'npm'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"private": true,
"license": "CC BY-NC-SA 4.0",
"devDependencies": {
"@shikijs/vitepress-twoslash": "^3.12.2",
"@shikijs/vitepress-twoslash": "^3.13.0",
"@type-challenges/utils": "^0.1.1",
"@types/express": "^5.0.3",
"@types/node": "^20.9.2",
Expand Down
Loading