From 2895e1ad2f5dd85b7ca3abaa2bf5f7f0660fad71 Mon Sep 17 00:00:00 2001 From: daiwei Date: Thu, 26 Dec 2024 21:44:59 +0800 Subject: [PATCH] refactor(cssVars): use css vars work with vapor mode --- packages/compiler-sfc/src/compileScript.ts | 5 +- packages/compiler-sfc/src/style/cssVars.ts | 9 +++- .../runtime-dom/src/helpers/useCssVars.ts | 35 ++++++++----- packages/runtime-dom/src/index.ts | 4 ++ packages/runtime-vapor/src/index.ts | 1 + packages/runtime-vapor/src/useCssVars.ts | 51 +++++++++++++++++++ 6 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 packages/runtime-vapor/src/useCssVars.ts diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 0d1dc0976f4..798e7465071 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -29,7 +29,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, @@ -759,7 +759,7 @@ export function compileScript( // no need to do this when targeting SSR !options.templateOptions?.ssr ) { - ctx.helperImports.add(CSS_VARS_HELPER) + ctx.helperImports.add(getCssVarsHelper(vapor)) ctx.helperImports.add('unref') ctx.s.prependLeft( startOffset, @@ -768,6 +768,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 0397c7d790a..95a7d53c8ae 100644 --- a/packages/compiler-sfc/src/style/cssVars.ts +++ b/packages/compiler-sfc/src/style/cssVars.ts @@ -10,10 +10,14 @@ import { import type { SFCDescriptor } from '../parse' import type { PluginCreator } from 'postcss' import hash from 'hash-sum' -import { getEscapedCssVarName } from '@vue/shared' +import { capitalize, getEscapedCssVarName } from '@vue/shared' export const CSS_VARS_HELPER = `useCssVars` +export function getCssVarsHelper(vapor: boolean | undefined): string { + return vapor ? `vapor${capitalize(CSS_VARS_HELPER)}` : CSS_VARS_HELPER +} + export function genCssVarsFromList( vars: string[], id: string, @@ -162,6 +166,7 @@ export function genCssVarsCode( bindings: BindingMetadata, id: string, isProd: boolean, + vapor?: boolean, ) { const varsExp = genCssVarsFromList(vars, id, isProd) const exp = createSimpleExpression(varsExp, false) @@ -182,7 +187,7 @@ export function genCssVarsCode( }) .join('') - return `_${CSS_VARS_HELPER}(_ctx => (${transformedString}))` + return `_${getCssVarsHelper(vapor)}(_ctx => (${transformedString}))` } //