UNPKG

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