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.

196 lines
4.0 KiB

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