postcss.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. declare namespace postcss {
  127. export {
  128. NodeErrorOptions,
  129. DeclarationProps,
  130. CssSyntaxError,
  131. ContainerProps,
  132. WarningOptions,
  133. DocumentProps,
  134. FilePosition,
  135. CommentProps,
  136. AtRuleProps,
  137. Declaration,
  138. ChildProps,
  139. LazyResult,
  140. ChildNode,
  141. NodeProps,
  142. Processor,
  143. RuleProps,
  144. RootProps,
  145. Container,
  146. Position,
  147. Document,
  148. AnyNode,
  149. Warning,
  150. Message,
  151. Comment,
  152. Source,
  153. AtRule,
  154. Result,
  155. Input,
  156. Node,
  157. list,
  158. Rule,
  159. Root
  160. }
  161. export type SourceMap = SourceMapGenerator & {
  162. toJSON(): RawSourceMap
  163. }
  164. export type Helpers = { result: Result; postcss: Postcss } & 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. | Plugin
  186. | PluginCreator<any>
  187. | OldPlugin<any>
  188. | TransformCallback
  189. | {
  190. postcss: TransformCallback | Processor
  191. }
  192. | Processor
  193. export interface Parser<RootNode = Root | Document> {
  194. (
  195. css: string | { toString(): string },
  196. opts?: Pick<ProcessOptions, 'map' | 'from'>
  197. ): RootNode
  198. }
  199. export interface Builder {
  200. (part: string, node?: AnyNode, type?: 'start' | 'end'): 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. * Indicates that the source map should be embedded in the output CSS
  222. * as a Base64-encoded comment. By default, it is `true`.
  223. * But if all previous maps are external, not inline, PostCSS will not embed
  224. * the map even if you do not set this option.
  225. *
  226. * If you have an inline source map, the result.map property will be empty,
  227. * as the source map will be contained within the text of `result.css`.
  228. */
  229. inline?: boolean
  230. /**
  231. * Source map content from a previous processing step (e.g., Sass).
  232. *
  233. * PostCSS will try to read the previous source map
  234. * automatically (based on comments within the source CSS), but you can use
  235. * this option to identify it manually.
  236. *
  237. * If desired, you can omit the previous map with prev: `false`.
  238. */
  239. prev?: string | boolean | object | ((file: string) => string)
  240. /**
  241. * Indicates that PostCSS should set the origin content (e.g., Sass source)
  242. * of the source map. By default, it is true. But if all previous maps do not
  243. * contain sources content, PostCSS will also leave it out even if you
  244. * do not set this option.
  245. */
  246. sourcesContent?: boolean
  247. /**
  248. * Indicates that PostCSS should add annotation comments to the CSS.
  249. * By default, PostCSS will always add a comment with a path
  250. * to the source map. PostCSS will not add annotations to CSS files
  251. * that do not contain any comments.
  252. *
  253. * By default, PostCSS presumes that you want to save the source map as
  254. * `opts.to + '.map'` and will use this path in the annotation comment.
  255. * A different path can be set by providing a string value for annotation.
  256. *
  257. * If you have set `inline: true`, annotation cannot be disabled.
  258. */
  259. annotation?: string | boolean | ((file: string, root: Root) => string)
  260. /**
  261. * Override `from` in map’s sources.
  262. */
  263. from?: string
  264. /**
  265. * Use absolute path in generated source map.
  266. */
  267. absolute?: 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. * The path where you'll put the output CSS file. You should always set `to`
  277. * to generate correct source maps.
  278. */
  279. to?: string
  280. /**
  281. * Function to generate AST by string.
  282. */
  283. parser?: Syntax | Parser
  284. /**
  285. * Class to generate string by AST.
  286. */
  287. stringifier?: Syntax | Stringifier
  288. /**
  289. * Object with parse and stringify.
  290. */
  291. syntax?: Syntax
  292. /**
  293. * Source map options
  294. */
  295. map?: SourceMapOptions | boolean
  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