UNPKG

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