1
1
import hash from 'hash-sum'
2
2
import { type MagicStringBase } from 'magic-string-ast'
3
3
import { ts } from '@ast-grep/napi'
4
+ import type { SgNode } from '@ast-grep/napi'
4
5
import MagicString from 'magic-string'
5
6
import type { IParseSFCRes , TMatchVariable } from '../parser'
7
+ import { BindingMetadata , BindingTypes } from "@vue/compiler-dom" ;
8
+ import { CSSVarsBindingTypes } from "../parser/utils" ;
6
9
7
10
const importer = 'import { useCssVars as _useCssVars } from "vue"\n'
8
11
@@ -28,9 +31,10 @@ export const injectCSSVars = (
28
31
isScriptSetup : boolean ,
29
32
parserRes : IParseSFCRes ,
30
33
mgcStr : MagicStringBase ,
34
+ bindings ?: BindingMetadata
31
35
) => {
32
36
if ( ! vbindVariableList || vbindVariableList . length === 0 ) return { vbindVariableList, mgcStr }
33
- return injectCSSVarsOnServer ( vbindVariableList , isScriptSetup , parserRes , mgcStr )
37
+ return injectCSSVarsOnServer ( vbindVariableList , isScriptSetup , parserRes , mgcStr , bindings )
34
38
}
35
39
36
40
// 分为三种种情况
@@ -48,10 +52,11 @@ export function injectCSSVarsOnServer(
48
52
isScriptSetup : boolean ,
49
53
parserRes : IParseSFCRes ,
50
54
mgcStr : MagicStringBase ,
55
+ bindings ?: BindingMetadata
51
56
) {
52
57
let resMgcStr = mgcStr
53
58
const hasUseCssVars = parserRes . hasCSSVars
54
- const cssvarsObjectCode = createCSSVarsObjCode ( vbindVariableList , isScriptSetup , resMgcStr )
59
+ const cssvarsObjectCode = createCSSVarsObjCode ( vbindVariableList , isScriptSetup , resMgcStr , bindings )
55
60
// 1
56
61
if ( isScriptSetup ) {
57
62
// 1.1
@@ -135,6 +140,7 @@ export function createCSSVarsObjCode(
135
140
vbindVariableList : TMatchVariable ,
136
141
isScriptSetup : boolean ,
137
142
mgcStr ?: MagicStringBase ,
143
+ bindings ?: BindingMetadata
138
144
) {
139
145
let resCode = ''
140
146
vbindVariableList . forEach ( ( vbVar ) => {
@@ -146,14 +152,16 @@ export function createCSSVarsObjCode(
146
152
const ms = new MagicString ( vbVar . value )
147
153
// get Identifier sgNode
148
154
const cssBindKeySgNodes = findIdentifierFromExp ( vbVar . value )
155
+ debugger
149
156
cssBindKeySgNodes . forEach ( ( node ) => {
150
157
const range = node . range ( )
151
158
ms . overwrite (
152
159
range . start . index ,
153
160
range . end . index ,
154
161
// non-inline composition api 和 option api 一直帶 _ctx
155
- ! isScriptSetup ? `(_ctx.${ node . text ( ) } )` : '' ,
156
- // genCSSVarsValue(node, bindings, propsAlias),
162
+ ! isScriptSetup ?
163
+ `(_ctx.${ node . text ( ) } )` :
164
+ genCSSVarsValue ( node , bindings ) ,
157
165
)
158
166
} )
159
167
varStr = ms . toString ( )
@@ -198,6 +206,35 @@ export function createUseCssVarsCode(
198
206
return resCode
199
207
}
200
208
209
+ // TODO unit test
210
+ function genCSSVarsValue (
211
+ node : SgNode ,
212
+ bindings ?: BindingMetadata ) {
213
+ let res = '()'
214
+ if ( bindings ) {
215
+ const binding = bindings [ node . text ( ) ]
216
+ switch ( binding ) {
217
+ case CSSVarsBindingTypes . PROPS :
218
+ case CSSVarsBindingTypes . SETUP_CONST :
219
+ case CSSVarsBindingTypes . SETUP_REACTIVE_CONST :
220
+ case CSSVarsBindingTypes . LITERAL_CONST :
221
+ res = node . text ( )
222
+ break
223
+ case CSSVarsBindingTypes . SETUP_MAYBE_REF :
224
+ case CSSVarsBindingTypes . SETUP_LET :
225
+ res = `_unref(${ node . text ( ) } )`
226
+ break
227
+ // The `vineProp` variable is inconsistent with vue here, and vue is `PROPS`
228
+ // Because vine compilation will use `toRefs` processing
229
+ case CSSVarsBindingTypes . SETUP_REF :
230
+ res = `${ node . text ( ) } .value`
231
+ break
232
+ default :
233
+ res = `_ctx.${ node . text ( ) } `
234
+ }
235
+ }
236
+ return res
237
+ }
201
238
// TODO non-inline css - vite - dev
202
239
// TODO inline bindingTypes - vite - dev
203
240
0 commit comments