You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3 KiB

2 years ago
  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 && 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. }