-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsfc.ts
More file actions
220 lines (188 loc) · 7.28 KB
/
sfc.ts
File metadata and controls
220 lines (188 loc) · 7.28 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
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
import type { SFCParseResult } from 'vue/compiler-sfc'
import type MagicString from 'magic-string'
import { parseVueComponent } from '../../utils/ast'
import type { PinceauContext, PinceauQuery } from '../../types'
import { variantsRegex } from '../../utils'
import { transformDtHelper } from '../dt'
import { transformCssFunction, transformKeyFrameFunction } from '../css'
import { message } from '../../utils/logger'
import { transformStyle } from './style'
import { transformVariants } from './variants'
import { transformAddPropsKey } from './props-key'
import { transformAddPinceauClass } from './add-class'
export function transformVueSFC(
code: string,
query: PinceauQuery,
magicString: MagicString,
ctx: PinceauContext,
): { code: string; magicString: MagicString; variants: any; computedStyles: any; localTokens: any } {
// Resolve from parsing the <style lang="ts"> tag for current component
const variants = {}
const computedStyles = {}
const localTokens = {}
// Parse component with compiler-sfc
const parsedComponent = parseVueComponent(code, { filename: query.id })
// Transform <style> blocks
if (parsedComponent?.descriptor?.styles) {
resolveStyle(query.id, parsedComponent, magicString, variants, computedStyles, localTokens, ctx, query)
}
// Check if runtime styles are enabled on this component
const hasRuntimeStyles = Object.keys(variants).length > 0 || Object.keys(computedStyles).length > 0
// Transform <template> blocks
if (parsedComponent?.descriptor?.template) {
resolveTemplate(query.id, parsedComponent, magicString, ctx, hasRuntimeStyles)
}
// Transform <script setup> blocks
if (parsedComponent?.descriptor?.scriptSetup) {
resolveScriptSetup(query.id, parsedComponent, magicString, variants, computedStyles, ctx, parsedComponent.descriptor.scriptSetup.lang === 'ts')
}
return { code, magicString, variants, computedStyles, localTokens }
}
/**
* Transform direct <style> queries.
*
* These does not need to resolve variants or populate computed styles.
*/
export function resolveStyleQuery(code: string, magicString: MagicString, query: PinceauQuery, ctx: PinceauContext, loc?: any) {
// Handle `lang="ts"` even though that should not happen here.
if (query.lang === 'ts') {
code = transformCssFunction(query.id, code, {}, {}, {}, ctx, loc)
}
// Transform <style> block
code = transformStyle(code, ctx)
return { code, magicString }
}
/**
* Transform <template> blocks.
*/
export function resolveTemplate(_: string, parsedComponent: SFCParseResult, magicString: MagicString, ctx: PinceauContext, hasRuntimeStyles: boolean) {
// Transform `$dt()` from template
const templateContent = parsedComponent.descriptor.template
let newTemplateContent = templateContent.content
newTemplateContent = transformDtHelper(newTemplateContent, ctx, '\'')
// Add class if runtime styles are enabled
if (ctx.options.runtime && hasRuntimeStyles) { newTemplateContent = transformAddPinceauClass(newTemplateContent) }
// Overwrite <template>
if (templateContent.loc.end?.offset && templateContent.loc.end?.offset > templateContent.loc.start.offset) {
magicString.overwrite(
templateContent.loc.start.offset,
templateContent.loc.end.offset,
newTemplateContent,
)
}
}
/**
* Transform all <style> blocks.
*/
export function resolveStyle(
id: string,
parsedComponent: SFCParseResult,
magicString: MagicString,
variants: any,
computedStyles: any,
localTokens: any,
ctx: PinceauContext,
query?: PinceauQuery,
) {
const styles = parsedComponent.descriptor.styles
styles.forEach(
(styleBlock) => {
const { loc, content } = styleBlock
let code = content
let keyframeCode = ''
if (
styleBlock.attrs.lang === 'ts'
|| styleBlock.lang === 'ts'
|| styleBlock.attrs?.transformed
) {
keyframeCode = transformKeyFrameFunction(id, code, { query, ...loc })
code = transformCssFunction(id, code, variants, computedStyles, localTokens, ctx, { query, ...loc })
}
code = transformStyle(code, ctx)
code = keyframeCode + code
magicString.remove(loc.start.offset, loc.end.offset)
magicString.appendRight(loc.end.offset, `\n${code}\n`)
},
)
}
/**
* Transforms <script setup> blocks.
*/
export function resolveScriptSetup(
id: string,
parsedComponent: SFCParseResult,
magicString: MagicString,
variants: any,
computedStyles: any,
ctx: PinceauContext,
isTs: boolean,
) {
const scriptSetup = parsedComponent.descriptor.scriptSetup
const hasVariants = Object.keys(variants).length
const hasComputedStyles = Object.keys(computedStyles).length
let code = scriptSetup.content
// Transform `$dt()` usage
code = transformDtHelper(code, ctx, '`')
// Cleanup `...variants` in any case
code = code.replace(variantsRegex, () => '')
if (ctx.options.runtime) {
// Inject runtime imports
if (hasVariants || hasComputedStyles) { code = transformAddRuntimeImports(code) }
// Check for variant props
if (hasVariants) { code = transformVariants(code, variants, isTs) }
// Check for computed styles
if (hasComputedStyles) { code = transformComputedStyles(code, computedStyles) }
// Push last runtime context
if (hasVariants || hasComputedStyles) { code = transformFinishRuntimeSetup(code, hasComputedStyles, hasVariants, computedStyles) }
}
else if (hasVariants || hasComputedStyles) {
// Warn on disabled runtime features used in components
message('RUNTIME_FEATURES_CONFLICT', [id])
}
// Overwrite <script setup> block with new content
magicString.overwrite(scriptSetup.loc.start.offset, scriptSetup.loc.end.offset, code)
}
/**
* Adds computed styles code to <script setup>
*/
export function transformComputedStyles(code: string, computedStyles: any): string {
code = Object
.entries(computedStyles)
.map(([key, styleFunction]) => `\nconst ${key} = computed(() => ((props = __$pProps) => ${styleFunction})())\n`)
.join('') + code
return code
}
export function transformAddRuntimeImports(code: string): string {
code = `import { usePinceauRuntime } from 'pinceau/runtime'\n${code}`
// Handle necessary Vue imports
const vueImports = []
if (!code.match(/reactive\(/gm)) { vueImports.push('reactive') }
if (!code.match(/computed\(/gm)) { vueImports.push('computed') }
if (!code.match(/getCurrentInstance\(/gm)) { vueImports.push('getCurrentInstance') }
if (!code.match(/ref\(/gm)) { vueImports.push('ref') }
if (vueImports.length) { code = `import { ${vueImports.join(', ')} } from 'vue'\n${code}` }
// Resolve defineProps reference or add it
const { propsKey, code: _code } = transformAddPropsKey(code)
code = _code
// Props w/o const
if (propsKey && propsKey === '__$pProps') { return code }
// Props w/ const or no props
code += `\nconst __$pProps = ${propsKey || '{}'}\n`
return code
}
export function transformFinishRuntimeSetup(
newScriptSetup,
hasComputedStyles,
hasVariants,
computedStyles,
) {
newScriptSetup += [
`\n${(hasVariants || hasComputedStyles) ? 'const { $pinceau } = ' : ''}`,
'usePinceauRuntime(',
'__$pProps, ',
`${hasVariants ? '__$pVariants' : 'undefined'}, `,
`${hasComputedStyles ? `{ ${Object.keys(computedStyles).map(key => `${key}`).join(',')} }` : 'undefined'}`,
')\n',
].join('')
return newScriptSetup
}