processor.d.ts 3.1 KB

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