UNPKG

2.94 kBTypeScriptView Raw
1import {
2 AcceptedPlugin,
3 Plugin,
4 ProcessOptions,
5 Transformer,
6 TransformCallback
7} from './postcss.js'
8import LazyResult from './lazy-result.js'
9import Result from './result.js'
10import Root from './root.js'
11
12/**
13 * Contains plugins to process CSS. Create one `Processor` instance,
14 * initialize its plugins, and then use that instance on numerous CSS files.
15 *
16 * ```js
17 * const processor = postcss([autoprefixer, precss])
18 * processor.process(css1).then(result => console.log(result.css))
19 * processor.process(css2).then(result => console.log(result.css))
20 * ```
21 */
22export default class Processor {
23 /**
24 * Current PostCSS version.
25 *
26 * ```js
27 * if (result.processor.version.split('.')[0] !== '6') {
28 * throw new Error('This plugin works only with PostCSS 6')
29 * }
30 * ```
31 */
32 version: string
33
34 /**
35 * Plugins added to this processor.
36 *
37 * ```js
38 * const processor = postcss([autoprefixer, precss])
39 * processor.plugins.length //=> 2
40 * ```
41 */
42 plugins: (Plugin | Transformer | TransformCallback)[]
43
44 /**
45 * @param plugins PostCSS plugins
46 */
47 constructor (plugins?: AcceptedPlugin[])
48
49 /**
50 * Adds a plugin to be used as a CSS processor.
51 *
52 * PostCSS plugin can be in 4 formats:
53 * * A plugin in `Plugin` format.
54 * * A plugin creator function with `pluginCreator.postcss = true`.
55 * PostCSS will call this function without argument to get plugin.
56 * * A function. PostCSS will pass the function a @{link Root}
57 * as the first argument and current `Result` instance
58 * as the second.
59 * * Another `Processor` instance. PostCSS will copy plugins
60 * from that instance into this one.
61 *
62 * Plugins can also be added by passing them as arguments when creating
63 * a `postcss` instance (see [`postcss(plugins)`]).
64 *
65 * Asynchronous plugins should return a `Promise` instance.
66 *
67 * ```js
68 * const processor = postcss()
69 * .use(autoprefixer)
70 * .use(precss)
71 * ```
72 *
73 * @param plugin PostCSS plugin or `Processor` with plugins.
74 * @return {Processes} Current processor to make methods chain.
75 */
76 use (plugin: AcceptedPlugin): this
77
78 /**
79 * Parses source CSS and returns a `LazyResult` Promise proxy.
80 * Because some plugins can be asynchronous it doesn’t make
81 * any transformations. Transformations will be applied
82 * in the `LazyResult` methods.
83 *
84 * ```js
85 * processor.process(css, { from: 'a.css', to: 'a.out.css' })
86 * .then(result => {
87 * console.log(result.css)
88 * })
89 * ```
90 *
91 * @param css String with input CSS or any object with a `toString()` method,
92 * like a Buffer. Optionally, senda `Result` instance
93 * and the processor will take the `Root` from it.
94 * @param opts Options.
95 * @return Promise proxy.
96 */
97 process (
98 css: string | { toString(): string } | Result | LazyResult | Root,
99 options?: ProcessOptions
100 ): LazyResult
101}