UNPKG

1.97 kBJavaScriptView Raw
1'use strict'
2
3let LazyResult = require('./lazy-result')
4let Root = require('./root')
5
6class Processor {
7 constructor (plugins = []) {
8 this.version = '8.1.8'
9 this.plugins = this.normalize(plugins)
10 }
11
12 use (plugin) {
13 this.plugins = this.plugins.concat(this.normalize([plugin]))
14 return this
15 }
16
17 process (css, opts = {}) {
18 if (
19 this.plugins.length === 0 &&
20 opts.parser === opts.stringifier &&
21 !opts.hideNothingWarning
22 ) {
23 if (process.env.NODE_ENV !== 'production') {
24 if (typeof console !== 'undefined' && console.warn) {
25 console.warn(
26 'You did not set any plugins, parser, or stringifier. ' +
27 'Right now, PostCSS does nothing. Pick plugins for your case ' +
28 'on https://www.postcss.parts/ and use them in postcss.config.js.'
29 )
30 }
31 }
32 }
33 return new LazyResult(this, css, opts)
34 }
35
36 normalize (plugins) {
37 let normalized = []
38 for (let i of plugins) {
39 if (i.postcss === true) {
40 i = i()
41 } else if (i.postcss) {
42 i = i.postcss
43 }
44
45 if (typeof i === 'object' && Array.isArray(i.plugins)) {
46 normalized = normalized.concat(i.plugins)
47 } else if (typeof i === 'object' && i.postcssPlugin) {
48 normalized.push(i)
49 } else if (typeof i === 'function') {
50 normalized.push(i)
51 } else if (typeof i === 'object' && (i.parse || i.stringify)) {
52 if (process.env.NODE_ENV !== 'production') {
53 throw new Error(
54 'PostCSS syntaxes cannot be used as plugins. Instead, please use ' +
55 'one of the syntax/parser/stringifier options as outlined ' +
56 'in your PostCSS runner documentation.'
57 )
58 }
59 } else {
60 throw new Error(i + ' is not a PostCSS plugin')
61 }
62 }
63 return normalized
64 }
65}
66
67module.exports = Processor
68Processor.default = Processor
69
70Root.registerProcessor(Processor)