@umijs/renderer-react 4.0.0-beta.9 → 4.0.0-rc.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.
@@ -1,9 +1,3 @@
1
1
  import React from 'react';
2
- export interface IAppContextType {
3
- routes: any;
4
- routeComponents: any;
5
- clientRoutes: any;
6
- pluginManager: any;
7
- }
8
- export declare const AppContext: React.Context<IAppContextType | undefined>;
9
- export declare function useAppContext(): IAppContextType;
2
+ export declare const AppContext: React.Context<any>;
3
+ export declare function useAppData(): any;
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- export const AppContext = React.createContext(undefined);
3
- export function useAppContext() {
2
+ export const AppContext = React.createContext(null);
3
+ export function useAppData() {
4
4
  return React.useContext(AppContext);
5
5
  }
package/dist/browser.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { IRoutesById } from './types';
2
- export declare function Browser(props: {
3
- routes: IRoutesById;
4
- routeComponents: Record<string, any>;
5
- pluginManager: any;
6
- }): any;
1
+ import { History } from 'history';
2
+ import React from 'react';
3
+ import { IRouteComponents, IRoutesById } from './types';
7
4
  export declare function renderClient(opts: {
8
5
  rootElement?: HTMLElement;
9
6
  routes: IRoutesById;
10
- routeComponents: Record<string, any>;
7
+ routeComponents: IRouteComponents;
11
8
  pluginManager: any;
9
+ basename?: string;
10
+ loadingComponent?: React.ReactNode;
11
+ history: History;
12
12
  }): void;
package/dist/browser.js CHANGED
@@ -1,27 +1,73 @@
1
- import { createBrowserHistory } from 'history';
2
1
  import React from 'react';
3
2
  import ReactDOM from 'react-dom';
4
- import { App } from './app';
5
- export function Browser(props) {
6
- const historyRef = React.useRef();
7
- if (historyRef.current === undefined) {
8
- historyRef.current = createBrowserHistory({ window });
9
- }
10
- const history = historyRef.current;
11
- const [state, dispatch] = React.useReducer((_, update) => update, {
3
+ import { Router, useRoutes } from 'react-router-dom';
4
+ import { AppContext, useAppData } from './appContext';
5
+ import { createClientRoutes } from './routes';
6
+ function BrowserRoutes(props) {
7
+ const { history } = props;
8
+ const [state, setState] = React.useState({
12
9
  action: history.action,
13
10
  location: history.location,
14
11
  });
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: {},
21
- });
12
+ React.useLayoutEffect(() => history.listen(setState), [history]);
13
+ React.useLayoutEffect(() => history.listen((location, action) => {
14
+ props.pluginManager.applyPlugins({
15
+ key: 'onRouteChange',
16
+ type: 'event',
17
+ args: {
18
+ routes: props.routes,
19
+ clientRoutes: props.clientRoutes,
20
+ location,
21
+ action,
22
+ },
23
+ });
24
+ }), [history, props.routes, props.clientRoutes]);
25
+ return (React.createElement(Router, { navigator: history, location: state.location, basename: props.basename }, props.children));
26
+ }
27
+ function Routes() {
28
+ const { clientRoutes } = useAppData();
29
+ return useRoutes(clientRoutes);
22
30
  }
23
31
  export function renderClient(opts) {
32
+ const basename = opts.basename || '/';
33
+ const rootElement = opts.rootElement || document.getElementById('root');
34
+ const clientRoutes = createClientRoutes({
35
+ routesById: opts.routes,
36
+ routeComponents: opts.routeComponents,
37
+ loadingComponent: opts.loadingComponent,
38
+ });
39
+ let rootContainer = (React.createElement(BrowserRoutes, { basename: basename, pluginManager: opts.pluginManager, routes: opts.routes, clientRoutes: clientRoutes, history: opts.history },
40
+ React.createElement(Routes, null)));
41
+ for (const key of [
42
+ // Lowest to the highest priority
43
+ 'innerProvider',
44
+ 'i18nProvider',
45
+ 'accessProvider',
46
+ 'dataflowProvider',
47
+ 'outerProvider',
48
+ 'rootContainer',
49
+ ]) {
50
+ rootContainer = opts.pluginManager.applyPlugins({
51
+ type: 'modify',
52
+ key: key,
53
+ initialValue: rootContainer,
54
+ args: {},
55
+ });
56
+ }
57
+ const browser = (React.createElement(AppContext.Provider, { value: {
58
+ routes: opts.routes,
59
+ routeComponents: opts.routeComponents,
60
+ clientRoutes,
61
+ pluginManager: opts.pluginManager,
62
+ rootElement: opts.rootElement,
63
+ basename,
64
+ } }, rootContainer));
24
65
  // @ts-ignore
25
- const root = ReactDOM.createRoot(opts.rootElement || document.getElementById('root'));
26
- root.render(React.createElement(Browser, { routes: opts.routes, routeComponents: opts.routeComponents, pluginManager: opts.pluginManager }));
66
+ if (ReactDOM.createRoot) {
67
+ // @ts-ignore
68
+ ReactDOM.createRoot(rootElement).render(browser);
69
+ }
70
+ else {
71
+ ReactDOM.render(browser, rootElement);
72
+ }
27
73
  }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
- export { Link, Outlet } from 'react-router-dom';
2
- export * from './browser';
1
+ export { createBrowserHistory, createHashHistory, createMemoryHistory, History, } from 'history';
2
+ export { createSearchParams, Link, matchPath, matchRoutes, NavLink, Outlet, useLocation, useMatch, useNavigate, useOutlet, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
3
+ export { useAppData } from './appContext';
4
+ export { renderClient } from './browser';
5
+ export { useRouteData } from './routeContext';
package/dist/index.js CHANGED
@@ -1,2 +1,5 @@
1
- export { Link, Outlet } from 'react-router-dom';
2
- export * from './browser';
1
+ export { createBrowserHistory, createHashHistory, createMemoryHistory, } from 'history';
2
+ export { createSearchParams, Link, matchPath, matchRoutes, NavLink, Outlet, useLocation, useMatch, useNavigate, useOutlet, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
3
+ export { useAppData } from './appContext';
4
+ export { renderClient } from './browser';
5
+ export { useRouteData } from './routeContext';
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ export interface IRouteContextType {
3
+ route: any;
4
+ }
5
+ export declare const RouteContext: React.Context<IRouteContextType | undefined>;
6
+ export declare function useRouteData(): IRouteContextType;
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ export const RouteContext = React.createContext(undefined);
3
+ export function useRouteData() {
4
+ return React.useContext(RouteContext);
5
+ }
package/dist/routes.d.ts CHANGED
@@ -1,10 +1,12 @@
1
- /// <reference types="react" />
1
+ import React from 'react';
2
2
  import { IRoute, IRoutesById } from './types';
3
3
  export declare function createClientRoutes(opts: {
4
4
  routesById: IRoutesById;
5
+ routeComponents: Record<string, any>;
5
6
  parentId?: string;
6
- Component: any;
7
+ loadingComponent?: React.ReactNode;
7
8
  }): {
9
+ parentId?: string | undefined;
8
10
  id: string;
9
11
  path: string | undefined;
10
12
  index: boolean | undefined;
@@ -12,8 +14,10 @@ export declare function createClientRoutes(opts: {
12
14
  }[];
13
15
  export declare function createClientRoute(opts: {
14
16
  route: IRoute;
15
- Component: any;
17
+ routeComponent: any;
18
+ loadingComponent?: React.ReactNode;
16
19
  }): {
20
+ parentId?: string | undefined;
17
21
  id: string;
18
22
  path: string | undefined;
19
23
  index: boolean | undefined;
package/dist/routes.js CHANGED
@@ -1,32 +1,60 @@
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
+ // @ts-ignore
13
+ import loadable from '@loadable/component';
1
14
  import React from 'react';
2
15
  import { Navigate } from 'react-router-dom';
16
+ import { RouteContext } from './routeContext';
3
17
  export function createClientRoutes(opts) {
4
- const { routesById, parentId, Component } = opts;
18
+ const { routesById, parentId, routeComponents } = opts;
5
19
  return Object.keys(routesById)
6
20
  .filter((id) => routesById[id].parentId === parentId)
7
21
  .map((id) => {
8
22
  const route = createClientRoute({
9
23
  route: routesById[id],
10
- Component,
24
+ routeComponent: routeComponents[id],
25
+ loadingComponent: opts.loadingComponent,
11
26
  });
12
27
  const children = createClientRoutes({
13
28
  routesById,
29
+ routeComponents,
14
30
  parentId: route.id,
15
- Component,
31
+ loadingComponent: opts.loadingComponent,
16
32
  });
17
33
  if (children.length > 0) {
18
34
  // @ts-ignore
19
35
  route.children = children;
36
+ // TODO: remove me
37
+ // compatible with @ant-design/pro-layout
38
+ // @ts-ignore
39
+ route.routes = children;
20
40
  }
21
41
  return route;
22
42
  });
23
43
  }
24
44
  export function createClientRoute(opts) {
25
- const { route, Component } = opts;
26
- return {
27
- id: route.id,
28
- path: route.path,
29
- index: route.index,
30
- element: route.redirect ? (React.createElement(Navigate, { to: route.redirect })) : (React.createElement(Component, { id: route.id })),
31
- };
45
+ const { route } = opts;
46
+ const { id, path, index, redirect } = route, props = __rest(route, ["id", "path", "index", "redirect"]);
47
+ return Object.assign({ id: id, path: path, index: index, element: redirect ? (React.createElement(Navigate, { to: redirect })) : (React.createElement(RouteContext.Provider, { value: {
48
+ route: opts.route,
49
+ } },
50
+ React.createElement(RemoteComponent, { loader: opts.routeComponent, loadingComponent: opts.loadingComponent || DefaultLoading }))) }, props);
51
+ }
52
+ function DefaultLoading() {
53
+ return React.createElement("div", null);
54
+ }
55
+ function RemoteComponent(props) {
56
+ const Component = loadable(props.loader, {
57
+ fallback: React.createElement(props.loadingComponent, null),
58
+ });
59
+ return React.createElement(Component, null);
32
60
  }
package/dist/types.d.ts CHANGED
@@ -8,3 +8,6 @@ export interface IRoute {
8
8
  export interface IRoutesById {
9
9
  [id: string]: IRoute;
10
10
  }
11
+ export interface IRouteComponents {
12
+ [id: string]: any;
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/renderer-react",
3
- "version": "4.0.0-beta.9",
3
+ "version": "4.0.0-rc.3",
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",
@@ -21,12 +21,13 @@
21
21
  "dev": "pnpm build -- --watch"
22
22
  },
23
23
  "dependencies": {
24
- "history": "5.0.1",
25
- "react-router-dom": "6.0.0-beta.8"
24
+ "@loadable/component": "5.15.2",
25
+ "history": "5.2.0",
26
+ "react-router-dom": "6.2.1"
26
27
  },
27
28
  "devDependencies": {
28
- "react": "18.0.0-alpha-f2c381131-20211004",
29
- "react-dom": "18.0.0-alpha-f2c381131-20211004"
29
+ "react": "17.0.2",
30
+ "react-dom": "17.0.2"
30
31
  },
31
32
  "peerDependencies": {
32
33
  "react": ">=16.8",
package/dist/app.d.ts DELETED
@@ -1,14 +0,0 @@
1
- /// <reference types="react" />
2
- import { Location } from 'history';
3
- import { Navigator } from 'react-router-dom';
4
- import { IRoutesById } from './types';
5
- export declare function App(props: {
6
- navigator: Navigator;
7
- location: Location;
8
- routes: IRoutesById;
9
- routeComponents: Record<string, any>;
10
- pluginManager: any;
11
- }): JSX.Element;
12
- export declare function RouteComponent(props: {
13
- id: string;
14
- }): JSX.Element;
package/dist/app.js DELETED
@@ -1,48 +0,0 @@
1
- import React from 'react';
2
- import { Router, useRoutes } from 'react-router-dom';
3
- import { AppContext, useAppContext } from './appContext';
4
- import { createClientRoutes } from './routes';
5
- export function App(props) {
6
- const clientRoutes = React.useMemo(() => {
7
- return createClientRoutes({
8
- routesById: props.routes,
9
- Component: RouteComponent,
10
- });
11
- }, [props.routes]);
12
- let ret = (React.createElement(AppContext.Provider, { value: {
13
- routes: props.routes,
14
- routeComponents: props.routeComponents,
15
- pluginManager: props.pluginManager,
16
- clientRoutes,
17
- } },
18
- React.createElement(Router, { navigator: props.navigator, location: props.location },
19
- React.createElement(Routes, null))));
20
- for (const key of [
21
- 'innerProvider',
22
- 'i18nProvider',
23
- 'dataflowProvider',
24
- 'outerProvider',
25
- ]) {
26
- ret = props.pluginManager.applyPlugins({
27
- type: 'modify',
28
- key: key,
29
- initialValue: ret,
30
- });
31
- }
32
- return ret;
33
- }
34
- function Routes() {
35
- const { clientRoutes } = useAppContext();
36
- return useRoutes(clientRoutes) || clientRoutes[0].element;
37
- }
38
- function Loading() {
39
- return React.createElement("div", null, "Loading...");
40
- }
41
- export function RouteComponent(props) {
42
- const loader = useAppContext().routeComponents[props.id];
43
- const RouteComponent = React.lazy(loader);
44
- // ref: https://reactjs.org/docs/code-splitting.html
45
- // TODO: replace with https://github.com/gregberge/loadable-components when we support ssr
46
- return (React.createElement(React.Suspense, { fallback: React.createElement(Loading, null) },
47
- React.createElement(RouteComponent, null)));
48
- }