diff --git a/guide/api-plugin.md b/guide/api-plugin.md index 3618f110..e48d1dae 100644 --- a/guide/api-plugin.md +++ b/guide/api-plugin.md @@ -549,6 +549,41 @@ normalizePath('foo/bar') // 'foo/bar' Vite 暴露了 [`@rollup/pluginutils` 的 `createFilter`](https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter) 函数,以支持 Vite 独有插件和集成使用标准的 include/exclude 过滤模式,Vite 核心自身也正在使用它。 +### 钩子过滤功能 {#hook-filters} + +Rolldown 引入了[钩子过滤器功能](https://rolldown.rs/plugins/hook-filters) ,以减少 Rust 和 JavaScript 运行时之间的通信开销。此功能允许插件指定确定何时调用钩子的模式,从而通过避免不必要的钩子调用来提高性能。 + +Rollup 4.38.0+ 和 Vite 6.3.0+ 也支持此功能。为了使你的插件向后兼容旧版本,请确保在钩子处理程序中也运行该过滤器。 + +```js +export default function myPlugin() { + const jsFileRegex = /\.js$/ + + return { + name: 'my-plugin', + // Example: only call transform for .js files + transform: { + filter: { + id: jsFileRegex, + }, + handler(code, id) { + // Additional check for backward compatibility + if (!jsFileRegex.test(id)) return null + + return { + code: transformCode(code), + map: null, + } + }, + }, + } +} +``` + +::: tip +[`@rolldown/pluginutils`](https://www.npmjs.com/package/@rolldown/pluginutils)导出一些用于钩子过滤器的实用程序,如 `exactRegex` 和 `prefixRegex`。 +::: + ## 客户端与服务端间通信 {#client-server-communication} 从 Vite 2.9 开始,我们为插件提供了一些实用工具,以帮助处理与客户端的通信。 diff --git a/guide/rolldown.md b/guide/rolldown.md index 2c0cd203..92936dd6 100644 --- a/guide/rolldown.md +++ b/guide/rolldown.md @@ -322,14 +322,9 @@ const plugin = { ### 钩子过滤功能 {#hook-filter-feature} -Rolldown 引入了[钩子过滤功能](https://rolldown.rs/plugins/hook-filters),以减少 Rust 和 JavaScript 运行时之间的通信开销。通过使用此功能,你可以使你的插件性能更高。 -这也在 Rollup 4.38.0+ 和 Vite 6.3.0+ 被支持。为了使你的插件向后兼容较旧的版本,请确保也在钩子处理程序内运行过滤器。 +Rolldown 引入了[钩子过滤功能](https://rolldown.rs/plugins/hook-filters),以减少 Rust 和 JavaScript 运行时之间的通信开销。此功能允许插件指定确定何时调用钩子的模式,从而通过避免不必要的钩子调用来提高性能。 -::: tip - -[`@rolldown/pluginutils`](https://www.npmjs.com/package/@rolldown/pluginutils) 导出了一些用于钩子过滤器的工具函数,例如 `exactRegex` 和 `prefixRegex`。 - -::: +请参阅 [Hook Filters 指南](/guide/api-plugin#hook-filters) 了解更多信息。 ### 在 `load` 或 `transform` 钩子中将内容转换为 JavaScript {#converting-content-to-javascript-in-load-or-transform-hooks}