@umijs/renderer-react 4.0.0-rc.2 → 4.0.0-rc.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/appContext.d.ts +1 -0
- package/dist/appContext.js +6 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +91 -27
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/link.d.ts +5 -0
- package/dist/link.js +8 -0
- package/dist/routes.d.ts +2 -0
- package/dist/routes.js +10 -15
- package/dist/types.d.ts +4 -0
- package/package.json +8 -7
package/dist/appContext.d.ts
CHANGED
package/dist/appContext.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import { useRouteData } from './routeContext';
|
|
2
3
|
export const AppContext = React.createContext(null);
|
|
3
4
|
export function useAppData() {
|
|
4
5
|
return React.useContext(AppContext);
|
|
5
6
|
}
|
|
7
|
+
export function useClientLoaderData() {
|
|
8
|
+
const route = useRouteData();
|
|
9
|
+
const appData = useAppData();
|
|
10
|
+
return appData.clientLoaderData[route.route.id];
|
|
11
|
+
}
|
package/dist/browser.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { History } from 'history';
|
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import { IRouteComponents, IRoutesById } from './types';
|
|
4
4
|
export declare function renderClient(opts: {
|
|
5
|
+
publicPath?: string;
|
|
6
|
+
runtimePublicPath?: boolean;
|
|
5
7
|
rootElement?: HTMLElement;
|
|
6
8
|
routes: IRoutesById;
|
|
7
9
|
routeComponents: IRouteComponents;
|
package/dist/browser.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
|
|
3
|
-
import
|
|
1
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
// compatible with < react@18 in @umijs/preset-umi/src/features/react
|
|
3
|
+
import ReactDOM from 'react-dom/client';
|
|
4
|
+
import { matchRoutes, Router, useRoutes } from 'react-router-dom';
|
|
4
5
|
import { AppContext, useAppData } from './appContext';
|
|
5
6
|
import { createClientRoutes } from './routes';
|
|
6
7
|
function BrowserRoutes(props) {
|
|
@@ -10,18 +11,22 @@ function BrowserRoutes(props) {
|
|
|
10
11
|
location: history.location,
|
|
11
12
|
});
|
|
12
13
|
React.useLayoutEffect(() => history.listen(setState), [history]);
|
|
13
|
-
React.useLayoutEffect(() =>
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
React.useLayoutEffect(() => {
|
|
15
|
+
function onRouteChange(opts) {
|
|
16
|
+
props.pluginManager.applyPlugins({
|
|
17
|
+
key: 'onRouteChange',
|
|
18
|
+
type: 'event',
|
|
19
|
+
args: {
|
|
20
|
+
routes: props.routes,
|
|
21
|
+
clientRoutes: props.clientRoutes,
|
|
22
|
+
location: opts.location,
|
|
23
|
+
action: opts.action,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
history.listen(onRouteChange);
|
|
28
|
+
onRouteChange({ location: state.location, action: state.action });
|
|
29
|
+
}, [history, props.routes, props.clientRoutes]);
|
|
25
30
|
return (React.createElement(Router, { navigator: history, location: state.location, basename: props.basename }, props.children));
|
|
26
31
|
}
|
|
27
32
|
function Routes() {
|
|
@@ -36,6 +41,13 @@ export function renderClient(opts) {
|
|
|
36
41
|
routeComponents: opts.routeComponents,
|
|
37
42
|
loadingComponent: opts.loadingComponent,
|
|
38
43
|
});
|
|
44
|
+
opts.pluginManager.applyPlugins({
|
|
45
|
+
key: 'patchClientRoutes',
|
|
46
|
+
type: 'event',
|
|
47
|
+
args: {
|
|
48
|
+
routes: clientRoutes,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
39
51
|
let rootContainer = (React.createElement(BrowserRoutes, { basename: basename, pluginManager: opts.pluginManager, routes: opts.routes, clientRoutes: clientRoutes, history: opts.history },
|
|
40
52
|
React.createElement(Routes, null)));
|
|
41
53
|
for (const key of [
|
|
@@ -54,20 +66,72 @@ export function renderClient(opts) {
|
|
|
54
66
|
args: {},
|
|
55
67
|
});
|
|
56
68
|
}
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
const Browser = () => {
|
|
70
|
+
const [clientLoaderData, setClientLoaderData] = useState({});
|
|
71
|
+
const handleRouteChange = useCallback((p) => {
|
|
72
|
+
var _a;
|
|
73
|
+
// Patched routes has to id
|
|
74
|
+
const matchedRouteIds = (((_a = matchRoutes(clientRoutes, p)) === null || _a === void 0 ? void 0 : _a.map(
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
(route) => route.route.id)) || []).filter(Boolean);
|
|
77
|
+
matchedRouteIds.forEach((id) => {
|
|
78
|
+
// preload
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
const manifest = window.__umi_manifest__;
|
|
81
|
+
if (manifest) {
|
|
82
|
+
const routeIdReplaced = id.replace(/[\/\-]/g, '_');
|
|
83
|
+
const preloadId = 'preload-' + routeIdReplaced;
|
|
84
|
+
if (!document.getElementById(preloadId)) {
|
|
85
|
+
const key = Object.keys(manifest).find((k) => k.startsWith(routeIdReplaced + '.'));
|
|
86
|
+
if (!key)
|
|
87
|
+
return;
|
|
88
|
+
let file = manifest[key];
|
|
89
|
+
const link = document.createElement('link');
|
|
90
|
+
link.id = preloadId;
|
|
91
|
+
link.rel = 'preload';
|
|
92
|
+
link.as = 'script';
|
|
93
|
+
// publicPath already in the manifest,
|
|
94
|
+
// but if runtimePublicPath is true, we need to replace it
|
|
95
|
+
if (opts.runtimePublicPath) {
|
|
96
|
+
file = file.replace(new RegExp(`^${opts.publicPath}`),
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
window.publicPath);
|
|
99
|
+
}
|
|
100
|
+
link.href = file;
|
|
101
|
+
document.head.appendChild(link);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// client loader
|
|
105
|
+
const clientLoader = opts.routes[id].clientLoader;
|
|
106
|
+
if (clientLoader && !clientLoaderData[id]) {
|
|
107
|
+
clientLoader().then((data) => {
|
|
108
|
+
setClientLoaderData((d) => ({ ...d, [id]: data }));
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}, [clientLoaderData]);
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
handleRouteChange(window.location.pathname);
|
|
115
|
+
return opts.history.listen((e) => {
|
|
116
|
+
handleRouteChange(e.location.pathname);
|
|
117
|
+
});
|
|
118
|
+
}, []);
|
|
119
|
+
return (React.createElement(AppContext.Provider, { value: {
|
|
120
|
+
routes: opts.routes,
|
|
121
|
+
routeComponents: opts.routeComponents,
|
|
122
|
+
clientRoutes,
|
|
123
|
+
pluginManager: opts.pluginManager,
|
|
124
|
+
rootElement: opts.rootElement,
|
|
125
|
+
basename,
|
|
126
|
+
clientLoaderData,
|
|
127
|
+
preloadRoute: handleRouteChange,
|
|
128
|
+
} }, rootContainer));
|
|
129
|
+
};
|
|
66
130
|
if (ReactDOM.createRoot) {
|
|
67
|
-
|
|
68
|
-
ReactDOM.createRoot(rootElement).render(browser);
|
|
131
|
+
ReactDOM.createRoot(rootElement).render(React.createElement(Browser, null));
|
|
69
132
|
}
|
|
70
133
|
else {
|
|
71
|
-
|
|
134
|
+
// @ts-ignore
|
|
135
|
+
ReactDOM.render(React.createElement(Browser, null), rootElement);
|
|
72
136
|
}
|
|
73
137
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { createBrowserHistory, createHashHistory, createMemoryHistory, History, } from 'history';
|
|
2
|
-
export { createSearchParams,
|
|
3
|
-
export { useAppData } from './appContext';
|
|
2
|
+
export { createSearchParams, matchPath, matchRoutes, Navigate, NavLink, Outlet, resolvePath, useLocation, useMatch, useNavigate, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
|
|
3
|
+
export { useAppData, useClientLoaderData } from './appContext';
|
|
4
4
|
export { renderClient } from './browser';
|
|
5
|
+
export { LinkWithPrefetch as Link } from './link';
|
|
5
6
|
export { useRouteData } from './routeContext';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { createBrowserHistory, createHashHistory, createMemoryHistory, } from 'history';
|
|
2
|
-
export { createSearchParams,
|
|
3
|
-
export { useAppData } from './appContext';
|
|
2
|
+
export { createSearchParams, matchPath, matchRoutes, Navigate, NavLink, Outlet, resolvePath, useLocation, useMatch, useNavigate, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
|
|
3
|
+
export { useAppData, useClientLoaderData } from './appContext';
|
|
4
4
|
export { renderClient } from './browser';
|
|
5
|
+
export { LinkWithPrefetch as Link } from './link';
|
|
5
6
|
export { useRouteData } from './routeContext';
|
package/dist/link.d.ts
ADDED
package/dist/link.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Link } from 'react-router-dom';
|
|
3
|
+
import { useAppData } from './appContext';
|
|
4
|
+
export function LinkWithPrefetch(props) {
|
|
5
|
+
const appData = useAppData();
|
|
6
|
+
const to = typeof props.to === 'string' ? props.to : props.to.pathname;
|
|
7
|
+
return (React.createElement(Link, { onMouseEnter: () => props.prefetch && appData.preloadRoute(to), ...props }, props.children));
|
|
8
|
+
}
|
package/dist/routes.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare function createClientRoutes(opts: {
|
|
|
7
7
|
loadingComponent?: React.ReactNode;
|
|
8
8
|
}): {
|
|
9
9
|
parentId?: string | undefined;
|
|
10
|
+
clientLoader?: (() => Promise<any>) | undefined;
|
|
10
11
|
id: string;
|
|
11
12
|
path: string | undefined;
|
|
12
13
|
index: boolean | undefined;
|
|
@@ -18,6 +19,7 @@ export declare function createClientRoute(opts: {
|
|
|
18
19
|
loadingComponent?: React.ReactNode;
|
|
19
20
|
}): {
|
|
20
21
|
parentId?: string | undefined;
|
|
22
|
+
clientLoader?: (() => Promise<any>) | undefined;
|
|
21
23
|
id: string;
|
|
22
24
|
path: string | undefined;
|
|
23
25
|
index: boolean | undefined;
|
package/dist/routes.js
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
-
var t = {};
|
|
3
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
-
t[p] = s[p];
|
|
5
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
-
t[p[i]] = s[p[i]];
|
|
9
|
-
}
|
|
10
|
-
return t;
|
|
11
|
-
};
|
|
12
1
|
// @ts-ignore
|
|
13
2
|
import loadable from '@loadable/component';
|
|
14
3
|
import React from 'react';
|
|
@@ -43,14 +32,20 @@ export function createClientRoutes(opts) {
|
|
|
43
32
|
}
|
|
44
33
|
export function createClientRoute(opts) {
|
|
45
34
|
const { route } = opts;
|
|
46
|
-
const { id, path, index, redirect
|
|
47
|
-
return
|
|
35
|
+
const { id, path, index, redirect, ...props } = route;
|
|
36
|
+
return {
|
|
37
|
+
id: id,
|
|
38
|
+
path: path,
|
|
39
|
+
index: index,
|
|
40
|
+
element: redirect ? (React.createElement(Navigate, { to: redirect })) : (React.createElement(RouteContext.Provider, { value: {
|
|
48
41
|
route: opts.route,
|
|
49
42
|
} },
|
|
50
|
-
React.createElement(RemoteComponent, { loader: opts.routeComponent, loadingComponent: opts.loadingComponent || DefaultLoading })))
|
|
43
|
+
React.createElement(RemoteComponent, { loader: opts.routeComponent, loadingComponent: opts.loadingComponent || DefaultLoading }))),
|
|
44
|
+
...props,
|
|
45
|
+
};
|
|
51
46
|
}
|
|
52
47
|
function DefaultLoading() {
|
|
53
|
-
return React.createElement("div", null
|
|
48
|
+
return React.createElement("div", null);
|
|
54
49
|
}
|
|
55
50
|
function RemoteComponent(props) {
|
|
56
51
|
const Component = loadable(props.loader, {
|
package/dist/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface IRoute {
|
|
|
4
4
|
index?: boolean;
|
|
5
5
|
parentId?: string;
|
|
6
6
|
redirect?: string;
|
|
7
|
+
clientLoader?: () => Promise<any>;
|
|
7
8
|
}
|
|
8
9
|
export interface IRoutesById {
|
|
9
10
|
[id: string]: IRoute;
|
|
@@ -11,3 +12,6 @@ export interface IRoutesById {
|
|
|
11
12
|
export interface IRouteComponents {
|
|
12
13
|
[id: string]: any;
|
|
13
14
|
}
|
|
15
|
+
export interface ILoaderData {
|
|
16
|
+
[routeKey: string]: any;
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/renderer-react",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.20",
|
|
4
4
|
"description": "@umijs/renderer-react",
|
|
5
5
|
"homepage": "https://github.com/umijs/umi-next/tree/master/packages/renderer-react#readme",
|
|
6
6
|
"bugs": "https://github.com/umijs/umi-next/issues",
|
|
@@ -17,17 +17,18 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "pnpm tsc",
|
|
20
|
-
"build:deps": "
|
|
21
|
-
"dev": "pnpm build -- --watch"
|
|
20
|
+
"build:deps": "umi-scripts bundleDeps",
|
|
21
|
+
"dev": "pnpm build -- --watch",
|
|
22
|
+
"test": "umi-scripts jest-turbo"
|
|
22
23
|
},
|
|
23
24
|
"dependencies": {
|
|
24
25
|
"@loadable/component": "5.15.2",
|
|
25
|
-
"history": "5.
|
|
26
|
-
"react-router-dom": "6.
|
|
26
|
+
"history": "5.3.0",
|
|
27
|
+
"react-router-dom": "6.3.0"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"react": "
|
|
30
|
-
"react-dom": "
|
|
30
|
+
"react": "18.1.0",
|
|
31
|
+
"react-dom": "18.1.0"
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
|
33
34
|
"react": ">=16.8",
|