Skip to content

Commit 0309fb5

Browse files
authored
fix(bridge-react): wrap react-router-dom path resolve with try catch (#3232)
1 parent a50b000 commit 0309fb5

File tree

7 files changed

+43
-27
lines changed

7 files changed

+43
-27
lines changed

.changeset/ten-games-suffer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/bridge-react-webpack-plugin': patch
3+
'@module-federation/bridge-react': patch
4+
---
5+
6+
fix: wrap try catch with react-router-dom path resolve

apps/router-demo/router-remote1-2001/rsbuild.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export default defineConfig({
1010
react: path.resolve(__dirname, 'node_modules/react'),
1111
'react-dom': path.resolve(__dirname, 'node_modules/react-dom'),
1212
// set `react-router-dom/` to reference react-router-dom v5 which shoule be find in node_modules/react-router-dom, otherwise it will cause app.tsx fail to work which in react-router-dom v5 mode.
13-
'react-router-dom': path.resolve(
14-
__dirname,
15-
'node_modules/react-router-dom',
16-
),
13+
// 'react-router-dom': path.resolve(
14+
// __dirname,
15+
// 'node_modules/react-router-dom',
16+
// ),
1717
},
1818
},
1919
server: {

packages/bridge/bridge-react-webpack-plugin/src/utis.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,28 @@ export const getBridgeRouterAlias = (
8787
originalAlias: string,
8888
): Record<string, string> => {
8989
const userDependencies = getDependencies();
90-
const reactRouterDomPath = originalAlias
91-
? originalAlias
92-
: userDependencies['react-router-dom']
93-
? require.resolve('react-router-dom')
94-
: '';
95-
const packageJsonPath = reactRouterDomPath
96-
? findPackageJson(reactRouterDomPath)
97-
: '';
90+
let reactRouterDomPath = '';
91+
92+
if (originalAlias) {
93+
reactRouterDomPath = originalAlias;
94+
} else if (userDependencies['react-router-dom']) {
95+
try {
96+
reactRouterDomPath = path.resolve(
97+
process.cwd(),
98+
'node_modules/react-router-dom',
99+
);
100+
} catch (error) {
101+
console.log(error);
102+
}
103+
}
104+
98105
// if find react-router-dom in package.json
99-
if (packageJsonPath) {
100-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
101-
const majorVersion = checkVersion(packageJson.version);
106+
if (reactRouterDomPath) {
107+
const packageJsonPath = findPackageJson(reactRouterDomPath) || '';
108+
const packageJsonContent = JSON.parse(
109+
fs.readFileSync(packageJsonPath, 'utf-8'),
110+
);
111+
const majorVersion = checkVersion(packageJsonContent.version);
102112
const bridgeRouterAlias = setRouterAlias(majorVersion, reactRouterDomPath);
103113
console.log(
104114
'<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>',

packages/bridge/bridge-react/src/router-v5.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function WraperRouter(
3636
}
3737

3838
// @ts-ignore
39-
// cause export directly from react-router-dom/index.js will cause build falied.
39+
// because export directly from react-router-dom/index.js will cause build falied.
4040
// it will be replace by react-router-dom/index.js in building phase
4141
export * from 'react-router-dom/';
4242

packages/bridge/bridge-react/src/router-v6.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function WraperRouterProvider(
6969
}
7070
}
7171

72-
export * from 'react-router-dom/dist/index.js';
72+
// export * from 'react-router-dom/dist/index.js';
73+
export * from 'react-router-dom/';
7374
export { WraperRouter as BrowserRouter };
7475
export { WraperRouterProvider as RouterProvider };

packages/bridge/bridge-react/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { FederationHost } from '@module-federation/runtime';
32
import { createLogger } from '@module-federation/sdk';
43

54
export const LoggerInstance = createLogger(

packages/bridge/bridge-react/vite.config.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { defineConfig } from 'vite';
2-
import vue from '@vitejs/plugin-vue';
2+
// import vue from '@vitejs/plugin-vue';
33
import path from 'path';
44
import dts from 'vite-plugin-dts';
5-
import react from '@vitejs/plugin-react';
5+
// import react from '@vitejs/plugin-react';
66
import packageJson from './package.json';
77

88
const perDepsKeys = Object.keys(packageJson.peerDependencies);
@@ -44,13 +44,13 @@ export default defineConfig({
4444
generateBundle(options, bundle) {
4545
for (const fileName in bundle) {
4646
const chunk = bundle[fileName];
47-
// if (fileName.includes('router-v6') && chunk.type === 'chunk') {
48-
// chunk.code = chunk.code.replace(
49-
// // Match 'react-router-dom/' followed by single quotes, double quotes, or backticks, replacing only 'react-router-dom/' to react-router-v6 dist file structure
50-
// /react-router-dom\/(?=[\'\"\`])/g,
51-
// 'react-router-dom/dist/index.js',
52-
// );
53-
// }
47+
if (fileName.includes('router-v6') && chunk.type === 'chunk') {
48+
chunk.code = chunk.code.replace(
49+
// Match 'react-router-dom/' followed by single quotes, double quotes, or backticks, replacing only 'react-router-dom/' to react-router-v6 dist file structure
50+
/react-router-dom\/(?=[\'\"\`])/g,
51+
'react-router-dom/dist/index.js',
52+
);
53+
}
5454

5555
if (fileName.includes('router-v5') && chunk.type === 'chunk') {
5656
chunk.code = chunk.code.replace(

0 commit comments

Comments
 (0)