blob: e3e3bbe59c84bc406a75d29cb443fa991e778c00 [file] [log] [blame]
Tim van der Lippe16b82282021-11-08 13:50:26 +00001'use strict'
Mathias Bynens79e2cf02020-05-29 16:46:17 +02002
Tim van der Lippe16b82282021-11-08 13:50:26 +00003let LazyResult = require('./lazy-result')
4let Document = require('./document')
5let Root = require('./root')
Mathias Bynens79e2cf02020-05-29 16:46:17 +02006
Tim van der Lippe16b82282021-11-08 13:50:26 +00007class Processor {
8 constructor(plugins = []) {
9 this.version = '8.3.11'
10 this.plugins = this.normalize(plugins)
Mathias Bynens79e2cf02020-05-29 16:46:17 +020011 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020012
Tim van der Lippe16b82282021-11-08 13:50:26 +000013 use(plugin) {
14 this.plugins = this.plugins.concat(this.normalize([plugin]))
15 return this
Mathias Bynens79e2cf02020-05-29 16:46:17 +020016 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020017
Tim van der Lippe16b82282021-11-08 13:50:26 +000018 process(css, opts = {}) {
19 if (
20 this.plugins.length === 0 &&
21 typeof opts.parser === 'undefined' &&
22 typeof opts.stringifier === 'undefined' &&
23 typeof opts.syntax === 'undefined' &&
24 !opts.hideNothingWarning
25 ) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020026 if (process.env.NODE_ENV !== 'production') {
27 if (typeof console !== 'undefined' && console.warn) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000028 console.warn(
29 'You did not set any plugins, parser, or stringifier. ' +
30 'Right now, PostCSS does nothing. Pick plugins for your case ' +
31 'on https://www.postcss.parts/ and use them in postcss.config.js.'
32 )
Mathias Bynens79e2cf02020-05-29 16:46:17 +020033 }
34 }
35 }
Tim van der Lippe16b82282021-11-08 13:50:26 +000036 return new LazyResult(this, css, opts)
37 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020038
Tim van der Lippe16b82282021-11-08 13:50:26 +000039 normalize(plugins) {
40 let normalized = []
41 for (let i of plugins) {
Tim van der Lippeefb716a2020-12-01 12:54:04 +000042 if (i.postcss === true) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000043 i = i()
44 } else if (i.postcss) {
45 i = i.postcss
Mathias Bynens79e2cf02020-05-29 16:46:17 +020046 }
47
Mathias Bynens79e2cf02020-05-29 16:46:17 +020048 if (typeof i === 'object' && Array.isArray(i.plugins)) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000049 normalized = normalized.concat(i.plugins)
50 } else if (typeof i === 'object' && i.postcssPlugin) {
51 normalized.push(i)
Mathias Bynens79e2cf02020-05-29 16:46:17 +020052 } else if (typeof i === 'function') {
Tim van der Lippe16b82282021-11-08 13:50:26 +000053 normalized.push(i)
Mathias Bynens79e2cf02020-05-29 16:46:17 +020054 } else if (typeof i === 'object' && (i.parse || i.stringify)) {
55 if (process.env.NODE_ENV !== 'production') {
Tim van der Lippe16b82282021-11-08 13:50:26 +000056 throw new Error(
57 'PostCSS syntaxes cannot be used as plugins. Instead, please use ' +
58 'one of the syntax/parser/stringifier options as outlined ' +
59 'in your PostCSS runner documentation.'
60 )
Mathias Bynens79e2cf02020-05-29 16:46:17 +020061 }
62 } else {
Tim van der Lippe16b82282021-11-08 13:50:26 +000063 throw new Error(i + ' is not a PostCSS plugin')
Mathias Bynens79e2cf02020-05-29 16:46:17 +020064 }
65 }
Tim van der Lippe16b82282021-11-08 13:50:26 +000066 return normalized
67 }
68}
Mathias Bynens79e2cf02020-05-29 16:46:17 +020069
Tim van der Lippe16b82282021-11-08 13:50:26 +000070module.exports = Processor
71Processor.default = Processor
Mathias Bynens79e2cf02020-05-29 16:46:17 +020072
Tim van der Lippe16b82282021-11-08 13:50:26 +000073Root.registerProcessor(Processor)
74Document.registerProcessor(Processor)