Skip to content

Commit 3a16ce5

Browse files
committed
test: ssr
1 parent edd1571 commit 3a16ce5

File tree

5 files changed

+115
-4
lines changed

5 files changed

+115
-4
lines changed

Diff for: lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ var component = normalizer(
117117
staticRenderFns,
118118
${hasFunctional ? `true` : `false`},
119119
null, // TODO style injection
120-
${JSON.stringify(id)},
120+
${hasScoped ? JSON.stringify(id) : `null`},
121121
${isServer ? JSON.stringify(hash(request)) : `null`}
122122
${incomingQuery.shadow ? `,true` : ``}
123123
)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ module.exports = function (template) {
1616
const loaderContext = this
1717
const query = qs.parse(this.resourceQuery.slice(1))
1818

19-
// TODO: actually get main vue-loader options
19+
// although this is not the main vue-loader, we can get access to the same
20+
// vue-loader options because we've set an ident in the plugin and used that
21+
// ident to create the request for this loader in the pitcher.
2022
const options = loaderUtils.getOptions(loaderContext) || {}
2123

2224
const cb = loaderContext.async()

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"sugarss": "^1.0.1",
5555
"url-loader": "^1.0.1",
5656
"vue": "^2.5.16",
57+
"vue-server-renderer": "^2.5.16",
5758
"vue-template-compiler": "^2.5.16",
5859
"webpack": "^4.1.0",
5960
"webpack-cli": "^2.0.10",

Diff for: test/ssr.spec.js

+69-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,71 @@
1-
test('ssr', () => {
1+
const SSR = require('vue-server-renderer')
22

3+
const {
4+
bundle
5+
} = require('./utils')
6+
7+
test('SSR style and moduleId extraction', done => {
8+
bundle({
9+
target: 'node',
10+
entry: './test/fixtures/ssr-style.js',
11+
output: {
12+
path: '/',
13+
filename: 'test.build.js',
14+
libraryTarget: 'commonjs2'
15+
},
16+
externals: ['vue']
17+
}, code => {
18+
const renderer = SSR.createBundleRenderer(code, {
19+
basedir: __dirname,
20+
runInNewContext: 'once'
21+
})
22+
const context = {
23+
_registeredComponents: new Set()
24+
}
25+
renderer.renderToString(context, (err, res) => {
26+
if (err) return done(err)
27+
expect(res).toContain('data-server-rendered')
28+
expect(res).toContain('<h1>Hello</h1>')
29+
expect(res).toContain('Hello from Component A!')
30+
expect(res).toContain('<div class="foo">functional</div>')
31+
// from main component
32+
expect(context.styles).toContain('h1 { color: green;')
33+
// from imported child component
34+
expect(context.styles).toContain('comp-a h2 {\n color: #f00;')
35+
// from imported css file
36+
expect(context.styles).toContain('h1 { color: red;')
37+
// from imported functional component
38+
expect(context.styles).toContain('.foo { color: red;')
39+
// collect component identifiers during render
40+
expect(Array.from(context._registeredComponents).length).toBe(3)
41+
done()
42+
})
43+
})
344
})
45+
46+
// TODO
47+
// test('css-modules in SSR', done => {
48+
// bundle({
49+
// entry: 'css-modules.vue',
50+
// target: 'node',
51+
// output: Object.assign({}, globalConfig.output, {
52+
// libraryTarget: 'commonjs2'
53+
// })
54+
// }, (code, warnings) => {
55+
// // http://stackoverflow.com/questions/17581830/load-node-js-module-from-string-in-memory
56+
// function requireFromString (src, filename) {
57+
// const Module = module.constructor
58+
// const m = new Module()
59+
// m._compile(src, filename)
60+
// return m.exports
61+
// }
62+
63+
// const output = interopDefault(requireFromString(code, './test.build.js'))
64+
// const mockInstance = {}
65+
66+
// output.beforeCreate.forEach(hook => hook.call(mockInstance))
67+
// expect(mockInstance.style.red).toBeDefined()
68+
69+
// done()
70+
// })
71+
// })

Diff for: yarn.lock

+41-1
Original file line numberDiff line numberDiff line change
@@ -4741,6 +4741,10 @@ locate-path@^2.0.0:
47414741
p-locate "^2.0.0"
47424742
path-exists "^3.0.0"
47434743

4744+
lodash._reinterpolate@~3.0.0:
4745+
version "3.0.0"
4746+
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
4747+
47444748
lodash.assign@^4.2.0:
47454749
version "4.2.0"
47464750
resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"
@@ -4773,6 +4777,19 @@ lodash.tail@^4.1.1:
47734777
version "4.1.1"
47744778
resolved "https://registry.yarnpkg.com/lodash.tail/-/lodash.tail-4.1.1.tgz#d2333a36d9e7717c8ad2f7cacafec7c32b444664"
47754779

4780+
lodash.template@^4.4.0:
4781+
version "4.4.0"
4782+
resolved "https://registry.yarnpkg.com/lodash.template/-/lodash.template-4.4.0.tgz#e73a0385c8355591746e020b99679c690e68fba0"
4783+
dependencies:
4784+
lodash._reinterpolate "~3.0.0"
4785+
lodash.templatesettings "^4.0.0"
4786+
4787+
lodash.templatesettings@^4.0.0:
4788+
version "4.1.0"
4789+
resolved "https://registry.yarnpkg.com/lodash.templatesettings/-/lodash.templatesettings-4.1.0.tgz#2b4d4e95ba440d915ff08bc899e4553666713316"
4790+
dependencies:
4791+
lodash._reinterpolate "~3.0.0"
4792+
47764793
lodash.uniq@^4.5.0:
47774794
version "4.5.0"
47784795
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
@@ -6744,6 +6761,12 @@ resolve@^1.1.6:
67446761
dependencies:
67456762
path-parse "^1.0.5"
67466763

6764+
resolve@^1.2.0:
6765+
version "1.6.0"
6766+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.6.0.tgz#0fbd21278b27b4004481c395349e7aba60a9ff5c"
6767+
dependencies:
6768+
path-parse "^1.0.5"
6769+
67476770
67486771
version "1.0.2"
67496772
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
@@ -6928,7 +6951,7 @@ [email protected]:
69286951
range-parser "~1.2.0"
69296952
statuses "~1.4.0"
69306953

6931-
serialize-javascript@^1.4.0:
6954+
serialize-javascript@^1.3.0, serialize-javascript@^1.4.0:
69326955
version "1.4.0"
69336956
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-1.4.0.tgz#7c958514db6ac2443a8abc062dc9f7886a7f6005"
69346957

@@ -7171,6 +7194,10 @@ [email protected], source-map@^0.4.2, source-map@^0.4.4:
71717194
dependencies:
71727195
amdefine ">=0.0.4"
71737196

7197+
7198+
version "0.5.6"
7199+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.6.tgz#75ce38f52bf0733c5a7f0c118d81334a2bb5f412"
7200+
71747201
source-map@^0.5.3, source-map@^0.5.6, source-map@^0.5.7, source-map@~0.5.1:
71757202
version "0.5.7"
71767203
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
@@ -7986,6 +8013,19 @@ vue-hot-reload-api@^2.1.0, vue-hot-reload-api@^2.3.0:
79868013
version "2.3.0"
79878014
resolved "https://registry.yarnpkg.com/vue-hot-reload-api/-/vue-hot-reload-api-2.3.0.tgz#97976142405d13d8efae154749e88c4e358cf926"
79888015

8016+
vue-server-renderer@^2.5.16:
8017+
version "2.5.16"
8018+
resolved "https://registry.yarnpkg.com/vue-server-renderer/-/vue-server-renderer-2.5.16.tgz#279ef8e37e502a0de3a9ae30758cc04a472eaac0"
8019+
dependencies:
8020+
chalk "^1.1.3"
8021+
hash-sum "^1.0.2"
8022+
he "^1.1.0"
8023+
lodash.template "^4.4.0"
8024+
lodash.uniq "^4.5.0"
8025+
resolve "^1.2.0"
8026+
serialize-javascript "^1.3.0"
8027+
source-map "0.5.6"
8028+
79898029
vue-style-loader@^4.0.2:
79908030
version "4.0.2"
79918031
resolved "https://registry.yarnpkg.com/vue-style-loader/-/vue-style-loader-4.0.2.tgz#e89aa4702a0c6b9630d8de70b1cbddb06b9ad254"

0 commit comments

Comments
 (0)