refactor: move runtime compile error handling to vue

This commit is contained in:
Evan You
2019-12-11 10:25:34 -05:00
parent c202bd6ac0
commit 4d730f464d
3 changed files with 21 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
// This package is the "full-build" that includes both the runtime
// and the compiler, and supports on-the-fly compilation of the template option.
import { compile, CompilerOptions } from '@vue/compiler-dom'
import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom'
import { registerRuntimeCompiler, RenderFunction, warn } from '@vue/runtime-dom'
import * as runtimeDom from '@vue/runtime-dom'
import { isString, NOOP } from '@vue/shared'
import { isString, NOOP, generateCodeFrame } from '@vue/shared'
const compileCache: Record<string, RenderFunction> = Object.create(null)
@@ -37,6 +37,19 @@ function compileToFunction(
const { code } = compile(template, {
hoistStatic: true,
cacheHandlers: true,
onError(err: CompilerError) {
if (__DEV__) {
const message = `Template compilation error: ${err.message}`
const codeFrame =
err.loc &&
generateCodeFrame(
template as string,
err.loc.start.offset,
err.loc.end.offset
)
warn(codeFrame ? `${message}\n${codeFrame}` : message)
}
},
...options
})