File tree 4 files changed +33
-16
lines changed
4 files changed +33
-16
lines changed Original file line number Diff line number Diff line change 1
1
/* @flow */
2
2
3
- import { noop } from 'shared/util'
4
- import { warn , tip } from 'core/util/debug'
3
+ import { noop , extend } from 'shared/util'
4
+ import { warn as baseWarn , tip } from 'core/util/debug'
5
5
6
6
type CompiledFunctionResult = {
7
7
render : Function ;
@@ -27,7 +27,9 @@ export function createCompileToFunctionFn (compile: Function): Function {
27
27
options ?: CompilerOptions ,
28
28
vm ?: Component
29
29
) : CompiledFunctionResult {
30
- options = options || { }
30
+ options = extend ( { } , options )
31
+ const warn = options . warn || baseWarn
32
+ delete options . warn
31
33
32
34
/* istanbul ignore if */
33
35
if ( process . env . NODE_ENV !== 'production' ) {
Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ import { noop } from 'shared/util'
5
5
6
6
export let warn = noop
7
7
export let tip = noop
8
- export let formatComponentName : Function = ( null : any ) // work around flow check
8
+ export let generateComponentTrace = ( noop : any ) // work around flow check
9
+ export let formatComponentName = ( noop : any )
9
10
10
11
if ( process . env . NODE_ENV !== 'production' ) {
11
12
const hasConsole = typeof console !== 'undefined'
@@ -66,7 +67,7 @@ if (process.env.NODE_ENV !== 'production') {
66
67
return res
67
68
}
68
69
69
- const generateComponentTrace = vm => {
70
+ generateComponentTrace = vm => {
70
71
if ( vm . _isVue && vm . $parent ) {
71
72
const tree = [ ]
72
73
let currentRecursiveSequence = 0
Original file line number Diff line number Diff line change 1
1
/* @flow */
2
2
3
- import {
4
- isDef ,
5
- isUndef ,
6
- isTrue ,
7
- extend
8
- } from 'shared/util'
9
-
10
3
import { escape } from 'web/server/util'
11
4
import { SSR_ATTR } from 'shared/constants'
12
5
import { RenderContext } from './render-context'
6
+ import { generateComponentTrace } from 'core/util/debug'
13
7
import { ssrCompileToFunctions } from 'web/server/compiler'
14
8
import { installSSRHelpers } from './optimizing-compiler/runtime-helpers'
15
9
10
+ import { isDef , isUndef , isTrue } from 'shared/util'
11
+
16
12
import {
17
13
createComponent ,
18
14
createComponentInstanceForVnode
@@ -26,13 +22,22 @@ const warnOnce = msg => {
26
22
}
27
23
}
28
24
25
+ const onCompilationError = ( err , vm ) => {
26
+ const trace = vm ? generateComponentTrace ( vm ) : ''
27
+ throw new Error ( `\n\u001b[31m${ err } ${ trace } \u001b[39m\n` )
28
+ }
29
+
29
30
const normalizeRender = vm => {
30
31
const { render, template, _scopeId } = vm . $options
31
32
if ( isUndef ( render ) ) {
32
33
if ( template ) {
33
- extend ( vm . $options , ssrCompileToFunctions ( template , {
34
- scopeId : _scopeId
35
- } ) )
34
+ const compiled = ssrCompileToFunctions ( template , {
35
+ scopeId : _scopeId ,
36
+ warn : onCompilationError
37
+ } , vm )
38
+
39
+ vm . $options . render = compiled . render
40
+ vm . $options . staticRenderFns = compiled . staticRenderFns
36
41
} else {
37
42
throw new Error (
38
43
`render function or template not defined in component: ${
Original file line number Diff line number Diff line change @@ -968,7 +968,7 @@ describe('SSR: renderToString', () => {
968
968
} )
969
969
} )
970
970
971
- it ( 'should Promise (error)' , done => {
971
+ it ( 'return Promise (error)' , done => {
972
972
Vue . config . silent = true
973
973
renderToString ( new Vue ( {
974
974
render ( ) {
@@ -980,6 +980,15 @@ describe('SSR: renderToString', () => {
980
980
done ( )
981
981
} )
982
982
} )
983
+
984
+ it ( 'should catch template compilation error' , done => {
985
+ renderToString ( new Vue ( {
986
+ template : `<div></div><div></div>`
987
+ } ) , ( err , res ) => {
988
+ expect ( err . toString ( ) ) . toContain ( 'Component template should contain exactly one root element' )
989
+ done ( )
990
+ } )
991
+ } )
983
992
} )
984
993
985
994
function renderVmWithOptions ( options , cb ) {
You can’t perform that action at this time.
0 commit comments