wishthis/node_modules/postcss/lib/processor.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-08-17 09:47:40 +00:00
'use strict'
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let NoWorkResult = require('./no-work-result')
let LazyResult = require('./lazy-result')
let Document = require('./document')
let Root = require('./root')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
class Processor {
constructor(plugins = []) {
this.version = '8.4.28'
this.plugins = this.normalize(plugins)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
normalize(plugins) {
let normalized = []
for (let i of plugins) {
2022-01-21 08:28:41 +00:00
if (i.postcss === true) {
2023-08-17 09:47:40 +00:00
i = i()
} else if (i.postcss) {
i = i.postcss
2022-01-21 08:28:41 +00:00
}
if (typeof i === 'object' && Array.isArray(i.plugins)) {
2023-08-17 09:47:40 +00:00
normalized = normalized.concat(i.plugins)
} else if (typeof i === 'object' && i.postcssPlugin) {
normalized.push(i)
2022-01-21 08:28:41 +00:00
} else if (typeof i === 'function') {
2023-08-17 09:47:40 +00:00
normalized.push(i)
2022-01-21 08:28:41 +00:00
} else if (typeof i === 'object' && (i.parse || i.stringify)) {
if (process.env.NODE_ENV !== 'production') {
2023-08-17 09:47:40 +00:00
throw new Error(
'PostCSS syntaxes cannot be used as plugins. Instead, please use ' +
'one of the syntax/parser/stringifier options as outlined ' +
'in your PostCSS runner documentation.'
)
2022-01-21 08:28:41 +00:00
}
} else {
2023-08-17 09:47:40 +00:00
throw new Error(i + ' is not a PostCSS plugin')
2022-01-21 08:28:41 +00:00
}
}
2023-08-17 09:47:40 +00:00
return normalized
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
process(css, opts = {}) {
if (
this.plugins.length === 0 &&
typeof opts.parser === 'undefined' &&
typeof opts.stringifier === 'undefined' &&
typeof opts.syntax === 'undefined'
) {
return new NoWorkResult(this, css, opts)
} else {
return new LazyResult(this, css, opts)
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
use(plugin) {
this.plugins = this.plugins.concat(this.normalize([plugin]))
return this
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
module.exports = Processor
Processor.default = Processor
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
Root.registerProcessor(Processor)
Document.registerProcessor(Processor)