Skip to content

Commit 13b3243

Browse files
authored
Merge pull request #1531 from vitejs/sync-93e15e7a-1
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 93e15e7
2 parents f702287 + c962e74 commit 13b3243

8 files changed

Lines changed: 94 additions & 72 deletions

File tree

config/build-options.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,10 @@ npm add -D esbuild
162162

163163
## build.dynamicImportVarsOptions {#build-dynamicimportvarsoptions}
164164

165-
- **类型:** [`RollupDynamicImportVarsOptions`](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#options)
166-
- **相关内容:** [动态导入](/guide/features#dynamic-import)
165+
- **类型:** `{ include?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[] }`
166+
- **相关内容:** [Dynamic Import](/guide/features#dynamic-import)
167167

168-
传递给 [@rollup/plugin-dynamic-import-vars](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars) 的选项。
169-
170-
<!-- TODO: we need to have a more detailed explanation here as we no longer use @rollup/plugin-dynamic-import-vars. we should say it's compatible with it though -->
168+
是否转换带有变量的动态导入。
171169

172170
## build.lib {#build-lib}
173171

@@ -223,9 +221,7 @@ export default defineConfig({
223221

224222
::: tip
225223

226-
如果你想在构建后的代码中引用许可文件,可以使用 `build.rolldownOptions.output.postBanner` 在文件顶部注入注释。例如:
227-
228-
<!-- TODO: add a link for output.postBanner above to Rolldown's documentation -->
224+
如果您想在构建的代码中引用许可证文件,可以使用 [`build.rolldownOptions.output.postBanner`](https://rolldown.rs/reference/OutputOptions.postBanner#postbanner) 在文件顶部插入注释。例如:
229225

230226
```js twoslash [vite.config.js]
231227
import { defineConfig } from 'vite'

config/dep-optimization-options.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,12 @@ export default defineConfig({
5353

5454
## optimizeDeps.rolldownOptions <NonInheritBadge /> {#optimizedeps-rolldownoptions}
5555

56-
- **类型:** [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)`<``RolldownOptions`, `'input' | 'logLevel' | 'output'> & {
56+
- **类型:** [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)`<`[`RolldownOptions`](https://rolldown.rs/reference/), `'input' | 'logLevel' | 'output'> & {
5757
output?: [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)`<`
58-
`RolldownOutputOptions`,
58+
[`RolldownOutputOptions`](https://rolldown.rs/reference/),
5959
`'format' | 'sourcemap' | 'dir' | 'banner'>`
6060
`}`
6161

62-
<!-- TODO: add link to RolldownOptions -->
63-
<!-- TODO: add link to RolldownOutputOptions -->
64-
6562
在依赖扫描和优化过程中传递给 Rolldown 的选项。
6663

6764
某些选项进行了省略,因为修改它们与 Vite 的优化方案并不兼容。

config/shared-options.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ declare const __APP_VERSION__: string
9494
- **类型:**
9595
`Record<string, string> | Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>`
9696
97-
将会被传递到 `@rollup/plugin-alias` 作为 [entries 的选项](https://github.com/rollup/plugins/tree/master/packages/alias#entries)。也可以是一个对象,或一个 `{ find, replacement, customResolver }` 的数组
97+
定义用于替换 `import``require` 语句中值的别名。其工作方式类似于 [`@rollup/plugin-alias`](https://github.com/rollup/plugins/tree/master/packages/alias)
9898
99-
<!-- TODO: we need to have a more detailed explanation here as we no longer use @rollup/plugin-alias. we should say it's compatible with it though -->
99+
条目的顺序很重要,因为最先定义的规则会首先应用。
100100
101101
当使用文件系统路径的别名时,请始终使用绝对路径。相对路径的别名值会原封不动地被使用,因此无法被正常解析。
102102
@@ -106,6 +106,40 @@ declare const __APP_VERSION__: string
106106
如果你已经为 [SSR 外部化的依赖](/guide/ssr.md#ssr-externals) 配置了别名,你可能想要为真实的 `node_modules` 包配别名。[Yarn](https://classic.yarnpkg.com/en/docs/cli/add/#toc-yarn-add-alias) 和 [pnpm](https://pnpm.io/aliases/) 都支持通过 `npm:` 前缀配置别名。
107107
:::
108108
109+
### Object Format (`Record<string, string>`)
110+
111+
对象格式允许将别名指定为键,并将对应的值指定为实际的导入值。例如:
112+
113+
```js
114+
resolve: {
115+
alias: {
116+
utils: '../../../utils',
117+
'batman-1.0.0': './joker-1.5.0'
118+
}
119+
}
120+
```
121+
122+
### Array Format (`Array<{ find: string | RegExp, replacement: string, customResolver?: ResolverFunction | ResolverObject }>`)
123+
124+
数组格式允许将别名指定为对象,这对于复杂的键/值对非常有用。
125+
126+
```js
127+
resolve: {
128+
alias: [
129+
{ find: 'utils', replacement: '../../../utils' },
130+
{ find: 'batman-1.0.0', replacement: './joker-1.5.0' },
131+
]
132+
}
133+
```
134+
135+
`find` 是一个正则表达式时,`replacement` 可以使用[替换模式](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement),例如 `$1`。例如,要删除带有扩展名的字符串,可以使用如下模式:
136+
137+
```js
138+
{ find:/^(.*)\.js$/, replacement: '$1.alias' }
139+
```
140+
141+
`customResolver` 选项可用于为单个别名提供单独的模块解析。
142+
109143
## resolve.dedupe {#resolve-dedupe}
110144

111145
- **类型:** `string[]`

guide/api-plugin.md

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# 插件 API {#plugin-api}
22

3-
Vite 插件扩展了设计出色的 Rollup 接口,带有一些 Vite 独有的配置项。因此,你只需要编写一个 Vite 插件,就可以同时为开发环境和生产环境工作
3+
Vite 插件扩展了 Rolldown 的插件接口,添加了一些 Vite 特有的选项。因此,您只需编写一次 Vite 插件,即可使其同时适用于开发环境和构建环境
44

5-
**推荐在阅读下面的章节之前,首先阅读下 [Rollup 插件文档](https://cn.rollupjs.org/plugin-development/)**
6-
7-
<!-- TODO: update the link above to Rolldown's documentation -->
5+
**建议先阅读 [Rolldown 的插件文档](https://rolldown.rs/apis/plugin-api),然后再阅读以下章节。**
86

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

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

2018
## 约定 {#conventions}
2119

22-
如果插件不使用 Vite 特有的钩子,可以作为 [兼容 Rollup 的插件](#rollup-plugin-compatibility) 来实现,推荐使用 [Rollup 插件名称约定](https://cn.rollupjs.org/plugin-development/#conventions)
20+
如果插件不使用 Vite 特定的钩子,并且可以作为 [兼容的 Rolldown 插件](#rolldown-plugin-compatibility) 实现,则建议使用 [Rolldown 插件命名约定](https://rolldown.rs/apis/plugin-api#conventions)
2321

24-
- Rollup 插件应该有一个带 `rollup-plugin-` 前缀、语义清晰的名称。
25-
- 在 package.json 中包含 `rollup-plugin``vite-plugin` 关键字。
22+
- Rolldown 插件应该有一个带 `rolldown-plugin-` 前缀、语义清晰的名称。
23+
- 在 package.json `keywords` 字段中包含 `rolldown-plugin``vite-plugin` 关键字。
2624

27-
这样,插件也可以用于纯 Rollup 或基于 WMR 的项目。
25+
这样,插件也可以用于纯 Rollup 或基于 Rollup 的项目。
2826

2927
对于 Vite 专属的插件:
3028

@@ -79,7 +77,7 @@ export default defineConfig({
7977
## 简单示例 {#simple-examples}
8078

8179
:::tip
82-
通常的惯例是创建一个 Vite/Rollup 插件作为一个返回实际插件对象的工厂函数。该函数可以接受允许用户自定义插件行为的选项。
80+
通常的惯例是创建一个 Vite/Rolldown/Rollup 插件作为一个返回实际插件对象的工厂函数。该函数可以接受允许用户自定义插件行为的选项。
8381
:::
8482

8583
### 转换自定义文件类型 {#transforming-custom-file-types}
@@ -140,37 +138,37 @@ import { msg } from 'virtual:my-module'
140138
console.log(msg)
141139
```
142140

143-
虚拟模块在 Vite(以及 Rollup)中都以 `virtual:` 为前缀,作为面向用户路径的一种约定。如果可能的话,插件名应该被用作命名空间,以避免与生态系统中的其他插件发生冲突。举个例子,`vite-plugin-posts` 可以要求用户导入一个 `virtual:posts` 或者 `virtual:posts/helpers` 虚拟模块来获得编译时信息。在内部,使用了虚拟模块的插件在解析时应该将模块 ID 加上前缀 `\0`,这一约定来自 rollup 生态。这避免了其他插件尝试处理这个 ID(比如 node 解析),而例如 sourcemap 这些核心功能可以利用这一信息来区别虚拟模块和正常文件。`\0` 在导入 URL 中不是一个被允许的字符,因此我们需要在导入分析时替换掉它们。一个虚拟 ID 为 `\0{id}` 在浏览器中开发时,最终会被编码为 `/@id/__x00__{id}`。这个 id 会被解码回进入插件处理管线前的样子,因此这对插件钩子的代码是不可见的。
141+
虚拟模块在 Vite(以及 Rolldown/ Rollup)中都以 `virtual:` 为前缀,作为面向用户路径的一种约定。如果可能的话,插件名应该被用作命名空间,以避免与生态系统中的其他插件发生冲突。举个例子,`vite-plugin-posts` 可以要求用户导入一个 `virtual:posts` 或者 `virtual:posts/helpers` 虚拟模块来获得编译时信息。在内部,使用了虚拟模块的插件在解析时应该将模块 ID 加上前缀 `\0`,这一约定来自 rollup 生态。这避免了其他插件尝试处理这个 ID(比如 node 解析),而例如 sourcemap 这些核心功能可以利用这一信息来区别虚拟模块和正常文件。`\0` 在导入 URL 中不是一个被允许的字符,因此我们需要在导入分析时替换掉它们。一个虚拟 ID 为 `\0{id}` 在浏览器中开发时,最终会被编码为 `/@id/__x00__{id}`。这个 id 会被解码回进入插件处理管线前的样子,因此这对插件钩子的代码是不可见的。
144142

145143
请注意,直接从真实文件派生出来的模块,就像单文件组件中的脚本模块(如.vue 或 .svelte SFC)不需要遵循这个约定。SFC 通常在处理时生成一组子模块,但这些模块中的代码可以映射回文件系统。对这些子模块使用 `\0` 会使 sourcemap 无法正常工作。
146144

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

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

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

153-
- [`options`](https://cn.rollupjs.org/plugin-development/#options)
154-
- [`buildStart`](https://cn.rollupjs.org/plugin-development/#buildstart)
151+
- [`options`](https://rolldown.rs/reference/interface.plugin#options)
152+
- [`buildStart`](https://rolldown.rs/reference/Interface.Plugin#buildstart)
155153

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

158-
- [`resolveId`](https://cn.rollupjs.org/plugin-development/#resolveid)
159-
- [`load`](https://cn.rollupjs.org/plugin-development/#load)
160-
- [`transform`](https://cn.rollupjs.org/plugin-development/#transform)
156+
- [`resolveId`](https://rolldown.rs/reference/Interface.Plugin#resolveid)
157+
- [`load`](https://rolldown.rs/reference/Interface.Plugin#load)
158+
- [`transform`](https://rolldown.rs/reference/Interface.Plugin#transform)
161159

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

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

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

168-
- [`buildEnd`](https://cn.rollupjs.org/plugin-development/#buildend)
169-
- [`closeBundle`](https://cn.rollupjs.org/plugin-development/#closebundle)
166+
- [`buildEnd`](https://rolldown.rs/reference/Interface.Plugin#buildend)
167+
- [`closeBundle`](https://rolldown.rs/reference/Interface.Plugin#closebundle)
170168

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

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

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

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

488-
请注意,这与钩子的排序是分开的,钩子的顺序仍然会受到它们的 `order` 属性的影响,这一点 [和 Rollup 钩子的表现一样](https://cn.rollupjs.org/plugin-development/#build-hooks)。
489-
486+
请注意,这与钩子排序是分开的,钩子仍然像往常一样单独受其 [`order` 属性](https://rolldown.rs/reference/TypeAlias.ObjectHook#order) 的约束。
490487
## 情景应用 {#conditional-application}
491488

492489
默认情况下插件在开发(serve)和构建(build)模式中都会调用。如果插件只需要在预览或构建期间有条件地应用,请使用 `apply` 属性指明它们仅在 `'build'``'serve'` 模式时调用:
@@ -509,21 +506,22 @@ apply(config, { command }) {
509506
}
510507
```
511508

512-
## Rollup 插件兼容性 {#rollup-plugin-compatibility}
509+
## Rolldown 插件兼容性 {#rolldown-plugin-compatibility}
513510

514-
相当数量的 Rollup 插件将直接作为 Vite 插件工作(例如:`@rollup/plugin-alias``@rollup/plugin-json`),但并不是所有的,因为有些插件钩子在非构建式的开发服务器上下文中没有意义。
511+
相当数量的 Rolldown / Rollup 插件将直接作为 Vite 插件工作(例如:`@rollup/plugin-alias``@rollup/plugin-json`),但并不是所有的,因为有些插件钩子在非构建式的开发服务器上下文中没有意义。
515512

516-
一般来说,只要 Rollup 插件符合以下标准,它就应该像 Vite 插件一样工作:
513+
一般来说,只要 Rolldown / Rollup 插件符合以下标准,它就应该像 Vite 插件一样工作:
517514

518-
- 没有使用 [`moduleParsed`](https://cn.rollupjs.org/plugin-development/#moduleparsed) 钩子。
515+
- 它不使用 `moduleParsed` 钩子。
516+
- 它不依赖 Rolldown 特有的选项,例如 `transform.inject`
519517
- 它在打包钩子和输出钩子之间没有很强的耦合。
520518

521-
如果一个 Rollup 插件只在构建阶段有意义,则在 `build.rollupOptions.plugins` 下指定即可。它的工作原理与 Vite 插件的 `enforce: 'post'``apply: 'build'` 相同。
519+
如果一个 Rolldown / Rollup 插件只在构建阶段有意义,则在 `build.rolldownOptions.plugins` 下指定即可。它的工作原理与 Vite 插件的 `enforce: 'post'``apply: 'build'` 相同。
522520

523-
你也可以用 Vite 独有的属性来扩展现有的 Rollup 插件:
521+
你也可以用 Vite 独有的属性来扩展现有的 Rolldown / Rollup 插件:
524522

525523
```js [vite.config.js]
526-
import example from 'rollup-plugin-example'
524+
import example from 'rolldown-plugin-example'
527525
import { defineConfig } from 'vite'
528526
529527
export default defineConfig({
@@ -556,7 +554,7 @@ Vite 暴露了 [`@rollup/pluginutils` 的 `createFilter`](https://github.com/rol
556554

557555
### 钩子过滤功能 {#hook-filters}
558556

559-
Rolldown 引入了[钩子过滤器功能](https://rolldown.rs/plugins/hook-filters) ,以减少 Rust 和 JavaScript 运行时之间的通信开销。此功能允许插件指定确定何时调用钩子的模式,从而通过避免不必要的钩子调用来提高性能。
557+
Rolldown 引入了[钩子过滤器功能](https://rolldown.rs/apis/plugin-api/hook-filters) ,以减少 Rust 和 JavaScript 运行时之间的通信开销。此功能允许插件指定确定何时调用钩子的模式,从而通过避免不必要的钩子调用来提高性能。
560558

561559
Rollup 4.38.0+ 和 Vite 6.3.0+ 也支持此功能。为了使你的插件向后兼容旧版本,请确保在钩子处理程序中也运行该过滤器。
562560

guide/build.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252

5353
构建过程可以通过多种 [构建配置选项](/config/#build-options) 来自定义构建。具体来说,你可以通过 `build.rolldownOptions` 直接调整底层的 [Rolldown 选项](https://rolldown.rs/reference/)
5454

55-
<!-- TODO: update the link above and below to Rolldown's documentation -->
56-
5755
```js [vite.config.js]
5856
export default defineConfig({
5957
build: {
@@ -86,8 +84,6 @@ window.addEventListener('vite:preloadError', (event) => {
8684

8785
你可以使用 `vite build --watch` 来启用 rollup 的监听器。或者,你可以直接通过 `build.watch` 调整底层的 [`WatcherOptions`](https://rolldown.rs/reference/InputOptions.watch) 选项:
8886

89-
<!-- TODO: update the link above to Rolldown's documentation -->
90-
9187
```js [vite.config.js]
9288
export default defineConfig({
9389
build: {

guide/dep-pre-bundling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default defineConfig({
5757

5858
`include``exclude` 都可以用来处理这个问题。如果依赖项很大(包含很多内部模块)或者是 CommonJS,那么你应该包含它;如果依赖项很小,并且已经是有效的 ESM,则可以排除它,让浏览器直接加载它。
5959

60-
你可以通过 [`optimizeDeps.esbuildOptions` 选项](/config/dep-optimization-options.md#optimizedeps-esbuildoptions) 进一步自定义 esbuild。例如,添加一个 esbuild 插件来处理依赖项中的特殊文件,或者更改 [build `target`](https://esbuild.github.io/api/#target)
60+
你还可以使用 [`optimizeDeps.rolldownOptions` 选项](/config/dep-optimization-options.md#optimizedeps-rolldownoptions) 进一步自定义 Rolldown。例如,添加 Rolldown 插件来处理依赖项中的特殊文件,或者更改 [构建 `target`](https://rolldown.rs/reference/InputOptions.transform#target)
6161

6262
<!-- TODO: update the link above to Rolldown's documentation -->
6363

guide/features.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ const module = await import(`./dir/${file}.js`)
642642

643643
注意变量仅代表一层深的文件名。如果 `file``foo/bar`,导入将会失败。对于更进阶的使用详情,你可以使用 [glob 导入](#glob-import) 功能。
644644

645+
另请注意,动态导入必须符合以下规则才能被打包:
646+
647+
- 导入语句必须以 `./``../` 开头:``import(`./dir/${foo}.js`)`` 有效,但 ``import(`${foo}.js`)`` 无效。
648+
- 导入语句必须以文件扩展名结尾:``import(`./dir/${foo}.js`)`` 有效,但 ``import(`./dir/${foo}`)`` 无效。
649+
- 导入到自身目录时,必须指定文件名模式:``import(`./prefix-${foo}.js`)`` 有效,但 ``import(`./${foo}.js`)`` 无效。
650+
651+
这些规则旨在防止意外导入不应打包的文件。例如,如果没有这些规则,`import(foo)` 会将文件系统中的所有内容都打包。
652+
645653
## WebAssembly {#webassembly}
646654

647655
预编译的 `.wasm` 文件可以通过 `?init` 来导入。

0 commit comments

Comments
 (0)