Skip to content

Commit 771635b

Browse files
authored
fix(sfc/style-vars): improve ignore style variable bindings in comments (#4202)
1 parent 204e194 commit 771635b

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ export default __default__"
5050
`;
5151
5252
exports[`CSS vars injection codegen should ignore comments 1`] = `
53-
"export default {
53+
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'
54+
55+
export default {
5456
setup(__props, { expose }) {
5557
expose()
56-
const color = 'red'
57-
return { color }
58+
59+
_useCssVars(_ctx => ({
60+
\\"xxxxxxxx-width\\": (width)
61+
}))
62+
const color = 'red';const width = 100
63+
return { color, width }
5864
}
5965
6066
}"

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,17 @@ describe('CSS vars injection', () => {
164164
//#4185
165165
test('should ignore comments', () => {
166166
const { content } = compileSFCScript(
167-
`<script setup>const color = 'red'</script>\n` +
167+
`<script setup>const color = 'red';const width = 100</script>\n` +
168168
`<style>
169+
/* comment **/
169170
div{ /* color: v-bind(color); */ width:20; }
171+
div{ width: v-bind(width); }
172+
/* comment */
170173
</style>`
171174
)
172175

173-
expect(content).not.toMatch(`_useCssVars`)
176+
expect(content).not.toMatch(`"${mockId}-color": (color)`)
177+
expect(content).toMatch(`"${mockId}-width": (width)`)
174178
assertCode(content)
175179
})
176180

packages/compiler-sfc/src/cssVars.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import { PluginCreator } from 'postcss'
1212
import hash from 'hash-sum'
1313

1414
export const CSS_VARS_HELPER = `useCssVars`
15-
export const cssVarRE =
16-
/\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g
15+
export const cssVarRE = /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g
1716

1817
export function genCssVarsFromList(
1918
vars: string[],
@@ -38,7 +37,7 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
3837
sfc.styles.forEach(style => {
3938
let match
4039
// ignore v-bind() in comments /* ... */
41-
const content = style.content.replace(/\/\*[\s\S]*\*\//g, '')
40+
const content = style.content.replace(/\/\*([\s\S]*?)\*\//g, '')
4241
while ((match = cssVarRE.exec(content))) {
4342
const variable = match[1] || match[2] || match[3]
4443
if (!vars.includes(variable)) {

0 commit comments

Comments
 (0)