Skip to content

Commit 5f27151

Browse files
committed
refactor: shorten scoped css var / animation prefix
1 parent 73807ae commit 5f27151

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ describe('SFC scoped CSS', () => {
155155
})
156156

157157
test('scoped keyframes', () => {
158-
const style = compileScoped(`
158+
const style = compileScoped(
159+
`
159160
.anim {
160161
animation: color 5s infinite, other 5s;
161162
}
@@ -190,23 +191,27 @@ describe('SFC scoped CSS', () => {
190191
from { opacity: 0; }
191192
to { opacity: 1; }
192193
}
193-
`)
194+
`,
195+
{ id: 'data-v-test' }
196+
)
194197

195198
expect(style).toContain(
196-
`.anim[test] {\n animation: color-test 5s infinite, other 5s;`
199+
`.anim[data-v-test] {\n animation: color-test 5s infinite, other 5s;`
200+
)
201+
expect(style).toContain(
202+
`.anim-2[data-v-test] {\n animation-name: color-test`
197203
)
198-
expect(style).toContain(`.anim-2[test] {\n animation-name: color-test`)
199204
expect(style).toContain(
200-
`.anim-3[test] {\n animation: 5s color-test infinite, 5s other;`
205+
`.anim-3[data-v-test] {\n animation: 5s color-test infinite, 5s other;`
201206
)
202207
expect(style).toContain(`@keyframes color-test {`)
203208
expect(style).toContain(`@-webkit-keyframes color-test {`)
204209

205210
expect(style).toContain(
206-
`.anim-multiple[test] {\n animation: color-test 5s infinite,opacity-test 2s;`
211+
`.anim-multiple[data-v-test] {\n animation: color-test 5s infinite,opacity-test 2s;`
207212
)
208213
expect(style).toContain(
209-
`.anim-multiple-2[test] {\n animation-name: color-test,opacity-test;`
214+
`.anim-multiple-2[data-v-test] {\n animation-name: color-test,opacity-test;`
210215
)
211216
expect(style).toContain(`@keyframes opacity-test {`)
212217
expect(style).toContain(`@-webkit-keyframes opacity-test {`)
@@ -268,11 +273,12 @@ describe('SFC scoped CSS', () => {
268273
font-size: var(--global:font);
269274
}`,
270275
{
276+
id: 'data-v-test',
271277
vars: true
272278
}
273279
)
274280
expect(code).toMatchInlineSnapshot(`
275-
".foo[test] {
281+
".foo[data-v-test] {
276282
color: var(--test-color);
277283
font-size: var(--font);
278284
}"

packages/compiler-sfc/src/stylePluginScoped.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const cssVarRE = /\bvar\(--(global:)?([^)]+)\)/g
88
export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
99
const { id, vars: hasInjectedVars } = options as { id: string; vars: boolean }
1010
const keyframes = Object.create(null)
11+
const shortId = id.replace(/^data-v-/, '')
1112

1213
root.each(function rewriteSelectors(node) {
1314
if (node.type !== 'rule') {
@@ -17,7 +18,7 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
1718
node.each(rewriteSelectors)
1819
} else if (/-?keyframes$/.test(node.name)) {
1920
// register keyframes
20-
keyframes[node.params] = node.params = node.params + '-' + id
21+
keyframes[node.params] = node.params = node.params + '-' + shortId
2122
}
2223
}
2324
return
@@ -170,7 +171,7 @@ export default postcss.plugin('vue-scoped', (options: any) => (root: Root) => {
170171
// rewrite CSS variables
171172
if (hasInjectedVars && cssVarRE.test(decl.value)) {
172173
decl.value = decl.value.replace(cssVarRE, (_, $1, $2) => {
173-
return $1 ? `var(--${$2})` : `var(--${id}-${$2})`
174+
return $1 ? `var(--${$2})` : `var(--${shortId}-${$2})`
174175
})
175176
}
176177
})

packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ describe('useCssVars', () => {
1515
const state = reactive({ color: 'red' })
1616
const App = getApp(state)
1717
const root = document.createElement('div')
18-
const prefix = scopeId ? `${scopeId}-` : ``
18+
const prefix = scopeId ? `${scopeId.replace(/^data-v-/, '')}-` : ``
1919

2020
render(h(App), root)
2121
for (const c of [].slice.call(root.children as any)) {
22-
expect((c as HTMLElement).style.getPropertyValue(`--${prefix}color`))
22+
expect(
23+
(c as HTMLElement).style.getPropertyValue(`--${prefix}color`)
24+
).toBe(`red`)
2325
}
2426

2527
state.color = 'green'
2628
await nextTick()
2729
for (const c of [].slice.call(root.children as any)) {
28-
expect((c as HTMLElement).style.getPropertyValue(`--${prefix}color`))
30+
expect(
31+
(c as HTMLElement).style.getPropertyValue(`--${prefix}color`)
32+
).toBe('green')
2933
}
3034
}
3135

@@ -65,7 +69,7 @@ describe('useCssVars', () => {
6569
})
6670

6771
test('with <style scoped>', async () => {
68-
const id = 'v-12345'
72+
const id = 'data-v-12345'
6973

7074
await assertCssVars(
7175
state => ({

packages/runtime-dom/src/helpers/useCssVars.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
watchEffect,
66
warn,
77
VNode,
8-
Fragment,
9-
ComponentInternalInstance
8+
Fragment
109
} from '@vue/runtime-core'
1110
import { ShapeFlags } from '@vue/shared/src'
1211

@@ -20,38 +19,34 @@ export function useCSSVars(
2019
warn(`useCssVars is called without current active component instance.`)
2120
return
2221
}
22+
23+
const prefix =
24+
scoped && instance.type.__scopeId
25+
? `${instance.type.__scopeId.replace(/^data-v-/, '')}-`
26+
: ``
27+
2328
onMounted(() => {
2429
watchEffect(() => {
25-
setVarsOnVNode(
26-
instance.subTree,
27-
getter(instance.proxy!),
28-
instance,
29-
scoped
30-
)
30+
setVarsOnVNode(instance.subTree, getter(instance.proxy!), prefix)
3131
})
3232
})
3333
}
3434

3535
function setVarsOnVNode(
3636
vnode: VNode,
3737
vars: Record<string, string>,
38-
owner: ComponentInternalInstance,
39-
scoped: boolean
38+
prefix: string
4039
) {
4140
// drill down HOCs until it's a non-component vnode
4241
while (vnode.component) {
4342
vnode = vnode.component.subTree
4443
}
4544
if (vnode.shapeFlag & ShapeFlags.ELEMENT && vnode.el) {
4645
const style = vnode.el.style
47-
const prefix =
48-
scoped && owner.type.__scopeId ? `${owner.type.__scopeId}-` : ``
4946
for (const key in vars) {
5047
style.setProperty(`--${prefix}${key}`, vars[key])
5148
}
5249
} else if (vnode.type === Fragment) {
53-
;(vnode.children as VNode[]).forEach(c =>
54-
setVarsOnVNode(c, vars, owner, scoped)
55-
)
50+
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars, prefix))
5651
}
5752
}

0 commit comments

Comments
 (0)