提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.2 KiB

  1. # @rolldown/pluginutils
  2. A utility library for building flexible, composable filter expressions that can be used in plugin hook filters of Rolldown/Vite/Rollup/Unplugin plugins.
  3. ## Installation
  4. ```sh
  5. pnpm add @rolldown/pluginutils
  6. ```
  7. ## Usage
  8. ### Simple Filters
  9. ```ts
  10. import {
  11. exactRegex,
  12. makeIdFiltersToMatchWithQuery,
  13. prefixRegex,
  14. } from '@rolldown/pluginutils';
  15. // Match exactly 'foo.js'
  16. const filter = exactRegex('foo.js');
  17. // Match any id starting with 'lib/'
  18. const prefix = prefixRegex('lib/');
  19. // Match ids with query params (e.g. 'foo.js?bar')
  20. const idFilters = makeIdFiltersToMatchWithQuery(['**/*.js', /\.ts$/]);
  21. // Usage in a plugin to define a hook filter
  22. const myPlugin = {
  23. resolveId: {
  24. filter: {
  25. id: [exactRegex('MY_ID_TO_CHECK'), /some-other-regex/],
  26. },
  27. handler(id) {
  28. // Your code here
  29. },
  30. },
  31. };
  32. ```
  33. ### Composable Filters
  34. > [!WARNING] Composable filters are not yet supported in Vite, Rolldown-Vite or unplugin. They can be used in Rolldown plugins only.
  35. ```ts
  36. import { and, id, include, moduleType, query } from '@rolldown/pluginutils';
  37. // Build a filter expression
  38. const filterExpr = and(
  39. id(/\.ts$/),
  40. moduleType('ts'),
  41. query('foo', true),
  42. );
  43. // Usage in a plugin to define a hook filter
  44. const myPlugin = {
  45. transform: {
  46. filter: [include(filterExpr)],
  47. handler(code, id, options) {
  48. // Your code here
  49. },
  50. },
  51. };
  52. ```
  53. ## API Reference
  54. ### Simple Filters
  55. - `exactRegex(str: string, flags?: string): RegExp` — Matches the exact string.
  56. - `prefixRegex(str: string, flags?: string): RegExp` — Matches values with the given prefix.
  57. - `makeIdFiltersToMatchWithQuery(input: string | RegExp | (string | RegExp)[]): string | RegExp | (string | RegExp)[]` — Adapts filters to match ids with query params.
  58. ### Composable Filters
  59. - `and(...exprs)` / `or(...exprs)` / `not(expr)` — Logical composition of filter expressions.
  60. - `id(pattern, params?)` — Filter by id (string or RegExp).
  61. - `moduleType(type)` — Filter by module type (e.g. 'js', 'tsx', or 'json').
  62. - `code(pattern)` — Filter by code content.
  63. - `query(key, pattern)` — Filter by query parameter.
  64. - `include(expr)` / `exclude(expr)` — Top-level include/exclude wrappers.
  65. - `queries(obj)` — Compose multiple query filters.