From bb343383f2b2b458f04529a9f61d020668b008fc Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 18 Nov 2020 11:07:10 -0500 Subject: [PATCH] refactor(compiler-sfc): parse css vars directly as part of sfc descriptor --- packages/compiler-sfc/src/compileScript.ts | 9 ++------- packages/compiler-sfc/src/cssVars.ts | 2 +- packages/compiler-sfc/src/parse.ts | 8 +++++++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index c9f0932c..aa44b99c 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -24,12 +24,7 @@ import { } from '@babel/types' import { walk } from 'estree-walker' import { RawSourceMap } from 'source-map' -import { - CSS_VARS_HELPER, - parseCssVars, - genCssVarsCode, - injectCssVarsCalls -} from './cssVars' +import { CSS_VARS_HELPER, genCssVarsCode, injectCssVarsCalls } from './cssVars' import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate' import { warnOnce } from './warn' @@ -99,7 +94,7 @@ export function compileScript( } const scopeId = options.id ? options.id.replace(/^data-v-/, '') : '' - const cssVars = parseCssVars(sfc) + const cssVars = sfc.cssVars const scriptLang = script && script.lang const scriptSetupLang = scriptSetup && scriptSetup.lang const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts' diff --git a/packages/compiler-sfc/src/cssVars.ts b/packages/compiler-sfc/src/cssVars.ts index 7cfcce1f..2b6036fc 100644 --- a/packages/compiler-sfc/src/cssVars.ts +++ b/packages/compiler-sfc/src/cssVars.ts @@ -26,7 +26,7 @@ export function generateCssVars( id: string, isProd: boolean ): string { - return genCssVarsFromList(parseCssVars(sfc), id, isProd) + return genCssVarsFromList(sfc.cssVars, id, isProd) } function genCssVarsFromList( diff --git a/packages/compiler-sfc/src/parse.ts b/packages/compiler-sfc/src/parse.ts index 7c44d5e2..4ec10b5e 100644 --- a/packages/compiler-sfc/src/parse.ts +++ b/packages/compiler-sfc/src/parse.ts @@ -10,6 +10,7 @@ import * as CompilerDOM from '@vue/compiler-dom' import { RawSourceMap, SourceMapGenerator } from 'source-map' import { TemplateCompiler } from './compileTemplate' import { Statement } from '@babel/types' +import { parseCssVars } from './cssVars' export interface SFCParseOptions { filename?: string @@ -56,6 +57,7 @@ export interface SFCDescriptor { scriptSetup: SFCScriptBlock | null styles: SFCStyleBlock[] customBlocks: SFCBlock[] + cssVars: string[] } export interface SFCParseResult { @@ -96,7 +98,8 @@ export function parse( script: null, scriptSetup: null, styles: [], - customBlocks: [] + customBlocks: [], + cssVars: [] } const errors: (CompilerError | SyntaxError)[] = [] @@ -209,6 +212,9 @@ export function parse( descriptor.customBlocks.forEach(genMap) } + // parse CSS vars + descriptor.cssVars = parseCssVars(descriptor) + const result = { descriptor, errors