Skip to content

Commit 2901050

Browse files
authored
fix(compiler-sfc): duplicated injected css var with repeated vars in style (#2802)
1 parent b31712e commit 2901050

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap

+19
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ return { color }
6666
}"
6767
`;
6868
69+
exports[`CSS vars injection codegen w/ <script setup> using the same var multiple times 1`] = `
70+
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
71+
72+
export default {
73+
expose: [],
74+
setup(__props) {
75+
76+
_useCssVars(_ctx => ({
77+
\\"xxxxxxxx-color\\": (color)
78+
}))
79+
80+
const color = 'red'
81+
82+
return { color }
83+
}
84+
85+
}"
86+
`;
87+
6988
exports[`CSS vars injection generating correct code for nested paths 1`] = `
7089
"const a = 1
7190
const __default__ = {}

packages/compiler-sfc/__tests__/cssVars.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,26 @@ describe('CSS vars injection', () => {
160160
).content
161161
)
162162
})
163+
164+
test('w/ <script setup> using the same var multiple times', () => {
165+
const { content } = compileSFCScript(
166+
`<script setup>
167+
const color = 'red'
168+
</script>\n` +
169+
`<style>
170+
div {
171+
color: v-bind(color);
172+
}
173+
p {
174+
color: v-bind(color);
175+
}
176+
</style>`
177+
)
178+
// color should only be injected once, even if it is twice in style
179+
expect(content).toMatch(`_useCssVars(_ctx => ({
180+
"${mockId}-color": (color)
181+
})`)
182+
assertCode(content)
183+
})
163184
})
164185
})

packages/compiler-sfc/src/cssVars.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
3737
sfc.styles.forEach(style => {
3838
let match
3939
while ((match = cssVarRE.exec(style.content))) {
40-
vars.push(match[1] || match[2] || match[3])
40+
const variable = match[1] || match[2] || match[3]
41+
if (!vars.includes(variable)) {
42+
vars.push(variable)
43+
}
4144
}
4245
})
4346
return vars

0 commit comments

Comments
 (0)