UNPKG

5.84 kBMarkdownView Raw
1[npm]: https://img.shields.io/npm/v/@rollup/pluginutils
2[npm-url]: https://www.npmjs.com/package/@rollup/pluginutils
3[size]: https://packagephobia.now.sh/badge?p=@rollup/pluginutils
4[size-url]: https://packagephobia.now.sh/result?p=@rollup/pluginutils
5
6[![npm][npm]][npm-url]
7[![size][size]][size-url]
8[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
9
10# @rollup/pluginutils
11
12A set of utility functions commonly used by 🍣 Rollup plugins.
13
14## Requirements
15
16This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
17
18## Install
19
20Using npm:
21
22```console
23npm install @rollup/pluginutils --save-dev
24```
25
26## Usage
27
28```js
29import utils from '@rollup/pluginutils';
30//...
31```
32
33## API
34
35Available utility functions are listed below:
36
37_Note: Parameter names immediately followed by a `?` indicate that the parameter is optional._
38
39### addExtension
40
41Adds an extension to a module ID if one does not exist.
42
43Parameters: `(filename: String, ext?: String)`<br>
44Returns: `String`
45
46```js
47import { addExtension } from '@rollup/pluginutils';
48
49export default function myPlugin(options = {}) {
50 return {
51 resolveId(code, id) {
52 // only adds an extension if there isn't one already
53 id = addExtension(id); // `foo` -> `foo.js`, `foo.js -> foo.js`
54 id = addExtension(id, '.myext'); // `foo` -> `foo.myext`, `foo.js -> `foo.js`
55 }
56 };
57}
58```
59
60### attachScopes
61
62Attaches `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.
63
64Parameters: `(ast: Node, propertyName?: String)`<br>
65Returns: `Object`
66
67See [rollup-plugin-inject](https://github.com/rollup/rollup-plugin-inject) or [rollup-plugin-commonjs](https://github.com/rollup/rollup-plugin-commonjs) for an example of usage.
68
69```js
70import { attachScopes } from '@rollup/pluginutils';
71import { walk } from 'estree-walker';
72
73export default function myPlugin(options = {}) {
74 return {
75 transform(code) {
76 const ast = this.parse(code);
77
78 let scope = attachScopes(ast, 'scope');
79
80 walk(ast, {
81 enter(node) {
82 if (node.scope) scope = node.scope;
83
84 if (!scope.contains('foo')) {
85 // `foo` is not defined, so if we encounter it,
86 // we assume it's a global
87 }
88 },
89 leave(node) {
90 if (node.scope) scope = scope.parent;
91 }
92 });
93 }
94 };
95}
96```
97
98### createFilter
99
100Constructs a filter function which can be used to determine whether or not certain modules should be operated upon.
101
102Parameters: `(include?: <minmatch>, exclude?: <minmatch>, options?: Object)`<br>
103Returns: `String`
104
105#### `include` and `exclude`
106
107Type: `String | RegExp | Array[...String|RegExp]`<br>
108
109A valid [`minimatch`](https://www.npmjs.com/package/minimatch) 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 `minimatch` patterns, and must not match any of the `options.exclude` patterns.
110
111#### `options`
112
113##### `resolve`
114
115Type: `String | Boolean | null`
116
117Optionally 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.
118
119#### Usage
120
121```js
122import { createFilter } from '@rollup/pluginutils';
123
124export default function myPlugin(options = {}) {
125 // assume that the myPlugin accepts options of `options.include` and `options.exclude`
126 var filter = createFilter(options.include, options.exclude, {
127 resolve: '/my/base/dir'
128 });
129
130 return {
131 transform(code, id) {
132 if (!filter(id)) return;
133
134 // proceed with the transformation...
135 }
136 };
137}
138```
139
140### dataToEsm
141
142Transforms objects into tree-shakable ES Module imports.
143
144Parameters: `(data: Object)`<br>
145Returns: `String`
146
147#### `data`
148
149Type: `Object`
150
151An object to transform into an ES module.
152
153#### Usage
154
155```js
156import { dataToEsm } from '@rollup/pluginutils';
157
158const esModuleSource = dataToEsm(
159 {
160 custom: 'data',
161 to: ['treeshake']
162 },
163 {
164 compact: false,
165 indent: '\t',
166 preferConst: false,
167 objectShorthand: false,
168 namedExports: true
169 }
170);
171/*
172Outputs the string ES module source:
173 export const custom = 'data';
174 export const to = ['treeshake'];
175 export default { custom, to };
176*/
177```
178
179### extractAssignedNames
180
181Extracts the names of all assignment targets based upon specified patterns.
182
183Parameters: `(param: Node)`<br>
184Returns: `Array[...String]`
185
186#### `param`
187
188Type: `Node`
189
190An `acorn` AST Node.
191
192#### Usage
193
194```js
195import { extractAssignedNames } from '@rollup/pluginutils';
196import { walk } from 'estree-walker';
197
198export default function myPlugin(options = {}) {
199 return {
200 transform(code) {
201 const ast = this.parse(code);
202
203 walk(ast, {
204 enter(node) {
205 if (node.type === 'VariableDeclarator') {
206 const declaredNames = extractAssignedNames(node.id);
207 // do something with the declared names
208 // e.g. for `const {x, y: z} = ... => declaredNames = ['x', 'z']
209 }
210 }
211 });
212 }
213 };
214}
215```
216
217### makeLegalIdentifier
218
219Constructs a bundle-safe identifier from a `String`.
220
221Parameters: `(str: String)`<br>
222Returns: `String`
223
224#### Usage
225
226```js
227import { makeLegalIdentifier } from '@rollup/pluginutils';
228
229makeLegalIdentifier('foo-bar'); // 'foo_bar'
230makeLegalIdentifier('typeof'); // '_typeof'
231```
232
233## Meta
234
235[CONTRIBUTING](/.github/CONTRIBUTING.md)
236
237[LICENSE (MIT)](/LICENSE)