Skip to content

Commit 918ee94

Browse files
committed
chore: temp commit
1 parent 4153dfe commit 918ee94

File tree

8 files changed

+182
-145
lines changed

8 files changed

+182
-145
lines changed

packages/core/inject/inject-cssvars.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import hash from 'hash-sum'
22
import { type MagicStringBase } from 'magic-string-ast'
33
import { ts } from '@ast-grep/napi'
4+
import type { SgNode } from '@ast-grep/napi'
45
import MagicString from 'magic-string'
56
import type { IParseSFCRes, TMatchVariable } from '../parser'
7+
import {BindingMetadata, BindingTypes} from "@vue/compiler-dom";
8+
import {CSSVarsBindingTypes} from "../parser/utils";
69

710
const importer = 'import { useCssVars as _useCssVars } from "vue"\n'
811

@@ -28,9 +31,10 @@ export const injectCSSVars = (
2831
isScriptSetup: boolean,
2932
parserRes: IParseSFCRes,
3033
mgcStr: MagicStringBase,
34+
bindings?: BindingMetadata
3135
) => {
3236
if (!vbindVariableList || vbindVariableList.length === 0) return { vbindVariableList, mgcStr }
33-
return injectCSSVarsOnServer(vbindVariableList, isScriptSetup, parserRes, mgcStr)
37+
return injectCSSVarsOnServer(vbindVariableList, isScriptSetup, parserRes, mgcStr, bindings)
3438
}
3539

3640
// 分为三种种情况
@@ -48,10 +52,11 @@ export function injectCSSVarsOnServer(
4852
isScriptSetup: boolean,
4953
parserRes: IParseSFCRes,
5054
mgcStr: MagicStringBase,
55+
bindings?: BindingMetadata
5156
) {
5257
let resMgcStr = mgcStr
5358
const hasUseCssVars = parserRes.hasCSSVars
54-
const cssvarsObjectCode = createCSSVarsObjCode(vbindVariableList, isScriptSetup, resMgcStr)
59+
const cssvarsObjectCode = createCSSVarsObjCode(vbindVariableList, isScriptSetup, resMgcStr, bindings)
5560
// 1
5661
if (isScriptSetup) {
5762
// 1.1
@@ -135,6 +140,7 @@ export function createCSSVarsObjCode(
135140
vbindVariableList: TMatchVariable,
136141
isScriptSetup: boolean,
137142
mgcStr?: MagicStringBase,
143+
bindings?: BindingMetadata
138144
) {
139145
let resCode = ''
140146
vbindVariableList.forEach((vbVar) => {
@@ -146,14 +152,16 @@ export function createCSSVarsObjCode(
146152
const ms = new MagicString(vbVar.value)
147153
// get Identifier sgNode
148154
const cssBindKeySgNodes = findIdentifierFromExp(vbVar.value)
155+
debugger
149156
cssBindKeySgNodes.forEach((node) => {
150157
const range = node.range()
151158
ms.overwrite(
152159
range.start.index,
153160
range.end.index,
154161
// 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),
157165
)
158166
})
159167
varStr = ms.toString()
@@ -198,6 +206,35 @@ export function createUseCssVarsCode(
198206
return resCode
199207
}
200208

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+
}
201238
// TODO non-inline css - vite - dev
202239
// TODO inline bindingTypes - vite - dev
203240

packages/core/parser/parser-compiled-sfc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function resetVar() {
8787
}
8888

8989
// test only
90-
export /*#__PURE__*/ function getVar() {
90+
export /* #__PURE__ */ function getVar() {
9191
return {
9292
isSetupEnter,
9393
setupBodyNode,
@@ -98,7 +98,7 @@ export /*#__PURE__*/ function getVar() {
9898
}
9999

100100
// test only
101-
export /*#__PURE__*/ function setVar(k: string, v: any) {
101+
export /* #__PURE__ */ function setVar(k: string, v: any) {
102102
if (k === 'isSetupEnter')
103103
isSetupEnter = v
104104

0 commit comments

Comments
 (0)