版博士V2.0程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. The plugin utils require an [LTS](https://github.com/nodejs/Release) Node version (v14.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?: <picomatch>, exclude?: <picomatch>, options?: Object)`<br>
  73. Returns: `String`
  74. #### `include` and `exclude`
  75. Type: `String | RegExp | Array[...String|RegExp]`<br>
  76. A valid [`picomatch`](https://github.com/micromatch/picomatch#globbing-features) 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.
  77. Note that `picomatch` patterns are very similar to [`minimatch`](https://github.com/isaacs/minimatch#readme) patterns, and in most use cases, they are interchangeable. If you have more specific pattern matching needs, you can view [this comparison table](https://github.com/micromatch/picomatch#library-comparisons) to learn more about where the libraries differ.
  78. #### `options`
  79. ##### `resolve`
  80. Type: `String | Boolean | null`
  81. 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.
  82. #### Usage
  83. ```js
  84. import { createFilter } from '@rollup/pluginutils';
  85. export default function myPlugin(options = {}) {
  86. // assume that the myPlugin accepts options of `options.include` and `options.exclude`
  87. var filter = createFilter(options.include, options.exclude, {
  88. resolve: '/my/base/dir'
  89. });
  90. return {
  91. transform(code, id) {
  92. if (!filter(id)) return;
  93. // proceed with the transformation...
  94. }
  95. };
  96. }
  97. ```
  98. ### dataToEsm
  99. Transforms objects into tree-shakable ES Module imports.
  100. Parameters: `(data: Object)`<br>
  101. Returns: `String`
  102. #### `data`
  103. Type: `Object`
  104. An object to transform into an ES module.
  105. #### Usage
  106. ```js
  107. import { dataToEsm } from '@rollup/pluginutils';
  108. const esModuleSource = dataToEsm(
  109. {
  110. custom: 'data',
  111. to: ['treeshake']
  112. },
  113. {
  114. compact: false,
  115. indent: '\t',
  116. preferConst: false,
  117. objectShorthand: false,
  118. namedExports: true
  119. }
  120. );
  121. /*
  122. Outputs the string ES module source:
  123. export const custom = 'data';
  124. export const to = ['treeshake'];
  125. export default { custom, to };
  126. */
  127. ```
  128. ### extractAssignedNames
  129. Extracts the names of all assignment targets based upon specified patterns.
  130. Parameters: `(param: Node)`<br>
  131. Returns: `Array[...String]`
  132. #### `param`
  133. Type: `Node`
  134. An `acorn` AST Node.
  135. #### Usage
  136. ```js
  137. import { extractAssignedNames } from '@rollup/pluginutils';
  138. import { walk } from 'estree-walker';
  139. export default function myPlugin(options = {}) {
  140. return {
  141. transform(code) {
  142. const ast = this.parse(code);
  143. walk(ast, {
  144. enter(node) {
  145. if (node.type === 'VariableDeclarator') {
  146. const declaredNames = extractAssignedNames(node.id);
  147. // do something with the declared names
  148. // e.g. for `const {x, y: z} = ...` => declaredNames = ['x', 'z']
  149. }
  150. }
  151. });
  152. }
  153. };
  154. }
  155. ```
  156. ### makeLegalIdentifier
  157. Constructs a bundle-safe identifier from a `String`.
  158. Parameters: `(str: String)`<br>
  159. Returns: `String`
  160. #### Usage
  161. ```js
  162. import { makeLegalIdentifier } from '@rollup/pluginutils';
  163. makeLegalIdentifier('foo-bar'); // 'foo_bar'
  164. makeLegalIdentifier('typeof'); // '_typeof'
  165. ```
  166. ### normalizePath
  167. Converts path separators to forward slash.
  168. Parameters: `(filename: String)`<br>
  169. Returns: `String`
  170. #### Usage
  171. ```js
  172. import { normalizePath } from '@rollup/pluginutils';
  173. normalizePath('foo\\bar'); // 'foo/bar'
  174. normalizePath('foo/bar'); // 'foo/bar'
  175. ```
  176. ## Meta
  177. [CONTRIBUTING](/.github/CONTRIBUTING.md)
  178. [LICENSE (MIT)](/LICENSE)