1 |
|
2 |
|
3 | import config from '../config'
|
4 | import { noop } from 'shared/util'
|
5 |
|
6 | export let warn = noop
|
7 | export let tip = noop
|
8 | export let generateComponentTrace = (noop: any)
|
9 | export let formatComponentName = (noop: any)
|
10 |
|
11 | if (process.env.NODE_ENV !== 'production') {
|
12 | const hasConsole = typeof console !== 'undefined'
|
13 | const classifyRE = /(?:^|[-_])(\w)/g
|
14 | const classify = str => str
|
15 | .replace(classifyRE, c => c.toUpperCase())
|
16 | .replace(/[-_]/g, '')
|
17 |
|
18 | warn = (msg, vm) => {
|
19 | const trace = vm ? generateComponentTrace(vm) : ''
|
20 |
|
21 | if (config.warnHandler) {
|
22 | config.warnHandler.call(null, msg, vm, trace)
|
23 | } else if (hasConsole && (!config.silent)) {
|
24 | console.error(`[Vue warn]: ${msg}${trace}`)
|
25 | }
|
26 | }
|
27 |
|
28 | tip = (msg, vm) => {
|
29 | if (hasConsole && (!config.silent)) {
|
30 | console.warn(`[Vue tip]: ${msg}` + (
|
31 | vm ? generateComponentTrace(vm) : ''
|
32 | ))
|
33 | }
|
34 | }
|
35 |
|
36 | formatComponentName = (vm, includeFile) => {
|
37 | if (vm.$root === vm) {
|
38 | return '<Root>'
|
39 | }
|
40 | const options = typeof vm === 'function' && vm.cid != null
|
41 | ? vm.options
|
42 | : vm._isVue
|
43 | ? vm.$options || vm.constructor.options
|
44 | : vm
|
45 | let name = options.name || options._componentTag
|
46 | const file = options.__file
|
47 | if (!name && file) {
|
48 | const match = file.match(/([^/\\]+)\.vue$/)
|
49 | name = match && match[1]
|
50 | }
|
51 |
|
52 | return (
|
53 | (name ? `<${classify(name)}>` : `<Anonymous>`) +
|
54 | (file && includeFile !== false ? ` at ${file}` : '')
|
55 | )
|
56 | }
|
57 |
|
58 | const repeat = (str, n) => {
|
59 | let res = ''
|
60 | while (n) {
|
61 | if (n % 2 === 1) res += str
|
62 | if (n > 1) str += str
|
63 | n >>= 1
|
64 | }
|
65 | return res
|
66 | }
|
67 |
|
68 | generateComponentTrace = vm => {
|
69 | if (vm._isVue && vm.$parent) {
|
70 | const tree = []
|
71 | let currentRecursiveSequence = 0
|
72 | while (vm) {
|
73 | if (tree.length > 0) {
|
74 | const last = tree[tree.length - 1]
|
75 | if (last.constructor === vm.constructor) {
|
76 | currentRecursiveSequence++
|
77 | vm = vm.$parent
|
78 | continue
|
79 | } else if (currentRecursiveSequence > 0) {
|
80 | tree[tree.length - 1] = [last, currentRecursiveSequence]
|
81 | currentRecursiveSequence = 0
|
82 | }
|
83 | }
|
84 | tree.push(vm)
|
85 | vm = vm.$parent
|
86 | }
|
87 | return '\n\nfound in\n\n' + tree
|
88 | .map((vm, i) => `${
|
89 | i === 0 ? '---> ' : repeat(' ', 5 + i * 2)
|
90 | }${
|
91 | Array.isArray(vm)
|
92 | ? `${formatComponentName(vm[0])}... (${vm[1]} recursive calls)`
|
93 | : formatComponentName(vm)
|
94 | }`)
|
95 | .join('\n')
|
96 | } else {
|
97 | return `\n\n(found in ${formatComponentName(vm)})`
|
98 | }
|
99 | }
|
100 | }
|