1 | "use strict"
|
---|
2 |
|
---|
3 | // builtin tooling
|
---|
4 | const path = require("path")
|
---|
5 |
|
---|
6 | // placeholder tooling
|
---|
7 | let sugarss
|
---|
8 |
|
---|
9 | module.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 | try {
|
---|
25 | sugarss = require("sugarss")
|
---|
26 | } catch {} // Ignore
|
---|
27 | }
|
---|
28 | if (sugarss)
|
---|
29 | return runPostcss(postcss, content, filename, plugins, [sugarss])
|
---|
30 | }
|
---|
31 |
|
---|
32 | // Syntax support:
|
---|
33 | if (result.opts.syntax && result.opts.syntax.parse) {
|
---|
34 | parserList.push(result.opts.syntax.parse)
|
---|
35 | }
|
---|
36 |
|
---|
37 | // Parser support:
|
---|
38 | if (result.opts.parser) parserList.push(result.opts.parser)
|
---|
39 | // Try the default as a last resort:
|
---|
40 | parserList.push(null)
|
---|
41 |
|
---|
42 | return runPostcss(postcss, content, filename, plugins, parserList)
|
---|
43 | }
|
---|
44 |
|
---|
45 | function runPostcss(postcss, content, filename, plugins, parsers, index) {
|
---|
46 | if (!index) index = 0
|
---|
47 | return postcss(plugins)
|
---|
48 | .process(content, {
|
---|
49 | from: filename,
|
---|
50 | parser: parsers[index],
|
---|
51 | })
|
---|
52 | .catch(err => {
|
---|
53 | // If there's an error, try the next parser
|
---|
54 | index++
|
---|
55 | // If there are no parsers left, throw it
|
---|
56 | if (index === parsers.length) throw err
|
---|
57 | return runPostcss(postcss, content, filename, plugins, parsers, index)
|
---|
58 | })
|
---|
59 | }
|
---|