@umijs/renderer-react 4.0.0-beta.12 → 4.0.0-beta.13

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