Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bridge-react): wrap react-router-dom path resolve with try catch #3232

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/ten-games-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@module-federation/bridge-react-webpack-plugin': patch
'@module-federation/bridge-react': patch
---

fix: wrap try catch with react-router-dom path resolve
8 changes: 4 additions & 4 deletions apps/router-demo/router-remote1-2001/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export default defineConfig({
react: path.resolve(__dirname, 'node_modules/react'),
'react-dom': path.resolve(__dirname, 'node_modules/react-dom'),
// 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.
'react-router-dom': path.resolve(
__dirname,
'node_modules/react-router-dom',
),
// 'react-router-dom': path.resolve(
// __dirname,
// 'node_modules/react-router-dom',
// ),
},
},
server: {
Expand Down
32 changes: 21 additions & 11 deletions packages/bridge/bridge-react-webpack-plugin/src/utis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,28 @@ export const getBridgeRouterAlias = (
originalAlias: string,
): Record<string, string> => {
const userDependencies = getDependencies();
const reactRouterDomPath = originalAlias
? originalAlias
: userDependencies['react-router-dom']
? require.resolve('react-router-dom')
: '';
const packageJsonPath = reactRouterDomPath
? findPackageJson(reactRouterDomPath)
: '';
let reactRouterDomPath = '';

if (originalAlias) {
reactRouterDomPath = originalAlias;
} else if (userDependencies['react-router-dom']) {
try {
reactRouterDomPath = path.resolve(
process.cwd(),
'node_modules/react-router-dom',
);
} catch (error) {
console.log(error);
}
}

// if find react-router-dom in package.json
if (packageJsonPath) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const majorVersion = checkVersion(packageJson.version);
if (reactRouterDomPath) {
const packageJsonPath = findPackageJson(reactRouterDomPath) || '';
const packageJsonContent = JSON.parse(
fs.readFileSync(packageJsonPath, 'utf-8'),
);
const majorVersion = checkVersion(packageJsonContent.version);
const bridgeRouterAlias = setRouterAlias(majorVersion, reactRouterDomPath);
console.log(
'<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>',
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/bridge-react/src/router-v5.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function WraperRouter(
}

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

Expand Down
3 changes: 2 additions & 1 deletion packages/bridge/bridge-react/src/router-v6.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function WraperRouterProvider(
}
}

export * from 'react-router-dom/dist/index.js';
// export * from 'react-router-dom/dist/index.js';
export * from 'react-router-dom/';
export { WraperRouter as BrowserRouter };
export { WraperRouterProvider as RouterProvider };
1 change: 0 additions & 1 deletion packages/bridge/bridge-react/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { FederationHost } from '@module-federation/runtime';
import { createLogger } from '@module-federation/sdk';

export const LoggerInstance = createLogger(
Expand Down
18 changes: 9 additions & 9 deletions packages/bridge/bridge-react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// import vue from '@vitejs/plugin-vue';
import path from 'path';
import dts from 'vite-plugin-dts';
import react from '@vitejs/plugin-react';
// import react from '@vitejs/plugin-react';
import packageJson from './package.json';

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

if (fileName.includes('router-v5') && chunk.type === 'chunk') {
chunk.code = chunk.code.replace(
Expand Down
Loading