[d565449] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | let NoWorkResult = require('./no-work-result')
|
---|
| 4 | let LazyResult = require('./lazy-result')
|
---|
| 5 | let Document = require('./document')
|
---|
| 6 | let Root = require('./root')
|
---|
| 7 |
|
---|
| 8 | class Processor {
|
---|
| 9 | constructor(plugins = []) {
|
---|
| 10 | this.version = '8.4.40'
|
---|
| 11 | this.plugins = this.normalize(plugins)
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | normalize(plugins) {
|
---|
| 15 | let normalized = []
|
---|
| 16 | for (let i of plugins) {
|
---|
| 17 | if (i.postcss === true) {
|
---|
| 18 | i = i()
|
---|
| 19 | } else if (i.postcss) {
|
---|
| 20 | i = i.postcss
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | if (typeof i === 'object' && Array.isArray(i.plugins)) {
|
---|
| 24 | normalized = normalized.concat(i.plugins)
|
---|
| 25 | } else if (typeof i === 'object' && i.postcssPlugin) {
|
---|
| 26 | normalized.push(i)
|
---|
| 27 | } else if (typeof i === 'function') {
|
---|
| 28 | normalized.push(i)
|
---|
| 29 | } else if (typeof i === 'object' && (i.parse || i.stringify)) {
|
---|
| 30 | if (process.env.NODE_ENV !== 'production') {
|
---|
| 31 | throw new Error(
|
---|
| 32 | 'PostCSS syntaxes cannot be used as plugins. Instead, please use ' +
|
---|
| 33 | 'one of the syntax/parser/stringifier options as outlined ' +
|
---|
| 34 | 'in your PostCSS runner documentation.'
|
---|
| 35 | )
|
---|
| 36 | }
|
---|
| 37 | } else {
|
---|
| 38 | throw new Error(i + ' is not a PostCSS plugin')
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | return normalized
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | process(css, opts = {}) {
|
---|
| 45 | if (
|
---|
| 46 | !this.plugins.length &&
|
---|
| 47 | !opts.parser &&
|
---|
| 48 | !opts.stringifier &&
|
---|
| 49 | !opts.syntax
|
---|
| 50 | ) {
|
---|
| 51 | return new NoWorkResult(this, css, opts)
|
---|
| 52 | } else {
|
---|
| 53 | return new LazyResult(this, css, opts)
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | use(plugin) {
|
---|
| 58 | this.plugins = this.plugins.concat(this.normalize([plugin]))
|
---|
| 59 | return this
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | module.exports = Processor
|
---|
| 64 | Processor.default = Processor
|
---|
| 65 |
|
---|
| 66 | Root.registerProcessor(Processor)
|
---|
| 67 | Document.registerProcessor(Processor)
|
---|