/
transformStyle.ts
46 lines (38 loc) · 1.35 KB
/
transformStyle.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
import { type UnoGenerator, toArray } from '@unocss/core'
import type { Processed } from 'svelte/types/compiler/preprocess'
import MagicString from 'magic-string'
import { transformApply } from './transformApply'
import { transformTheme } from './transformTheme'
import type { TransformApplyOptions } from './types'
const DEFAULT_APPLY_VARIABLES = ['--at-apply']
export function checkForApply(content: string, _applyVariables: TransformApplyOptions['applyVariables']) {
if (_applyVariables === false)
return { hasApply: false, applyVariables: [] }
const applyVariables = toArray(_applyVariables || DEFAULT_APPLY_VARIABLES)
return {
hasApply: content.includes('@apply') || applyVariables.some(v => content.includes(v)),
applyVariables,
}
}
export async function transformStyle({ content, uno, prepend, filename, applyVariables, hasThemeFn }: {
content: string
uno: UnoGenerator
filename?: string
prepend: string
applyVariables: string[]
hasThemeFn: boolean
}): Promise<Processed | void> {
const s = new MagicString(content)
if (applyVariables?.length)
await transformApply({ s, uno, applyVariables })
if (hasThemeFn)
transformTheme(s, uno.config.theme)
if (!s.hasChanged())
return
if (prepend)
s.prepend(prepend)
return {
code: s.toString(),
map: s.generateMap({ hires: true, source: filename || '' }),
}
}