process-content.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.plugins
  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 (e) {
  18. // Ignore
  19. }
  20. }
  21. if (sugarss) return runPostcss(content, filename, plugins, [sugarss])
  22. }
  23. // Syntax support:
  24. if (result.opts.syntax && result.opts.syntax.parse) {
  25. parserList.push(result.opts.syntax.parse)
  26. }
  27. // Parser support:
  28. if (result.opts.parser) parserList.push(result.opts.parser)
  29. // Try the default as a last resort:
  30. parserList.push(null)
  31. return runPostcss(content, filename, plugins, parserList)
  32. }
  33. function runPostcss(content, filename, plugins, parsers, index) {
  34. if (!index) index = 0
  35. return postcss(plugins)
  36. .process(content, {
  37. from: filename,
  38. parser: parsers[index],
  39. })
  40. .catch(err => {
  41. // If there's an error, try the next parser
  42. index++
  43. // If there are no parsers left, throw it
  44. if (index === parsers.length) throw err
  45. return runPostcss(content, filename, plugins, parsers, index)
  46. })
  47. }