postcss.d.ts 11 KB

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