1 |
|
2 |
|
3 | import config from 'core/config'
|
4 | import { warn, cached } from 'core/util/index'
|
5 | import { mark, measure } from 'core/util/perf'
|
6 |
|
7 | import Vue from './runtime/index'
|
8 | import { query } from './util/index'
|
9 | import { compileToFunctions } from './compiler/index'
|
10 | import { shouldDecodeNewlines, shouldDecodeNewlinesForHref } from './util/compat'
|
11 |
|
12 | const idToTemplate = cached(id => {
|
13 | const el = query(id)
|
14 | return el && el.innerHTML
|
15 | })
|
16 |
|
17 | const mount = Vue.prototype.$mount
|
18 | Vue.prototype.$mount = function (
|
19 | el?: string | Element,
|
20 | hydrating?: boolean
|
21 | ): Component {
|
22 | el = el && query(el)
|
23 |
|
24 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
87 |
|
88 |
|
89 | function 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 |
|
99 | Vue.compile = compileToFunctions
|
100 |
|
101 | export default Vue
|