Skip to content

Commit 41948b6

Browse files
committed
chore: updated unit test
1 parent 4d545e5 commit 41948b6

File tree

13 files changed

+333
-112
lines changed

13 files changed

+333
-112
lines changed

packages/core/inject/__test__/__snapshots__/inject-cssvars.spec.ts.snap

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`inject-cssvars > injectUseCssVarsOption > hasUseCssVars is false 1`] = `
44
"import { useCssVars as _useCssVars } from \\"vue\\"
5+
import { unref as _unref } from \\"vue\\"
56
const __default__ = {};const __injectCSSVars__ = () => {}
67
78
const __setup__ = __default__.setup
@@ -13,7 +14,8 @@ const __default__ = {};const __injectCSSVars__ = () => {}
1314
`;
1415

1516
exports[`inject-cssvars > injectUseCssVarsOption > hasUseCssVars is true 1`] = `
16-
"import { useCssVars as _useCssVars } from 'vue'
17+
"import { unref as _unref } from \\"vue\\"
18+
import { useCssVars as _useCssVars } from 'vue'
1719
const __injectCSSVars__ = () => {
1820
_useCssVars(_ctx => ({
1921
\\"color\\": (_ctx.color),
@@ -30,14 +32,16 @@ const _sfc_main = __default__"
3032

3133
exports[`inject-cssvars > injectUseCssVarsSetup > hasUseCssVars is false 1`] = `
3234
"import { useCssVars as _useCssVars } from \\"vue\\"
35+
import { unref as _unref } from \\"vue\\"
3336
function setup() {
3437
_useCssVars((_ctx) => ({
3538
\\"1439c43b\\": color.value,
3639
}));}"
3740
`;
3841

3942
exports[`inject-cssvars > injectUseCssVarsSetup > hasUseCssVars is true 1`] = `
40-
"function setup() { _useCssVars((_ctx) => ({\\"1439c43b\\": color.value,
43+
"import { unref as _unref } from \\"vue\\"
44+
function setup() { _useCssVars((_ctx) => ({\\"1439c43b\\": color.value,
4145
\\"229090c3-sassColor\\": sassColor.value
4246
})); }"
4347
`;

packages/core/inject/__test__/inject-css.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('inject-css', () => {
1212
test('injectCSSOnServer: vbindVariableList is undefined', () => {
1313
const code = 'v-bind-m(foo)'
1414
const mgcStr = new MagicString(code)
15-
expect(injectCSSOnServer(mgcStr, undefined, false).toString()).toBe(code)
15+
expect(injectCSSOnServer(mgcStr, undefined, false).toString()).toBe('var(foo)')
1616
})
1717

1818
test('removeStyleTagsAndContent: basic', () => {

packages/core/inject/__test__/inject-cssvars.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('inject-cssvars', () => {
117117
test('isScriptSetup=true, has=false, hash=false, isRef=true', () => {
118118
const vbindVariableList = [{ value: 'color', has: false, hash: false, isRef: true }]
119119
const result = createCSSVarsObjCode(vbindVariableList as any, true)
120-
expect(result).toContain('color.value')
120+
expect(result).toContain('_ctx.color')
121121
})
122122

123123
test('isScriptSetup=true, has=false, hash=true, isRef=false', () => {
@@ -130,7 +130,7 @@ describe('inject-cssvars', () => {
130130
test('isScriptSetup=true, has=false, hash=true, isRef=true', () => {
131131
const vbindVariableList = [{ value: 'color', has: false, hash: 'color', isRef: true }]
132132
const result = createCSSVarsObjCode(vbindVariableList as any, true)
133-
expect(result).toContain('color.value')
133+
expect(result).toContain('_ctx.color')
134134
expect(result).toContain('"color":')
135135
})
136136

@@ -143,7 +143,7 @@ describe('inject-cssvars', () => {
143143
test('isScriptSetup=true, has=true, hash=false, isRef=true', () => {
144144
const vbindVariableList = [{ value: 'color', has: true, hash: false, isRef: true }]
145145
const result = createCSSVarsObjCode(vbindVariableList as any, true)
146-
expect(result).toContain('color.value')
146+
expect(result).toContain('_ctx.color')
147147
})
148148

149149
test('isScriptSetup=true, has=true, hash=true, isRef=false', () => {
@@ -156,12 +156,12 @@ describe('inject-cssvars', () => {
156156
test('isScriptSetup=true, has=true, hash=true, isRef=true', () => {
157157
const vbindVariableList = [{ value: 'color', has: true, hash: 'color', isRef: true }]
158158
const result = createCSSVarsObjCode(vbindVariableList as any, true)
159-
expect(result).toContain('color.value')
159+
expect(result).toContain('_ctx.color')
160160
expect(result).toContain('"color":')
161161
})
162162

163163
test('avoid repeated injections', () => {
164-
const mockCode = '\n "color": color.value,'
164+
const mockCode = '\n "color": _ctx.color,'
165165
const vbindVariableList = [{ value: 'color', has: true, hash: 'color', isRef: true }]
166166
const result = createCSSVarsObjCode(vbindVariableList as any, true, new MagicString(mockCode))
167167
expect(result).not.toBeTruthy()
@@ -274,7 +274,7 @@ describe('inject-cssvars', () => {
274274
true,
275275
parseRes,
276276
mgcStr)
277-
expect(res.mgcStr.toString()).toContain('color.value')
277+
expect(res.mgcStr.toString()).toContain('_ctx.color')
278278
})
279279

280280
test('isScriptSetup=false hasUseCssVars=true', () => {
@@ -317,7 +317,7 @@ describe('inject-cssvars', () => {
317317
true,
318318
parseRes,
319319
mgcStr)
320-
expect(res.mgcStr.toString()).toContain('color.value')
320+
expect(res.mgcStr.toString()).toContain('_ctx.color')
321321
expect(res.mgcStr.toString()).toContain('_useCssVars')
322322
})
323323
})
@@ -340,7 +340,7 @@ describe('inject-cssvars', () => {
340340
true,
341341
parseRes,
342342
mgcStr)
343-
expect(res.mgcStr.toString()).toContain('color.value')
343+
expect(res.mgcStr.toString()).toContain('_ctx.color')
344344
expect(res.mgcStr.toString()).toContain('_useCssVars')
345345
})
346346

packages/core/inject/inject-css.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
import hash from 'hash-sum'
22
import { transformInjectCSS } from '../transform/transform-inject-css'
3-
import { parseImports } from '../parser'
3+
import {parseCssVars, parseImports} from '../parser'
44
import type { MagicStringBase } from 'magic-string-ast'
55
import type { TInjectCSSContent } from '../runtime/process-css'
66
import type { SFCDescriptor } from '@vue/compiler-sfc'
77
import type { TMatchVariable } from '../parser'
8-
import {ts} from "@ast-grep/napi";
8+
declare type PCSSVARARR = Array<{
9+
start: number,
10+
end: number,
11+
offset: number,
12+
variable: string}>
913
export function injectCSSOnServer(
1014
mgcStr: MagicStringBase,
1115
vbindVariableList: TMatchVariable | undefined,
1216
isHMR: boolean,
1317
) {
14-
vbindVariableList && vbindVariableList.forEach((vbVar) => {
15-
// 样式文件修改后,style热更新可能会先于 sfc 热更新运行,这里先设置hash
16-
// 详见 packages/core/index.ts的 handleHotUpdate
17-
if (!vbVar.hash && isHMR)
18-
vbVar.hash = hash(vbVar.value + vbVar.has)
1918

19+
const pCssVarsArr:PCSSVARARR = []
20+
parseCssVars([mgcStr.toString()], {
21+
getIndex(start: number, end: number, offset, variable) {
22+
pCssVarsArr.push({start, end, offset, variable})
23+
}
24+
})
2025

26+
pCssVarsArr.forEach(pca => {
27+
if(vbindVariableList){
28+
for (let i = 0; i < vbindVariableList.length; i++) {
29+
const vbVar = vbindVariableList[i]
30+
// 样式文件修改后,style热更新可能会先于 sfc 热更新运行,这里先设置hash
31+
// 详见 packages/core/index.ts的 handleHotUpdate
32+
if (!vbVar.hash && isHMR)
33+
vbVar.hash = hash(vbVar.value + vbVar.has)
2134

22-
const viteCSSSgNode = ts.parse(mgcStr.toString()).root().find({
23-
rule: {
24-
matches: 'VITE_CSS',
25-
},
26-
utils: {
27-
VITE_CSS: {
28-
any: [
29-
{
30-
pattern: "const __vite__css = \"$VAL\""
31-
},
32-
],
35+
if(vbVar.value === pca.variable){
36+
const offset = pca.offset
37+
const start = pca.start + offset
38+
const end = pca.end + offset
39+
vbVar.hash && (mgcStr = mgcStr.overwrite(start, end, `--${vbVar.hash}`))
40+
break
3341
}
3442
}
35-
})
36-
const content = viteCSSSgNode?.getMatch('VAL')
37-
vbVar.hash && (mgcStr = mgcStr.replaceAll(`v-bind-m`, `var`))
38-
vbVar.hash && (mgcStr = mgcStr.replaceAll(`${vbVar.value}`, `--${vbVar.hash}`))
39-
debugger
43+
}
4044
})
45+
46+
mgcStr = mgcStr.replace(/v-bind-m\s*\(/g, "var(");
4147
return mgcStr
4248
}
4349

packages/core/inject/inject-cssvars.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export function injectUseCssVarsSetup(
8989
parserRes: IParseSFCRes,
9090
) {
9191
let resMgcStr = mgcStr
92-
resMgcStr = resMgcStr.prependLeft(0, importerUnref)
92+
if(!resMgcStr.toString().includes('_unref')){
93+
resMgcStr = resMgcStr.prependLeft(0, importerUnref)
94+
}
95+
9396
if (parserRes) {
9497
if (!hasUseCssVars
9598
&& parserRes.setupBodyNode
@@ -115,18 +118,40 @@ export function injectUseCssVarsOption(
115118
parserRes: IParseSFCRes,
116119
) {
117120
let resMgcStr = mgcStr
118-
resMgcStr = resMgcStr.prependLeft(0, importerUnref)
121+
if(!resMgcStr.toString().includes('_unref')){
122+
resMgcStr = resMgcStr.prependLeft(0, importerUnref)
123+
}
119124
if (!hasUseCssVars) {
120-
resMgcStr = resMgcStr.replaceAll('const _sfc_main', 'const __default__')
121-
resMgcStr = resMgcStr.replaceAll(
122-
'function _sfc_render',
123-
`${useCssVars}\n
125+
if(resMgcStr.toString().includes('const _sfc_main')){
126+
resMgcStr = resMgcStr.replaceAll('const _sfc_main', 'const __default__')
127+
} else if(resMgcStr.toString().includes('import _sfc_main')){
128+
resMgcStr = resMgcStr.replaceAll('import _sfc_main', 'import __default__')
129+
} else {
130+
resMgcStr = resMgcStr.replaceAll('export default {', 'const __default__ = {')
131+
}
132+
133+
if(resMgcStr.toString().includes('function _sfc_render')){
134+
resMgcStr = resMgcStr.replaceAll(
135+
'function _sfc_render',
136+
`${useCssVars}\n
124137
const __setup__ = __default__.setup
125138
__default__.setup = __setup__
126139
? (props, ctx) => { __injectCSSVars__(); return __setup__(props, ctx) }
127140
: __injectCSSVars__
128141
const _sfc_main = __default__
129142
function _sfc_render`)
143+
} else if(resMgcStr.toString().includes('const __default__')){
144+
resMgcStr = resMgcStr.prependRight(
145+
resMgcStr.length(),
146+
`${useCssVars}\n
147+
const __setup__ = __default__.setup
148+
__default__.setup = __setup__
149+
? (props, ctx) => { __injectCSSVars__(); return __setup__(props, ctx) }
150+
: __injectCSSVars__
151+
const _sfc_main = __default__
152+
export default _sfc_main`)
153+
}
154+
130155
resMgcStr = resMgcStr.prependLeft(0, importer)
131156
} else if (hasUseCssVars
132157
&& parserRes.useCSSVarsNode
@@ -211,7 +236,7 @@ export function createUseCssVarsCode(
211236
function genCSSVarsValue(
212237
node: SgNode,
213238
bindings?: BindingMetadata){
214-
let res = '()'
239+
let res = `_ctx.${node.text()}`
215240
if(bindings){
216241
const binding = bindings[node.text()]
217242
switch (binding){
@@ -236,14 +261,3 @@ function genCSSVarsValue(
236261
}
237262
return res
238263
}
239-
// TODO non-inline css - vite - dev 🚧
240-
// TODO non-inline bindingTypes - vite - build
241-
242-
// TODO inline css - vite - dev
243-
// TODO inline bindingTypes - vite - build
244-
245-
// TODO non-inline css - webpack - dev
246-
// TODO non-inline bindingTypes - webpack - build
247-
248-
// TODO inline css - webpack - dev
249-
// TODO inline bindingTypes - webpack - build

packages/core/parser/parser-script-bindings.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,6 @@ import type { SgNode } from '@ast-grep/napi'
44
import type { SFCDescriptor } from '@vue/compiler-sfc'
55
import type { BindingMetadata } from '@vue/compiler-dom'
66

7-
const mpc = `const appAsd = () => 'red' // √
8-
const appTheme5 = { color: 'red' } // √
9-
const appTheme6 = () => 'red' // √
10-
const fn1 = function(){ // √
11-
console.log('aa')
12-
}
13-
function f2(){ // √
14-
console.log(1)
15-
}
16-
17-
const fooColor = appAsd() // √
18-
const appTheme3 = ref('red') // √
19-
const appTheme4 = reactive({ color: 'red' }) // √
20-
21-
const a = 100// √
22-
const appTheme2 = 'blue'// √
23-
// const props = defineProps({color: {type: String}}) // √ !!
24-
interface Props {
25-
msg?: string
26-
labels?: string[]
27-
}
28-
29-
const propss = withDefaults(defineProps<Props>(), {
30-
msg: 'hello', // √
31-
labels: () => ['one', 'two'] // √
32-
})
33-
const { color } = propss // √`
34-
357
// TODO: unit test
368
export function analyzeScriptBindings(descriptor: SFCDescriptor): BindingMetadata {
379
const scriptSetupContent = descriptor.scriptSetup?.content || ''

packages/core/parser/parser-vbind-m.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,29 @@ const vBindRE = /v-bind-m\s*\(/g
6060
export function parseCssVars(
6161
styles: string[],
6262
hook?: {
63-
getIndex(start: number, end: number): void
63+
getIndex(
64+
start: number,
65+
end: number,
66+
offset: number,
67+
variable: string
68+
): void
6469
},
6570
): string[] {
6671
const vars: string[] = []
6772
styles.forEach((style) => {
6873
let match: RegExpExecArray | null = null
69-
// ignore v-bind() in comments /* ... */
74+
// ignore v-bind-m() in comments /* ... */
7075
const content = style.replace(/\/\*([\s\S]*?)\*\//g, '')
71-
76+
const offset = style.length - content.length
7277
while ((match = vBindRE.exec(content))) {
7378
const start = match.index + match[0].length
7479
const end = lexBinding(content, start)
7580
if (end !== null) {
76-
hook && hook.getIndex(start, end)
7781
const variable = normalizeExpression(content.slice(start, end))
78-
if (!vars.includes(variable))
82+
hook && hook.getIndex(start, end, offset, variable)
83+
if (!vars.includes(variable)){
7984
vars.push(variable)
85+
}
8086
}
8187
}
8288
})

0 commit comments

Comments
 (0)