result.d.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {
  2. ProcessOptions,
  3. Plugin,
  4. SourceMap,
  5. TransformCallback,
  6. Root,
  7. Node,
  8. Warning,
  9. WarningOptions
  10. } from './postcss.js'
  11. import Processor from './processor.js'
  12. export interface Message {
  13. /**
  14. * Message type.
  15. */
  16. type: string
  17. /**
  18. * Source PostCSS plugin name.
  19. */
  20. plugin?: string
  21. [others: string]: any
  22. }
  23. export interface ResultOptions extends ProcessOptions {
  24. /**
  25. * The CSS node that was the source of the warning.
  26. */
  27. node?: Node
  28. /**
  29. * Name of plugin that created this warning. `Result#warn` will fill it
  30. * automatically with `Plugin#postcssPlugin` value.
  31. */
  32. plugin?: string
  33. }
  34. /**
  35. * Provides the result of the PostCSS transformations.
  36. *
  37. * A Result instance is returned by `LazyResult#then`
  38. * or `Root#toResult` methods.
  39. *
  40. * ```js
  41. * postcss([autoprefixer]).process(css).then(result => {
  42. * console.log(result.css)
  43. * })
  44. * ```
  45. *
  46. * ```js
  47. * const result2 = postcss.parse(css).toResult()
  48. * ```
  49. */
  50. export default class Result {
  51. /**
  52. * The Processor instance used for this transformation.
  53. *
  54. * ```js
  55. * for (const plugin of result.processor.plugins) {
  56. * if (plugin.postcssPlugin === 'postcss-bad') {
  57. * throw 'postcss-good is incompatible with postcss-bad'
  58. * }
  59. * })
  60. * ```
  61. */
  62. processor: Processor
  63. /**
  64. * Contains messages from plugins (e.g., warnings or custom messages).
  65. * Each message should have type and plugin properties.
  66. *
  67. * ```js
  68. * AtRule: {
  69. * import: (atRule, { result }) {
  70. * const importedFile = parseImport(atRule)
  71. * result.messages.push({
  72. * type: 'dependency',
  73. * plugin: 'postcss-import',
  74. * file: importedFile,
  75. * parent: result.opts.from
  76. * })
  77. * }
  78. * }
  79. * ```
  80. */
  81. messages: Message[]
  82. /**
  83. * Root node after all transformations.
  84. *
  85. * ```js
  86. * root.toResult().root === root
  87. * ```
  88. */
  89. root: Root
  90. /**
  91. * Options from the `Processor#process` or `Root#toResult` call
  92. * that produced this Result instance.]
  93. *
  94. * ```js
  95. * root.toResult(opts).opts === opts
  96. * ```
  97. */
  98. opts: ResultOptions
  99. /**
  100. * A CSS string representing of `Result#root`.
  101. *
  102. * ```js
  103. * postcss.parse('a{}').toResult().css //=> "a{}"
  104. * ```
  105. */
  106. css: string
  107. /**
  108. * An instance of `SourceMapGenerator` class from the `source-map` library,
  109. * representing changes to the `Result#root` instance.
  110. *
  111. * ```js
  112. * result.map.toJSON() //=> { version: 3, file: 'a.css', … }
  113. * ```
  114. *
  115. * ```js
  116. * if (result.map) {
  117. * fs.writeFileSync(result.opts.to + '.map', result.map.toString())
  118. * }
  119. * ```
  120. */
  121. map: SourceMap
  122. /**
  123. * Last runned PostCSS plugin.
  124. */
  125. lastPlugin: Plugin | TransformCallback
  126. /**
  127. * @param processor Processor used for this transformation.
  128. * @param root Root node after all transformations.
  129. * @param opts Options from the `Processor#process` or `Root#toResult`.
  130. */
  131. constructor(processor: Processor, root: Root, opts: ResultOptions)
  132. /**
  133. * An alias for the `Result#css` property.
  134. * Use it with syntaxes that generate non-CSS output.
  135. *
  136. * ```js
  137. * result.css === result.content
  138. * ```
  139. */
  140. get content(): string
  141. /**
  142. * Returns for `Result#css` content.
  143. *
  144. * ```js
  145. * result + '' === result.css
  146. * ```
  147. *
  148. * @return String representing of `Result#root`.
  149. */
  150. toString(): string
  151. /**
  152. * Creates an instance of `Warning` and adds it to `Result#messages`.
  153. *
  154. * ```js
  155. * if (decl.important) {
  156. * result.warn('Avoid !important', { node: decl, word: '!important' })
  157. * }
  158. * ```
  159. *
  160. * @param text Warning message.
  161. * @param opts Warning options.
  162. * @return Created warning.
  163. */
  164. warn(message: string, options?: WarningOptions): Warning
  165. /**
  166. * Returns warnings from plugins. Filters `Warning` instances
  167. * from `Result#messages`.
  168. *
  169. * ```js
  170. * result.warnings().forEach(warn => {
  171. * console.warn(warn.toString())
  172. * })
  173. * ```
  174. *
  175. * @return Warnings from plugins.
  176. */
  177. warnings(): Warning[]
  178. }