Skip to content

Commit c490716

Browse files
committed
handle ssr injection outside *.vue files
1 parent fd2f3ad commit c490716

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports.pitch = function (remainingRequest) {
1717
var addStylesClientPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesClient.js'))
1818
var addStylesServerPath = loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'lib/addStylesServer.js'))
1919

20+
var isVue = /\.vue$/.test(remainingRequest)
2021
var request = loaderUtils.stringifyRequest(this, '!!' + remainingRequest)
2122
var id = JSON.stringify(hash(request))
2223

@@ -56,12 +57,21 @@ module.exports.pitch = function (remainingRequest) {
5657
return shared.concat(code).join('\n')
5758
} else {
5859
// on the server: attach to Vue SSR context
59-
return shared.concat([
60-
'// add CSS to SSR context',
61-
'var add = require(' + addStylesServerPath + ')',
62-
'module.exports.__inject__ = function (context) {',
63-
' add(' + id + ', content, ' + isProduction + ', context)',
64-
'};'
65-
]).join('\n')
60+
if (isVue) {
61+
// inside *.vue file: expose a function so it can be called in
62+
// component's lifecycle hooks
63+
return shared.concat([
64+
'// add CSS to SSR context',
65+
'var add = require(' + addStylesServerPath + ')',
66+
'module.exports.__inject__ = function (context) {',
67+
' add(' + id + ', content, ' + isProduction + ', context)',
68+
'};'
69+
]).join('\n')
70+
} else {
71+
// normal import
72+
return shared.concat([
73+
'require(' + addStylesServerPath + ')(' + id + ', content, ' + isProduction + ')'
74+
]).join('\n')
75+
}
6676
}
6777
}

lib/addStylesServer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
var listToStyles = require('./listToStyles')
22

33
module.exports = function (parentId, list, isProduction, context) {
4+
if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
5+
context = __VUE_SSR_CONTEXT__
6+
}
47
if (context) {
58
var styles = context._styles
69

0 commit comments

Comments
 (0)