Skip to content

Commit fe3e3ab

Browse files
committed
adjust style injection calling by taking context as argument
1 parent a29681e commit fe3e3ab

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

lib/component-normalizer.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* globals __VUE_SSR_CONTEXT__ */
2+
13
// this module is a runtime utility for cleaner component module output and will
24
// be included in the final webpack user bundle
35

@@ -28,21 +30,30 @@ module.exports = function normalizeComponent (
2830
options.staticRenderFns = compiledTemplate.staticRenderFns
2931
}
3032

31-
if (injectStyles) {
32-
var existing = options.beforeCreate
33-
options.beforeCreate = existing
34-
? [].concat(existing, injectStyles)
35-
: [injectStyles]
36-
// used by ssr in case component is cached and beforeCreate
37-
// never gets called
38-
options._injectStyles = injectStyles
39-
}
40-
4133
// scopedId
4234
if (scopeId) {
4335
options._scopeId = scopeId
4436
}
4537

38+
var existing = options.beforeCreate
39+
var registerComponent = function (context) {
40+
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
41+
context = __VUE_SSR_CONTEXT__
42+
}
43+
if (injectStyles) {
44+
injectStyles(this, context)
45+
}
46+
}
47+
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
56+
4657
return {
4758
esModule: esModule,
4859
exports: scriptExports,

lib/loader.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
1818

1919
var hasBabel = false
2020
try {
21-
hasBabel = !!require('babel-loader')
21+
hasBabel = !!require(normalize.dep('babel-loader'))
2222
} catch (e) {}
2323

2424
var hasBuble = false
2525
try {
26-
hasBuble = !!require('buble-loader')
26+
hasBuble = !!require(normalize.dep('buble-loader'))
2727
} catch (e) {}
2828

2929
var rewriterInjectRE = /\b(css(?:-loader)?(?:\?[^!]+)?)(?:!|$)/
@@ -284,7 +284,7 @@ module.exports = function (content) {
284284
// add requires for styles
285285
if (parts.styles.length) {
286286
var styleInjectionCode =
287-
'function __injectVueStyle__ () {\n'
287+
'function __injectVueStyle__ (vm, context) {\n'
288288
var cssModules = {}
289289
parts.styles.forEach(function (style, i) {
290290
// require style
@@ -297,7 +297,7 @@ 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__()`
300+
? code => `;(${code}).__inject__(context)`
301301
: code => code
302302

303303
var moduleName = (style.module === true) ? '$style' : style.module
@@ -316,7 +316,7 @@ module.exports = function (content) {
316316
requireString += '.locals'
317317
}
318318

319-
styleInjectionCode += invokeStyle('this["' + moduleName + '"] = ' + requireString) + '\n'
319+
styleInjectionCode += invokeStyle('vm["' + moduleName + '"] = ' + requireString) + '\n'
320320
}
321321
} else {
322322
styleInjectionCode += invokeStyle(requireString) + '\n'

test/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,9 @@ describe('vue-loader', function () {
575575
rules: [{ test: /\.vue$/, loader: rawLoaderPath }]
576576
}
577577
}, code => {
578-
const renderer = SSR.createBundleRenderer(code)
578+
const renderer = SSR.createBundleRenderer(code, {
579+
basedir: __dirname
580+
})
579581
const context = {}
580582
renderer.renderToString(context, (err, res) => {
581583
if (err) return done(err)

0 commit comments

Comments
 (0)