@@ -53,18 +53,27 @@ module.exports = function assemble (source, filename, config) {
53
53
54
54
let cssModules
55
55
if ( styles . length ) {
56
- let styleInjectionCode = `function ${ INJECT_STYLE_FN } (ssrContext) {\n`
56
+ let styleInjectionCode = ''
57
57
58
- if ( needsHotReload ) styleInjectionCode += `if (${ DISPOSED } ) return`
59
- if ( config . isServer ) styleInjectionCode += ' var i\n'
58
+ if ( needsHotReload ) styleInjectionCode += `if (${ DISPOSED } ) return\n`
60
59
61
60
styles . forEach ( ( style , i ) => {
62
- const invokeStyle = config . isServer && config . hasStyleInjectFn
63
- ? code => ` ;i=${ code } ,i.__inject__&&i.__inject__(ssrContext),i)\n`
64
- : code => ` ${ code } \n`
65
-
61
+ const IMPORT_NAME = `__vue_style_${ i } __`
62
+ const IMPORT_STRING = _s ( style . id )
66
63
const moduleName = ( style . descriptor . module === true ) ? '$style' : style . descriptor . module
67
- const requireString = `require(${ _s ( style . id ) } )`
64
+ const needsStyleInjection = config . isServer && config . hasStyleInjectFn
65
+ const needsNamedImport = needsStyleInjection || typeof moduleName === 'string'
66
+ const runInjection = needsStyleInjection ? `${ IMPORT_NAME } && ${ IMPORT_NAME } .__inject__ && ${ IMPORT_NAME } .__inject__(ssrContext)\n` : ''
67
+
68
+ if ( needsNamedImport ) {
69
+ output += config . esModule
70
+ ? `import ${ IMPORT_NAME } from ${ IMPORT_STRING } \n`
71
+ : `const ${ IMPORT_NAME } = requrie(${ IMPORT_STRING } )\n`
72
+ } else {
73
+ output += config . esModule
74
+ ? `import ${ IMPORT_STRING } \n`
75
+ : `require(${ IMPORT_STRING } )\n`
76
+ }
68
77
69
78
if ( moduleName ) {
70
79
if ( ! cssModules ) {
@@ -77,16 +86,16 @@ module.exports = function assemble (source, filename, config) {
77
86
config . onWarn ( {
78
87
message : 'CSS module name "' + moduleName + '" is not unique!'
79
88
} )
80
- styleInjectionCode += invokeStyle ( requireString )
89
+ styleInjectionCode += runInjection
81
90
} else {
82
91
cssModules [ moduleName ] = true
83
92
const MODULE_KEY = _s ( moduleName )
84
93
85
94
if ( ! needsHotReload ) {
86
- styleInjectionCode += invokeStyle ( `this[${ MODULE_KEY } ] = ${ requireString } ` )
95
+ styleInjectionCode += runInjection + `this[${ MODULE_KEY } ] = ${ IMPORT_NAME } \n`
87
96
} else {
88
- styleInjectionCode +=
89
- invokeStyle ( `${ CSS_MODULES } [${ MODULE_KEY } ] = ${ requireString } ` ) +
97
+ styleInjectionCode += runInjection +
98
+ `${ CSS_MODULES } [${ MODULE_KEY } ] = ${ IMPORT_NAME } \n` +
90
99
`Object.defineProperty(this, ${ MODULE_KEY } , { get: function () { return ${ CSS_MODULES } [${ MODULE_KEY } ] }})\n`
91
100
92
101
output +=
@@ -95,7 +104,7 @@ module.exports = function assemble (source, filename, config) {
95
104
` var oldLocals = ${ CSS_MODULES } [${ MODULE_KEY } ]\n` +
96
105
` if (!oldLocals) return\n` +
97
106
// 2. re-import (side effect: updates the <style>)
98
- ` var newLocals = ${ requireString } \n` +
107
+ ` var newLocals = require( ${ IMPORT_STRING } ) \n` +
99
108
// 3. compare new and old locals to see if selectors changed
100
109
` if (JSON.stringify(newLocals) === JSON.stringify(oldLocals)) return\n` +
101
110
// 4. locals changed. Update and force re-render.
@@ -105,12 +114,9 @@ module.exports = function assemble (source, filename, config) {
105
114
}
106
115
}
107
116
} else {
108
- styleInjectionCode += invokeStyle ( requireString )
109
117
}
110
118
} )
111
-
112
- styleInjectionCode += '}\n'
113
- output += styleInjectionCode
119
+ output += `function ${ INJECT_STYLE_FN } (ssrContext) {\n` + pad ( styleInjectionCode ) + `}\n`
114
120
}
115
121
116
122
// we require the component normalizer function, and call it like so:
@@ -242,5 +248,5 @@ module.exports = function assemble (source, filename, config) {
242
248
}
243
249
244
250
function pad ( content ) {
245
- return content . split ( '\n' ) . map ( line => ' ' + line ) . join ( '\n' )
251
+ return content . trim ( ) . split ( '\n' ) . map ( line => ' ' + line ) . join ( '\n' ) + '\n'
246
252
}
0 commit comments