UNPKG

2.76 kBJavaScriptView Raw
1'use strict';
2
3/* eslint-disable no-console */
4
5if (process.env.NODE_ENV === 'development') {
6 var _require = require('preact'),
7 options = _require.options;
8
9 var oldVnodeOption = options.vnode;
10
11 options.vnode = function (vnode) {
12 var nodeName = vnode.nodeName,
13 attributes = vnode.attributes,
14 children = vnode.children;
15
16
17 if (nodeName === void 0) {
18 console.error('Undefined component passed to preact.h()\n' + serializeVNode(vnode));
19 }
20
21 if (attributes && attributes.ref !== void 0 && typeof attributes.ref !== 'function' && !('$$typeof' in vnode) // allow string refs when preact-compat is installed
22 ) {
23 throw new Error('Component\'s "ref" property should be a function,' + (' but [' + typeof attributes.ref + '] passed\n') + serializeVNode(vnode));
24 }
25
26 {
27 var keys = {};
28
29 inspectChildren(children, function (deepChild) {
30 if (!deepChild || deepChild.key == null) return;
31
32 // In Preact, all keys are stored as object values, i.e. being strings
33 var key = deepChild.key + '';
34
35 if (keys.hasOwnProperty(key)) {
36 console.error('Following component has two or more children with the ' + 'same "key" attribute. This may cause glitches and misbehavior ' + 'in rendering process. Component: \n\n' + serializeVNode(vnode));
37
38 // Return early to not spam the console
39 return true;
40 }
41
42 keys[key] = true;
43 });
44 }
45
46 if (oldVnodeOption) oldVnodeOption.call(this, vnode);
47 };
48
49 var inspectChildren = function inspectChildren(children, inspect) {
50 if (!Array.isArray(children)) {
51 children = [children];
52 }
53 return children.some(function (child, i) {
54 if (Array.isArray(child)) {
55 return inspectChildren(child, inspect);
56 }
57
58 return inspect(child, i);
59 });
60 };
61
62 var serializeVNode = function serializeVNode(_ref) {
63 var nodeName = _ref.nodeName,
64 attributes = _ref.attributes,
65 children = _ref.children;
66
67 if (typeof nodeName === 'function') {
68 nodeName = nodeName.name || nodeName.displayName;
69 }
70
71 var props = '';
72 if (attributes) {
73 for (var attr in attributes) {
74 if (attributes.hasOwnProperty(attr) && attr !== 'children') {
75 var value = attributes[attr];
76
77 // If it is an object but doesn't have toString(), use Object.toString
78 if (typeof value === 'function') {
79 value = 'function ' + (value.displayName || value.name) + '() {}';
80 }
81 if (Object(value) === value && !value.toString) {
82 value = Object.prototype.toString.call(value);
83 } else {
84 value = value + '';
85 }
86
87 props += ' ' + attr + '=' + JSON.stringify(value);
88 }
89 }
90 }
91
92 return '<' + nodeName + props + (children && children.length ? '>..</' + nodeName + '>' : ' />');
93 };
94
95 require('preact/devtools');
96}
97
98//# sourceMappingURL=debug.js.map
\No newline at end of file