Skip to content

Commit 28a3fbc

Browse files
committed
chore: temp commit
1 parent 40f4077 commit 28a3fbc

File tree

7 files changed

+134
-9
lines changed

7 files changed

+134
-9
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"fs-extra": "^11.1.1",
8282
"hash-sum": "^2.0.0",
8383
"magic-string": "^0.30.0",
84+
"@ast-grep/napi": "^0.6.3",
8485
"unplugin": "^1.3.1",
8586
"vue": "^3.2.47"
8687
},
@@ -93,6 +94,7 @@
9394
"hash-sum": "^2.0.0",
9495
"magic-string": "^0.30.0",
9596
"unplugin": "^1.3.1",
97+
"@ast-grep/napi": "^0.6.3",
9698
"vue": "^3.3.4"
9799
},
98100
"devDependencies": {

packages/core/hmr/hmr.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export function reloadSFCModules(
6868
const modules = server.moduleGraph.fileToModulesMap.get(sfcp) || new Set()
6969
const modulesList = setTArray(modules)
7070
for (let i = 0; i < modulesList.length; i++) {
71-
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
7271
if (modulesList[i].id && (modulesList[i].id as string).endsWith('.vue'))
7372
server.reloadModule(modulesList[i])
7473
}

packages/core/inject/inject-cssvars.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
import hash from 'hash-sum'
22
import { type MagicStringBase } from 'magic-string-ast'
3+
import { ts } from '@ast-grep/napi'
4+
import MagicString from "magic-string";
35
import type { IParseSFCRes, TMatchVariable } from '../parser'
46

57
const importer = 'import { useCssVars as _useCssVars } from "vue"\n'
8+
9+
function findIdentifierFromExp(cssContent: string) {
10+
return ts.parse(cssContent).root().findAll({
11+
rule: {
12+
matches: 'cssComplexExpIdentifier',
13+
},
14+
utils:{
15+
cssComplexExpIdentifier: {
16+
any: [
17+
{
18+
kind: 'identifier',
19+
},
20+
],
21+
}
22+
}
23+
})
24+
}
25+
626
export const injectCSSVars = (
727
vbindVariableList: TMatchVariable | undefined,
828
isScriptSetup: boolean,
@@ -123,13 +143,28 @@ export function createCSSVarsObjCode(
123143
vbVar.hash = hashVal
124144
let varStr = ''
125145
// composition api 和 option api 一直帶 _ctx
126-
if (!isScriptSetup) {
127-
varStr = `_ctx.${vbVar.value}`
146+
if (!isScriptSetup) { // non-inline
147+
varStr = vbVar.value ? `(_ctx.${vbVar.value})` : '()'
128148
} else {
129-
// vbVar.has === false, 要带上 _ctx.
130-
varStr = vbVar.has ? vbVar.value : `_ctx.${vbVar.value}`
131-
// ref 用.value
132-
varStr = vbVar.isRef ? `${vbVar.value}.value` : varStr
149+
if(!vbVar.has){
150+
varStr = `_ctx.${vbVar.value}`
151+
}else {
152+
// TODO use BindingsType
153+
debugger
154+
const ms = new MagicString(vbVar.value)
155+
// get Identifier sgNode
156+
const cssBindKeySgNodes = findIdentifierFromExp(vbVar.value)
157+
cssBindKeySgNodes.forEach((node) => {
158+
const range = node.range()
159+
ms.overwrite(
160+
range.start.index,
161+
range.end.index,
162+
`(_ctx.${node.text()})`
163+
// genCSSVarsValue(node, bindings, propsAlias),
164+
)
165+
})
166+
varStr = ms.toString()
167+
}
133168
}
134169
resCode = `\n "${hashVal}": ${varStr},${resCode}`
135170
})

packages/core/runtime/handle-variable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function handleVBindVariable(
88
id: string,
99
ctx: IVueCSSVarsCtx,
1010
) {
11+
debugger
1112
const { descriptor } = parse(code)
1213
// let lang = 'js'
1314
// if (descriptor?.scriptSetup?.lang)
@@ -16,7 +17,6 @@ export function handleVBindVariable(
1617
// if (descriptor?.script?.lang)
1718
// lang = descriptor.script.lang
1819

19-
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
2020
// if (!JSX_TSX_REG.test(`.${lang}`)) {
2121
ctx.isScriptSetup = !!descriptor.scriptSetup
2222
// 分析 @import 的链路

play/vite/src/assets/css/foo.css

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
/*
12
#foo{
23
color: v-bind-m(sassColor);
3-
background: v-bind-m(sc2);
4+
background: v-bind-m(bwsy);
45
width: 200px;
56
height: 30px;
67
}
78
p {
89
color: v-bind-m(color);
910
}
11+
*/
12+
13+
p{
14+
width: calc(v-bind-m(foo) - 3px);
15+
height: calc(v-bind-m('foo') - 3px);
16+
top: calc(v-bind-m(foo + 'px') - 3px);
17+
}
18+
div {
19+
color: v-bind-m((a + b) / 2 + 'px' );
20+
}
21+
div {
22+
color: v-bind-m ((a + b) / 2 + 'px' );
23+
}
24+
p {
25+
color: v-bind-m(((a + b)) / (2 * a));
26+
}

play/vite/src/views/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const appTheme4 = reactive({ color: 'red' })
1414
const appTheme5 = { color: 'red' }
1515
const appTheme6 = () => 'red'
1616
const compjsx = <div>test</div>
17+
let a = 100
18+
let b = 200
19+
let foo = 300
1720
</script>
1821

1922
<script lang="jsx">

pnpm-lock.yaml

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)