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 |
|
12 | A set of utility functions commonly used by 🍣 Rollup plugins.
|
13 |
|
14 | ## Requirements
|
15 |
|
16 | This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
|
17 |
|
18 | ## Install
|
19 |
|
20 | Using npm:
|
21 |
|
22 | ```console
|
23 | npm install @rollup/pluginutils --save-dev
|
24 | ```
|
25 |
|
26 | ## Usage
|
27 |
|
28 | ```js
|
29 | import utils from '@rollup/pluginutils';
|
30 | //...
|
31 | ```
|
32 |
|
33 | ## API
|
34 |
|
35 | Available utility functions are listed below:
|
36 |
|
37 | _Note: Parameter names immediately followed by a `?` indicate that the parameter is optional._
|
38 |
|
39 | ### addExtension
|
40 |
|
41 | Adds an extension to a module ID if one does not exist.
|
42 |
|
43 | Parameters: `(filename: String, ext?: String)`<br>
|
44 | Returns: `String`
|
45 |
|
46 | ```js
|
47 | import { addExtension } from '@rollup/pluginutils';
|
48 |
|
49 | export 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 |
|
62 | 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.
|
63 |
|
64 | Parameters: `(ast: Node, propertyName?: String)`<br>
|
65 | Returns: `Object`
|
66 |
|
67 | See [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
|
70 | import { attachScopes } from '@rollup/pluginutils';
|
71 | import { walk } from 'estree-walker';
|
72 |
|
73 | export 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 |
|
100 | Constructs a filter function which can be used to determine whether or not certain modules should be operated upon.
|
101 |
|
102 | Parameters: `(include?: <minmatch>, exclude?: <minmatch>, options?: Object)`<br>
|
103 | Returns: `String`
|
104 |
|
105 | #### `include` and `exclude`
|
106 |
|
107 | Type: `String | RegExp | Array[...String|RegExp]`<br>
|
108 |
|
109 | A 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 |
|
115 | Type: `String | Boolean | null`
|
116 |
|
117 | 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.
|
118 |
|
119 | #### Usage
|
120 |
|
121 | ```js
|
122 | import { createFilter } from '@rollup/pluginutils';
|
123 |
|
124 | export 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 |
|
142 | Transforms objects into tree-shakable ES Module imports.
|
143 |
|
144 | Parameters: `(data: Object)`<br>
|
145 | Returns: `String`
|
146 |
|
147 | #### `data`
|
148 |
|
149 | Type: `Object`
|
150 |
|
151 | An object to transform into an ES module.
|
152 |
|
153 | #### Usage
|
154 |
|
155 | ```js
|
156 | import { dataToEsm } from '@rollup/pluginutils';
|
157 |
|
158 | const 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 | /*
|
172 | Outputs 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 |
|
181 | Extracts the names of all assignment targets based upon specified patterns.
|
182 |
|
183 | Parameters: `(param: Node)`<br>
|
184 | Returns: `Array[...String]`
|
185 |
|
186 | #### `param`
|
187 |
|
188 | Type: `Node`
|
189 |
|
190 | An `acorn` AST Node.
|
191 |
|
192 | #### Usage
|
193 |
|
194 | ```js
|
195 | import { extractAssignedNames } from '@rollup/pluginutils';
|
196 | import { walk } from 'estree-walker';
|
197 |
|
198 | export 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 |
|
219 | Constructs a bundle-safe identifier from a `String`.
|
220 |
|
221 | Parameters: `(str: String)`<br>
|
222 | Returns: `String`
|
223 |
|
224 | #### Usage
|
225 |
|
226 | ```js
|
227 | import { makeLegalIdentifier } from '@rollup/pluginutils';
|
228 |
|
229 | makeLegalIdentifier('foo-bar'); // 'foo_bar'
|
230 | makeLegalIdentifier('typeof'); // '_typeof'
|
231 | ```
|
232 |
|
233 | ## Meta
|
234 |
|
235 | [CONTRIBUTING](/.github/CONTRIBUTING.md)
|
236 |
|
237 | [LICENSE (MIT)](/LICENSE)
|