UNPKG

4.63 kBTypeScriptView Raw
1import Result, { Message, ResultOptions } from './result.js'
2import { SourceMap } from './postcss.js'
3import Processor from './processor.js'
4import Warning from './warning.js'
5import Root from './root.js'
6
7/**
8 * A Promise proxy for the result of PostCSS transformations.
9 *
10 * A `LazyResult` instance is returned by `Processor#process`.
11 *
12 * ```js
13 * const lazy = postcss([autoprefixer]).process(css)
14 * ```
15 */
16export default class LazyResult implements PromiseLike<Result> {
17 /**
18 * Processes input CSS through synchronous and asynchronous plugins
19 * and calls `onFulfilled` with a Result instance. If a plugin throws
20 * an error, the `onRejected` callback will be executed.
21 *
22 * It implements standard Promise API.
23 *
24 * ```js
25 * postcss([autoprefixer]).process(css, { from: cssPath }).then(result => {
26 * console.log(result.css)
27 * })
28 * ```
29 */
30 then: Promise<Result>['then']
31
32 /**
33 * Processes input CSS through synchronous and asynchronous plugins
34 * and calls onRejected for each error thrown in any plugin.
35 *
36 * It implements standard Promise API.
37 *
38 * ```js
39 * postcss([autoprefixer]).process(css).then(result => {
40 * console.log(result.css)
41 * }).catch(error => {
42 * console.error(error)
43 * })
44 * ```
45 */
46 catch: Promise<Result>['catch']
47
48 /**
49 * Processes input CSS through synchronous and asynchronous plugins
50 * and calls onFinally on any error or when all plugins will finish work.
51 *
52 * It implements standard Promise API.
53 *
54 * ```js
55 * postcss([autoprefixer]).process(css).finally(() => {
56 * console.log('processing ended')
57 * })
58 * ```
59 */
60 finally: Promise<Result>['finally']
61
62 /**
63 * @param processor Processor used for this transformation.
64 * @param css CSS to parse and transform.
65 * @param opts Options from the `Processor#process` or `Root#toResult`.
66 */
67 constructor(processor: Processor, css: string, opts: ResultOptions)
68
69 /**
70 * Returns the default string description of an object.
71 * Required to implement the Promise interface.
72 */
73 get [Symbol.toStringTag](): string
74
75 /**
76 * Returns a `Processor` instance, which will be used
77 * for CSS transformations.
78 */
79 get processor(): Processor
80
81 /**
82 * Options from the `Processor#process` call.
83 */
84 get opts(): ResultOptions
85
86 /**
87 * Processes input CSS through synchronous plugins, converts `Root`
88 * to a CSS string and returns `Result#css`.
89 *
90 * This property will only work with synchronous plugins.
91 * If the processor contains any asynchronous plugins
92 * it will throw an error.
93 *
94 * PostCSS runners should always use `LazyResult#then`.
95 */
96 get css(): string
97
98 /**
99 * An alias for the `css` property. Use it with syntaxes
100 * that generate non-CSS output.
101 *
102 * This property will only work with synchronous plugins.
103 * If the processor contains any asynchronous plugins
104 * it will throw an error.
105 *
106 * PostCSS runners should always use `LazyResult#then`.
107 */
108 get content(): string
109
110 /**
111 * Processes input CSS through synchronous plugins
112 * and returns `Result#map`.
113 *
114 * This property will only work with synchronous plugins.
115 * If the processor contains any asynchronous plugins
116 * it will throw an error.
117 *
118 * PostCSS runners should always use `LazyResult#then`.
119 */
120 get map(): SourceMap
121
122 /**
123 * Processes input CSS through synchronous plugins
124 * and returns `Result#root`.
125 *
126 * This property will only work with synchronous plugins. If the processor
127 * contains any asynchronous plugins it will throw an error.
128 *
129 * PostCSS runners should always use `LazyResult#then`.
130 */
131 get root(): Root
132
133 /**
134 * Processes input CSS through synchronous plugins
135 * and returns `Result#messages`.
136 *
137 * This property will only work with synchronous plugins. If the processor
138 * contains any asynchronous plugins it will throw an error.
139 *
140 * PostCSS runners should always use `LazyResult#then`.
141 */
142 get messages(): Message[]
143
144 /**
145 * Processes input CSS through synchronous plugins
146 * and calls `Result#warnings`.
147 *
148 * @return Warnings from plugins.
149 */
150 warnings(): Warning[]
151
152 /**
153 * Alias for the `LazyResult#css` property.
154 *
155 * ```js
156 * lazy + '' === lazy.css
157 * ```
158 *
159 * @return Output CSS.
160 */
161 toString(): string
162
163 /**
164 * Run plugin in sync way and return `Result`.
165 *
166 * @return Result with output content.
167 */
168 sync(): Result
169
170 /**
171 * Run plugin in async way and return `Result`.
172 *
173 * @return Result with output content.
174 */
175 async(): Promise<Result>
176}