Skip to content

Commit 4951d43

Browse files
committed
fix(compiler-sfc): <style vars scoped> prefixing should only apply to pre-transform source
fix #1623
1 parent a28a11e commit 4951d43

File tree

3 files changed

+50
-40
lines changed

3 files changed

+50
-40
lines changed

packages/compiler-sfc/src/compileStyle.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import postcss, {
77
} from 'postcss'
88
import trimPlugin from './stylePluginTrim'
99
import scopedPlugin from './stylePluginScoped'
10+
import scopedVarsPlugin from './stylePluginScopedVars'
1011
import {
1112
processors,
1213
StylePreprocessor,
@@ -95,11 +96,16 @@ export function doCompileStyle(
9596
const source = preProcessedSource ? preProcessedSource.code : options.source
9697

9798
const plugins = (postcssPlugins || []).slice()
99+
if (vars && scoped) {
100+
// vars + scoped, only applies to raw source before other transforms
101+
// #1623
102+
plugins.unshift(scopedVarsPlugin(id))
103+
}
98104
if (trim) {
99105
plugins.push(trimPlugin())
100106
}
101107
if (scoped) {
102-
plugins.push(scopedPlugin({ id, vars }))
108+
plugins.push(scopedPlugin(id))
103109
}
104110
let cssModules: Record<string, string> | undefined
105111
if (modules) {

packages/compiler-sfc/src/stylePluginScoped.ts

+28-39
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import selectorParser, { Node, Selector } from 'postcss-selector-parser'
33

44
const animationNameRE = /^(-\w+-)?animation-name$/
55
const animationRE = /^(-\w+-)?animation$/
6-
const cssVarRE = /\bvar\(--(global:)?([^)]+)\)/g
76

8-
export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
9-
const { id, vars: hasInjectedVars } = options as { id: string; vars: boolean }
7+
export default postcss.plugin('vue-scoped', (id: any) => (root: Root) => {
108
const keyframes = Object.create(null)
119
const shortId = id.replace(/^data-v-/, '')
1210

@@ -135,46 +133,37 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
135133
}).processSync(node.selector)
136134
})
137135

138-
const hasKeyframes = Object.keys(keyframes).length
139-
if (hasKeyframes || hasInjectedVars)
136+
if (Object.keys(keyframes).length) {
137+
// If keyframes are found in this <style>, find and rewrite animation names
138+
// in declarations.
139+
// Caveat: this only works for keyframes and animation rules in the same
140+
// <style> element.
141+
// individual animation-name declaration
140142
root.walkDecls(decl => {
141-
// If keyframes are found in this <style>, find and rewrite animation names
142-
// in declarations.
143-
// Caveat: this only works for keyframes and animation rules in the same
144-
// <style> element.
145-
if (hasKeyframes) {
146-
// individual animation-name declaration
147-
if (animationNameRE.test(decl.prop)) {
148-
decl.value = decl.value
149-
.split(',')
150-
.map(v => keyframes[v.trim()] || v.trim())
151-
.join(',')
152-
}
153-
// shorthand
154-
if (animationRE.test(decl.prop)) {
155-
decl.value = decl.value
156-
.split(',')
157-
.map(v => {
158-
const vals = v.trim().split(/\s+/)
159-
const i = vals.findIndex(val => keyframes[val])
160-
if (i !== -1) {
161-
vals.splice(i, 1, keyframes[vals[i]])
162-
return vals.join(' ')
163-
} else {
164-
return v
165-
}
166-
})
167-
.join(',')
168-
}
143+
if (animationNameRE.test(decl.prop)) {
144+
decl.value = decl.value
145+
.split(',')
146+
.map(v => keyframes[v.trim()] || v.trim())
147+
.join(',')
169148
}
170-
171-
// rewrite CSS variables
172-
if (hasInjectedVars && cssVarRE.test(decl.value)) {
173-
decl.value = decl.value.replace(cssVarRE, (_, $1, $2) => {
174-
return $1 ? `var(--${$2})` : `var(--${shortId}-${$2})`
175-
})
149+
// shorthand
150+
if (animationRE.test(decl.prop)) {
151+
decl.value = decl.value
152+
.split(',')
153+
.map(v => {
154+
const vals = v.trim().split(/\s+/)
155+
const i = vals.findIndex(val => keyframes[val])
156+
if (i !== -1) {
157+
vals.splice(i, 1, keyframes[vals[i]])
158+
return vals.join(' ')
159+
} else {
160+
return v
161+
}
162+
})
163+
.join(',')
176164
}
177165
})
166+
}
178167
})
179168

180169
function isSpaceCombinator(node: Node) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import postcss, { Root } from 'postcss'
2+
3+
const cssVarRE = /\bvar\(--(global:)?([^)]+)\)/g
4+
5+
export default postcss.plugin('vue-scoped', (id: any) => (root: Root) => {
6+
const shortId = id.replace(/^data-v-/, '')
7+
root.walkDecls(decl => {
8+
// rewrite CSS variables
9+
if (cssVarRE.test(decl.value)) {
10+
decl.value = decl.value.replace(cssVarRE, (_, $1, $2) => {
11+
return $1 ? `var(--${$2})` : `var(--${shortId}-${$2})`
12+
})
13+
}
14+
})
15+
})

0 commit comments

Comments
 (0)