web 3d图形渲染器
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.

237 lines
5.7 KiB

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