postcss.d.ts 10 KB

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