Skip to content

Commit 06ebc7a

Browse files
authored
Merge pull request #1354 from vitejs/dev
sync into main
2 parents 88ea218 + 73d2524 commit 06ebc7a

8 files changed

Lines changed: 241 additions & 197 deletions

File tree

config/index.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ export default defineConfig(async ({ command, mode }) => {
101101

102102
## 在配置中使用环境变量 {#using-environment-variables-in-config}
103103

104-
环境变量通常可以从 `process.env` 获得
104+
在评估配置文件本身时,可用的环境变量仅限于当前进程环境中已经存在的变量(`process.env`)。Vite 有意推迟加载任何 `.env*` 文件,直到用户配置解析完成之后,因为要加载的文件集合依赖于配置选项如 [`root`](/guide/#index-html-and-project-root)[`envDir`](/config/shared-options.md#envdir),以及最终的 `mode`
105105

106-
注意 Vite 默认是不加载 `.env` 文件的,因为这些文件需要在执行完 Vite 配置后才能确定加载哪一个,举个例子,`root``envDir` 选项会影响加载行为。不过当你的确需要时,你可以使用 Vite 导出的 `loadEnv` 函数来加载指定的 `.env` 文件。
106+
这意味着:在你的 `vite.config.*` 运行时,定义在 `.env``.env.local``.env.[mode]``.env.[mode].local` 中的变量不会自动注入到 `process.env` 中。它们会在稍后自动加载,并通过 `import.meta.env` 暴露给应用程序代码(使用默认的 `VITE_` 前缀过滤器),正如[环境变量和模式](/guide/env-and-mode.html)中所记录的那样。因此,如果你只需要将 `.env*` 文件中的值传递给应用程序,则无需在配置中调用任何内容。
107+
108+
但是,如果 `.env*` 文件中的值必须影响配置本身(例如设置 `server.port`、条件性启用插件或计算 `define` 替换),你可以使用导出的 [`loadEnv`](/guide/api-javascript.html#loadenv) 辅助函数手动加载它们。
107109

108110
```js twoslash
109111
import { defineConfig, loadEnv } from 'vite'
@@ -114,10 +116,14 @@ export default defineConfig(({ mode }) => {
114116
// `VITE_` 前缀。
115117
const env = loadEnv(mode, process.cwd(), '')
116118
return {
117-
// vite 配置
118119
define: {
120+
// Provide an explicit app-level constant derived from an env var.
119121
__APP_ENV__: JSON.stringify(env.APP_ENV),
120122
},
123+
// Example: use an env var to set the dev server port conditionally.
124+
server: {
125+
port: env.APP_PORT ? Number(env.APP_PORT) : 5173,
126+
},
121127
}
122128
})
123129
```

guide/api-environment-plugins.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ Vite 服务器有一个共享的插件管道,但在处理模块时,它总是
3131

3232
## 使用钩子注册新环境 {#registering-new-environments-using-hooks}
3333

34-
插件可以在 `config` 钩子中添加新环境例如,为了有一个专门用于 [RSC](https://react.dev/blog/2023/03/22/react-labs-what-we-have-been-working-on-march-2023#react-server-components) 的模块图)
34+
插件可以在 `config` 钩子中添加新环境例如,[RSC 支持](/plugins/#vitejs-plugin-rsc)使用一个额外的环境来拥有一个带有 `react-server` 条件的独立模块图
3535

3636
```ts
3737
config(config: UserConfig) {
38-
config.environments.rsc ??= {}
38+
return {
39+
environments: {
40+
rsc: {
41+
resolve: {
42+
conditions: ['react-server', ...defaultServerConditions],
43+
},
44+
},
45+
},
46+
}
3947
}
4048
```
4149

@@ -48,8 +56,15 @@ Vite 服务器有一个共享的插件管道,但在处理模块时,它总是
4856

4957
```ts
5058
configEnvironment(name: string, options: EnvironmentOptions) {
59+
// add "workerd" condition to the rsc environment
5160
if (name === 'rsc') {
52-
options.resolve.conditions = // ...
61+
return {
62+
resolve: {
63+
conditions: ['workerd'],
64+
},
65+
}
66+
}
67+
}
5368
```
5469

5570
## `hotUpdate` 钩子 {#the-hotupdate-hook}

guide/features.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ HTML 文件位于 Vite 项目的[最前端和中心](/guide/#index-html-and-proj
213213
- Vue JSX 支持:[@vitejs/plugin-vue-jsx](https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue-jsx)
214214
- React 支持:[@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react)
215215
- React 使用 SWC 的支持:[@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react-swc)
216+
- [React Server Components (RSC)](https://react.dev/reference/rsc/server-components) 支持:[@vitejs/plugin-rsc](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-rsc)
216217

217218
查看 [插件指南](/plugins/) 了解更多信息。
218219

guide/rolldown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ const plugin = {
314314

315315
### 钩子过滤功能 {#hook-filter-feature}
316316

317-
Rolldown 引入了[钩子过滤功能](https://rolldown.rs/guide/plugin-development#plugin-hook-filters),以减少 Rust 和 JavaScript 运行时之间的通信开销。通过使用此功能,你可以使你的插件性能更高。
317+
Rolldown 引入了[钩子过滤功能](https://rolldown.rs/plugins/hook-filters),以减少 Rust 和 JavaScript 运行时之间的通信开销。通过使用此功能,你可以使你的插件性能更高。
318318
这也在 Rollup 4.38.0+ 和 Vite 6.3.0+ 被支持。为了使你的插件向后兼容较旧的版本,请确保也在钩子处理程序内运行过滤器。
319319

320320
::: tip

guide/static-deploy-github-pages.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
- name: Setup Pages
4747
uses: actions/configure-pages@v5
4848
- name: Upload artifact
49-
uses: actions/upload-pages-artifact@v3
49+
uses: actions/upload-pages-artifact@v4
5050
with:
5151
# 上传 dist 文件夹
5252
path: './dist'

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"private": true,
99
"license": "CC BY-NC-SA 4.0",
1010
"devDependencies": {
11-
"@shikijs/vitepress-twoslash": "^3.7.0",
11+
"@shikijs/vitepress-twoslash": "^3.11.0",
1212
"@type-challenges/utils": "^0.1.1",
1313
"@types/express": "^5.0.3",
1414
"@types/node": "^20.9.2",
@@ -22,7 +22,7 @@
2222
"vitepress": "^2.0.0-alpha.7",
2323
"vitepress-plugin-group-icons": "^1.6.1",
2424
"vitepress-plugin-llms": "^1.7.1",
25-
"vue": "^3.5.18",
25+
"vue": "^3.5.19",
2626
"vue-tsc": "^3.0.5",
2727
"yorkie": "^2.0.0"
2828
},

plugins/index.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ Vite 旨在为常见的 web 开发工作提供开箱即用的支持。在搜索
2424

2525
在开发时会将 Babel 替换为 SWC。在生产环境构建期间,若使用了插件则会使用 SWC+esbuild,若没有使用插件则仅会用到 esbuild。对不需要非标准 React 扩展的大型项目,冷启动和模块热替换(HMR)将会有显著提升。
2626

27-
### [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) {#vitejs-plugin-legacy}
27+
### [@vitejs/plugin-rsc](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-rsc)
28+
29+
Vite 通过该插件支持 [React Server Components (RSC)](https://react.dev/reference/rsc/server-components)。它利用 [Environment API](/guide/api-environment) 提供底层原语,React 框架可以使用这些原语来集成 RSC 功能。你可以通过以下方式尝试一个最小的独立 RSC 应用程序:
30+
31+
```bash
32+
npm create vite@latest -- --template rsc
33+
```
34+
35+
阅读[插件文档](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-rsc)了解更多详情。
36+
37+
### [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy)
2838

2939
为打包后的文件提供传统浏览器兼容性支持。
3040

0 commit comments

Comments
 (0)