UNPKG

2.89 kBJavaScriptView Raw
1/* @flow */
2
3import config from 'core/config'
4import { warn, cached } from 'core/util/index'
5import { mark, measure } from 'core/util/perf'
6
7import Vue from './runtime/index'
8import { query } from './util/index'
9import { compileToFunctions } from './compiler/index'
10import { shouldDecodeNewlines, shouldDecodeNewlinesForHref } from './util/compat'
11
12const idToTemplate = cached(id => {
13 const el = query(id)
14 return el && el.innerHTML
15})
16
17const mount = Vue.prototype.$mount
18Vue.prototype.$mount = function (
19 el?: string | Element,
20 hydrating?: boolean
21): Component {
22 el = el && query(el)
23
24 /* istanbul ignore if */
25 if (el === document.body || el === document.documentElement) {
26 process.env.NODE_ENV !== 'production' && warn(
27 `Do not mount Vue to <html> or <body> - mount to normal elements instead.`
28 )
29 return this
30 }
31
32 const options = this.$options
33 // resolve template/el and convert to render function
34 if (!options.render) {
35 let template = options.template
36 if (template) {
37 if (typeof template === 'string') {
38 if (template.charAt(0) === '#') {
39 template = idToTemplate(template)
40 /* istanbul ignore if */
41 if (process.env.NODE_ENV !== 'production' && !template) {
42 warn(
43 `Template element not found or is empty: ${options.template}`,
44 this
45 )
46 }
47 }
48 } else if (template.nodeType) {
49 template = template.innerHTML
50 } else {
51 if (process.env.NODE_ENV !== 'production') {
52 warn('invalid template option:' + template, this)
53 }
54 return this
55 }
56 } else if (el) {
57 template = getOuterHTML(el)
58 }
59 if (template) {
60 /* istanbul ignore if */
61 if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
62 mark('compile')
63 }
64
65 const { render, staticRenderFns } = compileToFunctions(template, {
66 outputSourceRange: process.env.NODE_ENV !== 'production',
67 shouldDecodeNewlines,
68 shouldDecodeNewlinesForHref,
69 delimiters: options.delimiters,
70 comments: options.comments
71 }, this)
72 options.render = render
73 options.staticRenderFns = staticRenderFns
74
75 /* istanbul ignore if */
76 if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
77 mark('compile end')
78 measure(`vue ${this._name} compile`, 'compile', 'compile end')
79 }
80 }
81 }
82 return mount.call(this, el, hydrating)
83}
84
85/**
86 * Get outerHTML of elements, taking care
87 * of SVG elements in IE as well.
88 */
89function getOuterHTML (el: Element): string {
90 if (el.outerHTML) {
91 return el.outerHTML
92 } else {
93 const container = document.createElement('div')
94 container.appendChild(el.cloneNode(true))
95 return container.innerHTML
96 }
97}
98
99Vue.compile = compileToFunctions
100
101export default Vue