UNPKG

10.1 kBTypeScriptView Raw
1import { SourceMapGenerator, RawSourceMap } from 'source-map'
2
3import Node, {
4 Position,
5 Source,
6 ChildNode,
7 NodeProps,
8 ChildProps,
9 AnyNode
10} from './node.js'
11import Declaration, { DeclarationProps } from './declaration.js'
12import Root, { RootProps } from './root.js'
13import Comment, { CommentProps } from './comment.js'
14import AtRule, { AtRuleProps } from './at-rule.js'
15import Result, { Message } from './result.js'
16import Rule, { RuleProps } from './rule.js'
17import Container, { ContainerProps } from './container.js'
18import Warning, { WarningOptions } from './warning.js'
19import Input, { FilePosition } from './input.js'
20import CssSyntaxError from './css-syntax-error.js'
21import list, { List } from './list.js'
22import Processor from './processor.js'
23
24export {
25 WarningOptions,
26 FilePosition,
27 Position,
28 Source,
29 ChildNode,
30 AnyNode,
31 Message,
32 NodeProps,
33 DeclarationProps,
34 ContainerProps,
35 CommentProps,
36 RuleProps,
37 ChildProps,
38 AtRuleProps,
39 RootProps,
40 Warning,
41 CssSyntaxError,
42 Node,
43 Container,
44 list,
45 Declaration,
46 Comment,
47 AtRule,
48 Rule,
49 Root,
50 Result,
51 Input
52}
53
54export type SourceMap = SourceMapGenerator & {
55 toJSON(): RawSourceMap
56}
57
58export type Helpers = { result: Result; postcss: Postcss } & Postcss
59
60type RootProcessor = (root: Root, helper: Helpers) => Promise<void> | void
61type DeclarationProcessor = (
62 decl: Declaration,
63 helper: Helpers
64) => Promise<void> | void
65type RuleProcessor = (rule: Rule, helper: Helpers) => Promise<void> | void
66type AtRuleProcessor = (atRule: AtRule, helper: Helpers) => Promise<void> | void
67type CommentProcessor = (
68 comment: Comment,
69 helper: Helpers
70) => Promise<void> | void
71
72interface Processors {
73 /**
74 * Will be called on `Root` node once.
75 */
76 Once?: RootProcessor
77
78 /**
79 * Will be called on `Root` node once, when all children will be processed.
80 */
81 OnceExit?: RootProcessor
82
83 /**
84 * Will be called on `Root` node.
85 *
86 * Will be called again on children changes.
87 */
88 Root?: RootProcessor
89
90 /**
91 * Will be called on `Root` node, when all children will be processed.
92 *
93 * Will be called again on children changes.
94 */
95 RootExit?: RootProcessor
96
97 /**
98 * Will be called on all `Declaration` nodes after listeners
99 * for `Declaration` event.
100 *
101 * Will be called again on node or children changes.
102 */
103 Declaration?: DeclarationProcessor | { [prop: string]: DeclarationProcessor }
104
105 /**
106 * Will be called on all `Declaration` nodes.
107 *
108 * Will be called again on node or children changes.
109 */
110 DeclarationExit?:
111 | DeclarationProcessor
112 | { [prop: string]: DeclarationProcessor }
113
114 /**
115 * Will be called on all `Rule` nodes.
116 *
117 * Will be called again on node or children changes.
118 */
119 Rule?: RuleProcessor
120
121 /**
122 * Will be called on all `Rule` nodes, when all children will be processed.
123 *
124 * Will be called again on node or children changes.
125 */
126 RuleExit?: RuleProcessor
127
128 /**
129 * Will be called on all`AtRule` nodes.
130 *
131 * Will be called again on node or children changes.
132 */
133 AtRule?: AtRuleProcessor | { [name: string]: AtRuleProcessor }
134
135 /**
136 * Will be called on all `AtRule` nodes, when all children will be processed.
137 *
138 * Will be called again on node or children changes.
139 */
140 AtRuleExit?: AtRuleProcessor | { [name: string]: AtRuleProcessor }
141
142 /**
143 * Will be called on all `Comment` nodes.
144 *
145 * Will be called again on node or children changes.
146 */
147 Comment?: CommentProcessor
148
149 /**
150 * Will be called on all `Comment` nodes after listeners
151 * for `Comment` event.
152 *
153 * Will be called again on node or children changes.
154 */
155 CommentExit?: CommentProcessor
156
157 /**
158 * Will be called when all other listeners processed the document.
159 *
160 * This listener will not be called again.
161 */
162 Exit?: RootProcessor
163}
164
165export interface Plugin extends Processors {
166 postcssPlugin: string
167 prepare?: (result: Result) => Processors
168}
169
170export interface PluginCreator<PluginOptions> {
171 (opts?: PluginOptions): Plugin
172 postcss: true
173}
174
175export interface Transformer extends TransformCallback {
176 postcssPlugin: string
177 postcssVersion: string
178}
179
180export interface TransformCallback {
181 (root: Root, result: Result): Promise<void> | void
182}
183
184export interface OldPlugin<T> extends Transformer {
185 (opts?: T): Transformer
186 postcss: Transformer
187}
188
189export type AcceptedPlugin =
190 | Plugin
191 | PluginCreator<any>
192 | OldPlugin<any>
193 | TransformCallback
194 | {
195 postcss: TransformCallback | Processor
196 }
197 | Processor
198
199export interface Parser {
200 (
201 css: string | { toString(): string },
202 opts?: Pick<ProcessOptions, 'map' | 'from'>
203 ): Root
204}
205
206export interface Builder {
207 (part: string, node?: AnyNode, type?: 'start' | 'end'): void
208}
209
210export interface Stringifier {
211 (node: AnyNode, builder: Builder): void
212}
213
214export interface Syntax {
215 /**
216 * Function to generate AST by string.
217 */
218 parse?: Parser
219
220 /**
221 * Class to generate string by AST.
222 */
223 stringify?: Stringifier
224}
225
226export interface SourceMapOptions {
227 /**
228 * Indicates that the source map should be embedded in the output CSS
229 * as a Base64-encoded comment. By default, it is `true`.
230 * But if all previous maps are external, not inline, PostCSS will not embed
231 * the map even if you do not set this option.
232 *
233 * If you have an inline source map, the result.map property will be empty,
234 * as the source map will be contained within the text of `result.css`.
235 */
236 inline?: boolean
237
238 /**
239 * Source map content from a previous processing step (e.g., Sass).
240 *
241 * PostCSS will try to read the previous source map
242 * automatically (based on comments within the source CSS), but you can use
243 * this option to identify it manually.
244 *
245 * If desired, you can omit the previous map with prev: `false`.
246 */
247 prev?: string | boolean | object | ((file: string) => string)
248
249 /**
250 * Indicates that PostCSS should set the origin content (e.g., Sass source)
251 * of the source map. By default, it is true. But if all previous maps do not
252 * contain sources content, PostCSS will also leave it out even if you
253 * do not set this option.
254 */
255 sourcesContent?: boolean
256
257 /**
258 * Indicates that PostCSS should add annotation comments to the CSS.
259 * By default, PostCSS will always add a comment with a path
260 * to the source map. PostCSS will not add annotations to CSS files
261 * that do not contain any comments.
262 *
263 * By default, PostCSS presumes that you want to save the source map as
264 * `opts.to + '.map'` and will use this path in the annotation comment.
265 * A different path can be set by providing a string value for annotation.
266 *
267 * If you have set `inline: true`, annotation cannot be disabled.
268 */
269 annotation?: string | boolean | ((file: string, root: Root) => string)
270
271 /**
272 * Override `from` in maps sources.
273 */
274 from?: string
275
276 /**
277 * Use absolute path in generated source map.
278 */
279 absolute?: boolean
280}
281
282export interface ProcessOptions {
283 /**
284 * The path of the CSS source file. You should always set `from`,
285 * because it is used in source map generation and syntax error messages.
286 */
287 from?: string
288
289 /**
290 * The path where you'll put the output CSS file. You should always set `to`
291 * to generate correct source maps.
292 */
293 to?: string
294
295 /**
296 * Function to generate AST by string.
297 */
298 parser?: Syntax | Parser
299
300 /**
301 * Class to generate string by AST.
302 */
303 stringifier?: Syntax | Stringifier
304
305 /**
306 * Object with parse and stringify.
307 */
308 syntax?: Syntax
309
310 /**
311 * Source map options
312 */
313 map?: SourceMapOptions | boolean
314}
315
316export interface Postcss {
317 /**
318 * Create a new `Processor` instance that will apply `plugins`
319 * as CSS processors.
320 *
321 * ```js
322 * let postcss = require('postcss')
323 *
324 * postcss(plugins).process(css, { from, to }).then(result => {
325 * console.log(result.css)
326 * })
327 * ```
328 *
329 * @param plugins PostCSS plugins.
330 * @return Processor to process multiple CSS.
331 */
332 (plugins?: AcceptedPlugin[]): Processor
333 (...plugins: AcceptedPlugin[]): Processor
334
335 /**
336 * Default function to convert a node tree into a CSS string.
337 */
338 stringify: Stringifier
339
340 /**
341 * Parses source css and returns a new `Root` node,
342 * which contains the source CSS nodes.
343 *
344 * ```js
345 * // Simple CSS concatenation with source map support
346 * const root1 = postcss.parse(css1, { from: file1 })
347 * const root2 = postcss.parse(css2, { from: file2 })
348 * root1.append(root2).toResult().css
349 * ```
350 */
351 parse: Parser
352
353 /**
354 * Contains the `list` module.
355 */
356 list: List
357
358 /**
359 * Creates a new `Comment` node.
360 *
361 * @param defaults Properties for the new node.
362 * @return New comment node
363 */
364 comment(defaults?: CommentProps): Comment
365
366 /**
367 * Creates a new `AtRule` node.
368 *
369 * @param defaults Properties for the new node.
370 * @return New at-rule node.
371 */
372 atRule(defaults?: AtRuleProps): AtRule
373
374 /**
375 * Creates a new `Declaration` node.
376 *
377 * @param defaults Properties for the new node.
378 * @return New declaration node.
379 */
380 decl(defaults?: DeclarationProps): Declaration
381
382 /**
383 * Creates a new `Rule` node.
384 *
385 * @param default Properties for the new node.
386 * @return New rule node.
387 */
388 rule(defaults?: RuleProps): Rule
389
390 /**
391 * Creates a new `Root` node.
392 *
393 * @param defaults Properties for the new node.
394 * @return New root node.
395 */
396 root(defaults?: RootProps): Root
397
398 CssSyntaxError: typeof CssSyntaxError
399 Declaration: typeof Declaration
400 Container: typeof Container
401 Comment: typeof Comment
402 Warning: typeof Warning
403 AtRule: typeof AtRule
404 Result: typeof Result
405 Input: typeof Input
406 Rule: typeof Rule
407 Root: typeof Root
408 Node: typeof Node
409}
410
411export const stringify: Stringifier
412export const parse: Parser
413
414export const comment: Postcss['comment']
415export const atRule: Postcss['atRule']
416export const decl: Postcss['decl']
417export const rule: Postcss['rule']
418export const root: Postcss['root']
419
420declare const postcss: Postcss
421
422export default postcss
423
\No newline at end of file