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 | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 3 | let LazyResult = require('./lazy-result') |
| 4 | let Document = require('./document') |
| 5 | let Root = require('./root') |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 6 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 7 | class Processor { |
| 8 | constructor(plugins = []) { |
| 9 | this.version = '8.3.11' |
| 10 | this.plugins = this.normalize(plugins) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 11 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 12 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 13 | use(plugin) { |
| 14 | this.plugins = this.plugins.concat(this.normalize([plugin])) |
| 15 | return this |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 16 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 17 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 18 | 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 26 | if (process.env.NODE_ENV !== 'production') { |
| 27 | if (typeof console !== 'undefined' && console.warn) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 28 | 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | } |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 36 | return new LazyResult(this, css, opts) |
| 37 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 38 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 39 | normalize(plugins) { |
| 40 | let normalized = [] |
| 41 | for (let i of plugins) { |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 42 | if (i.postcss === true) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 43 | i = i() |
| 44 | } else if (i.postcss) { |
| 45 | i = i.postcss |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 48 | if (typeof i === 'object' && Array.isArray(i.plugins)) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 49 | normalized = normalized.concat(i.plugins) |
| 50 | } else if (typeof i === 'object' && i.postcssPlugin) { |
| 51 | normalized.push(i) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 52 | } else if (typeof i === 'function') { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 53 | normalized.push(i) |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 54 | } else if (typeof i === 'object' && (i.parse || i.stringify)) { |
| 55 | if (process.env.NODE_ENV !== 'production') { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 56 | 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 61 | } |
| 62 | } else { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 63 | throw new Error(i + ' is not a PostCSS plugin') |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 64 | } |
| 65 | } |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 66 | return normalized |
| 67 | } |
| 68 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 69 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 70 | module.exports = Processor |
| 71 | Processor.default = Processor |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 72 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 73 | Root.registerProcessor(Processor) |
| 74 | Document.registerProcessor(Processor) |