Skip to content

Commit

Permalink
fix(compiler-sfc): duplicated injected css var with repeated vars in …
Browse files Browse the repository at this point in the history
…style (#2802)
  • Loading branch information
patak-dev authored Jul 15, 2021
1 parent b31712e commit 2901050
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ return { color }
}"
`;
exports[`CSS vars injection codegen w/ <script setup> using the same var multiple times 1`] = `
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
export default {
expose: [],
setup(__props) {
_useCssVars(_ctx => ({
\\"xxxxxxxx-color\\": (color)
}))
const color = 'red'
return { color }
}
}"
`;
exports[`CSS vars injection generating correct code for nested paths 1`] = `
"const a = 1
const __default__ = {}
Expand Down
21 changes: 21 additions & 0 deletions packages/compiler-sfc/__tests__/cssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,26 @@ describe('CSS vars injection', () => {
).content
)
})

test('w/ <script setup> using the same var multiple times', () => {
const { content } = compileSFCScript(
`<script setup>
const color = 'red'
</script>\n` +
`<style>
div {
color: v-bind(color);
}
p {
color: v-bind(color);
}
</style>`
)
// color should only be injected once, even if it is twice in style
expect(content).toMatch(`_useCssVars(_ctx => ({
"${mockId}-color": (color)
})`)
assertCode(content)
})
})
})
5 changes: 4 additions & 1 deletion packages/compiler-sfc/src/cssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
sfc.styles.forEach(style => {
let match
while ((match = cssVarRE.exec(style.content))) {
vars.push(match[1] || match[2] || match[3])
const variable = match[1] || match[2] || match[3]
if (!vars.includes(variable)) {
vars.push(variable)
}
}
})
return vars
Expand Down

0 comments on commit 2901050

Please sign in to comment.