-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.ts
100 lines (86 loc) · 2.09 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import type { Preset, PresetOptions } from '@unocss/core'
import { preflights } from './preflights'
import { rules } from './rules'
import type { Theme, ThemeAnimation } from './theme'
import { theme } from './theme'
import { transformSelector } from './transform'
import { variants } from './variants'
export { preflights } from './preflights'
export { theme, colors } from './theme'
export { parseColor } from './utils'
// v0.44.0
export type { ThemeAnimation, Theme }
export interface DarkModeSelectors {
/**
* Selector for light variant.
*
* @default '.light'
*/
light?: string
/**
* Selector for dark variant.
*
* @default '.dark'
*/
dark?: string
}
export interface PresetMiniOptions extends PresetOptions {
/**
* Dark mode options
*
*/
dark?: 'class' | 'media' | DarkModeSelectors
/**
* @default false
*/
attributifyPseudo?: Boolean
/**
* Prefix for CSS variables.
*
* @default 'un-'
*/
variablePrefix?: string
/**
* Utils prefix
*
* @default undefined
*/
prefix?: string
/**
* 是否转换微信class
*
* @default true
*/
transform?: boolean
}
export const presetWeapp = (options: PresetMiniOptions = {}): Preset<Theme> => {
options.dark = options.dark ?? 'class'
options.attributifyPseudo = options.attributifyPseudo ?? false
options.transform = options.transform ?? true
return {
name: 'unocss-preset-weapp',
theme,
rules,
variants: variants(options),
options,
postprocess(css) {
if (options.transform) {
// 转换
css.selector = transformSelector(css.selector)
}
// 设置变量前缀
if (options.variablePrefix && options.variablePrefix !== 'un-')
VarPrefixPostprocessor(options.variablePrefix, css)
},
preflights,
prefix: options.prefix,
}
}
export default presetWeapp
function VarPrefixPostprocessor(prefix: string, obj: any) {
obj.entries.forEach((i: any) => {
i[0] = i[0].replace(/^--un-/, `--${prefix}`)
if (typeof i[1] === 'string')
i[1] = i[1].replace(/var\(--un-/g, `var(--${prefix}`)
})
}