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. 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?: readonly 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. ): LazyResult | NoWorkResult
  72. process<RootNode extends Document | Root = Root>(
  73. css: { toString(): string } | LazyResult | Result | Root | string,
  74. options: ProcessOptions<RootNode>
  75. ): LazyResult<RootNode>
  76. /**
  77. * Adds a plugin to be used as a CSS processor.
  78. *
  79. * PostCSS plugin can be in 4 formats:
  80. * * A plugin in `Plugin` format.
  81. * * A plugin creator function with `pluginCreator.postcss = true`.
  82. * PostCSS will call this function without argument to get plugin.
  83. * * A function. PostCSS will pass the function a {@link Root}
  84. * as the first argument and current `Result` instance
  85. * as the second.
  86. * * Another `Processor` instance. PostCSS will copy plugins
  87. * from that instance into this one.
  88. *
  89. * Plugins can also be added by passing them as arguments when creating
  90. * a `postcss` instance (see [`postcss(plugins)`]).
  91. *
  92. * Asynchronous plugins should return a `Promise` instance.
  93. *
  94. * ```js
  95. * const processor = postcss()
  96. * .use(autoprefixer)
  97. * .use(postcssNested)
  98. * ```
  99. *
  100. * @param plugin PostCSS plugin or `Processor` with plugins.
  101. * @return Current processor to make methods chain.
  102. */
  103. use(plugin: AcceptedPlugin): this
  104. }
  105. declare class Processor extends Processor_ {}
  106. export = Processor