-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcss.ts
More file actions
148 lines (131 loc) · 4.19 KB
/
css.ts
File metadata and controls
148 lines (131 loc) · 4.19 KB
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
import type { ASTNode } from 'ast-types'
import { defu } from 'defu'
import { parse } from 'acorn'
import { hash } from 'ohash'
import type { AnimationAst, PinceauContext } from '../types'
import { resolveCssProperty, stringify, stringifyKeyFrames } from '../utils'
import { message } from '../utils/logger'
import { parseAst, printAst, visitAst } from '../utils/ast'
import { resolveRuntimeContents } from './vue/computed'
function genTransformError(e, id: string, loc?: any) {
e.loc.line = (loc.start.line + e.loc.line) - 1
const filePath = `${id.split('?')[0]}:${e.loc.line}:${e.loc.column}`
message('TRANSFORM_ERROR', [filePath, e])
}
/**
* Stringify every call of css() into a valid Vue <style> declaration.
*/
export function transformCssFunction(id: string,
code = '',
variants: any,
computedStyles: any,
localTokens: any,
ctx: PinceauContext,
loc?: any) {
// Enhance error logging for `css()`
try {
parse(code, { ecmaVersion: 'latest' })
}
catch (e) {
genTransformError(e, id, loc)
return ''
}
// Resolve stringifiable declaration from `css()` content
const declaration = resolveCssCallees(
code,
ast => evalCssDeclaration(ast, computedStyles, localTokens),
)
// Handle variants and remove them from declaration and drop the key
if (declaration && declaration?.variants) {
Object.assign(variants || {}, defu(variants || {}, declaration?.variants || {}))
delete declaration.variants
}
return stringify(declaration, (property: any, value: any, _style: any, _selectors: any) => resolveCssProperty(property, value, _style, _selectors, Object.keys(localTokens || {}), ctx, loc))
}
export function transformKeyFrameFunction(id: string, code = '', loc?: any) {
try {
parse(code, { ecmaVersion: 'latest' })
}
catch (e) {
genTransformError(e, id, loc)
return ''
}
const declaration = resolveKeyFrameCallees(code, ast => evalKeyframeDeclaration(ast))
return stringifyKeyFrames(declaration)
}
/**
* Transform a variants property to nested selectors.
*/
export function castVariants(property: any, value: any) {
return Object.entries(value).reduce(
(acc: any, [key, value]) => {
acc[key] = value
return acc
},
{},
)
}
/**
* Find all calls of css() and call a callback on each.
*/
export function resolveCssCallees(code: string, cb: (ast: ASTNode) => any): any {
const ast = parseAst(code)
let result: any = false
visitAst(ast, {
visitCallExpression(path: any) {
if (path.value.callee.name === 'css') { result = defu(result || {}, cb(path.value.arguments[0])) }
return this.traverse(path)
},
})
return result
}
export function resolveKeyFrameCallees(code: string, cb: (body: AnimationAst) => any): any {
const ast = parseAst(code)
let result: any = false
visitAst(ast, {
visitCallExpression(path: any) {
if (path.value.callee.name === 'keyFrames') {
result = defu(result || {}, cb({
animationName: path?.parentPath.value.id.name,
animationCode: path.value.arguments[0],
}))
}
return this.traverse(path)
},
})
return result
}
/**
* Resolve computed styles found in css() declaration.
*/
export function evalCssDeclaration(cssAst: ASTNode, computedStyles: any = {}, localTokens: any = {}) {
// Resolve computed styled from AST of css() call
resolveRuntimeContents(cssAst, computedStyles, localTokens)
try {
// eslint-disable-next-line no-eval
const _eval = eval
// const transformed = transform({ source: recast.print(ast).code })
_eval(`var cssDeclaration = ${printAst(cssAst).code}`)
// @ts-expect-error - Evaluated code
return cssDeclaration
}
catch (e) {
return {}
}
}
export function evalKeyframeDeclaration(body: AnimationAst) {
try {
const { animationCode, animationName } = body
// eslint-disable-next-line no-eval
const _eval = eval
const keyFramesCode = printAst(animationCode).code
const keyFrameName = `pa-${hash(keyFramesCode)}`
_eval(`var ${animationName} = '${keyFrameName}'`)
_eval(`var keyFrameDeclaration = ${keyFramesCode}`)
// @ts-expect-error - Evaluated code
return { keyFrameDeclaration, keyFrameName }
}
catch (error) {
return {}
}
}