diff --git a/packages/compiler-sfc/src/compileStyle.ts b/packages/compiler-sfc/src/compileStyle.ts index b0aa484cd23..cf0761ffc09 100644 --- a/packages/compiler-sfc/src/compileStyle.ts +++ b/packages/compiler-sfc/src/compileStyle.ts @@ -7,6 +7,7 @@ import postcss, { } from 'postcss' import trimPlugin from './stylePluginTrim' import scopedPlugin from './stylePluginScoped' +import scopedVarsPlugin from './stylePluginScopedVars' import { processors, StylePreprocessor, @@ -95,11 +96,16 @@ export function doCompileStyle( const source = preProcessedSource ? preProcessedSource.code : options.source const plugins = (postcssPlugins || []).slice() + if (vars && scoped) { + // vars + scoped, only applies to raw source before other transforms + // #1623 + plugins.unshift(scopedVarsPlugin(id)) + } if (trim) { plugins.push(trimPlugin()) } if (scoped) { - plugins.push(scopedPlugin({ id, vars })) + plugins.push(scopedPlugin(id)) } let cssModules: Record | undefined if (modules) { diff --git a/packages/compiler-sfc/src/stylePluginScoped.ts b/packages/compiler-sfc/src/stylePluginScoped.ts index ad90bf5c1bc..6274da818a6 100644 --- a/packages/compiler-sfc/src/stylePluginScoped.ts +++ b/packages/compiler-sfc/src/stylePluginScoped.ts @@ -3,10 +3,8 @@ import selectorParser, { Node, Selector } from 'postcss-selector-parser' const animationNameRE = /^(-\w+-)?animation-name$/ const animationRE = /^(-\w+-)?animation$/ -const cssVarRE = /\bvar\(--(global:)?([^)]+)\)/g -export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => { - const { id, vars: hasInjectedVars } = options as { id: string; vars: boolean } +export default postcss.plugin('vue-scoped', (id: any) => (root: Root) => { const keyframes = Object.create(null) const shortId = id.replace(/^data-v-/, '') @@ -135,46 +133,37 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => { }).processSync(node.selector) }) - const hasKeyframes = Object.keys(keyframes).length - if (hasKeyframes || hasInjectedVars) + if (Object.keys(keyframes).length) { + // If keyframes are found in this