postcss.d.ts 11 KB

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