@umijs/renderer-react 4.0.0-beta.7 → 4.0.0-canary.202200505.1

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,84 @@
1
- import { createBrowserHistory } from 'history';
2
1
  import React from 'react';
3
- 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, {
2
+ // compatible with < react@18 in @umijs/preset-umi/src/features/react
3
+ import ReactDOM from 'react-dom/client';
4
+ import { Router, useRoutes } from 'react-router-dom';
5
+ import { AppContext, useAppData } from './appContext';
6
+ import { createClientRoutes } from './routes';
7
+ function BrowserRoutes(props) {
8
+ const { history } = props;
9
+ const [state, setState] = React.useState({
12
10
  action: history.action,
13
11
  location: history.location,
14
12
  });
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
- });
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
+ });
26
+ }
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));
31
+ }
32
+ function Routes() {
33
+ const { clientRoutes } = useAppData();
34
+ return useRoutes(clientRoutes);
22
35
  }
23
36
  export function renderClient(opts) {
24
- // @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 }));
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
+ });
68
+ }
69
+ const browser = (React.createElement(AppContext.Provider, { value: {
70
+ routes: opts.routes,
71
+ routeComponents: opts.routeComponents,
72
+ clientRoutes,
73
+ pluginManager: opts.pluginManager,
74
+ rootElement: opts.rootElement,
75
+ basename,
76
+ } }, rootContainer));
77
+ if (ReactDOM.createRoot) {
78
+ ReactDOM.createRoot(rootElement).render(browser);
79
+ }
80
+ else {
81
+ // @ts-ignore
82
+ ReactDOM.render(browser, rootElement);
83
+ }
27
84
  }
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, Navigate, 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, Navigate, 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,31 +1,55 @@
1
+ // @ts-ignore
2
+ import loadable from '@loadable/component';
1
3
  import React from 'react';
4
+ import { Navigate } from 'react-router-dom';
5
+ import { RouteContext } from './routeContext';
2
6
  export function createClientRoutes(opts) {
3
- const { routesById, parentId, Component } = opts;
7
+ const { routesById, parentId, routeComponents } = opts;
4
8
  return Object.keys(routesById)
5
9
  .filter((id) => routesById[id].parentId === parentId)
6
10
  .map((id) => {
7
11
  const route = createClientRoute({
8
12
  route: routesById[id],
9
- Component,
13
+ routeComponent: routeComponents[id],
14
+ loadingComponent: opts.loadingComponent,
10
15
  });
11
16
  const children = createClientRoutes({
12
17
  routesById,
18
+ routeComponents,
13
19
  parentId: route.id,
14
- Component,
20
+ loadingComponent: opts.loadingComponent,
15
21
  });
16
22
  if (children.length > 0) {
17
23
  // @ts-ignore
18
24
  route.children = children;
25
+ // TODO: remove me
26
+ // compatible with @ant-design/pro-layout
27
+ // @ts-ignore
28
+ route.routes = children;
19
29
  }
20
30
  return route;
21
31
  });
22
32
  }
23
33
  export function createClientRoute(opts) {
24
- const { route, Component } = opts;
34
+ const { route } = opts;
35
+ const { id, path, index, redirect, ...props } = route;
25
36
  return {
26
- id: route.id,
27
- path: route.path,
28
- index: route.index,
29
- element: React.createElement(Component, { id: route.id }),
37
+ id: id,
38
+ path: path,
39
+ index: index,
40
+ element: redirect ? (React.createElement(Navigate, { to: redirect })) : (React.createElement(RouteContext.Provider, { value: {
41
+ route: opts.route,
42
+ } },
43
+ React.createElement(RemoteComponent, { loader: opts.routeComponent, loadingComponent: opts.loadingComponent || DefaultLoading }))),
44
+ ...props,
30
45
  };
31
46
  }
47
+ function DefaultLoading() {
48
+ return React.createElement("div", null);
49
+ }
50
+ function RemoteComponent(props) {
51
+ const Component = loadable(props.loader, {
52
+ fallback: React.createElement(props.loadingComponent, null),
53
+ });
54
+ return React.createElement(Component, null);
55
+ }
package/dist/types.d.ts CHANGED
@@ -3,7 +3,11 @@ export interface IRoute {
3
3
  path?: string;
4
4
  index?: boolean;
5
5
  parentId?: string;
6
+ redirect?: string;
6
7
  }
7
8
  export interface IRoutesById {
8
9
  [id: string]: IRoute;
9
10
  }
11
+ export interface IRouteComponents {
12
+ [id: string]: any;
13
+ }
package/package.json CHANGED
@@ -1,7 +1,15 @@
1
1
  {
2
2
  "name": "@umijs/renderer-react",
3
- "version": "4.0.0-beta.7",
3
+ "version": "4.0.0-canary.202200505.1",
4
4
  "description": "@umijs/renderer-react",
5
+ "homepage": "https://github.com/umijs/umi-next/tree/master/packages/renderer-react#readme",
6
+ "bugs": "https://github.com/umijs/umi-next/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/umijs/umi-next"
10
+ },
11
+ "license": "MIT",
12
+ "sideEffects": false,
5
13
  "main": "dist/index.js",
6
14
  "types": "dist/index.d.ts",
7
15
  "files": [
@@ -9,33 +17,27 @@
9
17
  ],
10
18
  "scripts": {
11
19
  "build": "pnpm tsc",
12
- "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
13
- "dev": "pnpm build -- --watch"
14
- },
15
- "repository": {
16
- "type": "git",
17
- "url": "https://github.com/umijs/umi-next"
18
- },
19
- "authors": [
20
- "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
21
- ],
22
- "license": "MIT",
23
- "bugs": "https://github.com/umijs/umi-next/issues",
24
- "homepage": "https://github.com/umijs/umi-next/tree/master/packages/renderer-react#readme",
25
- "publishConfig": {
26
- "access": "public"
20
+ "build:deps": "umi-scripts bundleDeps",
21
+ "dev": "pnpm build -- --watch",
22
+ "test": "umi-scripts jest-turbo"
27
23
  },
28
24
  "dependencies": {
29
- "history": "5.0.1",
30
- "react-router-dom": "6.0.0-beta.8"
25
+ "@loadable/component": "5.15.2",
26
+ "history": "5.3.0",
27
+ "react-router-dom": "6.3.0"
31
28
  },
32
29
  "devDependencies": {
33
- "react": "18.0.0-alpha-f2c381131-20211004",
34
- "react-dom": "18.0.0-alpha-f2c381131-20211004"
30
+ "react": "18.1.0",
31
+ "react-dom": "18.1.0"
35
32
  },
36
33
  "peerDependencies": {
37
34
  "react": ">=16.8",
38
35
  "react-dom": ">=16.8"
39
36
  },
40
- "sideEffects": false
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "authors": [
41
+ "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
42
+ ]
41
43
  }
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,35 +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
- return (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
- }
21
- function Routes() {
22
- const { clientRoutes } = useAppContext();
23
- return useRoutes(clientRoutes) || clientRoutes[0].element;
24
- }
25
- function Loading() {
26
- return React.createElement("div", null, "Loading...");
27
- }
28
- export function RouteComponent(props) {
29
- const loader = useAppContext().routeComponents[props.id];
30
- const RouteComponent = React.lazy(loader);
31
- // ref: https://reactjs.org/docs/code-splitting.html
32
- // TODO: replace with https://github.com/gregberge/loadable-components when we support ssr
33
- return (React.createElement(React.Suspense, { fallback: React.createElement(Loading, null) },
34
- React.createElement(RouteComponent, null)));
35
- }