process-content.js 1.3 KB

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