postcss.d.ts 11 KB

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