1
+ import { isObject } from 'shared/util'
2
+
1
3
const vm = require ( 'vm' )
2
4
const path = require ( 'path' )
3
5
const resolve = require ( 'resolve' )
@@ -70,6 +72,20 @@ function compileModule (files, basedir) {
70
72
return evaluateModule
71
73
}
72
74
75
+ function deepClone ( val ) {
76
+ if ( isObject ( val ) ) {
77
+ const res = { }
78
+ for ( const key in val ) {
79
+ res [ key ] = deepClone ( val [ key ] )
80
+ }
81
+ return res
82
+ } else if ( Array . isArray ( val ) ) {
83
+ return val . slice ( )
84
+ } else {
85
+ return val
86
+ }
87
+ }
88
+
73
89
export function createBundleRunner ( entry , files , basedir , direct ) {
74
90
const evaluate = compileModule ( files , basedir )
75
91
if ( ! direct ) {
@@ -87,13 +103,18 @@ export function createBundleRunner (entry, files, basedir, direct) {
87
103
// each render, it simply calls the exported function. This avoids the
88
104
// module evaluation costs but requires the source code to be structured
89
105
// slightly differently.
90
- const context = createContext ( )
106
+ const initialExposedContext = { }
107
+ const context = createContext ( initialExposedContext )
91
108
const runner = evaluate ( entry , context , { } )
92
109
if ( typeof runner !== 'function' ) {
93
110
throw new Error ( 'direct mode expects bundle export to be a function.' )
94
111
}
95
112
return ( _context = { } ) => {
96
113
context . __VUE_SSR_CONTEXT__ = _context
114
+ // vue-style-loader styles imported outside of component lifecycle hooks
115
+ if ( initialExposedContext . _styles ) {
116
+ _context . _styles = deepClone ( initialExposedContext . _styles )
117
+ }
97
118
return runner ( _context )
98
119
}
99
120
}
0 commit comments