postcss.d.ts 11 KB

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