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.

169 lines
4.1 KiB

  1. # rollup-pluginutils
  2. A set of functions commonly used by Rollup plugins.
  3. ## Installation
  4. ```bash
  5. npm install --save rollup-pluginutils
  6. ```
  7. ## Usage
  8. ### addExtension
  9. ```js
  10. import { addExtension } from 'rollup-pluginutils';
  11. export default function myPlugin ( options = {} ) {
  12. return {
  13. resolveId ( code, id ) {
  14. // only adds an extension if there isn't one already
  15. id = addExtension( id ); // `foo` -> `foo.js`, `foo.js -> foo.js`
  16. id = addExtension( id, '.myext' ); // `foo` -> `foo.myext`, `foo.js -> `foo.js`
  17. }
  18. };
  19. }
  20. ```
  21. ### attachScopes
  22. This function 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.
  23. 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.
  24. ```js
  25. import { attachScopes } from 'rollup-pluginutils';
  26. import { walk } from 'estree-walker';
  27. export default function myPlugin ( options = {} ) {
  28. return {
  29. transform ( code ) {
  30. const ast = this.parse( code );
  31. let scope = attachScopes( ast, 'scope' );
  32. walk( ast, {
  33. enter ( node ) {
  34. if ( node.scope ) scope = node.scope;
  35. if ( !scope.contains( 'foo' ) ) {
  36. // `foo` is not defined, so if we encounter it,
  37. // we assume it's a global
  38. }
  39. },
  40. leave ( node ) {
  41. if ( node.scope ) scope = scope.parent;
  42. }
  43. });
  44. }
  45. };
  46. }
  47. ```
  48. ### createFilter
  49. ```js
  50. import { createFilter } from 'rollup-pluginutils';
  51. export default function myPlugin ( options = {} ) {
  52. // `options.include` and `options.exclude` can each be a minimatch
  53. // pattern, or an array of minimatch patterns, relative to process.cwd()
  54. var filter = createFilter( options.include, options.exclude );
  55. return {
  56. transform ( code, id ) {
  57. // if `options.include` is omitted or has zero length, filter
  58. // will return `true` by default. Otherwise, an ID must match
  59. // one or more of the minimatch patterns, and must not match
  60. // any of the `options.exclude` patterns.
  61. if ( !filter( id ) ) return;
  62. // proceed with the transformation...
  63. }
  64. };
  65. }
  66. ```
  67. If you want to resolve the patterns against a directory other than
  68. `process.cwd()`, you can additionally pass a `resolve` option:
  69. ```js
  70. var filter = createFilter( options.include, options.exclude, {resolve: '/my/base/dir'} )
  71. ```
  72. If `resolve` is a string, then this value will be used as the base directory.
  73. Relative paths will be resolved against `process.cwd()` first. If `resolve` is
  74. `false`, then the patterns will not be resolved against any directory. This can
  75. be useful if you want to create a filter for virtual module names.
  76. ### makeLegalIdentifier
  77. ```js
  78. import { makeLegalIdentifier } from 'rollup-pluginutils';
  79. makeLegalIdentifier( 'foo-bar' ); // 'foo_bar'
  80. makeLegalIdentifier( 'typeof' ); // '_typeof'
  81. ```
  82. ### dataToEsm
  83. Helper for treeshakable data imports
  84. ```js
  85. import { dataToEsm } from 'rollup-pluginutils';
  86. const esModuleSource = dataToEsm({
  87. custom: 'data',
  88. to: ['treeshake']
  89. }, {
  90. compact: false,
  91. indent: '\t',
  92. preferConst: false,
  93. objectShorthand: false,
  94. namedExports: true
  95. });
  96. /*
  97. Outputs the string ES module source:
  98. export const custom = 'data';
  99. export const to = ['treeshake'];
  100. export default { custom, to };
  101. */
  102. ```
  103. ### extractAssignedNames
  104. Extract the names of all assignment targets from patterns.
  105. ```js
  106. import { extractAssignedNames } from 'rollup-pluginutils';
  107. import { walk } from 'estree-walker';
  108. export default function myPlugin ( options = {} ) {
  109. return {
  110. transform ( code ) {
  111. const ast = this.parse( code );
  112. walk( ast, {
  113. enter ( node ) {
  114. if ( node.type === 'VariableDeclarator' ) {
  115. const declaredNames = extractAssignedNames(node.id);
  116. // do something with the declared names
  117. // e.g. for `const {x, y: z} = ... => declaredNames = ['x', 'z']
  118. }
  119. }
  120. });
  121. }
  122. };
  123. }
  124. ```
  125. ## License
  126. MIT