process-content.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict"
  2. // builtin tooling
  3. const path = require("path")
  4. // external tooling
  5. const postcss = require("postcss")
  6. // placeholder tooling
  7. let sugarss
  8. module.exports = function processContent(result, content, filename, options) {
  9. const { plugins } = options
  10. const ext = path.extname(filename)
  11. const parserList = []
  12. // SugarSS support:
  13. if (ext === ".sss") {
  14. if (!sugarss) {
  15. try {
  16. sugarss = require("sugarss")
  17. } catch {} // Ignore
  18. }
  19. if (sugarss) return runPostcss(content, filename, plugins, [sugarss])
  20. }
  21. // Syntax support:
  22. if (result.opts.syntax && result.opts.syntax.parse) {
  23. parserList.push(result.opts.syntax.parse)
  24. }
  25. // Parser support:
  26. if (result.opts.parser) parserList.push(result.opts.parser)
  27. // Try the default as a last resort:
  28. parserList.push(null)
  29. return runPostcss(content, filename, plugins, parserList)
  30. }
  31. function runPostcss(content, filename, plugins, parsers, index) {
  32. if (!index) index = 0
  33. return postcss(plugins)
  34. .process(content, {
  35. from: filename,
  36. parser: parsers[index],
  37. })
  38. .catch(err => {
  39. // If there's an error, try the next parser
  40. index++
  41. // If there are no parsers left, throw it
  42. if (index === parsers.length) throw err
  43. return runPostcss(content, filename, plugins, parsers, index)
  44. })
  45. }