@module-federation/bridge-react 0.0.0-next-20240620052430 → 0.0.0-next-20240620060503

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/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @module-federation/bridge-react
2
2
 
3
- ## 0.0.0-next-20240620052430
3
+ ## 0.0.0-next-20240620060503
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - @module-federation/bridge-shared@0.0.0-next-20240620052430
7
+ - @module-federation/bridge-shared@0.0.0-next-20240620060503
@@ -48,8 +48,26 @@ function atLeastReact18(React2) {
48
48
  return false;
49
49
  }
50
50
  }
51
+ function pathJoin(...args) {
52
+ const res = args.reduce((res2, path) => {
53
+ let nPath = path;
54
+ if (!nPath || typeof nPath !== "string") {
55
+ return res2;
56
+ }
57
+ if (nPath[0] !== "/") {
58
+ nPath = `/${nPath}`;
59
+ }
60
+ const lastIndex = nPath.length - 1;
61
+ if (nPath[lastIndex] === "/") {
62
+ nPath = nPath.substring(0, lastIndex);
63
+ }
64
+ return res2 + nPath;
65
+ }, "");
66
+ return res || "/";
67
+ }
51
68
  const RouterContext = React.createContext(null);
52
69
  exports.LoggerInstance = LoggerInstance;
53
70
  exports.RouterContext = RouterContext;
54
71
  exports.atLeastReact18 = atLeastReact18;
55
72
  exports.f = f;
73
+ exports.pathJoin = pathJoin;
@@ -47,10 +47,28 @@ function atLeastReact18(React2) {
47
47
  return false;
48
48
  }
49
49
  }
50
+ function pathJoin(...args) {
51
+ const res = args.reduce((res2, path) => {
52
+ let nPath = path;
53
+ if (!nPath || typeof nPath !== "string") {
54
+ return res2;
55
+ }
56
+ if (nPath[0] !== "/") {
57
+ nPath = `/${nPath}`;
58
+ }
59
+ const lastIndex = nPath.length - 1;
60
+ if (nPath[lastIndex] === "/") {
61
+ nPath = nPath.substring(0, lastIndex);
62
+ }
63
+ return res2 + nPath;
64
+ }, "");
65
+ return res || "/";
66
+ }
50
67
  const RouterContext = React__default.createContext(null);
51
68
  export {
52
69
  LoggerInstance as L,
53
70
  RouterContext as R,
54
71
  atLeastReact18 as a,
55
- f
72
+ f,
73
+ pathJoin as p
56
74
  };
package/dist/index.cjs.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const React = require("react");
4
4
  const ReactRouterDOM = require("react-router-dom");
5
- const context = require("./context-ePt4wynZ.cjs");
5
+ const context = require("./context--mtFt3tp.cjs");
6
6
  const ReactDOM = require("react-dom");
7
7
  function _interopNamespaceDefault(e) {
8
8
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
@@ -86,6 +86,7 @@ const RemoteApp = ({
86
86
  RemoteApp["__APP_VERSION__"] = "0.0.1";
87
87
  function createRemoteComponent(lazyComponent, info) {
88
88
  return (props) => {
89
+ var _a;
89
90
  const exportName = (info == null ? void 0 : info.export) || "default";
90
91
  let basename = "/";
91
92
  let enableDispathPopstate = false;
@@ -96,6 +97,36 @@ function createRemoteComponent(lazyComponent, info) {
96
97
  } catch {
97
98
  enableDispathPopstate = false;
98
99
  }
100
+ if (props.basename) {
101
+ basename = props.basename;
102
+ } else if (enableDispathPopstate) {
103
+ const ReactRouterDOMAny = ReactRouterDOM__namespace;
104
+ const useRouteMatch = ReactRouterDOMAny["useRouteMatch"];
105
+ const useHistory = ReactRouterDOMAny["useHistory"];
106
+ const useHref = ReactRouterDOMAny["useHref"];
107
+ const UNSAFE_RouteContext = ReactRouterDOMAny["UNSAFE_RouteContext"];
108
+ if (UNSAFE_RouteContext) {
109
+ if (useHref) {
110
+ basename = useHref == null ? void 0 : useHref("/");
111
+ }
112
+ routerContextVal = React.useContext(UNSAFE_RouteContext);
113
+ if (routerContextVal && routerContextVal.matches && routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
114
+ basename = context.pathJoin(
115
+ basename,
116
+ routerContextVal.matches[0].pathnameBase || "/"
117
+ );
118
+ }
119
+ } else {
120
+ const match = useRouteMatch == null ? void 0 : useRouteMatch();
121
+ if (useHistory) {
122
+ const history = useHistory == null ? void 0 : useHistory();
123
+ basename = (_a = history == null ? void 0 : history.createHref) == null ? void 0 : _a.call(history, { pathname: "/" });
124
+ }
125
+ if (match) {
126
+ basename = context.pathJoin(basename, (match == null ? void 0 : match.path) || "/");
127
+ }
128
+ }
129
+ }
99
130
  const LazyComponent = React.useMemo(() => {
100
131
  return React.lazy(async () => {
101
132
  context.LoggerInstance.log(`createRemoteComponent LazyComponent create >>>`, {
package/dist/index.es.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as React from "react";
2
- import React__default, { useMemo, useRef, useState, useEffect } from "react";
2
+ import React__default, { useContext, useMemo, useRef, useState, useEffect } from "react";
3
3
  import * as ReactRouterDOM from "react-router-dom";
4
- import { L as LoggerInstance, f, a as atLeastReact18, R as RouterContext } from "./context-CPtN38Ur.js";
4
+ import { p as pathJoin, L as LoggerInstance, f, a as atLeastReact18, R as RouterContext } from "./context-Bw2PEwa6.js";
5
5
  import ReactDOM from "react-dom";
6
6
  const RemoteApp = ({
7
7
  name,
@@ -67,6 +67,7 @@ const RemoteApp = ({
67
67
  RemoteApp["__APP_VERSION__"] = "0.0.1";
68
68
  function createRemoteComponent(lazyComponent, info) {
69
69
  return (props) => {
70
+ var _a;
70
71
  const exportName = (info == null ? void 0 : info.export) || "default";
71
72
  let basename = "/";
72
73
  let enableDispathPopstate = false;
@@ -77,6 +78,36 @@ function createRemoteComponent(lazyComponent, info) {
77
78
  } catch {
78
79
  enableDispathPopstate = false;
79
80
  }
81
+ if (props.basename) {
82
+ basename = props.basename;
83
+ } else if (enableDispathPopstate) {
84
+ const ReactRouterDOMAny = ReactRouterDOM;
85
+ const useRouteMatch = ReactRouterDOMAny["useRouteMatch"];
86
+ const useHistory = ReactRouterDOMAny["useHistory"];
87
+ const useHref = ReactRouterDOMAny["useHref"];
88
+ const UNSAFE_RouteContext = ReactRouterDOMAny["UNSAFE_RouteContext"];
89
+ if (UNSAFE_RouteContext) {
90
+ if (useHref) {
91
+ basename = useHref == null ? void 0 : useHref("/");
92
+ }
93
+ routerContextVal = useContext(UNSAFE_RouteContext);
94
+ if (routerContextVal && routerContextVal.matches && routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
95
+ basename = pathJoin(
96
+ basename,
97
+ routerContextVal.matches[0].pathnameBase || "/"
98
+ );
99
+ }
100
+ } else {
101
+ const match = useRouteMatch == null ? void 0 : useRouteMatch();
102
+ if (useHistory) {
103
+ const history = useHistory == null ? void 0 : useHistory();
104
+ basename = (_a = history == null ? void 0 : history.createHref) == null ? void 0 : _a.call(history, { pathname: "/" });
105
+ }
106
+ if (match) {
107
+ basename = pathJoin(basename, (match == null ? void 0 : match.path) || "/");
108
+ }
109
+ }
110
+ }
80
111
  const LazyComponent = useMemo(() => {
81
112
  return React__default.lazy(async () => {
82
113
  LoggerInstance.log(`createRemoteComponent LazyComponent create >>>`, {
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const React = require("react");
4
4
  const ReactRouterDom = require("react-router-dom/");
5
- const context = require("./context-ePt4wynZ.cjs");
5
+ const context = require("./context--mtFt3tp.cjs");
6
6
  function _interopNamespaceDefault(e) {
7
7
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
8
8
  if (e) {
package/dist/router.es.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import React__default, { useContext } from "react";
2
2
  import * as ReactRouterDom from "react-router-dom/";
3
3
  export * from "react-router-dom/";
4
- import { R as RouterContext, L as LoggerInstance } from "./context-CPtN38Ur.js";
4
+ import { R as RouterContext, L as LoggerInstance } from "./context-Bw2PEwa6.js";
5
5
  function WraperRouter(props) {
6
6
  const { basename, ...propsRes } = props;
7
7
  const routerContextProps = useContext(RouterContext) || {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/bridge-react",
3
- "version": "0.0.0-next-20240620052430",
3
+ "version": "0.0.0-next-20240620060503",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -25,7 +25,7 @@
25
25
  "dependencies": {
26
26
  "@loadable/component": "^5.16.4",
27
27
  "react-error-boundary": "^4.0.13",
28
- "@module-federation/bridge-shared": "0.0.0-next-20240620052430"
28
+ "@module-federation/bridge-shared": "0.0.0-next-20240620060503"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "react": ">=16.9.0",
package/src/create.tsx CHANGED
@@ -1,7 +1,14 @@
1
- import React, { ReactNode, useEffect, useMemo, useRef, useState } from 'react';
1
+ import React, {
2
+ ReactNode,
3
+ useContext,
4
+ useEffect,
5
+ useMemo,
6
+ useRef,
7
+ useState,
8
+ } from 'react';
2
9
  import * as ReactRouterDOM from 'react-router-dom';
3
10
  import type { ProviderParams } from '@module-federation/bridge-shared';
4
- import { LoggerInstance } from './utils';
11
+ import { LoggerInstance, pathJoin } from './utils';
5
12
  import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
6
13
 
7
14
  declare const __APP_VERSION__: string;
@@ -128,44 +135,46 @@ export function createRemoteComponent<T, E extends keyof T>(
128
135
  enableDispathPopstate = false;
129
136
  }
130
137
 
131
- // if (props.basename) {
132
- // basename = props.basename
133
- // } else if (enableDispathPopstate) {
134
- // const routerV5Api = 'useRoute' + 'Match';
135
- // const routerV6Api = 'useMatches';
136
- // const routerV5History = 'use' + 'History';
137
- // const useHref = 'useHref';
138
-
139
- // const match = (ReactRouterDOM as any)[routerV5Api]?.();
140
- // const matchs = (ReactRouterDOM as any)[routerV6Api]?.();
141
- // const location = ReactRouterDOM.useLocation();
142
-
143
- // if ((ReactRouterDOM as any)[routerV5History] /* react-router@5 */) {
144
- // // there is no dynamic switching of the router version in the project
145
- // // so hooks can be used in conditional judgment
146
- // // eslint-disable-next-line react-hooks/rules-of-hooks
147
- // const history = (ReactRouterDOM as any)[routerV5History]?.();
148
- // // To be compatible to history@4.10.1 and @5.3.0 we cannot write like this `history.createHref(pathname)`
149
- // basename = history?.createHref?.({ pathname: '/' });
150
- // } else if (useHref /* react-router@6 */) {
151
- // // eslint-disable-next-line react-hooks/rules-of-hooks
152
- // basename = useHref?.('/');
153
- // }
154
- // }
155
-
156
- // if (ReactRouterDOM.UNSAFE_RouteContext && enableDispathPopstate) {
157
- // routerContextVal = eval('useContext(ReactRouterDOM.UNSAFE_RouteContext)');
158
- // if (
159
- // routerContextVal &&
160
- // routerContextVal.matches &&
161
- // routerContextVal.matches[0] &&
162
- // routerContextVal.matches[0].pathnameBase
163
- // ) {
164
- // basename = routerContextVal.matches[0].pathnameBase;
165
- // }
166
- // enableDispathPopstate =
167
- // routerContextVal?.matches && routerContextVal?.matches.length > 0;
168
- // }
138
+ if (props.basename) {
139
+ basename = props.basename;
140
+ } else if (enableDispathPopstate) {
141
+ const ReactRouterDOMAny: any = ReactRouterDOM;
142
+ // Avoid building tools checking references
143
+ const useRouteMatch = ReactRouterDOMAny['use' + 'RouteMatch']; //v5
144
+ const useHistory = ReactRouterDOMAny['use' + 'History']; //v5
145
+ const useHref = ReactRouterDOMAny['use' + 'Href'];
146
+ const UNSAFE_RouteContext = ReactRouterDOMAny['UNSAFE_' + 'RouteContext'];
147
+
148
+ if (UNSAFE_RouteContext /* react-router@6 */) {
149
+ if (useHref) {
150
+ basename = useHref?.('/');
151
+ }
152
+ routerContextVal = useContext(UNSAFE_RouteContext);
153
+ if (
154
+ routerContextVal &&
155
+ routerContextVal.matches &&
156
+ routerContextVal.matches[0] &&
157
+ routerContextVal.matches[0].pathnameBase
158
+ ) {
159
+ basename = pathJoin(
160
+ basename,
161
+ routerContextVal.matches[0].pathnameBase || '/',
162
+ );
163
+ }
164
+ } /* react-router@5 */ else {
165
+ const match = useRouteMatch?.(); // v5
166
+ if (useHistory /* react-router@5 */) {
167
+ // there is no dynamic switching of the router version in the project
168
+ // so hooks can be used in conditional judgment
169
+ const history = useHistory?.();
170
+ // To be compatible to history@4.10.1 and @5.3.0 we cannot write like this `history.createHref(pathname)`
171
+ basename = history?.createHref?.({ pathname: '/' });
172
+ }
173
+ if (match /* react-router@5 */) {
174
+ basename = pathJoin(basename, match?.path || '/');
175
+ }
176
+ }
177
+ }
169
178
 
170
179
  const LazyComponent = useMemo(() => {
171
180
  //@ts-ignore
package/src/utils.ts CHANGED
@@ -21,3 +21,21 @@ export function atLeastReact18(React: typeReact) {
21
21
  return false;
22
22
  }
23
23
  }
24
+
25
+ export function pathJoin(...args: string[]) {
26
+ const res = args.reduce((res, path: string) => {
27
+ let nPath = path;
28
+ if (!nPath || typeof nPath !== 'string') {
29
+ return res;
30
+ }
31
+ if (nPath[0] !== '/') {
32
+ nPath = `/${nPath}`;
33
+ }
34
+ const lastIndex = nPath.length - 1;
35
+ if (nPath[lastIndex] === '/') {
36
+ nPath = nPath.substring(0, lastIndex);
37
+ }
38
+ return res + nPath;
39
+ }, '');
40
+ return res || '/';
41
+ }