@umijs/renderer-react 4.0.7 → 4.0.10

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.
@@ -1,16 +1,20 @@
1
1
  import React from 'react';
2
- import { useRouteData } from './routeContext';
3
- export const AppContext = React.createContext({});
2
+ import { useRouteData } from "./routeContext";
3
+ export var AppContext = /*#__PURE__*/React.createContext({});
4
4
  export function useAppData() {
5
- return React.useContext(AppContext);
5
+ return React.useContext(AppContext);
6
6
  }
7
7
  export function useServerLoaderData() {
8
- const route = useRouteData();
9
- const appData = useAppData();
10
- return { data: appData.serverLoaderData[route.route.id] };
8
+ var route = useRouteData();
9
+ var appData = useAppData();
10
+ return {
11
+ data: appData.serverLoaderData[route.route.id]
12
+ };
11
13
  }
12
14
  export function useClientLoaderData() {
13
- const route = useRouteData();
14
- const appData = useAppData();
15
- return { data: appData.clientLoaderData[route.route.id] };
16
- }
15
+ var route = useRouteData();
16
+ var appData = useAppData();
17
+ return {
18
+ data: appData.clientLoaderData[route.route.id]
19
+ };
20
+ }
package/dist/browser.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { History } from 'history';
2
2
  import React from 'react';
3
+ import ReactDOM from 'react-dom/client';
3
4
  import { IRouteComponents, IRoutesById } from './types';
5
+ export declare function __getRoot(): ReactDOM.Root | null;
4
6
  export declare function Routes(): React.ReactElement<any, string | React.JSXElementConstructor<any>> | null;
5
7
  export declare function renderClient(opts: {
6
8
  publicPath?: string;
package/dist/browser.js CHANGED
@@ -1,167 +1,226 @@
1
- import React, { useCallback, useEffect, useState } from 'react';
2
- // compatible with < react@18 in @umijs/preset-umi/src/features/react
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
3
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
4
+ import React, { useCallback, useEffect, useState } from 'react'; // compatible with < react@18 in @umijs/preset-umi/src/features/react
5
+
3
6
  import ReactDOM from 'react-dom/client';
4
7
  import { matchRoutes, Router, useRoutes } from 'react-router-dom';
5
- import { AppContext, useAppData } from './appContext';
6
- import { createClientRoutes } from './routes';
8
+ import { AppContext, useAppData } from "./appContext";
9
+ import { createClientRoutes } from "./routes";
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+ var root = null; // react 18 some scenarios need unmount such as micro app
12
+
13
+ export function __getRoot() {
14
+ return root;
15
+ }
16
+
7
17
  function BrowserRoutes(props) {
8
- const { history } = props;
9
- const [state, setState] = React.useState({
10
- action: history.action,
11
- location: history.location,
12
- });
13
- React.useLayoutEffect(() => history.listen(setState), [history]);
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
- });
18
+ var history = props.history;
19
+
20
+ var _React$useState = React.useState({
21
+ action: history.action,
22
+ location: history.location
23
+ }),
24
+ _React$useState2 = _slicedToArray(_React$useState, 2),
25
+ state = _React$useState2[0],
26
+ setState = _React$useState2[1];
27
+
28
+ React.useLayoutEffect(function () {
29
+ return history.listen(setState);
30
+ }, [history]);
31
+ React.useLayoutEffect(function () {
32
+ function onRouteChange(opts) {
33
+ props.pluginManager.applyPlugins({
34
+ key: 'onRouteChange',
35
+ type: 'event',
36
+ args: {
37
+ routes: props.routes,
38
+ clientRoutes: props.clientRoutes,
39
+ location: opts.location,
40
+ action: opts.action
26
41
  }
27
- history.listen(onRouteChange);
28
- onRouteChange({ location: state.location, action: state.action });
29
- }, [history, props.routes, props.clientRoutes]);
30
- return (React.createElement(Router, { navigator: history, location: state.location, basename: props.basename }, props.children));
42
+ });
43
+ }
44
+
45
+ history.listen(onRouteChange);
46
+ onRouteChange({
47
+ location: state.location,
48
+ action: state.action
49
+ });
50
+ }, [history, props.routes, props.clientRoutes]);
51
+ return /*#__PURE__*/_jsx(Router, {
52
+ navigator: history,
53
+ location: state.location,
54
+ basename: props.basename,
55
+ children: props.children
56
+ });
31
57
  }
58
+
32
59
  export function Routes() {
33
- const { clientRoutes } = useAppData();
34
- return useRoutes(clientRoutes);
60
+ var _useAppData = useAppData(),
61
+ clientRoutes = _useAppData.clientRoutes;
62
+
63
+ return useRoutes(clientRoutes);
35
64
  }
36
65
  export function renderClient(opts) {
37
- const basename = opts.basename || '/';
38
- const rootElement = opts.rootElement || document.getElementById('root');
39
- const clientRoutes = createClientRoutes({
40
- routesById: opts.routes,
41
- routeComponents: opts.routeComponents,
42
- loadingComponent: opts.loadingComponent,
43
- });
44
- opts.pluginManager.applyPlugins({
45
- key: 'patchClientRoutes',
46
- type: 'event',
47
- args: {
48
- routes: clientRoutes,
49
- },
50
- });
51
- let rootContainer = (React.createElement(BrowserRoutes, { basename: basename, pluginManager: opts.pluginManager, routes: opts.routes, clientRoutes: clientRoutes, history: opts.history },
52
- React.createElement(Routes, null)));
53
- for (const key of [
54
- // Lowest to the highest priority
55
- 'innerProvider',
56
- 'i18nProvider',
57
- 'accessProvider',
58
- 'dataflowProvider',
59
- 'outerProvider',
60
- 'rootContainer',
61
- ]) {
62
- rootContainer = opts.pluginManager.applyPlugins({
63
- type: 'modify',
64
- key: key,
65
- initialValue: rootContainer,
66
- args: {},
67
- });
66
+ var basename = opts.basename || '/';
67
+ var rootElement = opts.rootElement || document.getElementById('root');
68
+ var clientRoutes = createClientRoutes({
69
+ routesById: opts.routes,
70
+ routeComponents: opts.routeComponents,
71
+ loadingComponent: opts.loadingComponent
72
+ });
73
+ opts.pluginManager.applyPlugins({
74
+ key: 'patchClientRoutes',
75
+ type: 'event',
76
+ args: {
77
+ routes: clientRoutes
68
78
  }
69
- const Browser = () => {
70
- const [clientLoaderData, setClientLoaderData] = useState({});
71
- const [serverLoaderData, setServerLoaderData] = useState(
79
+ });
80
+
81
+ var rootContainer = /*#__PURE__*/_jsx(BrowserRoutes, {
82
+ basename: basename,
83
+ pluginManager: opts.pluginManager,
84
+ routes: opts.routes,
85
+ clientRoutes: clientRoutes,
86
+ history: opts.history,
87
+ children: /*#__PURE__*/_jsx(Routes, {})
88
+ });
89
+
90
+ for (var _i = 0, _arr = [// Lowest to the highest priority
91
+ 'innerProvider', 'i18nProvider', 'accessProvider', 'dataflowProvider', 'outerProvider', 'rootContainer']; _i < _arr.length; _i++) {
92
+ var key = _arr[_i];
93
+ rootContainer = opts.pluginManager.applyPlugins({
94
+ type: 'modify',
95
+ key: key,
96
+ initialValue: rootContainer,
97
+ args: {}
98
+ });
99
+ }
100
+
101
+ var Browser = function Browser() {
102
+ var _useState = useState({}),
103
+ _useState2 = _slicedToArray(_useState, 2),
104
+ clientLoaderData = _useState2[0],
105
+ setClientLoaderData = _useState2[1];
106
+
107
+ var _useState3 = useState( // @ts-ignore
108
+ window.__UMI_LOADER_DATA__ || {}),
109
+ _useState4 = _slicedToArray(_useState3, 2),
110
+ serverLoaderData = _useState4[0],
111
+ setServerLoaderData = _useState4[1];
112
+
113
+ var handleRouteChange = useCallback(function (id, isFirst) {
114
+ var _matchRoutes;
115
+
116
+ // Patched routes has to id
117
+ var matchedRouteIds = (((_matchRoutes = matchRoutes(clientRoutes, id)) === null || _matchRoutes === void 0 ? void 0 : _matchRoutes.map( // @ts-ignore
118
+ function (route) {
119
+ return route.route.id;
120
+ })) || []).filter(Boolean);
121
+ matchedRouteIds.forEach(function (id) {
122
+ var _opts$routes$id, _opts$routes$id2;
123
+
124
+ // preload
72
125
  // @ts-ignore
73
- window.__UMI_LOADER_DATA__ || {});
74
- const handleRouteChange = useCallback((id, isFirst) => {
75
- var _a;
76
- // Patched routes has to id
77
- const matchedRouteIds = (((_a = matchRoutes(clientRoutes, id)) === null || _a === void 0 ? void 0 : _a.map(
78
- // @ts-ignore
79
- (route) => route.route.id)) || []).filter(Boolean);
80
- matchedRouteIds.forEach((id) => {
81
- var _a;
82
- // preload
83
- // @ts-ignore
84
- const manifest = window.__umi_manifest__;
85
- if (manifest) {
86
- const routeIdReplaced = id.replace(/[\/\-]/g, '_');
87
- const preloadId = `preload-${routeIdReplaced}.js`;
88
- if (!document.getElementById(preloadId)) {
89
- const keys = Object.keys(manifest).filter((k) => k.startsWith(routeIdReplaced + '.'));
90
- keys.forEach((key) => {
91
- if (!/\.(js|css)$/.test(key)) {
92
- throw Error(`preload not support ${key} file`);
93
- }
94
- let file = manifest[key];
95
- const link = document.createElement('link');
96
- link.rel = 'preload';
97
- link.as = 'style';
98
- if (key.endsWith('.js')) {
99
- link.as = 'script';
100
- link.id = preloadId;
101
- }
102
- // publicPath already in the manifest,
103
- // but if runtimePublicPath is true, we need to replace it
104
- if (opts.runtimePublicPath) {
105
- file = file.replace(new RegExp(`^${opts.publicPath}`),
106
- // @ts-ignore
107
- window.publicPath);
108
- }
109
- link.href = file;
110
- document.head.appendChild(link);
111
- });
112
- }
113
- }
114
- // server loader
115
- if (!isFirst && opts.routes[id].hasServerLoader) {
116
- fetch('/__serverLoader?route=' + id)
117
- .then((d) => d.json())
118
- .then((data) => {
119
- // setServerLoaderData when startTransition because if ssr is enabled,
120
- // the component may being hydrated and setLoaderData will break the hydration
121
- React.startTransition(() => {
122
- setServerLoaderData((d) => ({ ...d, [id]: data }));
123
- });
124
- })
125
- .catch(console.error);
126
- }
127
- // client loader
128
- // onPatchClientRoutes 添加的 route 在 opts.routes 里是不存在的
129
- const clientLoader = (_a = opts.routes[id]) === null || _a === void 0 ? void 0 : _a.clientLoader;
130
- if (clientLoader && !clientLoaderData[id]) {
131
- clientLoader().then((data) => {
132
- setClientLoaderData((d) => ({ ...d, [id]: data }));
133
- });
134
- }
126
+ var manifest = window.__umi_manifest__;
127
+
128
+ if (manifest) {
129
+ var routeIdReplaced = id.replace(/[\/\-]/g, '_');
130
+ var preloadId = "preload-".concat(routeIdReplaced, ".js");
131
+
132
+ if (!document.getElementById(preloadId)) {
133
+ var keys = Object.keys(manifest).filter(function (k) {
134
+ return k.startsWith(routeIdReplaced + '.');
135
135
  });
136
- }, [clientLoaderData]);
137
- useEffect(() => {
138
- handleRouteChange(window.location.pathname, true);
139
- return opts.history.listen((e) => {
140
- handleRouteChange(e.location.pathname);
136
+ keys.forEach(function (key) {
137
+ if (!/\.(js|css)$/.test(key)) {
138
+ throw Error("preload not support ".concat(key, " file"));
139
+ }
140
+
141
+ var file = manifest[key];
142
+ var link = document.createElement('link');
143
+ link.rel = 'preload';
144
+ link.as = 'style';
145
+
146
+ if (key.endsWith('.js')) {
147
+ link.as = 'script';
148
+ link.id = preloadId;
149
+ } // publicPath already in the manifest,
150
+ // but if runtimePublicPath is true, we need to replace it
151
+
152
+
153
+ if (opts.runtimePublicPath) {
154
+ file = file.replace(new RegExp("^".concat(opts.publicPath)), // @ts-ignore
155
+ window.publicPath);
156
+ }
157
+
158
+ link.href = file;
159
+ document.head.appendChild(link);
141
160
  });
142
- }, []);
143
- return (React.createElement(AppContext.Provider, { value: {
144
- routes: opts.routes,
145
- routeComponents: opts.routeComponents,
146
- clientRoutes,
147
- pluginManager: opts.pluginManager,
148
- rootElement: opts.rootElement,
149
- basename,
150
- clientLoaderData,
151
- serverLoaderData,
152
- preloadRoute: handleRouteChange,
153
- } }, rootContainer));
154
- };
155
- if (opts.hydrate) {
156
- ReactDOM.hydrateRoot(rootElement, React.createElement(Browser, null));
157
- }
158
- else {
159
- if (ReactDOM.createRoot) {
160
- ReactDOM.createRoot(rootElement).render(React.createElement(Browser, null));
161
- }
162
- else {
163
- // @ts-ignore
164
- ReactDOM.render(React.createElement(Browser, null), rootElement);
161
+ }
162
+ } // server loader
163
+ // use ?. since routes patched with patchClientRoutes is not exists in opts.routes
164
+
165
+
166
+ if (!isFirst && (_opts$routes$id = opts.routes[id]) !== null && _opts$routes$id !== void 0 && _opts$routes$id.hasServerLoader) {
167
+ fetch('/__serverLoader?route=' + id).then(function (d) {
168
+ return d.json();
169
+ }).then(function (data) {
170
+ // setServerLoaderData when startTransition because if ssr is enabled,
171
+ // the component may being hydrated and setLoaderData will break the hydration
172
+ React.startTransition(function () {
173
+ setServerLoaderData(function (d) {
174
+ return _objectSpread(_objectSpread({}, d), {}, _defineProperty({}, id, data));
175
+ });
176
+ });
177
+ }).catch(console.error);
178
+ } // client loader
179
+ // onPatchClientRoutes 添加的 route 在 opts.routes 里是不存在的
180
+
181
+
182
+ var clientLoader = (_opts$routes$id2 = opts.routes[id]) === null || _opts$routes$id2 === void 0 ? void 0 : _opts$routes$id2.clientLoader;
183
+
184
+ if (clientLoader && !clientLoaderData[id]) {
185
+ clientLoader().then(function (data) {
186
+ setClientLoaderData(function (d) {
187
+ return _objectSpread(_objectSpread({}, d), {}, _defineProperty({}, id, data));
188
+ });
189
+ });
165
190
  }
191
+ });
192
+ }, [clientLoaderData]);
193
+ useEffect(function () {
194
+ handleRouteChange(window.location.pathname, true);
195
+ return opts.history.listen(function (e) {
196
+ handleRouteChange(e.location.pathname);
197
+ });
198
+ }, []);
199
+ return /*#__PURE__*/_jsx(AppContext.Provider, {
200
+ value: {
201
+ routes: opts.routes,
202
+ routeComponents: opts.routeComponents,
203
+ clientRoutes: clientRoutes,
204
+ pluginManager: opts.pluginManager,
205
+ rootElement: opts.rootElement,
206
+ basename: basename,
207
+ clientLoaderData: clientLoaderData,
208
+ serverLoaderData: serverLoaderData,
209
+ preloadRoute: handleRouteChange
210
+ },
211
+ children: rootContainer
212
+ });
213
+ };
214
+
215
+ if (opts.hydrate) {
216
+ ReactDOM.hydrateRoot(rootElement, /*#__PURE__*/_jsx(Browser, {}));
217
+ } else {
218
+ if (ReactDOM.createRoot) {
219
+ root = ReactDOM.createRoot(rootElement);
220
+ root.render( /*#__PURE__*/_jsx(Browser, {}));
221
+ } else {
222
+ // @ts-ignore
223
+ ReactDOM.render( /*#__PURE__*/_jsx(Browser, {}), rootElement);
166
224
  }
167
- }
225
+ }
226
+ }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- export { createBrowserHistory, createHashHistory, createMemoryHistory, History, } from 'history';
1
+ export { createBrowserHistory, createHashHistory, createMemoryHistory, type History, } from 'history';
2
2
  export { createSearchParams, matchPath, matchRoutes, Navigate, NavLink, Outlet, resolvePath, useLocation, useMatch, useNavigate, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
3
3
  export { useAppData, useClientLoaderData, useServerLoaderData, } from './appContext';
4
- export { renderClient } from './browser';
4
+ export { renderClient, __getRoot } from './browser';
5
5
  export { LinkWithPrefetch as Link } from './link';
6
6
  export { useRouteData } from './routeContext';
7
7
  export { __useFetcher } from './useFetcher';
8
+ export { withRouter, type RouteComponentProps } from './withRouter';
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
- export { createBrowserHistory, createHashHistory, createMemoryHistory, } from 'history';
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, useServerLoaderData, } from './appContext';
4
- export { renderClient } from './browser';
5
- export { LinkWithPrefetch as Link } from './link';
6
- export { useRouteData } from './routeContext';
7
- export { __useFetcher } from './useFetcher';
1
+ export { createBrowserHistory, createHashHistory, createMemoryHistory } from 'history';
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, useServerLoaderData } from "./appContext";
4
+ export { renderClient, __getRoot } from "./browser";
5
+ export { LinkWithPrefetch as Link } from "./link";
6
+ export { useRouteData } from "./routeContext";
7
+ export { __useFetcher } from "./useFetcher";
8
+ export { withRouter } from "./withRouter";
package/dist/link.js CHANGED
@@ -1,8 +1,18 @@
1
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
1
2
  import React from 'react';
2
3
  import { Link } from 'react-router-dom';
3
- import { useAppData } from './appContext';
4
+ import { useAppData } from "./appContext";
5
+ import { jsx as _jsx } from "react/jsx-runtime";
4
6
  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: () => { var _a; return props.prefetch && to && ((_a = appData.preloadRoute) === null || _a === void 0 ? void 0 : _a.call(appData, to)); }, ...props }, props.children));
8
- }
7
+ var appData = useAppData();
8
+ var to = typeof props.to === 'string' ? props.to : props.to.pathname;
9
+ return /*#__PURE__*/_jsx(Link, _objectSpread(_objectSpread({
10
+ onMouseEnter: function onMouseEnter() {
11
+ var _appData$preloadRoute;
12
+
13
+ return props.prefetch && to && ((_appData$preloadRoute = appData.preloadRoute) === null || _appData$preloadRoute === void 0 ? void 0 : _appData$preloadRoute.call(appData, to));
14
+ }
15
+ }, props), {}, {
16
+ children: props.children
17
+ }));
18
+ }
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- export const RouteContext = React.createContext(undefined);
2
+ export var RouteContext = /*#__PURE__*/React.createContext(undefined);
3
3
  export function useRouteData() {
4
- return React.useContext(RouteContext);
5
- }
4
+ return React.useContext(RouteContext);
5
+ }
package/dist/routes.js CHANGED
@@ -1,68 +1,93 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
3
+ var _excluded = ["redirect"];
1
4
  // @ts-ignore
2
5
  import React from 'react';
3
6
  import { generatePath, Navigate, useParams } from 'react-router-dom';
4
- import { RouteContext } from './routeContext';
7
+ import { RouteContext } from "./routeContext";
8
+ import { jsx as _jsx } from "react/jsx-runtime";
5
9
  export function createClientRoutes(opts) {
6
- const { routesById, parentId, routeComponents } = opts;
7
- return Object.keys(routesById)
8
- .filter((id) => routesById[id].parentId === parentId)
9
- .map((id) => {
10
- const route = createClientRoute({
11
- route: routesById[id],
12
- routeComponent: routeComponents[id],
13
- loadingComponent: opts.loadingComponent,
14
- });
15
- const children = createClientRoutes({
16
- routesById,
17
- routeComponents,
18
- parentId: route.id,
19
- loadingComponent: opts.loadingComponent,
20
- });
21
- if (children.length > 0) {
22
- route.children = children;
23
- // TODO: remove me
24
- // compatible with @ant-design/pro-layout
25
- route.routes = children;
26
- }
27
- return route;
10
+ var routesById = opts.routesById,
11
+ parentId = opts.parentId,
12
+ routeComponents = opts.routeComponents;
13
+ return Object.keys(routesById).filter(function (id) {
14
+ return routesById[id].parentId === parentId;
15
+ }).map(function (id) {
16
+ var route = createClientRoute({
17
+ route: routesById[id],
18
+ routeComponent: routeComponents[id],
19
+ loadingComponent: opts.loadingComponent
28
20
  });
21
+ var children = createClientRoutes({
22
+ routesById: routesById,
23
+ routeComponents: routeComponents,
24
+ parentId: route.id,
25
+ loadingComponent: opts.loadingComponent
26
+ });
27
+
28
+ if (children.length > 0) {
29
+ route.children = children; // TODO: remove me
30
+ // compatible with @ant-design/pro-layout
31
+
32
+ route.routes = children;
33
+ }
34
+
35
+ return route;
36
+ });
29
37
  }
38
+
30
39
  function NavigateWithParams(props) {
31
- const params = useParams();
32
- const propsWithParams = {
33
- ...params,
34
- to: generatePath(props.to, params),
35
- };
36
- return React.createElement(Navigate, { ...propsWithParams });
40
+ var params = useParams();
41
+
42
+ var propsWithParams = _objectSpread(_objectSpread({}, props), {}, {
43
+ to: generatePath(props.to, params)
44
+ });
45
+
46
+ return /*#__PURE__*/_jsx(Navigate, _objectSpread({
47
+ replace: true
48
+ }, propsWithParams));
37
49
  }
50
+
38
51
  function createClientRoute(opts) {
39
- const { route } = opts;
40
- const { redirect, ...props } = route;
41
- return {
42
- element: redirect ? (React.createElement(NavigateWithParams, { to: redirect })) : (React.createElement(RouteContext.Provider, { value: {
43
- route: opts.route,
44
- } },
45
- React.createElement(RemoteComponent, { loader: opts.routeComponent, loadingComponent: opts.loadingComponent || DefaultLoading }))),
46
- ...props,
47
- };
52
+ var route = opts.route;
53
+
54
+ var redirect = route.redirect,
55
+ props = _objectWithoutProperties(route, _excluded);
56
+
57
+ return _objectSpread({
58
+ element: redirect ? /*#__PURE__*/_jsx(NavigateWithParams, {
59
+ to: redirect
60
+ }) : /*#__PURE__*/_jsx(RouteContext.Provider, {
61
+ value: {
62
+ route: opts.route
63
+ },
64
+ children: /*#__PURE__*/_jsx(RemoteComponent, {
65
+ loader: opts.routeComponent,
66
+ loadingComponent: opts.loadingComponent || DefaultLoading
67
+ })
68
+ })
69
+ }, props);
48
70
  }
71
+
49
72
  function DefaultLoading() {
50
- return React.createElement("div", null);
73
+ return /*#__PURE__*/_jsx("div", {});
51
74
  }
75
+
52
76
  function RemoteComponent(props) {
53
- const useSuspense = true; // !!React.startTransition;
54
- if (useSuspense) {
55
- const Component = props.loader;
56
- return (React.createElement(React.Suspense, { fallback: React.createElement(props.loadingComponent, null) },
57
- React.createElement(Component, null)));
58
- }
59
- else {
60
- return null;
61
- // // @ts-ignore
62
- // import loadable from '@loadable/component';
63
- // const Component = loadable(props.loader, {
64
- // fallback: <props.loadingComponent />,
65
- // });
66
- // return <Component />;
67
- }
68
- }
77
+ var useSuspense = true; // !!React.startTransition;
78
+
79
+ if (useSuspense) {
80
+ var Component = props.loader;
81
+ return /*#__PURE__*/_jsx(React.Suspense, {
82
+ fallback: /*#__PURE__*/_jsx(props.loadingComponent, {}),
83
+ children: /*#__PURE__*/_jsx(Component, {})
84
+ });
85
+ } else {
86
+ return null; // // @ts-ignore
87
+ // import loadable from '@loadable/component';
88
+ // const Component = loadable(props.loader, {
89
+ // fallback: <props.loadingComponent />,
90
+ // });
91
+ // return <Component />;
92
+ }
93
+ }
package/dist/server.js CHANGED
@@ -1,59 +1,107 @@
1
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
1
4
  import React from 'react';
2
5
  import { StaticRouter } from 'react-router-dom/server';
3
- import { AppContext } from './appContext';
4
- import { Routes } from './browser';
5
- import { createClientRoutes } from './routes';
6
+ import { AppContext } from "./appContext";
7
+ import { Routes } from "./browser";
8
+ import { createClientRoutes } from "./routes";
9
+ import { jsx as _jsx } from "react/jsx-runtime";
10
+ import { jsxs as _jsxs } from "react/jsx-runtime";
6
11
  // Get the root React component for ReactDOMServer.renderToString
7
- export async function getClientRootComponent(opts) {
8
- const basename = '/';
9
- const components = { ...opts.routeComponents };
10
- const clientRoutes = createClientRoutes({
11
- routesById: opts.routes,
12
- routeComponents: components,
13
- });
14
- let rootContainer = (React.createElement(StaticRouter, { basename: basename, location: opts.location },
15
- React.createElement(Routes, null)));
16
- for (const key of [
17
- // Lowest to the highest priority
18
- 'innerProvider',
19
- 'i18nProvider',
20
- 'accessProvider',
21
- 'dataflowProvider',
22
- 'outerProvider',
23
- 'rootContainer',
24
- ]) {
25
- rootContainer = opts.pluginManager.applyPlugins({
26
- type: 'modify',
27
- key: key,
28
- initialValue: rootContainer,
29
- args: {},
30
- });
31
- }
32
- return (React.createElement(Html, { loaderData: opts.loaderData, manifest: opts.manifest },
33
- React.createElement(AppContext.Provider, { value: {
34
- routes: opts.routes,
35
- routeComponents: opts.routeComponents,
36
- clientRoutes,
37
- pluginManager: opts.pluginManager,
38
- basename,
39
- clientLoaderData: {},
40
- serverLoaderData: opts.loaderData,
41
- } }, rootContainer)));
12
+ export function getClientRootComponent(_x) {
13
+ return _getClientRootComponent.apply(this, arguments);
42
14
  }
43
- function Html({ children, loaderData, manifest }) {
44
- // TODO: 处理 head 标签,比如 favicon.ico 的一致性
45
- // TODO: root 支持配置
46
- return (React.createElement("html", { lang: "en" },
47
- React.createElement("head", null,
48
- React.createElement("meta", { charSet: "utf-8" }),
49
- React.createElement("meta", { name: "viewport", content: "width=device-width, initial-scale=1" }),
50
- manifest.assets['umi.css'] && (React.createElement("link", { rel: "stylesheet", href: manifest.assets['umi.css'] }))),
51
- React.createElement("body", null,
52
- React.createElement("noscript", { dangerouslySetInnerHTML: {
53
- __html: `<b>Enable JavaScript to run this app.</b>`,
54
- } }),
55
- React.createElement("div", { id: "root" }, children),
56
- React.createElement("script", { dangerouslySetInnerHTML: {
57
- __html: `window.__UMI_LOADER_DATA__ = ${JSON.stringify(loaderData)}`,
58
- } }))));
15
+
16
+ function _getClientRootComponent() {
17
+ _getClientRootComponent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(opts) {
18
+ var basename, components, clientRoutes, rootContainer, _i, _arr, key;
19
+
20
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
21
+ while (1) {
22
+ switch (_context.prev = _context.next) {
23
+ case 0:
24
+ basename = '/';
25
+ components = _objectSpread({}, opts.routeComponents);
26
+ clientRoutes = createClientRoutes({
27
+ routesById: opts.routes,
28
+ routeComponents: components
29
+ });
30
+ rootContainer = /*#__PURE__*/_jsx(StaticRouter, {
31
+ basename: basename,
32
+ location: opts.location,
33
+ children: /*#__PURE__*/_jsx(Routes, {})
34
+ });
35
+
36
+ for (_i = 0, _arr = [// Lowest to the highest priority
37
+ 'innerProvider', 'i18nProvider', 'accessProvider', 'dataflowProvider', 'outerProvider', 'rootContainer']; _i < _arr.length; _i++) {
38
+ key = _arr[_i];
39
+ rootContainer = opts.pluginManager.applyPlugins({
40
+ type: 'modify',
41
+ key: key,
42
+ initialValue: rootContainer,
43
+ args: {}
44
+ });
45
+ }
46
+
47
+ return _context.abrupt("return", /*#__PURE__*/_jsx(Html, {
48
+ loaderData: opts.loaderData,
49
+ manifest: opts.manifest,
50
+ children: /*#__PURE__*/_jsx(AppContext.Provider, {
51
+ value: {
52
+ routes: opts.routes,
53
+ routeComponents: opts.routeComponents,
54
+ clientRoutes: clientRoutes,
55
+ pluginManager: opts.pluginManager,
56
+ basename: basename,
57
+ clientLoaderData: {},
58
+ serverLoaderData: opts.loaderData
59
+ },
60
+ children: rootContainer
61
+ })
62
+ }));
63
+
64
+ case 6:
65
+ case "end":
66
+ return _context.stop();
67
+ }
68
+ }
69
+ }, _callee);
70
+ }));
71
+ return _getClientRootComponent.apply(this, arguments);
59
72
  }
73
+
74
+ function Html(_ref) {
75
+ var children = _ref.children,
76
+ loaderData = _ref.loaderData,
77
+ manifest = _ref.manifest;
78
+ // TODO: 处理 head 标签,比如 favicon.ico 的一致性
79
+ // TODO: root 支持配置
80
+ return /*#__PURE__*/_jsxs("html", {
81
+ lang: "en",
82
+ children: [/*#__PURE__*/_jsxs("head", {
83
+ children: [/*#__PURE__*/_jsx("meta", {
84
+ charSet: "utf-8"
85
+ }), /*#__PURE__*/_jsx("meta", {
86
+ name: "viewport",
87
+ content: "width=device-width, initial-scale=1"
88
+ }), manifest.assets['umi.css'] && /*#__PURE__*/_jsx("link", {
89
+ rel: "stylesheet",
90
+ href: manifest.assets['umi.css']
91
+ })]
92
+ }), /*#__PURE__*/_jsxs("body", {
93
+ children: [/*#__PURE__*/_jsx("noscript", {
94
+ dangerouslySetInnerHTML: {
95
+ __html: "<b>Enable JavaScript to run this app.</b>"
96
+ }
97
+ }), /*#__PURE__*/_jsx("div", {
98
+ id: "root",
99
+ children: children
100
+ }), /*#__PURE__*/_jsx("script", {
101
+ dangerouslySetInnerHTML: {
102
+ __html: "window.__UMI_LOADER_DATA__ = ".concat(JSON.stringify(loaderData))
103
+ }
104
+ })]
105
+ })]
106
+ });
107
+ }
package/dist/types.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,3 +1,3 @@
1
1
  export declare function __useFetcher(): {
2
- load(path?: string): void;
2
+ load(path?: string | undefined): void;
3
3
  };
@@ -1,11 +1,13 @@
1
1
  import { useLocation } from 'react-router-dom';
2
- import { useAppData } from './appContext';
2
+ import { useAppData } from "./appContext";
3
3
  export function __useFetcher() {
4
- const { preloadRoute } = useAppData();
5
- const location = useLocation();
6
- return {
7
- load(path) {
8
- preloadRoute(path || location.pathname);
9
- },
10
- };
11
- }
4
+ var _useAppData = useAppData(),
5
+ preloadRoute = _useAppData.preloadRoute;
6
+
7
+ var location = useLocation();
8
+ return {
9
+ load: function load(path) {
10
+ preloadRoute(path || location.pathname);
11
+ }
12
+ };
13
+ }
@@ -0,0 +1,17 @@
1
+ import React from 'react';
2
+ import { useLocation, useNavigate, useParams } from 'react-router-dom';
3
+ export interface RouteComponentProps<T = ReturnType<typeof useParams>> {
4
+ history: {
5
+ back: () => void;
6
+ goBack: () => void;
7
+ location: ReturnType<typeof useLocation>;
8
+ push: (url: string, state?: any) => void;
9
+ };
10
+ location: ReturnType<typeof useLocation>;
11
+ match: {
12
+ params: T;
13
+ };
14
+ params: T;
15
+ navigate: ReturnType<typeof useNavigate>;
16
+ }
17
+ export declare function withRouter<P extends RouteComponentProps<P>>(Component: React.ComponentType<P>): (props: P) => JSX.Element;
@@ -0,0 +1,43 @@
1
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
+ import React from 'react';
3
+ import { useLocation, useNavigate, useParams } from 'react-router-dom';
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ export function withRouter(Component) {
6
+ function ComponentWithRouterProp(props) {
7
+ var location = useLocation();
8
+ var navigate = useNavigate();
9
+ var params = useParams();
10
+ var match = {
11
+ params: params
12
+ };
13
+ var history = {
14
+ back: function back() {
15
+ return navigate(-1);
16
+ },
17
+ goBack: function goBack() {
18
+ return navigate(-1);
19
+ },
20
+ location: location,
21
+ push: function push(url, state) {
22
+ return navigate(url, {
23
+ state: state
24
+ });
25
+ },
26
+ replace: function replace(url, state) {
27
+ return navigate(url, {
28
+ replace: true,
29
+ state: state
30
+ });
31
+ }
32
+ };
33
+ return /*#__PURE__*/_jsx(Component, _objectSpread(_objectSpread({}, props), {}, {
34
+ history: history,
35
+ location: location,
36
+ match: match,
37
+ params: params,
38
+ navigate: navigate
39
+ }));
40
+ }
41
+
42
+ return ComponentWithRouterProp;
43
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/renderer-react",
3
- "version": "4.0.7",
3
+ "version": "4.0.10",
4
4
  "description": "@umijs/renderer-react",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/renderer-react#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",
@@ -16,12 +16,13 @@
16
16
  "dist"
17
17
  ],
18
18
  "scripts": {
19
- "build": "pnpm tsc",
19
+ "build": "umi-scripts father build",
20
20
  "build:deps": "umi-scripts bundleDeps",
21
- "dev": "pnpm build --watch",
21
+ "dev": "umi-scripts father dev",
22
22
  "test": "umi-scripts jest-turbo"
23
23
  },
24
24
  "dependencies": {
25
+ "@babel/runtime": "7.18.9",
25
26
  "@loadable/component": "5.15.2",
26
27
  "history": "5.3.0",
27
28
  "react-router-dom": "6.3.0"