Skip to content

Commit 3a75d5d

Browse files
authored
fix(sfc/style-vars): should ignore style variable bindings in comments (#4188)
fix #4185
1 parent 1c7f5d3 commit 3a75d5d

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ __default__.setup = __setup__
4949
export default __default__"
5050
`;
5151
52+
exports[`CSS vars injection codegen should ignore comments 1`] = `
53+
"export default {
54+
setup(__props, { expose }) {
55+
expose()
56+
const color = 'red'
57+
return { color }
58+
}
59+
60+
}"
61+
`;
62+
5263
exports[`CSS vars injection codegen w/ <script setup> 1`] = `
5364
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
5465

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

+13
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ describe('CSS vars injection', () => {
161161
)
162162
})
163163

164+
//#4185
165+
test('should ignore comments', () => {
166+
const { content } = compileSFCScript(
167+
`<script setup>const color = 'red'</script>\n` +
168+
`<style>
169+
div{ /* color: v-bind(color); */ width:20; }
170+
</style>`
171+
)
172+
173+
expect(content).not.toMatch(`_useCssVars`)
174+
assertCode(content)
175+
})
176+
164177
test('w/ <script setup> using the same var multiple times', () => {
165178
const { content } = compileSFCScript(
166179
`<script setup>

packages/compiler-sfc/src/cssVars.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
3737
const vars: string[] = []
3838
sfc.styles.forEach(style => {
3939
let match
40-
while ((match = cssVarRE.exec(style.content))) {
40+
// ignore v-bind() in comments /* ... */
41+
const content = style.content.replace(/\/\*[\s\S]*\*\//g, '')
42+
while ((match = cssVarRE.exec(content))) {
4143
const variable = match[1] || match[2] || match[3]
4244
if (!vars.includes(variable)) {
4345
vars.push(variable)

0 commit comments

Comments
 (0)