Skip to content

Commit

Permalink
fix: do not skip style post loader for v-bind() in CSS
Browse files Browse the repository at this point in the history
These styles may or may not be scoped.

Fixes #2061
  • Loading branch information
haoqunjiang committed Oct 31, 2023
1 parent d67c85c commit d7071bb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/stylePostLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ const { compileStyle } = compiler
const StylePostLoader: LoaderDefinitionFunction = function (source, inMap) {
const query = qs.parse(this.resourceQuery.slice(1))

// skip normal CSS files without scoped flag
if (
!('vue' in query) ||
query.type !== 'style' ||
!query.id ||
!query.scoped
) {
// skip normal CSS files
if (!('vue' in query) || query.type !== 'style' || !query.id) {
this.callback(null, source, inMap)
return
}
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/style-v-bind.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div class="text">hello</div>
</template>

<script>
export default {
data() {
return {
color: 'red',
font: {
size: '2em',
},
}
},
}
</script>

<style>
.text {
color: v-bind(color);
/* expressions (wrap in quotes) */
font-size: v-bind('font.size');
}
</style>
20 changes: 19 additions & 1 deletion test/style.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,22 @@ test('CSS Modules Extend', async () => {
expect(style).toContain(`.${instance.$style.red} {\n color: #FF0000;\n}`)
})

test.todo('experimental <style vars>')
test('v-bind() in CSS', async () => {
const { window, instance } = await mockBundleAndRun({
entry: 'style-v-bind.vue',
})

const shortId = genId('style-v-bind.vue')
const style = normalizeNewline(
window.document.querySelector('style')!.textContent!
)

expect(style).toMatch(`color: var(--${shortId}-color);`)
expect(style).toMatch(`font-size: var(--${shortId}-font\\.size);`)

const computedStyle = window.getComputedStyle(instance.$el)
// Because the tests run in JSDOM, we can't directly get the computed `color` value.
// To get around this, we test the corresponding CSS variable instead.
expect(computedStyle.getPropertyValue(`--${shortId}-color`)).toBe('red')
expect(computedStyle.getPropertyValue(`--${shortId}-font.size`)).toBe('2em')
})

0 comments on commit d7071bb

Please sign in to comment.