UNPKG

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