From 739abc6dd2601e1e856a455ed86e75205f9cc568 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sat, 9 Aug 2025 21:31:17 +0800 Subject: [PATCH] docs: use hook filters in plugins --- docs/guide/api-plugin.md | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/guide/api-plugin.md b/docs/guide/api-plugin.md index e4606161588bb4..2b362ea95b0ea7 100644 --- a/docs/guide/api-plugin.md +++ b/docs/guide/api-plugin.md @@ -89,13 +89,16 @@ export default function myPlugin() { return { name: 'transform-file', - transform(src, id) { - if (fileRegex.test(id)) { + transform: { + filter: { + id: fileRegex, + }, + handler(src, id) { return { code: compileFileToJS(src), map: null, // provide source map if available } - } + }, }, } } @@ -110,21 +113,25 @@ See the example in the [next section](#virtual-modules-convention). Virtual modules are a useful scheme that allows you to pass build time information to the source files using normal ESM import syntax. ```js +import { exactRegex } from '@rolldown/pluginutils' + export default function myPlugin() { const virtualModuleId = 'virtual:my-module' const resolvedVirtualModuleId = '\0' + virtualModuleId return { name: 'my-plugin', // required, will show up in warnings and errors - resolveId(id) { - if (id === virtualModuleId) { + resolveId: { + filter: { id: exactRegex(virtualModuleId) }, + handler() { return resolvedVirtualModuleId - } + }, }, - load(id) { - if (id === resolvedVirtualModuleId) { + load: { + filter: { id: exactRegex(resolvedVirtualModuleId) }, + handler() { return `export const msg = "from virtual module"` - } + }, }, } }