You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

458 lines
11 KiB

  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, 'document' | '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 maps 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. * Input file if it is not simple CSS file, but HTML with <style> or JS with CSS-in-JS blocks.
  282. */
  283. document?: string
  284. /**
  285. * The path of the CSS source file. You should always set `from`,
  286. * because it is used in source map generation and syntax error messages.
  287. */
  288. from?: string | undefined
  289. /**
  290. * Source map options
  291. */
  292. map?: boolean | SourceMapOptions
  293. /**
  294. * Function to generate AST by string.
  295. */
  296. parser?: Parser<RootNode> | Syntax<RootNode>
  297. /**
  298. * Class to generate string by AST.
  299. */
  300. stringifier?: Stringifier | Syntax<RootNode>
  301. /**
  302. * Object with parse and stringify.
  303. */
  304. syntax?: Syntax<RootNode>
  305. /**
  306. * The path where you'll put the output CSS file. You should always set `to`
  307. * to generate correct source maps.
  308. */
  309. to?: string
  310. }
  311. export type Postcss = typeof postcss
  312. /**
  313. * Default function to convert a node tree into a CSS string.
  314. */
  315. export let stringify: Stringifier
  316. /**
  317. * Parses source css and returns a new `Root` or `Document` node,
  318. * which contains the source CSS nodes.
  319. *
  320. * ```js
  321. * // Simple CSS concatenation with source map support
  322. * const root1 = postcss.parse(css1, { from: file1 })
  323. * const root2 = postcss.parse(css2, { from: file2 })
  324. * root1.append(root2).toResult().css
  325. * ```
  326. */
  327. export let parse: Parser<Root>
  328. /**
  329. * Rehydrate a JSON AST (from `Node#toJSON`) back into the AST classes.
  330. *
  331. * ```js
  332. * const json = root.toJSON()
  333. * // save to file, send by network, etc
  334. * const root2 = postcss.fromJSON(json)
  335. * ```
  336. */
  337. export let fromJSON: JSONHydrator
  338. /**
  339. * Creates a new `Comment` node.
  340. *
  341. * @param defaults Properties for the new node.
  342. * @return New comment node
  343. */
  344. export function comment(defaults?: CommentProps): Comment
  345. /**
  346. * Creates a new `AtRule` node.
  347. *
  348. * @param defaults Properties for the new node.
  349. * @return New at-rule node.
  350. */
  351. export function atRule(defaults?: AtRuleProps): AtRule
  352. /**
  353. * Creates a new `Declaration` node.
  354. *
  355. * @param defaults Properties for the new node.
  356. * @return New declaration node.
  357. */
  358. export function decl(defaults?: DeclarationProps): Declaration
  359. /**
  360. * Creates a new `Rule` node.
  361. *
  362. * @param default Properties for the new node.
  363. * @return New rule node.
  364. */
  365. export function rule(defaults?: RuleProps): Rule
  366. /**
  367. * Creates a new `Root` node.
  368. *
  369. * @param defaults Properties for the new node.
  370. * @return New root node.
  371. */
  372. export function root(defaults?: RootProps): Root
  373. /**
  374. * Creates a new `Document` node.
  375. *
  376. * @param defaults Properties for the new node.
  377. * @return New document node.
  378. */
  379. export function document(defaults?: DocumentProps): Document
  380. export { postcss as default }
  381. }
  382. /**
  383. * Create a new `Processor` instance that will apply `plugins`
  384. * as CSS processors.
  385. *
  386. * ```js
  387. * let postcss = require('postcss')
  388. *
  389. * postcss(plugins).process(css, { from, to }).then(result => {
  390. * console.log(result.css)
  391. * })
  392. * ```
  393. *
  394. * @param plugins PostCSS plugins.
  395. * @return Processor to process multiple CSS.
  396. */
  397. declare function postcss(
  398. plugins?: readonly postcss.AcceptedPlugin[]
  399. ): Processor
  400. declare function postcss(...plugins: postcss.AcceptedPlugin[]): Processor
  401. export = postcss