processor.d.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {
  2. AcceptedPlugin,
  3. Plugin,
  4. ProcessOptions,
  5. Transformer,
  6. TransformCallback
  7. } from './postcss.js'
  8. import LazyResult from './lazy-result.js'
  9. import Result from './result.js'
  10. import Root from './root.js'
  11. import NoWorkResult from './no-work-result.js'
  12. declare namespace Processor {
  13. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  14. export { Processor_ as default }
  15. }
  16. /**
  17. * Contains plugins to process CSS. Create one `Processor` instance,
  18. * initialize its plugins, and then use that instance on numerous CSS files.
  19. *
  20. * ```js
  21. * const processor = postcss([autoprefixer, postcssNested])
  22. * processor.process(css1).then(result => console.log(result.css))
  23. * processor.process(css2).then(result => console.log(result.css))
  24. * ```
  25. */
  26. declare class Processor_ {
  27. /**
  28. * Current PostCSS version.
  29. *
  30. * ```js
  31. * if (result.processor.version.split('.')[0] !== '6') {
  32. * throw new Error('This plugin works only with PostCSS 6')
  33. * }
  34. * ```
  35. */
  36. version: string
  37. /**
  38. * Plugins added to this processor.
  39. *
  40. * ```js
  41. * const processor = postcss([autoprefixer, postcssNested])
  42. * processor.plugins.length //=> 2
  43. * ```
  44. */
  45. plugins: (Plugin | Transformer | TransformCallback)[]
  46. /**
  47. * @param plugins PostCSS plugins
  48. */
  49. constructor(plugins?: AcceptedPlugin[])
  50. /**
  51. * Adds a plugin to be used as a CSS processor.
  52. *
  53. * PostCSS plugin can be in 4 formats:
  54. * * A plugin in `Plugin` format.
  55. * * A plugin creator function with `pluginCreator.postcss = true`.
  56. * PostCSS will call this function without argument to get plugin.
  57. * * A function. PostCSS will pass the function a {@link Root}
  58. * as the first argument and current `Result` instance
  59. * as the second.
  60. * * Another `Processor` instance. PostCSS will copy plugins
  61. * from that instance into this one.
  62. *
  63. * Plugins can also be added by passing them as arguments when creating
  64. * a `postcss` instance (see [`postcss(plugins)`]).
  65. *
  66. * Asynchronous plugins should return a `Promise` instance.
  67. *
  68. * ```js
  69. * const processor = postcss()
  70. * .use(autoprefixer)
  71. * .use(postcssNested)
  72. * ```
  73. *
  74. * @param plugin PostCSS plugin or `Processor` with plugins.
  75. * @return Current processor to make methods chain.
  76. */
  77. use(plugin: AcceptedPlugin): this
  78. /**
  79. * Parses source CSS and returns a `LazyResult` Promise proxy.
  80. * Because some plugins can be asynchronous it doesn’t make
  81. * any transformations. Transformations will be applied
  82. * in the `LazyResult` methods.
  83. *
  84. * ```js
  85. * processor.process(css, { from: 'a.css', to: 'a.out.css' })
  86. * .then(result => {
  87. * console.log(result.css)
  88. * })
  89. * ```
  90. *
  91. * @param css String with input CSS or any object with a `toString()` method,
  92. * like a Buffer. Optionally, send a `Result` instance
  93. * and the processor will take the `Root` from it.
  94. * @param opts Options.
  95. * @return Promise proxy.
  96. */
  97. process(
  98. css: string | { toString(): string } | Result | LazyResult | Root,
  99. options?: ProcessOptions
  100. ): LazyResult | NoWorkResult
  101. }
  102. declare class Processor extends Processor_ {}
  103. export = Processor