-
-
Notifications
You must be signed in to change notification settings - Fork 779
/
index.ts
274 lines (229 loc) · 8.5 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { cssIdRE, expandVariantGroup, notNull, regexScopePlaceholder } from '@unocss/core'
import type { SourceCodeTransformer, StringifiedUtil, UnoGenerator } from '@unocss/core'
import type { Atrule, CssNode, Declaration, List, ListItem, Rule, Selector, SelectorList } from 'css-tree'
import { clone, generate, parse, walk } from 'css-tree'
import type MagicString from 'magic-string'
import { calcMaxWidthBySize } from '@unocss/preset-mini/variants'
import type { Theme } from '@unocss/preset-mini'
type Writeable<T> = { -readonly [P in keyof T]: T[P] }
export interface TransformerDirectivesOptions {
enforce?: SourceCodeTransformer['enforce']
/**
* Treat CSS variables as directives for CSS syntax compatible.
*
* Pass `false` to disable, or a string to use as a prefix.
*
* @default '--at-'
*/
varStyle?: false | string
/**
* Throw an error if utils or themes are not found.
*
* @default true
*/
throwOnMissing?: boolean
}
export default function transformerDirectives(options: TransformerDirectivesOptions = {}): SourceCodeTransformer {
return {
name: 'css-directive',
enforce: options?.enforce,
idFilter: id => !!id.match(cssIdRE),
transform: (code, id, ctx) => {
return transformDirectives(code, ctx.uno, options, id)
},
}
}
const themeFnRE = /theme\((.*?)\)/g
const screenRuleRE = /(@screen) (.+) /g
export async function transformDirectives(
code: MagicString,
uno: UnoGenerator,
options: TransformerDirectivesOptions,
filename?: string,
originalCode?: string,
offset?: number,
) {
const {
varStyle = '--at-',
throwOnMissing = true,
} = options
const isApply = code.original.includes('@apply') || (varStyle !== false && code.original.includes(varStyle))
const isScreen = code.original.includes('@screen')
const hasThemeFn = code.original.match(themeFnRE)
if (!isApply && !hasThemeFn && !isScreen)
return
const ast = parse(originalCode || code.original, {
parseAtrulePrelude: false,
positions: true,
filename,
})
if (ast.type !== 'StyleSheet')
return
const calcOffset = (pos: number) => offset ? pos + offset : pos
const handleApply = async (node: Rule, childNode: CssNode) => {
let body: string | undefined
if (childNode.type === 'Atrule' && childNode.name === 'apply' && childNode.prelude && childNode.prelude.type === 'Raw') {
body = childNode.prelude.value.trim()
}
else if (varStyle !== false && childNode.type === 'Declaration' && childNode.property === `${varStyle}apply` && childNode.value.type === 'Raw') {
body = childNode.value.value.trim()
// remove quotes
if (body.match(/^(['"]).*\1$/))
body = body.slice(1, -1)
}
if (!body)
return
const classNames = expandVariantGroup(body)
.split(/\s+/g)
.map(className => className.trim().replace(/\\/, ''))
const utils = (
await Promise.all(
classNames.map(i => uno.parseToken(i, '-')),
))
.filter(notNull).flat()
.sort((a, b) => a[0] - b[0])
.sort((a, b) => (a[3] ? uno.parentOrders.get(a[3]) ?? 0 : 0) - (b[3] ? uno.parentOrders.get(b[3]) ?? 0 : 0))
.reduce((acc, item) => {
const target = acc.find(i => i[1] === item[1] && i[3] === item[3])
if (target)
target[2] += item[2]
else
// use spread operator to prevent reassign to uno internal cache
acc.push([...item] as Writeable<StringifiedUtil>)
return acc
}, [] as Writeable<StringifiedUtil>[])
if (!utils.length)
return
for (const i of utils) {
const [, _selector, body, parent] = i
const selector = _selector?.replace(regexScopePlaceholder, ' ') || _selector
if (parent || (selector && selector !== '.\\-')) {
let newSelector = generate(node.prelude)
if (selector && selector !== '.\\-') {
const selectorAST = parse(selector, {
context: 'selector',
}) as Selector
const prelude = clone(node.prelude) as SelectorList
prelude.children.forEach((child) => {
const parentSelectorAst = clone(selectorAST) as Selector
parentSelectorAst.children.forEach((i) => {
if (i.type === 'ClassSelector' && i.name === '\\-')
Object.assign(i, clone(child))
})
Object.assign(child, parentSelectorAst)
})
newSelector = generate(prelude)
}
let css = `${newSelector}{${body}}`
if (parent)
css = `${parent}{${css}}`
code.appendLeft(calcOffset(node.loc!.end.offset), css)
}
else {
code.appendRight(calcOffset(childNode.loc!.end.offset), body)
}
}
code.remove(
calcOffset(childNode.loc!.start.offset),
calcOffset(childNode.loc!.end.offset),
)
}
const handleThemeFn = (node: Declaration) => {
const value = node.value
const offset = value.loc!.start.offset
const str = code.original.slice(offset, value.loc!.end.offset)
const matches = Array.from(str.matchAll(themeFnRE))
if (!matches.length)
return
for (const match of matches) {
const rawArg = match[1].trim()
if (!rawArg)
throw new Error('theme() expect exact one argument, but got 0')
let value: any = uno.config.theme
const keys = rawArg.slice(1, -1).split('.')
keys.every((key) => {
if (value[key] != null)
value = value[key]
else if (value[+key] != null)
value = value[+key]
else
return false
return true
})
if (typeof value === 'string') {
code.overwrite(
offset + match.index!,
offset + match.index! + match[0].length,
value,
)
}
else if (throwOnMissing) {
throw new Error(`theme of "${rawArg.slice(1, -1)}" did not found`)
}
}
}
const handleScreen = (node: Atrule) => {
let breakpointName = ''; let prefix
if (node.name === 'screen' && node.prelude?.type === 'Raw')
breakpointName = node.prelude.value.trim()
if (!breakpointName)
return
const match = breakpointName.match(/^(?:(lt|at)-)?(\w+)$/)
if (match) {
prefix = match[1]
breakpointName = match[2]
}
const resolveBreakpoints = () => {
let breakpoints: Record<string, string> | undefined
if (uno.userConfig && uno.userConfig.theme)
breakpoints = (uno.userConfig.theme as Theme).breakpoints
if (!breakpoints)
breakpoints = (uno.config.theme as Theme).breakpoints
return breakpoints
}
const variantEntries: Array<[string, string, number]> = Object.entries(resolveBreakpoints() ?? {}).map(([point, size], idx) => [point, size, idx])
const generateMediaQuery = (breakpointName: string, prefix?: string) => {
const [, size, idx] = variantEntries.find(i => i[0] === breakpointName)!
if (prefix) {
if (prefix === 'lt')
return `@media (max-width: ${calcMaxWidthBySize(size)})`
else if (prefix === 'at')
return `@media (min-width: ${size})${variantEntries[idx + 1] ? ` and (max-width: ${calcMaxWidthBySize(variantEntries[idx + 1][1])})` : ''}`
else throw new Error(`breakpoint variant not surpported: ${prefix}`)
}
return `@media (min-width: ${size})`
}
if (!variantEntries.find(i => i[0] === breakpointName))
throw new Error(`breakpoint ${breakpointName} not found`)
const offset = node.loc!.start.offset
const str = code.original.slice(offset, node.loc!.end.offset)
const matches = Array.from(str.matchAll(screenRuleRE))
if (!matches.length)
return
for (const match of matches) {
code.overwrite(
offset + match.index!,
offset + match.index! + match[0].length,
`${generateMediaQuery(breakpointName, prefix)} `,
)
}
}
const stack: Promise<void>[] = []
const processNode = async (node: CssNode, _item: ListItem<CssNode>, _list: List<CssNode>) => {
if (isScreen && node.type === 'Atrule')
handleScreen(node)
if (hasThemeFn && node.type === 'Declaration')
handleThemeFn(node)
if (isApply && node.type === 'Rule') {
await Promise.all(
node.block.children.map(async (childNode, _childItem) => {
if (childNode.type === 'Raw')
return transformDirectives(code, uno, options, filename, childNode.value, calcOffset(childNode.loc!.start.offset))
await handleApply(node, childNode)
}).toArray(),
)
}
}
walk(ast, (...args) => stack.push(processNode(...args)))
await Promise.all(stack)
}