-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpostcss.ts
55 lines (50 loc) · 1.35 KB
/
postcss.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { AST } from "svelte-eslint-parser"
import postcss from "postcss"
import postcssLoadConfig from "postcss-load-config"
import type { RuleContext } from "../../../types"
import type { TransformResult } from "./types"
/**
* Transform with postcss
*/
export function transform(
node: AST.SvelteStyleElement,
text: string,
context: RuleContext,
): TransformResult | null {
const postcssConfig = context.settings?.svelte?.compileOptions?.postcss
if (postcssConfig === false) {
return null
}
let inputRange: AST.Range
if (node.endTag) {
inputRange = [node.startTag.range[1], node.endTag.range[0]]
} else {
inputRange = [node.startTag.range[1], node.range[1]]
}
const code = text.slice(...inputRange)
const filename = `${context.getFilename()}.css`
try {
const configFilePath = postcssConfig?.configFilePath
const config = postcssLoadConfig.sync(
{
cwd: context.getCwd?.() ?? process.cwd(),
from: filename,
},
typeof configFilePath === "string" ? configFilePath : undefined,
)
const result = postcss(config.plugins).process(code, {
...config.options,
map: {
inline: false,
},
})
return {
inputRange,
output: result.content,
mappings: result.map.toJSON().mappings,
}
} catch (_e) {
// console.log(e)
return null
}
}