Gathering detailed insights and metrics for @rollup/pluginutils
Gathering detailed insights and metrics for @rollup/pluginutils
Gathering detailed insights and metrics for @rollup/pluginutils
Gathering detailed insights and metrics for @rollup/pluginutils
rollup-pluginutils
Functionality commonly needed by Rollup plugins
@rolldown/pluginutils
A utility library for building flexible, composable filter expressions that can be used in plugin hook filters of Rolldown/Vite/Rollup/Unplugin plugins.
@jamesernator/rollup-pluginutils
Functionality commonly needed by Rollup plugins
@trusktr/rollup-pluginutils
Functionality commonly needed by Rollup plugins
🍣 The one-stop shop for official Rollup plugins
npm install @rollup/pluginutils
Typescript
Module System
Min. Node Version
Node Version
NPM Version
92.5
Supply Chain
99.4
Quality
84.3
Maintenance
100
Vulnerability
99.6
License
JavaScript (75.16%)
TypeScript (24.83%)
Shell (0.01%)
Total Downloads
2,441,860,902
Last Day
1,316,897
Last Week
24,031,100
Last Month
104,803,383
Last Year
950,678,266
MIT License
3,721 Stars
1,228 Commits
605 Forks
35 Watchers
6 Branches
270 Contributors
Updated on Sep 03, 2025
Latest Version
5.2.0
Package Id
@rollup/pluginutils@5.2.0
Unpacked Size
59.39 kB
Size
9.74 kB
File Count
8
NPM Version
10.8.2
Node Version
20.19.2
Published on
Jun 17, 2025
Cumulative downloads
Total Downloads
Last Day
-9%
1,316,897
Compared to previous day
Last Week
1.1%
24,031,100
Compared to previous week
Last Month
5.3%
104,803,383
Compared to previous month
Last Year
41.7%
950,678,266
Compared to previous year
3
1
A set of utility functions commonly used by 🍣 Rollup plugins.
The plugin utils require an LTS Node version (v14.0.0+) and Rollup v1.20.0+.
Using npm:
1npm install @rollup/pluginutils --save-dev
1import utils from '@rollup/pluginutils'; 2//...
Available utility functions are listed below:
Note: Parameter names immediately followed by a ?
indicate that the parameter is optional.
Adds an extension to a module ID if one does not exist.
Parameters: (filename: String, ext?: String)
Returns: String
1import { addExtension } from '@rollup/pluginutils'; 2 3export default function myPlugin(options = {}) { 4 return { 5 resolveId(code, id) { 6 // only adds an extension if there isn't one already 7 id = addExtension(id); // `foo` -> `foo.js`, `foo.js` -> `foo.js` 8 id = addExtension(id, '.myext'); // `foo` -> `foo.myext`, `foo.js` -> `foo.js` 9 } 10 }; 11}
Attaches Scope
objects to the relevant nodes of an AST. Each Scope
object has a scope.contains(name)
method that returns true
if a given name is defined in the current scope or a parent scope.
Parameters: (ast: Node, propertyName?: String)
Returns: Object
See @rollup/plugin-inject or @rollup/plugin-commonjs for an example of usage.
1import { attachScopes } from '@rollup/pluginutils'; 2import { walk } from 'estree-walker'; 3 4export default function myPlugin(options = {}) { 5 return { 6 transform(code) { 7 const ast = this.parse(code); 8 9 let scope = attachScopes(ast, 'scope'); 10 11 walk(ast, { 12 enter(node) { 13 if (node.scope) scope = node.scope; 14 15 if (!scope.contains('foo')) { 16 // `foo` is not defined, so if we encounter it, 17 // we assume it's a global 18 } 19 }, 20 leave(node) { 21 if (node.scope) scope = scope.parent; 22 } 23 }); 24 } 25 }; 26}
Constructs a filter function which can be used to determine whether or not certain modules should be operated upon.
Parameters: (include?: <picomatch>, exclude?: <picomatch>, options?: Object)
Returns: (id: string | unknown) => boolean
include
and exclude
Type: String | RegExp | Array[...String|RegExp]
A valid picomatch
pattern, or array of patterns. If options.include
is omitted or has zero length, filter will return true
by default. Otherwise, an ID must match one or more of the picomatch
patterns, and must not match any of the options.exclude
patterns.
Note that picomatch
patterns are very similar to minimatch
patterns, and in most use cases, they are interchangeable. If you have more specific pattern matching needs, you can view this comparison table to learn more about where the libraries differ.
options
resolve
Type: String | Boolean | null
Optionally resolves the patterns against a directory other than process.cwd()
. If a String
is specified, then the value will be used as the base directory. Relative paths will be resolved against process.cwd()
first. If false
, then the patterns will not be resolved against any directory. This can be useful if you want to create a filter for virtual module names.
1import { createFilter } from '@rollup/pluginutils'; 2 3export default function myPlugin(options = {}) { 4 // assume that the myPlugin accepts options of `options.include` and `options.exclude` 5 var filter = createFilter(options.include, options.exclude, { 6 resolve: '/my/base/dir' 7 }); 8 9 return { 10 transform(code, id) { 11 if (!filter(id)) return; 12 13 // proceed with the transformation... 14 } 15 }; 16}
Transforms objects into tree-shakable ES Module imports.
Parameters: (data: Object, options: DataToEsmOptions)
Returns: String
data
Type: Object
An object to transform into an ES module.
options
Type: DataToEsmOptions
Note: Please see the TypeScript definition for complete documentation of these options
1import { dataToEsm } from '@rollup/pluginutils'; 2 3const esModuleSource = dataToEsm( 4 { 5 custom: 'data', 6 to: ['treeshake'] 7 }, 8 { 9 compact: false, 10 indent: '\t', 11 preferConst: true, 12 objectShorthand: true, 13 namedExports: true, 14 includeArbitraryNames: false 15 } 16); 17/* 18Outputs the string ES module source: 19 export const custom = 'data'; 20 export const to = ['treeshake']; 21 export default { custom, to }; 22*/
Extracts the names of all assignment targets based upon specified patterns.
Parameters: (param: Node)
Returns: Array[...String]
param
Type: Node
An acorn
AST Node.
1import { extractAssignedNames } from '@rollup/pluginutils'; 2import { walk } from 'estree-walker'; 3 4export default function myPlugin(options = {}) { 5 return { 6 transform(code) { 7 const ast = this.parse(code); 8 9 walk(ast, { 10 enter(node) { 11 if (node.type === 'VariableDeclarator') { 12 const declaredNames = extractAssignedNames(node.id); 13 // do something with the declared names 14 // e.g. for `const {x, y: z} = ...` => declaredNames = ['x', 'z'] 15 } 16 } 17 }); 18 } 19 }; 20}
Constructs a RegExp that matches the exact string specified. This is useful for plugin hook filters.
Parameters: (str: String, flags?: String)
Returns: RegExp
1import { exactRegex } from '@rollup/pluginutils'; 2 3exactRegex('foobar'); // /^foobar$/ 4exactRegex('foo(bar)', 'i'); // /^foo\(bar\)$/i
Constructs a bundle-safe identifier from a String
.
Parameters: (str: String)
Returns: String
1import { makeLegalIdentifier } from '@rollup/pluginutils'; 2 3makeLegalIdentifier('foo-bar'); // 'foo_bar' 4makeLegalIdentifier('typeof'); // '_typeof'
Converts path separators to forward slash.
Parameters: (filename: String)
Returns: String
1import { normalizePath } from '@rollup/pluginutils'; 2 3normalizePath('foo\\bar'); // 'foo/bar' 4normalizePath('foo/bar'); // 'foo/bar'
Constructs a RegExp that matches a value that has the specified prefix. This is useful for plugin hook filters.
Parameters: (str: String, flags?: String)
Returns: RegExp
1import { prefixRegex } from '@rollup/pluginutils'; 2 3prefixRegex('foobar'); // /^foobar/ 4prefixRegex('foo(bar)', 'i'); // /^foo\(bar\)/i
No vulnerabilities found.