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.

473 lines
11 KiB

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