UNPKG

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