UNPKG

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