You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB

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