-
Notifications
You must be signed in to change notification settings - Fork 186
/
render.ts
106 lines (85 loc) · 2.67 KB
/
render.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type * as React from 'react';
import * as ReactDOM from 'react-dom';
import type { Root } from 'react-dom/client';
// Let compiler not to search module usage
const fullClone = {
...ReactDOM,
} as typeof ReactDOM & {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: {
usingClientEntryPoint?: boolean;
};
createRoot?: CreateRoot;
};
type CreateRoot = (container: ContainerType) => Root;
const { version, render: reactRender, unmountComponentAtNode } = fullClone;
let createRoot: CreateRoot;
try {
const mainVersion = Number((version || '').split('.')[0]);
if (mainVersion >= 18) {
({ createRoot } = fullClone);
}
} catch (e) {
// Do nothing;
}
function toggleWarning(skip: boolean) {
const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = fullClone;
if (
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED &&
typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object'
) {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint =
skip;
}
}
const MARK = '__rc_react_root__';
// ========================== Render ==========================
type ContainerType = (Element | DocumentFragment) & {
[MARK]?: Root;
};
function modernRender(node: React.ReactElement, container: ContainerType) {
toggleWarning(true);
const root = container[MARK] || createRoot(container);
toggleWarning(false);
root.render(node);
container[MARK] = root;
}
function legacyRender(node: React.ReactElement, container: ContainerType) {
reactRender(node, container);
}
/** @private Test usage. Not work in prod */
export function _r(node: React.ReactElement, container: ContainerType) {
if (process.env.NODE_ENV !== 'production') {
return legacyRender(node, container);
}
}
export function render(node: React.ReactElement, container: ContainerType) {
if (createRoot) {
modernRender(node, container);
return;
}
legacyRender(node, container);
}
// ========================= Unmount ==========================
async function modernUnmount(container: ContainerType) {
// Delay to unmount to avoid React 18 sync warning
return Promise.resolve().then(() => {
container[MARK]?.unmount();
delete container[MARK];
});
}
function legacyUnmount(container: ContainerType) {
unmountComponentAtNode(container);
}
/** @private Test usage. Not work in prod */
export function _u(container: ContainerType) {
if (process.env.NODE_ENV !== 'production') {
return legacyUnmount(container);
}
}
export async function unmount(container: ContainerType) {
if (createRoot !== undefined) {
// Delay to unmount to avoid React 18 sync warning
return modernUnmount(container);
}
legacyUnmount(container);
}