Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cssVars): should ignore comments when parseCssVars #4188

Merged
merged 3 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ __default__.setup = __setup__
export default __default__"
`;

exports[`CSS vars injection codegen should ignore comments 1`] = `
"export default {
setup(__props, { expose }) {
expose()
const color = 'red'
return { color }
}

}"
`;

exports[`CSS vars injection codegen w/ <script setup> 1`] = `
"import { useCssVars as _useCssVars, unref as _unref } from 'vue'

Expand Down
13 changes: 13 additions & 0 deletions packages/compiler-sfc/__tests__/cssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ describe('CSS vars injection', () => {
)
})

//#4185
test('should ignore comments', () => {
const { content } = compileSFCScript(
`<script setup>const color = 'red'</script>\n` +
`<style>
div{ /* color: v-bind(color); */ width:20; }
</style>`
)

expect(content).not.toMatch(`_useCssVars`)
assertCode(content)
})

test('w/ <script setup> using the same var multiple times', () => {
const { content } = compileSFCScript(
`<script setup>
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-sfc/src/cssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export function parseCssVars(sfc: SFCDescriptor): string[] {
const vars: string[] = []
sfc.styles.forEach(style => {
let match
while ((match = cssVarRE.exec(style.content))) {
// ignore v-bind() in comments /* ... */
const content = style.content.replace(/\/\*[\s\S]*\*\//g, '')
while ((match = cssVarRE.exec(content))) {
const variable = match[1] || match[2] || match[3]
if (!vars.includes(variable)) {
vars.push(variable)
Expand Down