-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.ts
167 lines (151 loc) · 4.69 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import postcss, {type Rule, type ChildNode} from 'postcss'
import autoprefixer from 'autoprefixer'
import {parse, type CssInJs} from 'postcss-js'
import {tokenize, type ClassToken} from 'parsel-js'
import type {Preset, DynamicRule, Preflight} from 'unocss'
import camelCase from 'camelcase'
import colors from 'daisyui/src/theming/index.js'
import utilities from 'daisyui/dist/utilities.js'
import base from 'daisyui/dist/base.js'
import unstyled from 'daisyui/dist/unstyled.js'
import unstyledRtl from 'daisyui/dist/unstyled.rtl.js'
import styled from 'daisyui/dist/styled.js'
import styledRtl from 'daisyui/dist/styled.rtl.js'
import utilitiesUnstyled from 'daisyui/dist/utilities-unstyled.js'
import utilitiesStyled from 'daisyui/dist/utilities-styled.js'
import themes from 'daisyui/src/theming/themes.js'
import colorFunctions from 'daisyui/src/theming/functions.js'
const processor = postcss(autoprefixer)
const process = (object: CssInJs) => processor.process(object, {parser: parse})
const replacePrefix = (css: string) => css.replaceAll('--tw-', '--un-')
const defaultOptions = {
styled: true,
themes: false as
| boolean
| Array<string | Record<string, Record<string, string>>>,
base: true,
utils: true,
rtl: false,
darkTheme: 'dark',
}
export const presetDaisy = (
options: Partial<typeof defaultOptions> = {},
): Preset => {
options = {...defaultOptions, ...options}
const rules = new Map<string, string>()
const keyframes: string[] = []
const styles = [
options.styled
? (options.rtl ? styledRtl : styled)
: (options.rtl ? unstyledRtl : unstyled),
]
if (options.utils) {
styles.push(utilities, utilitiesUnstyled, utilitiesStyled)
}
for (const node of styles.flatMap(style => process(style).root.nodes as ChildNode[])) {
const isAtRule = node.type === 'atrule'
// @keyframes
if (isAtRule && node.name === 'keyframes') {
keyframes.push(String(node))
continue
}
// Unwrap @media if necessary
const rule = (isAtRule ? node.nodes[0]! : node) as Rule
const selector = rule.selectors[0]!
const tokens = tokenize(selector)
const token = tokens[0]!
let base = ''
if (token.type === 'class') {
// Resolve conflicts with @unocss/preset-wind link variant
// .link-* -> .link
if (selector.startsWith('.link-')) {
base = 'link'
} else if (selector.startsWith('.modal-open')) {
base = 'modal'
} else {
base = token.name
}
} else if (token.type === 'pseudo-class' && token.name === 'where') {
// :where(.foo) -> .foo
base = (tokenize(token.argument!)[0] as ClassToken).name
} else if (['[dir="rtl"]', ':root'].includes(token.content)) {
// Special case for https://github.com/saadeghi/daisyui/blob/6db14181733915278621d9b2d128b0af43c52323/src/components/unstyled/modal.css#LL28C1-L28C89
base = tokens[1]!.content.includes('.modal-open')
? 'modal'
// Skip prefixes
: (tokens[2] as ClassToken).name
}
rules.set(base, (rules.get(base) ?? '') + String(rule) + '\n')
}
const preflights: Preflight[] = [
{
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () => keyframes.join('\n'),
layer: 'daisy-keyframes',
},
]
if (options.base) {
preflights.unshift({
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () => replacePrefix(process(base).css),
layer: 'daisy-base',
})
}
colorFunctions.injectThemes(
theme => {
preflights.push({
// eslint-disable-next-line @typescript-eslint/naming-convention
getCSS: () => process(theme).css,
layer: 'daisy-themes',
})
},
// @ts-expect-error Return never
key => {
if (key === 'daisyui.themes') {
return options.themes
}
if (key === 'daisyui.darkTheme') {
return options.darkTheme
}
},
themes,
'hsl',
)
return {
name: 'unocss-preset-daisy',
preflights,
theme: {
colors: {
...Object.fromEntries(
Object.entries(colors)
.filter(
([color]) =>
// Already in @unocss/preset-mini
// https://github.com/unocss/unocss/blob/0f7efcba592e71d81fbb295332b27e6894a0b4fa/packages/preset-mini/src/_theme/colors.ts#L11-L12
!['transparent', 'current'].includes(color)
// Added below
&& !color.startsWith('base'),
)
.map(([color, value]) => [camelCase(color), value]),
),
base: Object.fromEntries(
Object.entries(colors)
.filter(([color]) => color.startsWith('base'))
.map(([color, value]) => [color.replace('base-', ''), value]),
),
},
},
rules: [...rules].map(
([base, rule]) =>
[
new RegExp(`^${base}$`),
() => replacePrefix(rule),
{
layer: base.startsWith('checkbox-')
? 'daisy-components-post'
: 'daisy-components',
},
] satisfies DynamicRule,
),
}
}