forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderToString.js
39 lines (31 loc) · 945 Bytes
/
renderToString.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// @flow
import Vue from 'vue'
import createInstance from './lib/create-instance'
import './lib/polyfills/object-assign-polyfill'
import { throwError } from './lib/util'
Vue.config.productionTip = false
Vue.config.devtools = false
export default function renderToString (component: Component, options: Options = {}): string {
let renderer
try {
renderer = require('vue-server-renderer').createRenderer()
} catch (e) {}
if (!renderer) {
throwError('renderToString must be run in node. It cannot be run in a browser')
}
// Remove cached constructor
delete component._Ctor
if (options.attachToDocument) {
throwError('you cannot use attachToDocument with renderToString')
}
const vm = createInstance(component, options)
let renderedString = ''
// $FlowIgnore
renderer.renderToString(vm, (err, res) => {
if (err) {
console.log(err)
}
renderedString = res
})
return renderedString
}