@rolldown / pluginutils 
插件实用程序 利差。
包括用于插件挂钩过滤器的regex帮助器、可组合过滤器表达式,以及用于过滤仅支持vite - service的插件的帮助器。
安装
pnpm add -D @rolldown/pluginutils
用法
import { exactRegex, prefixRegex, makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils'
所有筛选器帮助器也通过 /filter 子路径:
import { and, or, id, include } from '@rolldown/pluginutils/filter'
Regex助手
exactRegex
- 类型:
(str: string, flags?: string) => RegExp
构造一个 RegExp 匹配指定的字符串。可用作插件钩子过滤器。
import { exactRegex } from '@rolldown/pluginutils'
const plugin = {
name: 'plugin',
resolveId: {
filter: { id: exactRegex('foo') },
handler(id) {}, // only called for `foo`
},
}
prefixRegex
- 类型:
(str: string, flags?: string) => RegExp
构造一个 RegExp 匹配以指定前缀开头的值。
import { prefixRegex } from '@rolldown/pluginutils'
const plugin = {
name: 'plugin',
resolveId: {
filter: { id: prefixRegex('foo') },
handler(id) {}, // called for IDs starting with `foo`
},
}
makeIdFiltersToMatchWithQuery
- 类型:
(input: string | RegExp | (string | RegExp)[]) => string | RegExp | (string | RegExp)[]
转换id筛选器,使其也匹配包含查询字符串的id。
import { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils'
const plugin = {
name: 'plugin',
transform: {
filter: { id: makeIdFiltersToMatchWithQuery(['**/*.js', /\.ts$/]) },
// Matches:
// foo.js, foo.js?foo, foo.txt?foo.js,
// foo.ts, foo.ts?foo, foo.txt?foo.ts
handler(code, id) {},
},
}
可组合过滤器
可组合过滤器表达式 对于一个简单的用例 id/include/exclude 是不够的。例如,当一个插件需要合并时 id, moduleType, code, query 条件。
import { and, code, id, include, interpreter, moduleType, or } from '@rolldown/pluginutils'
const expr = include(and(or(id(/\.tsx?$/), id(/\.jsx?$/)), moduleType('tsx'), code(/import React/)))
interpreter(expr, sourceCode, sourceId, 'tsx') // boolean
建筑商
| 建设者 | 描述 |
|---|---|
and(...exprs) | 所有操作数必须匹配。 |
or(...exprs) | 至少有一个操作数必须匹配。 |
not(expr) | 对操作数求反。 |
id(pattern, params?) | 匹配模块id。 pattern 是 string 或 RegExp。 params.cleanUrl 在匹配之前剥离查询/哈希。 |
importerId(pattern, params?) | 匹配进口商的身份证。形状相同 id。 |
moduleType(type) | 匹配Rolldown的模块类型('js', 'jsx', 'ts', 'tsx', 'json', 'text', 'base64', 'dataurl', 'binary', 'empty',或自定义字符串)。 |
code(pattern) | 匹配模块源。 string 匹配与 includes; RegExp 与 test。 |
query(key, pattern) | 匹配单个查询参数。 pattern 是 boolean (关键业务/真实), string (精确值),或 RegExp (价值模式)。 |
queries(obj) | 缩写 and(...) 在多个 query 条目。 |
include(expr) | 顶层包装标记 expr 作为一个包含规则。 |
exclude(expr) | 顶层包装标记 expr 作为排除规则。 |
interpreter
- 类型:
(exprs, code?, id?, moduleType?, importerId?) => boolean
根据给定的输入计算一个或多个顶级表达式。返回 true 当至少一个 include 是否匹配 exclude 匹配;当没有 include 存在,默认为 true 除非一个 exclude 匹配。
必须提供每个表达式所需的参数。例如,计算an id(...) 不传递的表达式 id 将抛出。
filterVitePlugins
- 类型:
<T>(plugins: T | T[] | null | undefined | false) => T[]
删除针对开发服务器的Vite插件(apply: 'serve')从一个(可能嵌套的)插件数组。插件的 apply 函数是否被调用 command: 'build' 语境决定。在Rolldown配置中重用Vite插件数组时非常有用。
import { defineConfig } from 'rolldown'
import { filterVitePlugins } from '@rolldown/pluginutils'
import viteReact from '@vitejs/plugin-react'
export default defineConfig({
plugins: filterVitePlugins([
viteReact(),
{
name: 'dev-only',
apply: 'serve', // filtered out
// ...
},
]),
})
许可证
麻省理工学院