-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpostcss.ts
57 lines (51 loc) · 1.26 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
56
57
import type { AST } from 'svelte-eslint-parser';
import { createSyncFn } from 'synckit';
import type { RuleContext } from '../../../types';
import { getCwd, getFilename } from '../../../utils/compat';
import type { TransformResult } from './types';
const postcssProcess = createSyncFn(require.resolve('./postcss.worker')) as (options: {
cwd: string;
filename: string;
code: string;
configFilePath?: unknown;
}) => {
output: string;
mappings: string;
};
/**
* 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 = `${getFilename(context)}.css`;
try {
const configFilePath = postcssConfig?.configFilePath;
const result = postcssProcess({
cwd: getCwd(context),
filename,
code,
configFilePath
});
return {
inputRange,
...result
};
} catch (_e) {
// console.error(_e);
return null;
}
}