Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 1 | 'use strict' |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 2 | |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 3 | let NoWorkResult = require('./no-work-result') |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 4 | let LazyResult = require('./lazy-result') |
| 5 | let Document = require('./document') |
| 6 | let Root = require('./root') |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 7 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 8 | class Processor { |
| 9 | constructor(plugins = []) { |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 10 | this.version = '8.4.5' |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 11 | this.plugins = this.normalize(plugins) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 12 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 13 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 14 | use(plugin) { |
| 15 | this.plugins = this.plugins.concat(this.normalize([plugin])) |
| 16 | return this |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 17 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 18 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 19 | process(css, opts = {}) { |
| 20 | if ( |
| 21 | this.plugins.length === 0 && |
| 22 | typeof opts.parser === 'undefined' && |
| 23 | typeof opts.stringifier === 'undefined' && |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 24 | typeof opts.syntax === 'undefined' |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 25 | ) { |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 26 | return new NoWorkResult(this, css, opts) |
| 27 | } else { |
| 28 | return new LazyResult(this, css, opts) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 29 | } |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 30 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 31 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 32 | normalize(plugins) { |
| 33 | let normalized = [] |
| 34 | for (let i of plugins) { |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 35 | if (i.postcss === true) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 36 | i = i() |
| 37 | } else if (i.postcss) { |
| 38 | i = i.postcss |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 41 | if (typeof i === 'object' && Array.isArray(i.plugins)) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 42 | normalized = normalized.concat(i.plugins) |
| 43 | } else if (typeof i === 'object' && i.postcssPlugin) { |
| 44 | normalized.push(i) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 45 | } else if (typeof i === 'function') { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 46 | normalized.push(i) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 47 | } else if (typeof i === 'object' && (i.parse || i.stringify)) { |
| 48 | if (process.env.NODE_ENV !== 'production') { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 49 | 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 54 | } |
| 55 | } else { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 56 | throw new Error(i + ' is not a PostCSS plugin') |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 57 | } |
| 58 | } |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 59 | return normalized |
| 60 | } |
| 61 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 62 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 63 | module.exports = Processor |
| 64 | Processor.default = Processor |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 65 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 66 | Root.registerProcessor(Processor) |
| 67 | Document.registerProcessor(Processor) |