processor.d.ts 2.9 KB

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