Skip to content

Commit f49bfa5

Browse files
committed
collect component identifier during render
1 parent e5a421c commit f49bfa5

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

lib/component-normalizer.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function normalizeComponent (
88
compiledTemplate,
99
injectStyles,
1010
scopeId,
11-
isServer
11+
moduleIdentifier /* server only */
1212
) {
1313
var esModule
1414
var scriptExports = rawScriptExports = rawScriptExports || {}
@@ -37,16 +37,20 @@ module.exports = function normalizeComponent (
3737
}
3838

3939
var hook
40-
if (isServer) {
40+
if (moduleIdentifier) { // server build
4141
hook = function (context) {
4242
// context is injected if this is a cached call
4343
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
4444
context = __VUE_SSR_CONTEXT__
4545
}
46+
// inject component styles
4647
if (injectStyles) {
4748
injectStyles(this, context)
4849
}
49-
// TODO: register module identifier for async chunk inferrence
50+
// register component module identifier for async chunk inferrence
51+
if (context && context._registeredComponents) {
52+
context._registeredComponents.add(moduleIdentifier)
53+
}
5054
}
5155
// used by ssr in case component is cached and beforeCreate
5256
// never gets called

lib/loader.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var path = require('path')
2+
var hash = require('hash-sum')
23
var parse = require('./parser')
34
var genId = require('./utils/gen-id')
45
var normalize = require('./utils/normalize')
@@ -338,7 +339,7 @@ module.exports = function (content) {
338339
// compiledTemplate,
339340
// injectStyles,
340341
// scopeId,
341-
// isServer
342+
// moduleIdentifier (server only)
342343
// )
343344
output += 'var Component = require(' +
344345
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
@@ -380,9 +381,9 @@ module.exports = function (content) {
380381
output += ' /* scopeId */\n '
381382
output += (hasScoped ? JSON.stringify(moduleId) : 'null') + ',\n'
382383

383-
// isServer
384-
output += ' /* isServer */\n '
385-
output += (isServer ? 'true' : 'false') + '\n'
384+
// moduleIdentifier (server only)
385+
output += ' /* moduleIdentifier (server only) */\n '
386+
output += (isServer ? JSON.stringify(hash(this.request)) : 'null') + '\n'
386387

387388
// close normalizeComponent call
388389
output += ')\n'

test/test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ describe('vue-loader', function () {
561561
})
562562
})
563563

564-
it('SSR style extraction', done => {
564+
it('SSR style and moduleId extraction', done => {
565565
bundle({
566566
target: 'node',
567567
entry: './test/fixtures/ssr-style.js',
@@ -578,7 +578,9 @@ describe('vue-loader', function () {
578578
const renderer = SSR.createBundleRenderer(code, {
579579
basedir: __dirname
580580
})
581-
const context = {}
581+
const context = {
582+
_registeredComponents: new Set()
583+
}
582584
renderer.renderToString(context, (err, res) => {
583585
if (err) return done(err)
584586
expect(res).to.contain('server-rendered')
@@ -590,6 +592,8 @@ describe('vue-loader', function () {
590592
expect(context.styles).to.contain('comp-a h2 {\n color: #f00;')
591593
// from imported css file
592594
expect(context.styles).to.contain('h1 { color: red;')
595+
// collect component identifiers during render
596+
expect(Array.from(context._registeredComponents).length).to.equal(2)
593597
done()
594598
})
595599
})

0 commit comments

Comments
 (0)