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
@jamesernator/rollup-pluginutils
Functionality commonly needed by Rollup plugins
@trusktr/rollup-pluginutils
Functionality commonly needed by Rollup plugins
@naiable/rollup-config
Use rollup like tsup, but opinionated my way.
🍣 The one-stop shop for official Rollup plugins
npm install @rollup/pluginutils
Typescript
Module System
Min. Node Version
Node Version
NPM Version
56.3
Supply Chain
99.4
Quality
85.3
Maintenance
100
Vulnerability
99.6
License
Updated on 06 Dec 2024
JavaScript (75.24%)
TypeScript (24.75%)
Shell (0.01%)
Cumulative downloads
Total Downloads
Last day
16.6%
Compared to previous day
Last week
-4.2%
Compared to previous week
Last month
8%
Compared to previous month
Last year
37.6%
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 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'
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
30 commit(s) and 9 issue activity found in the last 90 days -- score normalized to 10
Reason
license file detected
Details
Reason
binaries present in source code
Details
Reason
Found 13/30 approved changesets -- score normalized to 4
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
14 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More