From b9dca57c205be99d08690ab24d3c5420ec166f85 Mon Sep 17 00:00:00 2001 From: edison Date: Thu, 20 Nov 2025 21:07:32 +0800 Subject: [PATCH] feat(runtime-vapor): support `v-bind()` in CSS (#12621) --- packages/compiler-sfc/src/compileScript.ts | 5 +- packages/compiler-sfc/src/style/cssVars.ts | 7 +- packages/runtime-core/src/component.ts | 23 +- .../runtime-dom/src/helpers/useCssVars.ts | 102 +++-- packages/runtime-dom/src/index.ts | 4 + .../__tests__/helpers/useCssVars.spec.ts | 414 ++++++++++++++++++ .../runtime-vapor/__tests__/hydration.spec.ts | 138 +++--- packages/runtime-vapor/src/apiCreateFor.ts | 2 + .../runtime-vapor/src/components/Teleport.ts | 46 +- packages/runtime-vapor/src/dom/prop.ts | 100 ++++- packages/runtime-vapor/src/fragment.ts | 8 +- .../runtime-vapor/src/helpers/useCssVars.ts | 55 +++ packages/runtime-vapor/src/index.ts | 1 + packages/runtime-vapor/src/vdomInterop.ts | 3 + 14 files changed, 770 insertions(+), 138 deletions(-) create mode 100644 packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts create mode 100644 packages/runtime-vapor/src/helpers/useCssVars.ts diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 022dd2ba02..835daa2fc6 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -34,7 +34,7 @@ import { normalScriptDefaultVar, processNormalScript, } from './script/normalScript' -import { CSS_VARS_HELPER, genCssVarsCode } from './style/cssVars' +import { genCssVarsCode, getCssVarsHelper } from './style/cssVars' import { type SFCTemplateCompileOptions, compileTemplate, @@ -825,7 +825,7 @@ export function compileScript( // no need to do this when targeting SSR !ssr ) { - ctx.helperImports.add(CSS_VARS_HELPER) + ctx.helperImports.add(getCssVarsHelper(vapor)) ctx.helperImports.add('unref') ctx.s.prependLeft( startOffset, @@ -834,6 +834,7 @@ export function compileScript( ctx.bindingMetadata, scopeId, !!options.isProd, + vapor, )}\n`, ) } diff --git a/packages/compiler-sfc/src/style/cssVars.ts b/packages/compiler-sfc/src/style/cssVars.ts index 313380c3b4..b626663b51 100644 --- a/packages/compiler-sfc/src/style/cssVars.ts +++ b/packages/compiler-sfc/src/style/cssVars.ts @@ -14,6 +14,10 @@ import { getEscapedCssVarName } from '@vue/shared' export const CSS_VARS_HELPER = `useCssVars` +export function getCssVarsHelper(vapor: boolean | undefined): string { + return vapor ? `useVaporCssVars` : CSS_VARS_HELPER +} + export function genCssVarsFromList( vars: string[], id: string, @@ -168,6 +172,7 @@ export function genCssVarsCode( bindings: BindingMetadata, id: string, isProd: boolean, + vapor?: boolean, ) { const varsExp = genCssVarsFromList(vars, id, isProd) const exp = createSimpleExpression(varsExp, false) @@ -188,7 +193,7 @@ export function genCssVarsCode( }) .join('') - return `_${CSS_VARS_HELPER}(_ctx => (${transformedString}))` + return `_${getCssVarsHelper(vapor)}(_ctx => (${transformedString}))` } //