UNPKG

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