blob: 82430a422cc1179ebe4c7c57d01f98961d8b9adb [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 Lippe4cb09742022-01-07 14:25:03 +01003let NoWorkResult = require('./no-work-result')
Tim van der Lippe16b82282021-11-08 13:50:26 +00004let LazyResult = require('./lazy-result')
5let Document = require('./document')
6let Root = require('./root')
Mathias Bynens79e2cf02020-05-29 16:46:17 +02007
Tim van der Lippe16b82282021-11-08 13:50:26 +00008class Processor {
9 constructor(plugins = []) {
Tim van der Lippe4cb09742022-01-07 14:25:03 +010010 this.version = '8.4.5'
Tim van der Lippe16b82282021-11-08 13:50:26 +000011 this.plugins = this.normalize(plugins)
Mathias Bynens79e2cf02020-05-29 16:46:17 +020012 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020013
Tim van der Lippe16b82282021-11-08 13:50:26 +000014 use(plugin) {
15 this.plugins = this.plugins.concat(this.normalize([plugin]))
16 return this
Mathias Bynens79e2cf02020-05-29 16:46:17 +020017 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020018
Tim van der Lippe16b82282021-11-08 13:50:26 +000019 process(css, opts = {}) {
20 if (
21 this.plugins.length === 0 &&
22 typeof opts.parser === 'undefined' &&
23 typeof opts.stringifier === 'undefined' &&
Tim van der Lippe4cb09742022-01-07 14:25:03 +010024 typeof opts.syntax === 'undefined'
Tim van der Lippe16b82282021-11-08 13:50:26 +000025 ) {
Tim van der Lippe4cb09742022-01-07 14:25:03 +010026 return new NoWorkResult(this, css, opts)
27 } else {
28 return new LazyResult(this, css, opts)
Mathias Bynens79e2cf02020-05-29 16:46:17 +020029 }
Tim van der Lippe16b82282021-11-08 13:50:26 +000030 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020031
Tim van der Lippe16b82282021-11-08 13:50:26 +000032 normalize(plugins) {
33 let normalized = []
34 for (let i of plugins) {
Tim van der Lippeefb716a2020-12-01 12:54:04 +000035 if (i.postcss === true) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000036 i = i()
37 } else if (i.postcss) {
38 i = i.postcss
Mathias Bynens79e2cf02020-05-29 16:46:17 +020039 }
40
Mathias Bynens79e2cf02020-05-29 16:46:17 +020041 if (typeof i === 'object' && Array.isArray(i.plugins)) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000042 normalized = normalized.concat(i.plugins)
43 } else if (typeof i === 'object' && i.postcssPlugin) {
44 normalized.push(i)
Mathias Bynens79e2cf02020-05-29 16:46:17 +020045 } else if (typeof i === 'function') {
Tim van der Lippe16b82282021-11-08 13:50:26 +000046 normalized.push(i)
Mathias Bynens79e2cf02020-05-29 16:46:17 +020047 } else if (typeof i === 'object' && (i.parse || i.stringify)) {
48 if (process.env.NODE_ENV !== 'production') {
Tim van der Lippe16b82282021-11-08 13:50:26 +000049 throw new Error(
50 'PostCSS syntaxes cannot be used as plugins. Instead, please use ' +
51 'one of the syntax/parser/stringifier options as outlined ' +
52 'in your PostCSS runner documentation.'
53 )
Mathias Bynens79e2cf02020-05-29 16:46:17 +020054 }
55 } else {
Tim van der Lippe16b82282021-11-08 13:50:26 +000056 throw new Error(i + ' is not a PostCSS plugin')
Mathias Bynens79e2cf02020-05-29 16:46:17 +020057 }
58 }
Tim van der Lippe16b82282021-11-08 13:50:26 +000059 return normalized
60 }
61}
Mathias Bynens79e2cf02020-05-29 16:46:17 +020062
Tim van der Lippe16b82282021-11-08 13:50:26 +000063module.exports = Processor
64Processor.default = Processor
Mathias Bynens79e2cf02020-05-29 16:46:17 +020065
Tim van der Lippe16b82282021-11-08 13:50:26 +000066Root.registerProcessor(Processor)
67Document.registerProcessor(Processor)