Skip to content

sync main into dev #1228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025
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
2 changes: 1 addition & 1 deletion .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ Vite is a new breed of frontend build tooling that significantly improves the fr

- A dev server that serves your source files over [native ES modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), with [rich built-in features](https://vite.dev/guide/features.md) and astonishingly fast [Hot Module Replacement (HMR)](https://vite.dev/guide/features.md#hot-module-replacement).

- A [build command](https://vite.dev/guide/build.md) that bundles your code with [Rollup](https://rollupjs.org), pre-configured to output highly optimized static assets for production.
- A [build command](https://vite.dev/guide/build.md) that bundles your code with [Rollup](https://cn.rollupjs.org), pre-configured to output highly optimized static assets for production.

In addition, Vite is highly extensible via its [Plugin API](https://vite.dev/guide/api-plugin.md) and [JavaScript API](https://vite.dev/guide/api-javascript.md) with full typing support.`,
}) as PluginOption,
Expand Down
2 changes: 1 addition & 1 deletion blog/announcing-vite4.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Vite 3 [was released](./announcing-vite3.md) five months ago. npm downloads per

[![Vite 4 Ecosystem](/ecosystem-vite4.png)](https://viteconf.org/2022/replay)

As a showcase of the growth Vite and related projects have experienced, the Vite ecosystem gathered on October 11th at [ViteConf 2022](https://viteconf.org/2022/replay). We saw representatives from the main web framework and tools tell stories of innovation and collaboration. And in a symbolic move, the Rollup team choose that exact day to release [Rollup 3](https://rollupjs.org).
As a showcase of the growth Vite and related projects have experienced, the Vite ecosystem gathered on October 11th at [ViteConf 2022](https://viteconf.org/2022/replay). We saw representatives from the main web framework and tools tell stories of innovation and collaboration. And in a symbolic move, the Rollup team choose that exact day to release [Rollup 3](https://cn.rollupjs.org).

Today, the Vite [team](https://vite.dev/team) with the help of our ecosystem partners, is happy to announce the release of Vite 4, powered during build time by Rollup 3. We've worked with the ecosystem to ensure a smooth upgrade path for this new major. Vite is now using [Rollup 3](https://github.com/vitejs/vite/issues/9870), which allowed us to simplify Vite's internal asset handling and has many improvements. See the [Rollup 3 release notes here](https://github.com/rollup/rollup/releases/tag/v3.0.0).

Expand Down
2 changes: 1 addition & 1 deletion blog/announcing-vite5.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ _2023年11月16日_

Vite 4 发布了将近一年,它为生态系统奠定了坚实的基础。每周的 npm 下载量从250万增加到了750万,因为项目不断在共享基础设施上构建。各种框架不断创新,除了 [Astro](https://astro.build/)、[Nuxt](https://nuxt.com/)、[SvelteKit](https://kit.svelte.dev/)、[Solid Start](https://www.solidjs.com/blog/introducing-solidstart)、[Qwik City](https://qwik.builder.io/qwikcity/overview/) 等等之外,我们看到新的框架加入并使生态系统变得更加强大。[RedwoodJS](https://redwoodjs.com/) 和 [Remix](https://remix.run/) 转向 Vite 为 React 生态系统的进一步采用铺平了道路。[Vitest](https://vitest.dev) 的增长速度甚至比 Vite 还要快。其团队一直在努力工作,并将很快 [发布 Vitest 1.0](https://github.com/vitest-dev/vitest/issues/3596)。当与其他工具一起使用时,如 [Storybook](https://storybook.js.org)、[Nx](https://nx.dev) 和 [Playwright](https://playwright.dev),Vite 的故事不断改善,环境也是如此,Vite 的开发在 [Deno](https://deno.com) 和 [Bun](https://bun.sh) 中都可以工作。

一个月前,我们举办了第二届 [ViteConf](https://viteconf.org/23/replay),由 [StackBlitz](https://stackblitz.com) 主办。像去年一样,生态系统中的大多数项目聚在一起分享想法,并保持连接以持续扩展共同体。我们也看到新的组件补充了元框架工具包,比如 [Volar](https://volarjs.dev/) 和 [Nitro](https://nitro.unjs.io/)。Rollup 团队在同一天发布了 [Rollup 4](https://rollupjs.org),这是去年 Lukas 开创的传统。
一个月前,我们举办了第二届 [ViteConf](https://viteconf.org/23/replay),由 [StackBlitz](https://stackblitz.com) 主办。像去年一样,生态系统中的大多数项目聚在一起分享想法,并保持连接以持续扩展共同体。我们也看到新的组件补充了元框架工具包,比如 [Volar](https://volarjs.dev/) 和 [Nitro](https://nitro.unjs.io/)。Rollup 团队在同一天发布了 [Rollup 4](https://cn.rollupjs.org),这是去年 Lukas 开创的传统。

六个月前,Vite 4.3 [发布了](./announcing-vite4.md)。这个版本显著改善了开发服务器的性能。但是,还有很大的改进空间。在 ViteConf 上,[Evan You 揭示了 Vite 的长期计划,即着手开发 Rolldown](https://www.youtube.com/watch?v=hrdwQHoAp0M),这是 Rollup 的 Rust 版本,具有兼容的 API。一旦准备就绪,我们打算在 Vite Core 中使用它,以执行 Rollup 和 esbuild 的任务。这将意味着构建性能的提升(随着我们将 Vite 本身的性能敏感部分转移到 Rust,开发性能也将在以后提升),以及减少开发和构建之间的不一致性。Rolldown 目前处于早期阶段,团队正准备在年底之前开源代码库。敬请期待!

Expand Down
6 changes: 3 additions & 3 deletions config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ Git LFS 占位符会自动排除在内联之外,因为它们不包含其所表

## build.rollupOptions {#build-rollupoptions}

- **类型:** [`RollupOptions`](https://rollupjs.org/configuration-options/)
- **类型:** [`RollupOptions`](https://cn.rollupjs.org/configuration-options/)

自定义底层的 Rollup 打包配置。这与从 Rollup 配置文件导出的选项相同,并将与 Vite 的内部 Rollup 选项合并。查看 [Rollup 选项文档](https://rollupjs.org/configuration-options/) 获取更多细节。
自定义底层的 Rollup 打包配置。这与从 Rollup 配置文件导出的选项相同,并将与 Vite 的内部 Rollup 选项合并。查看 [Rollup 选项文档](https://cn.rollupjs.org/configuration-options/) 获取更多细节。

## build.commonjsOptions {#build-commonjsoptions}

Expand Down Expand Up @@ -287,7 +287,7 @@ npm add -D terser

## build.watch {#build-watch}

- **类型:** [`WatcherOptions`](https://rollupjs.org/configuration-options/#watch)`| null`
- **类型:** [`WatcherOptions`](https://cn.rollupjs.org/configuration-options/#watch)`| null`
- **默认:** `null`

设置为 `{}` 则会启用 rollup 的监听器。对于只在构建阶段或者集成流程使用的插件很常用。
Expand Down
4 changes: 2 additions & 2 deletions config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export default defineConfig({

是否忽略服务器 sourcemap 中的源文件,用于填充 [`x_google_ignoreList` source map 扩展](https://developer.chrome.com/articles/x-google-ignore-list/)。

对开发服务器来说 `server.sourcemapIgnoreList` 等价于 [`build.rollupOptions.output.sourcemapIgnoreList`](https://rollupjs.org/configuration-options/#output-sourcemapignorelist)。两个配置选项之间的区别在于,rollup 函数使用相对路径调用 `sourcePath`,而 `server.sourcemapIgnoreList` 使用绝对路径调用。在开发过程中,大多数模块的映射和源文件位于同一个文件夹中,因此 `sourcePath` 的相对路径就是文件名本身。在这些情况下,使用绝对路径更加方便。
对开发服务器来说 `server.sourcemapIgnoreList` 等价于 [`build.rollupOptions.output.sourcemapIgnoreList`](https://cn.rollupjs.org/configuration-options/#output-sourcemapignorelist)。两个配置选项之间的区别在于,rollup 函数使用相对路径调用 `sourcePath`,而 `server.sourcemapIgnoreList` 使用绝对路径调用。在开发过程中,大多数模块的映射和源文件位于同一个文件夹中,因此 `sourcePath` 的相对路径就是文件名本身。在这些情况下,使用绝对路径更加方便。

默认情况下,它会排除所有包含 `node_modules` 的路径。你可以传递 `false` 来禁用此行为,或者为了获得完全的控制,可以传递一个函数,该函数接受源路径和 sourcemap 的路径,并返回是否忽略源路径。

Expand All @@ -421,5 +421,5 @@ export default defineConfig({
```

::: tip 注意
需要单独设置 [`server.sourcemapIgnoreList`](#server-sourcemapignorelist) 和 [`build.rollupOptions.output.sourcemapIgnoreList`](https://rollupjs.org/configuration-options/#output-sourcemapignorelist)。`server.sourcemapIgnoreList` 是一个仅适用于服务端的配置,并不从定义好的 rollup 选项中获得其默认值。
需要单独设置 [`server.sourcemapIgnoreList`](#server-sourcemapignorelist) 和 [`build.rollupOptions.output.sourcemapIgnoreList`](https://cn.rollupjs.org/configuration-options/#output-sourcemapignorelist)。`server.sourcemapIgnoreList` 是一个仅适用于服务端的配置,并不从定义好的 rollup 选项中获得其默认值。
:::
2 changes: 1 addition & 1 deletion config/worker-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ worker 打包时的输出类型。

## worker.rollupOptions

- **类型:** [`RollupOptions`](https://rollupjs.org/configuration-options/)
- **类型:** [`RollupOptions`](https://cn.rollupjs.org/configuration-options/)

用于打包 worker 的 Rollup 配置项。
28 changes: 14 additions & 14 deletions guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Vite 插件扩展了设计出色的 Rollup 接口,带有一些 Vite 独有的配置项。因此,你只需要编写一个 Vite 插件,就可以同时为开发环境和生产环境工作。

**推荐在阅读下面的章节之前,首先阅读下 [Rollup 插件文档](https://rollupjs.org/plugin-development/)**
**推荐在阅读下面的章节之前,首先阅读下 [Rollup 插件文档](https://cn.rollupjs.org/plugin-development/)**

## 致插件创作者 {#authoring-a-plugin}

Expand All @@ -17,7 +17,7 @@ Vite 努力秉承开箱即用的原则,因此在创作一款新插件前,请

## 约定 {#conventions}

如果插件不使用 Vite 特有的钩子,可以作为 [兼容 Rollup 的插件](#rollup-plugin-compatibility) 来实现,推荐使用 [Rollup 插件名称约定](https://rollupjs.org/plugin-development/#conventions)。
如果插件不使用 Vite 特有的钩子,可以作为 [兼容 Rollup 的插件](#rollup-plugin-compatibility) 来实现,推荐使用 [Rollup 插件名称约定](https://cn.rollupjs.org/plugin-development/#conventions)。

- Rollup 插件应该有一个带 `rollup-plugin-` 前缀、语义清晰的名称。
- 在 package.json 中包含 `rollup-plugin` 和 `vite-plugin` 关键字。
Expand Down Expand Up @@ -144,31 +144,31 @@ console.log(msg)

## 通用钩子 {#universal-hooks}

在开发中,Vite 开发服务器会创建一个插件容器来调用 [Rollup 构建钩子](https://rollupjs.org/plugin-development/#build-hooks),与 Rollup 如出一辙。
在开发中,Vite 开发服务器会创建一个插件容器来调用 [Rollup 构建钩子](https://cn.rollupjs.org/plugin-development/#build-hooks),与 Rollup 如出一辙。

以下钩子在服务器启动时被调用:

- [`options`](https://rollupjs.org/plugin-development/#options)
- [`buildStart`](https://rollupjs.org/plugin-development/#buildstart)
- [`options`](https://cn.rollupjs.org/plugin-development/#options)
- [`buildStart`](https://cn.rollupjs.org/plugin-development/#buildstart)

以下钩子会在每个传入模块请求时被调用:

- [`resolveId`](https://rollupjs.org/plugin-development/#resolveid)
- [`load`](https://rollupjs.org/plugin-development/#load)
- [`transform`](https://rollupjs.org/plugin-development/#transform)
- [`resolveId`](https://cn.rollupjs.org/plugin-development/#resolveid)
- [`load`](https://cn.rollupjs.org/plugin-development/#load)
- [`transform`](https://cn.rollupjs.org/plugin-development/#transform)

它们还有一个扩展的 `options` 参数,包含其他特定于 Vite 的属性。你可以在 [SSR 文档](/guide/ssr#ssr-specific-plugin-logic) 中查阅更多内容。

一些 `resolveId` 调用的 `importer` 值可能是根目录下的通用 `index.html` 的绝对路径,这是由于 Vite 非打包的开发服务器模式无法始终推断出实际的导入者。对于在 Vite 的解析管道中处理的导入,可以在导入分析阶段跟踪导入者,提供正确的 `importer` 值。

以下钩子在服务器关闭时被调用:

- [`buildEnd`](https://rollupjs.org/plugin-development/#buildend)
- [`closeBundle`](https://rollupjs.org/plugin-development/#closebundle)
- [`buildEnd`](https://cn.rollupjs.org/plugin-development/#buildend)
- [`closeBundle`](https://cn.rollupjs.org/plugin-development/#closebundle)

请注意 [`moduleParsed`](https://rollupjs.org/plugin-development/#moduleparsed) 钩子在开发中是 **不会** 被调用的,因为 Vite 为了性能会避免完整的 AST 解析。
请注意 [`moduleParsed`](https://cn.rollupjs.org/plugin-development/#moduleparsed) 钩子在开发中是 **不会** 被调用的,因为 Vite 为了性能会避免完整的 AST 解析。

[Output Generation Hooks](https://rollupjs.org/plugin-development/#output-generation-hooks)(除了 `closeBundle`) 在开发中是 **不会** 被调用的。你可以认为 Vite 的开发服务器只调用了 `rollup.rollup()` 而没有调用 `bundle.generate()`。
[Output Generation Hooks](https://cn.rollupjs.org/plugin-development/#output-generation-hooks)(除了 `closeBundle`) 在开发中是 **不会** 被调用的。你可以认为 Vite 的开发服务器只调用了 `rollup.rollup()` 而没有调用 `bundle.generate()`。

## Vite 独有钩子 {#vite-specific-hooks}

Expand Down Expand Up @@ -479,7 +479,7 @@ Vite 插件也可以提供钩子来服务于特定的 Vite 目标。这些钩子
- 带有 `enforce: 'post'` 的用户插件
- Vite 后置构建插件(最小化,manifest,报告)

请注意,这与钩子的排序是分开的,钩子的顺序仍然会受到它们的 `order` 属性的影响,这一点 [和 Rollup 钩子的表现一样](https://rollupjs.org/plugin-development/#build-hooks)。
请注意,这与钩子的排序是分开的,钩子的顺序仍然会受到它们的 `order` 属性的影响,这一点 [和 Rollup 钩子的表现一样](https://cn.rollupjs.org/plugin-development/#build-hooks)。

## 情景应用 {#conditional-application}

Expand Down Expand Up @@ -509,7 +509,7 @@ apply(config, { command }) {

一般来说,只要 Rollup 插件符合以下标准,它就应该像 Vite 插件一样工作:

- 没有使用 [`moduleParsed`](https://rollupjs.org/plugin-development/#moduleparsed) 钩子。
- 没有使用 [`moduleParsed`](https://cn.rollupjs.org/plugin-development/#moduleparsed) 钩子。
- 它在打包钩子和输出钩子之间没有很强的耦合。

如果一个 Rollup 插件只在构建阶段有意义,则在 `build.rollupOptions.plugins` 下指定即可。它的工作原理与 Vite 插件的 `enforce: 'post'` 和 `apply: 'build'` 相同。
Expand Down
10 changes: 5 additions & 5 deletions guide/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@

## 自定义构建 {#customizing-the-build}

构建过程可以通过多种 [构建配置选项](/config/#build-options) 来自定义构建。具体来说,你可以通过 `build.rollupOptions` 直接调整底层的 [Rollup 选项](https://rollupjs.org/configuration-options/):
构建过程可以通过多种 [构建配置选项](/config/#build-options) 来自定义构建。具体来说,你可以通过 `build.rollupOptions` 直接调整底层的 [Rollup 选项](https://cn.rollupjs.org/configuration-options/):

```js [vite.config.js]
export default defineConfig({
build: {
rollupOptions: {
// https://rollupjs.org/configuration-options/
// https://cn.rollupjs.org/configuration-options/
},
},
})
Expand All @@ -82,13 +82,13 @@ window.addEventListener('vite:preloadError', (event) => {

## 文件变化时重新构建 {#rebuild-on-files-changes}

你可以使用 `vite build --watch` 来启用 rollup 的监听器。或者,你可以直接通过 `build.watch` 调整底层的 [`WatcherOptions`](https://rollupjs.org/configuration-options/#watch) 选项:
你可以使用 `vite build --watch` 来启用 rollup 的监听器。或者,你可以直接通过 `build.watch` 调整底层的 [`WatcherOptions`](https://cn.rollupjs.org/configuration-options/#watch) 选项:

```js [vite.config.js]
export default defineConfig({
build: {
watch: {
// https://rollupjs.org/configuration-options/#watch
// https://cn.rollupjs.org/configuration-options/#watch
},
},
})
Expand Down Expand Up @@ -306,7 +306,7 @@ dist/my-lib.umd.cjs 0.30 kB / gzip: 0.16 kB
:::

::: warning 进阶用法
库模式包括了一种简单而又有见地的配置,适用于面向浏览器和 JS 框架的库。如果你正在构建非面向浏览器的库,或需要高级构建流程,可以直接使用 [Rollup](https://rollupjs.org) 或 [esbuild](https://esbuild.github.io)。
库模式包括了一种简单而又有见地的配置,适用于面向浏览器和 JS 框架的库。如果你正在构建非面向浏览器的库,或需要高级构建流程,可以直接使用 [Rollup](https://cn.rollupjs.org) 或 [esbuild](https://esbuild.github.io)。
:::

## 进阶基础路径选项 {#advanced-base-options}
Expand Down
2 changes: 1 addition & 1 deletion guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Vite(法语意为 "快速的",发音 `/vit/`<button id="play-vite-audio" onc

- 一个开发服务器,它基于 [原生 ES 模块](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) 提供了 [丰富的内建功能](./features),如速度快到惊人的 [模块热替换(HMR)](./features#hot-module-replacement)。

- 一套构建指令,它使用 [Rollup](https://rollupjs.org) 打包你的代码,并且它是预配置的,可输出用于生产环境的高度优化过的静态资源。
- 一套构建指令,它使用 [Rollup](https://cn.rollupjs.org) 打包你的代码,并且它是预配置的,可输出用于生产环境的高度优化过的静态资源。

Vite 是一种具有明确建议的工具,具备合理的默认设置。您可以在 [功能指南](./features) 中了解 Vite 的各种可能性。通过 [插件](./using-plugins),Vite 支持与其他框架或工具的集成。如有需要,您可以通过 [配置部分](../config/) 自定义适应你的项目。

Expand Down
6 changes: 3 additions & 3 deletions guide/migration-from-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ Vite 2 使用了一套完全重定义的,扩展了 Rollup 插件的接口。

一些将 v1 插件迁移到 v2 的提示:

- `resolvers` -> 使用 [`resolveId`](https://rollupjs.org/guide/en/#resolveid) 钩子
- `transforms` -> 使用 [`transform`](https://rollupjs.org/guide/en/#transform) 钩子
- `resolvers` -> 使用 [`resolveId`](https://cn.rollupjs.org/guide/en/#resolveid) 钩子
- `transforms` -> 使用 [`transform`](https://cn.rollupjs.org/guide/en/#transform) 钩子
- `indexHtmlTransforms` -> 使用 [`transformIndexHtml`](./api-plugin#transformindexhtml) 钩子
- 虚拟文件支持 -> 使用 [`resolveId`](https://rollupjs.org/guide/en/#resolveid) + [`load`](https://rollupjs.org/guide/en/#load) 钩子
- 虚拟文件支持 -> 使用 [`resolveId`](https://cn.rollupjs.org/guide/en/#resolveid) + [`load`](https://cn.rollupjs.org/guide/en/#load) 钩子
- 添加 `alias`,`define` 或其他配置项 -> 使用 [`config`](./api-plugin#config) 钩子

由于大多数逻辑应通过插件钩子实现,而无需使用中间件,因此对中间件的需求大大减少。内部服务器应用现在看起来像旧版的 [connect](https://github.com/senchalabs/connect) 实例,而不是 Koa。
2 changes: 1 addition & 1 deletion guide/migration-from-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Vite v3 默认在 SSR 构建时使用 ESM 格式。当使用 ESM 时,[SSR 外

如果你无法在你的 SSR 项目中使用 ESM,你可以设置 `ssr.format: 'cjs'` 来生成一个 CJS 格式的产物。在这种情况下,会使用和 Vite v2 相同的外部化策略。

同样 [`build.rollupOptions.output.inlineDynamicImports`](https://rollupjs.org/guide/en/#outputinlinedynamicimports) 现在在 `ssr.target` 是 `node` 时,也默认置为了 `false`。`inlineDynamicImports` 它会改变执行顺序,并且 node 构建不需要打包到单个文件。
同样 [`build.rollupOptions.output.inlineDynamicImports`](https://cn.rollupjs.org/guide/en/#outputinlinedynamicimports) 现在在 `ssr.target` 是 `node` 时,也默认置为了 `false`。`inlineDynamicImports` 它会改变执行顺序,并且 node 构建不需要打包到单个文件。

## 其他一般性变化 {#general-changes}

Expand Down
Loading