Skip to content

Commit 010db30

Browse files
committed
preserve old client-side behavior
1 parent fe3e3ab commit 010db30

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

lib/component-normalizer.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = function normalizeComponent (
77
rawScriptExports,
88
compiledTemplate,
99
injectStyles,
10-
scopeId
10+
scopeId,
11+
isServer
1112
) {
1213
var esModule
1314
var scriptExports = rawScriptExports = rawScriptExports || {}
@@ -35,24 +36,38 @@ module.exports = function normalizeComponent (
3536
options._scopeId = scopeId
3637
}
3738

38-
var existing = options.beforeCreate
39-
var registerComponent = function (context) {
40-
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
41-
context = __VUE_SSR_CONTEXT__
39+
var hook
40+
if (isServer) {
41+
hook = function (context) {
42+
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
43+
context = __VUE_SSR_CONTEXT__
44+
}
45+
if (injectStyles) {
46+
injectStyles(this, context)
47+
}
48+
// TODO: register module identifier for async chunk inferrence
4249
}
43-
if (injectStyles) {
44-
injectStyles(this, context)
50+
// used by ssr in case component is cached and beforeCreate
51+
// never gets called
52+
options._ssrRegister = hook
53+
} else if (injectStyles) {
54+
var cssModules = injectStyles({})
55+
if (cssModules) {
56+
hook = function () {
57+
for (var key in cssModules) {
58+
this[key] = cssModules[key]
59+
}
60+
}
4561
}
4662
}
4763

48-
// inject component registration as beforeCreate hook
49-
options.beforeCreate = existing
50-
? [].concat(existing, registerComponent)
51-
: [registerComponent]
52-
53-
// used by ssr in case component is cached and beforeCreate
54-
// never gets called
55-
options._ssrRegister = registerComponent
64+
if (hook) {
65+
// inject component registration as beforeCreate hook
66+
var existing = options.beforeCreate
67+
options.beforeCreate = existing
68+
? [].concat(existing, hook)
69+
: [hook]
70+
}
5671

5772
return {
5873
esModule: esModule,

lib/loader.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ module.exports = function (content) {
282282
var hasScoped = parts.styles.some(function (s) { return s.scoped })
283283

284284
// add requires for styles
285+
var cssModules
285286
if (parts.styles.length) {
286287
var styleInjectionCode =
287-
'function __injectVueStyle__ (vm, context) {\n'
288-
var cssModules = {}
288+
'function injectStyle (cssModuleTarget, ssrContext) {\n'
289289
parts.styles.forEach(function (style, i) {
290290
// require style
291291
var requireString = style.src
@@ -297,15 +297,16 @@ module.exports = function (content) {
297297
// vue-style-loader exposes inject functions during SSR so they are
298298
// always called
299299
var invokeStyle = isServer && hasVueStyleLoader
300-
? code => `;(${code}).__inject__(context)`
301-
: code => code
300+
? code => ` ;(${code}).__inject__(ssrContext)\n`
301+
: code => ` ${code}\n`
302302

303303
var moduleName = (style.module === true) ? '$style' : style.module
304304
// setCssModule
305305
if (moduleName) {
306+
cssModules = cssModules || {}
306307
if (moduleName in cssModules) {
307308
loaderContext.emitError('CSS module name "' + moduleName + '" is not unique!')
308-
styleInjectionCode += invokeStyle(requireString) + '\n'
309+
styleInjectionCode += invokeStyle(requireString)
309310
} else {
310311
cssModules[moduleName] = true
311312

@@ -316,12 +317,15 @@ module.exports = function (content) {
316317
requireString += '.locals'
317318
}
318319

319-
styleInjectionCode += invokeStyle('vm["' + moduleName + '"] = ' + requireString) + '\n'
320+
styleInjectionCode += invokeStyle('cssModuleTarget["' + moduleName + '"] = ' + requireString)
320321
}
321322
} else {
322-
styleInjectionCode += invokeStyle(requireString) + '\n'
323+
styleInjectionCode += invokeStyle(requireString)
323324
}
324325
})
326+
if (cssModules) {
327+
styleInjectionCode += ' return cssModuleTarget\n'
328+
}
325329
styleInjectionCode += '}\n'
326330
output += styleInjectionCode
327331
}
@@ -331,7 +335,8 @@ module.exports = function (content) {
331335
// scriptExports,
332336
// compiledTemplate,
333337
// injectStyles,
334-
// scopeId
338+
// scopeId,
339+
// isServer
335340
// )
336341
output += 'var Component = require(' +
337342
loaderUtils.stringifyRequest(loaderContext, '!' + componentNormalizerPath) +
@@ -367,11 +372,15 @@ module.exports = function (content) {
367372

368373
// style
369374
output += ' /* styles */\n '
370-
output += (parts.styles.length ? '__injectVueStyle__' : 'null') + ',\n'
375+
output += (parts.styles.length ? 'injectStyle' : 'null') + ',\n'
371376

372377
// scopeId
373378
output += ' /* scopeId */\n '
374-
output += (hasScoped ? JSON.stringify(moduleId) : 'null') + '\n'
379+
output += (hasScoped ? JSON.stringify(moduleId) : 'null') + ',\n'
380+
381+
// isServer
382+
output += ' /* isServer */\n '
383+
output += (isServer ? 'true' : 'false') + '\n'
375384

376385
// close normalizeComponent call
377386
output += ')\n'

0 commit comments

Comments
 (0)