UNPKG

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