Skip to content

Commit 542aacc

Browse files
committed
test: ssr + scoped CSS
1 parent 5d0c4f7 commit 542aacc

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

Diff for: lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ module.exports = function (source) {
7575
const src = descriptor.template.src || resourcePath
7676
const langQuery = getLangQuery(descriptor.template)
7777
const idQuery = `&id=${id}`
78+
const scopedQuery = hasScoped ? `&scoped` : ``
7879
const fnQuery = hasFunctional ? `&functional` : ``
7980
const commentQuery = hasComment ? `&comment` : ``
80-
const query = `?vue&type=template${idQuery}${langQuery}${fnQuery}${commentQuery}`
81+
const query = `?vue&type=template${scopedQuery}${idQuery}${langQuery}${fnQuery}${commentQuery}`
8182
const request = stringifyRequest(src + query)
8283
templateImport = `import { render, staticRenderFns } from ${request}`
8384
}

Diff for: lib/styleInjection.js

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module.exports = function genStyleInjectionCode (
2020
function genStyleRequest (style, i) {
2121
const src = style.src || resourcePath
2222
const langQuery = getLangQuery(style, 'css')
23+
// make sure to only pass id when necessary so that we don't inject
24+
// duplicate tags when multiple components import the same css file
2325
const scopedQuery = style.scoped ? `&scoped&id=${id}` : ``
2426
const query = `?vue&type=style&index=${i}${langQuery}${scopedQuery}`
2527
return stringifyRequest(src + query)

Diff for: lib/template-loader/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ function actuallyCompile (sourceTemplate, options, loaderContext, query) {
8080
} = options.compilerOptions || {}
8181

8282
const compilerOptions = {
83+
scopeId: query.scoped != null ? `data-v-${id}` : null,
8384
preserveWhitespace,
8485
modules: defaultModules.concat(modules || []),
8586
directives: directives || {},
86-
scopeId: id,
8787
comments: hasComment
8888
}
8989

Diff for: test/fixtures/scoped-css.vue

+8
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ h1 {
5555
<svg><template><p></p></template></svg>
5656
</div>
5757
</template>
58+
59+
<script>
60+
export default {
61+
data () {
62+
return { ok: true }
63+
}
64+
}
65+
</script>

Diff for: test/fixtures/ssr-scoped-style.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Vue from 'vue'
2+
import App from './scoped-css.vue'
3+
4+
export default () => new Vue({
5+
render: h => h(App)
6+
})

Diff for: test/ssr.spec.js

+53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const SSR = require('vue-server-renderer')
22

33
const {
4+
genId,
45
bundle
56
} = require('./utils')
67

@@ -43,6 +44,58 @@ test('SSR style and moduleId extraction', done => {
4344
})
4445
})
4546

47+
test('SSR with scoped CSS', done => {
48+
bundle({
49+
target: 'node',
50+
entry: './test/fixtures/ssr-scoped-style.js',
51+
output: {
52+
path: '/',
53+
filename: 'test.build.js',
54+
libraryTarget: 'commonjs2'
55+
},
56+
externals: ['vue']
57+
}, code => {
58+
const renderer = SSR.createBundleRenderer(code, {
59+
basedir: __dirname,
60+
runInNewContext: 'once'
61+
})
62+
const context = {
63+
_registeredComponents: new Set()
64+
}
65+
renderer.renderToString(context, (err, res) => {
66+
if (err) return done(err)
67+
68+
const id = `data-v-${genId('scoped-css.vue')}`
69+
70+
expect(res).toContain('data-server-rendered')
71+
expect(res).toContain(`<div ${id}>`)
72+
expect(res).toContain(`<svg ${id}>`)
73+
74+
const style = context.styles
75+
76+
expect(style).toContain(`.test[${id}] {\n color: yellow;\n}`)
77+
expect(style).toContain(`.test[${id}]:after {\n content: \'bye!\';\n}`)
78+
expect(style).toContain(`h1[${id}] {\n color: green;\n}`)
79+
// scoped keyframes
80+
expect(style).toContain(`.anim[${id}] {\n animation: color-${id} 5s infinite, other 5s;`)
81+
expect(style).toContain(`.anim-2[${id}] {\n animation-name: color-${id}`)
82+
expect(style).toContain(`.anim-3[${id}] {\n animation: 5s color-${id} infinite, 5s other;`)
83+
expect(style).toContain(`@keyframes color-${id} {`)
84+
expect(style).toContain(`@-webkit-keyframes color-${id} {`)
85+
86+
expect(style).toContain(
87+
`.anim-multiple[${id}] {\n animation: color-${id} 5s infinite,opacity-${id} 2s;`
88+
)
89+
expect(style).toContain(`.anim-multiple-2[${id}] {\n animation-name: color-${id},opacity-${id};`)
90+
expect(style).toContain(`@keyframes opacity-${id} {`)
91+
expect(style).toContain(`@-webkit-keyframes opacity-${id} {`)
92+
// >>> combinator
93+
expect(style).toContain(`.foo p[${id}] .bar {\n color: red;\n}`)
94+
done()
95+
})
96+
})
97+
})
98+
4699
// TODO
47100
// test('css-modules in SSR', done => {
48101
// bundle({

0 commit comments

Comments
 (0)