processor.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict'
  2. let LazyResult = require('./lazy-result')
  3. let Root = require('./root')
  4. class Processor {
  5. constructor (plugins = []) {
  6. this.version = '8.2.6'
  7. this.plugins = this.normalize(plugins)
  8. }
  9. use (plugin) {
  10. this.plugins = this.plugins.concat(this.normalize([plugin]))
  11. return this
  12. }
  13. process (css, opts = {}) {
  14. if (
  15. this.plugins.length === 0 &&
  16. opts.parser === opts.stringifier &&
  17. !opts.hideNothingWarning
  18. ) {
  19. if (process.env.NODE_ENV !== 'production') {
  20. if (typeof console !== 'undefined' && console.warn) {
  21. console.warn(
  22. 'You did not set any plugins, parser, or stringifier. ' +
  23. 'Right now, PostCSS does nothing. Pick plugins for your case ' +
  24. 'on https://www.postcss.parts/ and use them in postcss.config.js.'
  25. )
  26. }
  27. }
  28. }
  29. return new LazyResult(this, css, opts)
  30. }
  31. normalize (plugins) {
  32. let normalized = []
  33. for (let i of plugins) {
  34. if (i.postcss === true) {
  35. i = i()
  36. } else if (i.postcss) {
  37. i = i.postcss
  38. }
  39. if (typeof i === 'object' && Array.isArray(i.plugins)) {
  40. normalized = normalized.concat(i.plugins)
  41. } else if (typeof i === 'object' && i.postcssPlugin) {
  42. normalized.push(i)
  43. } else if (typeof i === 'function') {
  44. normalized.push(i)
  45. } else if (typeof i === 'object' && (i.parse || i.stringify)) {
  46. if (process.env.NODE_ENV !== 'production') {
  47. throw new Error(
  48. 'PostCSS syntaxes cannot be used as plugins. Instead, please use ' +
  49. 'one of the syntax/parser/stringifier options as outlined ' +
  50. 'in your PostCSS runner documentation.'
  51. )
  52. }
  53. } else {
  54. throw new Error(i + ' is not a PostCSS plugin')
  55. }
  56. }
  57. return normalized
  58. }
  59. }
  60. module.exports = Processor
  61. Processor.default = Processor
  62. Root.registerProcessor(Processor)