forked from preactjs/preact
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (67 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
if (process.env.NODE_ENV === 'development') {
const { options } = require('preact');
const oldVnodeOption = options.vnode;
options.vnode = function(vnode) {
const { nodeName, attributes, children } = vnode;
if (nodeName === void 0) {
throw new Error('Undefined component passed to preact.h()');
}
if (
attributes && attributes.ref !== void 0 &&
typeof attributes.ref !== 'function'
) {
throw new Error(
`Component's "ref" property should be a function,` +
` but [${typeof attributes.ref}] passed`
);
}
{
const keys = {};
inspectChildren(children, (deepChild) => {
if (!deepChild) return;
// In Preact, all keys are stored as object values, i.e. being strings
const key = deepChild.key + '';
if (keys.hasOwnProperty(key)) {
/* eslint-disable no-console */
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) + '\n\n'
);
// Return early to not spam the console
return true;
}
keys[key] = true;
});
}
return oldVnodeOption.call(this, vnode);
};
const inspectChildren = (children, inspect) => {
return children.some((child, i) => {
if (Array.isArray(child)) {
return inspectChildren(child, inspect);
}
return inspect(child, i);
});
};
const serializeVNode = ({ nodeName, attributes }) => {
let name;
let props;
if (typeof nodeName === 'function') {
name = nodeName.name || nodeName.displayName;
} else {
name = nodeName;
}
if (attributes) {
props = Object.keys(attributes).map(attr => {
return `${attr}=${JSON.stringify(attributes[attr] + '')}`;
});
}
if (!props) {
return `<${name} />`;
}
return `<${name} ${props.join(' ')} />`;
};
require('preact/devtools');
}