Skip to content

Commit d7071bb

Browse files
committed
fix: do not skip style post loader for v-bind() in CSS
These styles may or may not be scoped. Fixes #2061
1 parent d67c85c commit d7071bb

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

src/stylePostLoader.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ const { compileStyle } = compiler
1010
const StylePostLoader: LoaderDefinitionFunction = function (source, inMap) {
1111
const query = qs.parse(this.resourceQuery.slice(1))
1212

13-
// skip normal CSS files without scoped flag
14-
if (
15-
!('vue' in query) ||
16-
query.type !== 'style' ||
17-
!query.id ||
18-
!query.scoped
19-
) {
13+
// skip normal CSS files
14+
if (!('vue' in query) || query.type !== 'style' || !query.id) {
2015
this.callback(null, source, inMap)
2116
return
2217
}

test/fixtures/style-v-bind.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<div class="text">hello</div>
3+
</template>
4+
5+
<script>
6+
export default {
7+
data() {
8+
return {
9+
color: 'red',
10+
font: {
11+
size: '2em',
12+
},
13+
}
14+
},
15+
}
16+
</script>
17+
18+
<style>
19+
.text {
20+
color: v-bind(color);
21+
22+
/* expressions (wrap in quotes) */
23+
font-size: v-bind('font.size');
24+
}
25+
</style>

test/style.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,22 @@ test('CSS Modules Extend', async () => {
198198
expect(style).toContain(`.${instance.$style.red} {\n color: #FF0000;\n}`)
199199
})
200200

201-
test.todo('experimental <style vars>')
201+
test('v-bind() in CSS', async () => {
202+
const { window, instance } = await mockBundleAndRun({
203+
entry: 'style-v-bind.vue',
204+
})
205+
206+
const shortId = genId('style-v-bind.vue')
207+
const style = normalizeNewline(
208+
window.document.querySelector('style')!.textContent!
209+
)
210+
211+
expect(style).toMatch(`color: var(--${shortId}-color);`)
212+
expect(style).toMatch(`font-size: var(--${shortId}-font\\.size);`)
213+
214+
const computedStyle = window.getComputedStyle(instance.$el)
215+
// Because the tests run in JSDOM, we can't directly get the computed `color` value.
216+
// To get around this, we test the corresponding CSS variable instead.
217+
expect(computedStyle.getPropertyValue(`--${shortId}-color`)).toBe('red')
218+
expect(computedStyle.getPropertyValue(`--${shortId}-font.size`)).toBe('2em')
219+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy