processor.d.ts 3.3 KB

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