@umijs/renderer-react 4.0.0-beta.8 → 4.0.0-canary-20240513.3
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/LICENSE +21 -0
- package/dist/appContext.d.ts +24 -6
- package/dist/appContext.js +92 -3
- package/dist/browser.d.ts +86 -9
- package/dist/browser.js +289 -23
- package/dist/dataFetcher.d.ts +5 -0
- package/dist/dataFetcher.js +21 -0
- package/dist/html.d.ts +3 -0
- package/dist/html.js +171 -0
- package/dist/index.d.ts +10 -2
- package/dist/index.js +9 -2
- package/dist/link.d.ts +5 -0
- package/dist/link.js +22 -0
- package/dist/routeContext.d.ts +7 -0
- package/dist/routeContext.js +5 -0
- package/dist/routes.d.ts +6 -18
- package/dist/routes.js +102 -26
- package/dist/server.d.ts +2 -0
- package/dist/server.js +67 -0
- package/dist/types.d.ts +74 -1
- package/dist/types.js +1 -1
- package/dist/useFetcher.d.ts +3 -0
- package/dist/useFetcher.js +12 -0
- package/dist/withRouter.d.ts +17 -0
- package/dist/withRouter.js +41 -0
- package/package.json +29 -25
- package/dist/app.d.ts +0 -14
- package/dist/app.js +0 -35
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present ChenCheng (sorrycc@gmail.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/appContext.d.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import { IClientRoute, ILoaderData, IRouteComponents, IRoutesById, ISelectedRoutes } from './types';
|
|
3
|
+
interface IAppContextType {
|
|
4
|
+
routes: IRoutesById;
|
|
5
|
+
routeComponents: IRouteComponents;
|
|
6
|
+
clientRoutes: IClientRoute[];
|
|
6
7
|
pluginManager: any;
|
|
8
|
+
rootElement?: HTMLElement;
|
|
9
|
+
basename?: string;
|
|
10
|
+
clientLoaderData: ILoaderData;
|
|
11
|
+
preloadRoute?: (to: string) => void;
|
|
12
|
+
serverLoaderData: ILoaderData;
|
|
13
|
+
history?: any;
|
|
7
14
|
}
|
|
8
|
-
export declare const AppContext: React.Context<IAppContextType
|
|
9
|
-
export declare function
|
|
15
|
+
export declare const AppContext: React.Context<IAppContextType>;
|
|
16
|
+
export declare function useAppData(): IAppContextType;
|
|
17
|
+
export declare function useSelectedRoutes(): ISelectedRoutes[];
|
|
18
|
+
export declare function useRouteProps<T extends Record<string, any> = any>(): T;
|
|
19
|
+
declare type ServerLoaderFunc = (...args: any[]) => Promise<any> | any;
|
|
20
|
+
export declare function useServerLoaderData<T extends ServerLoaderFunc = any>(): {
|
|
21
|
+
data: Awaited<ReturnType<T>> | undefined;
|
|
22
|
+
};
|
|
23
|
+
export declare function useClientLoaderData(): {
|
|
24
|
+
data: any;
|
|
25
|
+
};
|
|
26
|
+
export declare function useLoaderData<T extends ServerLoaderFunc = any>(): Awaited<ReturnType<T>>;
|
|
27
|
+
export {};
|
package/dist/appContext.js
CHANGED
|
@@ -1,5 +1,94 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
+
var _excluded = ["element"];
|
|
1
5
|
import React from 'react';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
6
|
+
import { matchRoutes, useLocation } from 'react-router-dom';
|
|
7
|
+
import { fetchServerLoader } from "./dataFetcher";
|
|
8
|
+
import { useRouteData } from "./routeContext";
|
|
9
|
+
export var AppContext = /*#__PURE__*/React.createContext({});
|
|
10
|
+
export function useAppData() {
|
|
11
|
+
return React.useContext(AppContext);
|
|
5
12
|
}
|
|
13
|
+
export function useSelectedRoutes() {
|
|
14
|
+
var location = useLocation();
|
|
15
|
+
var _useAppData = useAppData(),
|
|
16
|
+
clientRoutes = _useAppData.clientRoutes;
|
|
17
|
+
// use `useLocation` get location without `basename`, not need `basename` param
|
|
18
|
+
var routes = matchRoutes(clientRoutes, location.pathname);
|
|
19
|
+
return routes || [];
|
|
20
|
+
}
|
|
21
|
+
export function useRouteProps() {
|
|
22
|
+
var _currentRoute$;
|
|
23
|
+
var currentRoute = useSelectedRoutes().slice(-1);
|
|
24
|
+
var _ref = ((_currentRoute$ = currentRoute[0]) === null || _currentRoute$ === void 0 ? void 0 : _currentRoute$.route) || {},
|
|
25
|
+
_ = _ref.element,
|
|
26
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
27
|
+
return props;
|
|
28
|
+
}
|
|
29
|
+
// @deprecated Please use `useLoaderData` instead.
|
|
30
|
+
export function useServerLoaderData() {
|
|
31
|
+
var routes = useSelectedRoutes();
|
|
32
|
+
var _useAppData2 = useAppData(),
|
|
33
|
+
serverLoaderData = _useAppData2.serverLoaderData,
|
|
34
|
+
basename = _useAppData2.basename;
|
|
35
|
+
var _React$useState = React.useState(function () {
|
|
36
|
+
var ret = {};
|
|
37
|
+
var has = false;
|
|
38
|
+
routes.forEach(function (route) {
|
|
39
|
+
// 多级路由嵌套时,需要合并多级路由 serverLoader 的数据
|
|
40
|
+
var routeData = serverLoaderData[route.route.id];
|
|
41
|
+
if (routeData) {
|
|
42
|
+
Object.assign(ret, routeData);
|
|
43
|
+
has = true;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return has ? ret : undefined;
|
|
47
|
+
}),
|
|
48
|
+
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
49
|
+
data = _React$useState2[0],
|
|
50
|
+
setData = _React$useState2[1];
|
|
51
|
+
React.useEffect(function () {
|
|
52
|
+
if (!window.__UMI_LOADER_DATA__) {
|
|
53
|
+
// 支持 ssr 降级,客户端兜底加载 serverLoader 数据
|
|
54
|
+
Promise.all(routes.filter(function (route) {
|
|
55
|
+
return route.route.hasServerLoader;
|
|
56
|
+
}).map(function (route) {
|
|
57
|
+
return new Promise(function (resolve) {
|
|
58
|
+
fetchServerLoader({
|
|
59
|
+
id: route.route.id,
|
|
60
|
+
basename: basename,
|
|
61
|
+
cb: resolve
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
})).then(function (datas) {
|
|
65
|
+
if (datas.length) {
|
|
66
|
+
var res = {};
|
|
67
|
+
datas.forEach(function (data) {
|
|
68
|
+
Object.assign(res, data);
|
|
69
|
+
});
|
|
70
|
+
setData(res);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}, []);
|
|
75
|
+
return {
|
|
76
|
+
data: data
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// @deprecated Please use `useLoaderData` instead.
|
|
81
|
+
export function useClientLoaderData() {
|
|
82
|
+
var route = useRouteData();
|
|
83
|
+
var appData = useAppData();
|
|
84
|
+
return {
|
|
85
|
+
data: appData.clientLoaderData[route.route.id]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function useLoaderData() {
|
|
89
|
+
var serverLoaderData = useServerLoaderData();
|
|
90
|
+
var clientLoaderData = useClientLoaderData();
|
|
91
|
+
return {
|
|
92
|
+
data: _objectSpread(_objectSpread({}, serverLoaderData.data), clientLoaderData.data)
|
|
93
|
+
};
|
|
94
|
+
}
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,12 +1,89 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { History } from 'history';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import ReactDOM from 'react-dom/client';
|
|
4
|
+
import { IRouteComponents, IRoutesById } from './types';
|
|
5
|
+
export declare function __getRoot(): ReactDOM.Root | null;
|
|
6
|
+
export declare function Routes(): React.ReactElement<any, string | React.JSXElementConstructor<any>> | null;
|
|
7
|
+
/**
|
|
8
|
+
* umi 渲染需要的配置,在node端调用的哦
|
|
9
|
+
*/
|
|
10
|
+
export declare type RenderClientOpts = {
|
|
11
|
+
/**
|
|
12
|
+
* 配置 webpack 的 publicPath。
|
|
13
|
+
* @doc https://umijs.org/docs/api/config#publicpath
|
|
14
|
+
*/
|
|
15
|
+
publicPath?: string;
|
|
16
|
+
/**
|
|
17
|
+
* 是否是 runtimePublicPath
|
|
18
|
+
* @doc https://umijs.org/docs/api/config#runtimepublicpath
|
|
19
|
+
*/
|
|
20
|
+
runtimePublicPath?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* react dom 渲染的的目标节点 id
|
|
23
|
+
* @doc 一般不需要改,微前端的时候会变化
|
|
24
|
+
*/
|
|
25
|
+
mountElementId?: string;
|
|
26
|
+
/**
|
|
27
|
+
* react dom 渲染的的目标 dom
|
|
28
|
+
* @doc 一般不需要改,微前端的时候会变化
|
|
29
|
+
*/
|
|
8
30
|
rootElement?: HTMLElement;
|
|
31
|
+
/**
|
|
32
|
+
* 是否从根节点开始渲染, 默认 false, 即从 html 开始渲染
|
|
33
|
+
*/
|
|
34
|
+
renderFromRoot?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 内部流程, 渲染特殊 html 节点, 不要使用!!!
|
|
37
|
+
*/
|
|
38
|
+
__SPECIAL_HTML_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* 当前的路由配置
|
|
41
|
+
*/
|
|
9
42
|
routes: IRoutesById;
|
|
10
|
-
|
|
43
|
+
/**
|
|
44
|
+
* 当前的路由对应的dom组件
|
|
45
|
+
*/
|
|
46
|
+
routeComponents: IRouteComponents;
|
|
47
|
+
/**
|
|
48
|
+
* 插件的执行实例
|
|
49
|
+
*/
|
|
11
50
|
pluginManager: any;
|
|
12
|
-
|
|
51
|
+
/**
|
|
52
|
+
* 设置路由 base,部署项目到非根目录下时使用。
|
|
53
|
+
* @doc https://umijs.org/docs/api/config#base
|
|
54
|
+
*/
|
|
55
|
+
basename?: string;
|
|
56
|
+
/**
|
|
57
|
+
* loading 中展示的组件 dom
|
|
58
|
+
*/
|
|
59
|
+
loadingComponent?: React.ReactNode;
|
|
60
|
+
/**
|
|
61
|
+
* react router 的 history,用于控制列表渲染
|
|
62
|
+
* @doc https://umijs.org/docs/api/config#history
|
|
63
|
+
* 有多种不同的类型,测试的时候建议用 内存路由,默认是 browserHistory
|
|
64
|
+
*/
|
|
65
|
+
history: History;
|
|
66
|
+
/**
|
|
67
|
+
* ssr 的配置
|
|
68
|
+
*/
|
|
69
|
+
hydrate?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* 直接返回组件,是为了方便测试
|
|
72
|
+
*/
|
|
73
|
+
components?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* 启用 react-router 5 兼容模式。
|
|
76
|
+
* 此模式下,路由组件的 props 会包含 location、match、history 和 params 属性,和 react-router 5 的保持一致。
|
|
77
|
+
*/
|
|
78
|
+
reactRouter5Compat?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* 应用渲染完成的回调函数
|
|
81
|
+
*/
|
|
82
|
+
callback?: () => void;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* 执行 react dom 的 render 方法
|
|
86
|
+
* @param {RenderClientOpts} opts - 插件相关的配置
|
|
87
|
+
* @returns void
|
|
88
|
+
*/
|
|
89
|
+
export declare function renderClient(opts: RenderClientOpts): (() => JSX.Element) | undefined;
|
package/dist/browser.js
CHANGED
|
@@ -1,27 +1,293 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
4
|
+
import React, { useCallback, useEffect, useLayoutEffect, useState } from 'react';
|
|
5
|
+
// compatible with < react@18 in @umijs/preset-umi/src/features/react
|
|
6
|
+
import ReactDOM from 'react-dom/client';
|
|
7
|
+
import { matchRoutes, Router, useRoutes } from 'react-router-dom';
|
|
8
|
+
import { AppContext, useAppData } from "./appContext";
|
|
9
|
+
import { fetchServerLoader } from "./dataFetcher";
|
|
10
|
+
import { Html } from "./html";
|
|
11
|
+
import { createClientRoutes } from "./routes";
|
|
12
|
+
var root = null;
|
|
13
|
+
|
|
14
|
+
// react 18 some scenarios need unmount such as micro app
|
|
15
|
+
export function __getRoot() {
|
|
16
|
+
return root;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 这个组件的功能是 history 发生改变的时候重新触发渲染
|
|
21
|
+
* @param props
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
function BrowserRoutes(props) {
|
|
25
|
+
var history = props.history;
|
|
26
|
+
var _React$useState = React.useState({
|
|
27
|
+
action: history.action,
|
|
28
|
+
location: history.location
|
|
29
|
+
}),
|
|
30
|
+
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
31
|
+
state = _React$useState2[0],
|
|
32
|
+
setState = _React$useState2[1];
|
|
33
|
+
useLayoutEffect(function () {
|
|
34
|
+
return history.listen(setState);
|
|
35
|
+
}, [history]);
|
|
36
|
+
useLayoutEffect(function () {
|
|
37
|
+
function onRouteChange(opts) {
|
|
38
|
+
props.pluginManager.applyPlugins({
|
|
39
|
+
key: 'onRouteChange',
|
|
40
|
+
type: 'event',
|
|
41
|
+
args: {
|
|
42
|
+
routes: props.routes,
|
|
43
|
+
clientRoutes: props.clientRoutes,
|
|
44
|
+
location: opts.location,
|
|
45
|
+
action: opts.action,
|
|
46
|
+
basename: props.basename,
|
|
47
|
+
isFirst: Boolean(opts.isFirst)
|
|
48
|
+
}
|
|
49
|
+
});
|
|
9
50
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
});
|
|
15
|
-
React.useLayoutEffect(() => history.listen(dispatch), [history]);
|
|
16
|
-
return props.pluginManager.applyPlugins({
|
|
17
|
-
type: 'modify',
|
|
18
|
-
key: 'rootContainer',
|
|
19
|
-
initialValue: (React.createElement(App, { navigator: history, location: state.location, routes: props.routes, routeComponents: props.routeComponents, pluginManager: props.pluginManager })),
|
|
20
|
-
args: {},
|
|
51
|
+
onRouteChange({
|
|
52
|
+
location: state.location,
|
|
53
|
+
action: state.action,
|
|
54
|
+
isFirst: true
|
|
21
55
|
});
|
|
56
|
+
return history.listen(onRouteChange);
|
|
57
|
+
}, [history, props.routes, props.clientRoutes]);
|
|
58
|
+
return /*#__PURE__*/React.createElement(Router, {
|
|
59
|
+
navigator: history,
|
|
60
|
+
location: state.location,
|
|
61
|
+
basename: props.basename
|
|
62
|
+
}, props.children);
|
|
22
63
|
}
|
|
23
|
-
export function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
64
|
+
export function Routes() {
|
|
65
|
+
var _useAppData = useAppData(),
|
|
66
|
+
clientRoutes = _useAppData.clientRoutes;
|
|
67
|
+
return useRoutes(clientRoutes);
|
|
27
68
|
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* umi 渲染需要的配置,在node端调用的哦
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* umi max 所需要的所有插件列表,用于获取provide
|
|
76
|
+
*/
|
|
77
|
+
var UMI_CLIENT_RENDER_REACT_PLUGIN_LIST = [
|
|
78
|
+
// Lowest to the highest priority
|
|
79
|
+
'innerProvider', 'i18nProvider', 'accessProvider', 'dataflowProvider', 'outerProvider', 'rootContainer'];
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* @param {RenderClientOpts} opts - 插件相关的配置
|
|
84
|
+
* @param {React.ReactElement} routesElement 需要渲染的 routers,为了方便测试注入才导出
|
|
85
|
+
* @returns @returns A function that returns a React component.
|
|
86
|
+
*/
|
|
87
|
+
var getBrowser = function getBrowser(opts, routesElement) {
|
|
88
|
+
var basename = opts.basename || '/';
|
|
89
|
+
var clientRoutes = createClientRoutes({
|
|
90
|
+
routesById: opts.routes,
|
|
91
|
+
routeComponents: opts.routeComponents,
|
|
92
|
+
loadingComponent: opts.loadingComponent,
|
|
93
|
+
reactRouter5Compat: opts.reactRouter5Compat
|
|
94
|
+
});
|
|
95
|
+
opts.pluginManager.applyPlugins({
|
|
96
|
+
key: 'patchClientRoutes',
|
|
97
|
+
type: 'event',
|
|
98
|
+
args: {
|
|
99
|
+
routes: clientRoutes
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
var rootContainer = /*#__PURE__*/React.createElement(BrowserRoutes, {
|
|
103
|
+
basename: basename,
|
|
104
|
+
pluginManager: opts.pluginManager,
|
|
105
|
+
routes: opts.routes,
|
|
106
|
+
clientRoutes: clientRoutes,
|
|
107
|
+
history: opts.history
|
|
108
|
+
}, routesElement);
|
|
109
|
+
|
|
110
|
+
// 加载所有需要的插件
|
|
111
|
+
for (var _i = 0, _UMI_CLIENT_RENDER_RE = UMI_CLIENT_RENDER_REACT_PLUGIN_LIST; _i < _UMI_CLIENT_RENDER_RE.length; _i++) {
|
|
112
|
+
var key = _UMI_CLIENT_RENDER_RE[_i];
|
|
113
|
+
rootContainer = opts.pluginManager.applyPlugins({
|
|
114
|
+
type: 'modify',
|
|
115
|
+
key: key,
|
|
116
|
+
initialValue: rootContainer,
|
|
117
|
+
args: {
|
|
118
|
+
routes: opts.routes,
|
|
119
|
+
history: opts.history,
|
|
120
|
+
plugin: opts.pluginManager
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* umi 增加完 Provide 的 react dom,可以直接交给 react-dom 渲染
|
|
127
|
+
* @returns {React.ReactElement}
|
|
128
|
+
*/
|
|
129
|
+
var Browser = function Browser() {
|
|
130
|
+
var _useState = useState({}),
|
|
131
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
132
|
+
clientLoaderData = _useState2[0],
|
|
133
|
+
setClientLoaderData = _useState2[1];
|
|
134
|
+
var _useState3 = useState(window.__UMI_LOADER_DATA__ || {}),
|
|
135
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
136
|
+
serverLoaderData = _useState4[0],
|
|
137
|
+
setServerLoaderData = _useState4[1];
|
|
138
|
+
var handleRouteChange = useCallback(function (id, isFirst) {
|
|
139
|
+
var _matchRoutes;
|
|
140
|
+
// Patched routes has to id
|
|
141
|
+
var matchedRouteIds = (((_matchRoutes = matchRoutes(clientRoutes, id, basename)) === null || _matchRoutes === void 0 ? void 0 : _matchRoutes.map(
|
|
142
|
+
// @ts-ignore
|
|
143
|
+
function (route) {
|
|
144
|
+
return route.route.id;
|
|
145
|
+
})) || []).filter(Boolean);
|
|
146
|
+
matchedRouteIds.forEach(function (id) {
|
|
147
|
+
var _opts$routes$id, _opts$routes$id2;
|
|
148
|
+
// preload
|
|
149
|
+
// @ts-ignore
|
|
150
|
+
var manifest = window.__umi_manifest__;
|
|
151
|
+
if (manifest) {
|
|
152
|
+
var routeIdReplaced = id.replace(/[\/\-]/g, '_');
|
|
153
|
+
var preloadId = "preload-".concat(routeIdReplaced, ".js");
|
|
154
|
+
if (!document.getElementById(preloadId)) {
|
|
155
|
+
var keys = Object.keys(manifest).filter(function (k) {
|
|
156
|
+
return k.startsWith(routeIdReplaced + '.');
|
|
157
|
+
});
|
|
158
|
+
keys.forEach(function (key) {
|
|
159
|
+
if (!/\.(js|css)$/.test(key)) {
|
|
160
|
+
throw Error("preload not support ".concat(key, " file"));
|
|
161
|
+
}
|
|
162
|
+
var file = manifest[key];
|
|
163
|
+
var link = document.createElement('link');
|
|
164
|
+
link.rel = 'preload';
|
|
165
|
+
link.as = 'style';
|
|
166
|
+
if (key.endsWith('.js')) {
|
|
167
|
+
link.as = 'script';
|
|
168
|
+
link.id = preloadId;
|
|
169
|
+
}
|
|
170
|
+
// publicPath already in the manifest,
|
|
171
|
+
// but if runtimePublicPath is true, we need to replace it
|
|
172
|
+
if (opts.runtimePublicPath) {
|
|
173
|
+
file = file.replace(new RegExp("^".concat(opts.publicPath)),
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
window.publicPath);
|
|
176
|
+
}
|
|
177
|
+
link.href = file;
|
|
178
|
+
document.head.appendChild(link);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
var clientLoader = (_opts$routes$id = opts.routes[id]) === null || _opts$routes$id === void 0 ? void 0 : _opts$routes$id.clientLoader;
|
|
183
|
+
var hasClientLoader = !!clientLoader;
|
|
184
|
+
var hasServerLoader = (_opts$routes$id2 = opts.routes[id]) === null || _opts$routes$id2 === void 0 ? void 0 : _opts$routes$id2.hasServerLoader;
|
|
185
|
+
// server loader
|
|
186
|
+
// use ?. since routes patched with patchClientRoutes is not exists in opts.routes
|
|
187
|
+
|
|
188
|
+
if (!isFirst && hasServerLoader && !hasClientLoader && !window.__UMI_LOADER_DATA__) {
|
|
189
|
+
fetchServerLoader({
|
|
190
|
+
id: id,
|
|
191
|
+
basename: basename,
|
|
192
|
+
cb: function cb(data) {
|
|
193
|
+
// setServerLoaderData when startTransition because if ssr is enabled,
|
|
194
|
+
// the component may being hydrated and setLoaderData will break the hydration
|
|
195
|
+
React.startTransition(function () {
|
|
196
|
+
setServerLoaderData(function (d) {
|
|
197
|
+
return _objectSpread(_objectSpread({}, d), {}, _defineProperty({}, id, data));
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
// client loader
|
|
204
|
+
// onPatchClientRoutes 添加的 route 在 opts.routes 里是不存在的
|
|
205
|
+
var hasClientLoaderDataInRoute = !!clientLoaderData[id];
|
|
206
|
+
|
|
207
|
+
// Check if hydration is needed or there's no server loader for the current route
|
|
208
|
+
var shouldHydrateOrNoServerLoader = hasClientLoader && clientLoader.hydrate || !hasServerLoader;
|
|
209
|
+
|
|
210
|
+
// Check if server loader data is missing in the global window object
|
|
211
|
+
var isServerLoaderDataMissing = hasServerLoader && !window.__UMI_LOADER_DATA__;
|
|
212
|
+
if (hasClientLoader && !hasClientLoaderDataInRoute && (shouldHydrateOrNoServerLoader || isServerLoaderDataMissing)) {
|
|
213
|
+
// ...
|
|
214
|
+
clientLoader({
|
|
215
|
+
serverLoader: function serverLoader() {
|
|
216
|
+
return fetchServerLoader({
|
|
217
|
+
id: id,
|
|
218
|
+
basename: basename,
|
|
219
|
+
cb: function cb(data) {
|
|
220
|
+
// setServerLoaderData when startTransition because if ssr is enabled,
|
|
221
|
+
// the component may being hydrated and setLoaderData will break the hydration
|
|
222
|
+
React.startTransition(function () {
|
|
223
|
+
setServerLoaderData(function (d) {
|
|
224
|
+
return _objectSpread(_objectSpread({}, d), {}, _defineProperty({}, id, data));
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}).then(function (data) {
|
|
231
|
+
setClientLoaderData(function (d) {
|
|
232
|
+
return _objectSpread(_objectSpread({}, d), {}, _defineProperty({}, id, data));
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}, [clientLoaderData]);
|
|
238
|
+
useEffect(function () {
|
|
239
|
+
handleRouteChange(window.location.pathname, true);
|
|
240
|
+
return opts.history.listen(function (e) {
|
|
241
|
+
handleRouteChange(e.location.pathname);
|
|
242
|
+
});
|
|
243
|
+
}, []);
|
|
244
|
+
useLayoutEffect(function () {
|
|
245
|
+
if (typeof opts.callback === 'function') opts.callback();
|
|
246
|
+
}, []);
|
|
247
|
+
return /*#__PURE__*/React.createElement(AppContext.Provider, {
|
|
248
|
+
value: {
|
|
249
|
+
routes: opts.routes,
|
|
250
|
+
routeComponents: opts.routeComponents,
|
|
251
|
+
clientRoutes: clientRoutes,
|
|
252
|
+
pluginManager: opts.pluginManager,
|
|
253
|
+
rootElement: opts.rootElement,
|
|
254
|
+
basename: basename,
|
|
255
|
+
clientLoaderData: clientLoaderData,
|
|
256
|
+
serverLoaderData: serverLoaderData,
|
|
257
|
+
preloadRoute: handleRouteChange,
|
|
258
|
+
history: opts.history
|
|
259
|
+
}
|
|
260
|
+
}, rootContainer);
|
|
261
|
+
};
|
|
262
|
+
return Browser;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* 执行 react dom 的 render 方法
|
|
267
|
+
* @param {RenderClientOpts} opts - 插件相关的配置
|
|
268
|
+
* @returns void
|
|
269
|
+
*/
|
|
270
|
+
export function renderClient(opts) {
|
|
271
|
+
var rootElement = opts.rootElement || document.getElementById('root');
|
|
272
|
+
var Browser = getBrowser(opts, /*#__PURE__*/React.createElement(Routes, null));
|
|
273
|
+
// 为了测试,直接返回组件
|
|
274
|
+
if (opts.components) return Browser;
|
|
275
|
+
if (opts.hydrate) {
|
|
276
|
+
var loaderData = window.__UMI_LOADER_DATA__ || {};
|
|
277
|
+
var metadata = window.__UMI_METADATA_LOADER_DATA__ || {};
|
|
278
|
+
var hydtateHtmloptions = {
|
|
279
|
+
metadata: metadata,
|
|
280
|
+
loaderData: loaderData,
|
|
281
|
+
mountElementId: opts.mountElementId
|
|
282
|
+
};
|
|
283
|
+
ReactDOM.hydrateRoot(opts.renderFromRoot ? rootElement : document, /*#__PURE__*/React.createElement(Html, hydtateHtmloptions, /*#__PURE__*/React.createElement(Browser, null)));
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (ReactDOM.createRoot) {
|
|
287
|
+
root = ReactDOM.createRoot(rootElement);
|
|
288
|
+
root.render( /*#__PURE__*/React.createElement(Browser, null));
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
// @ts-ignore
|
|
292
|
+
ReactDOM.render( /*#__PURE__*/React.createElement(Browser, null), rootElement);
|
|
293
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function fetchServerLoader(_ref) {
|
|
2
|
+
var id = _ref.id,
|
|
3
|
+
basename = _ref.basename,
|
|
4
|
+
cb = _ref.cb;
|
|
5
|
+
var query = new URLSearchParams({
|
|
6
|
+
route: id,
|
|
7
|
+
url: window.location.href
|
|
8
|
+
}).toString();
|
|
9
|
+
// 在有basename的情况下__serverLoader的请求路径需要加上basename
|
|
10
|
+
// FIXME: 先临时解自定义 serverLoader 请求路径的问题,后续改造 serverLoader 时再提取成类似 runtimeServerLoader 的配置项
|
|
11
|
+
var url = "".concat(withEndSlash(window.umiServerLoaderPath || basename), "__serverLoader?").concat(query);
|
|
12
|
+
fetch(url, {
|
|
13
|
+
credentials: 'include'
|
|
14
|
+
}).then(function (d) {
|
|
15
|
+
return d.json();
|
|
16
|
+
}).then(cb).catch(console.error);
|
|
17
|
+
}
|
|
18
|
+
function withEndSlash() {
|
|
19
|
+
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
20
|
+
return str.endsWith('/') ? str : "".concat(str, "/");
|
|
21
|
+
}
|
package/dist/html.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { IHtmlProps } from './types';
|
|
3
|
+
export declare function Html({ children, loaderData, manifest, htmlPageOpts, renderFromRoot, __SPECIAL_HTML_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, mountElementId, }: React.PropsWithChildren<IHtmlProps>): JSX.Element;
|