@module-federation/bridge-react-webpack-plugin 0.20.0 → 0.21.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @module-federation/bridge-react-webpack-plugin
2
2
 
3
+ ## 0.21.1
4
+
5
+ ### Patch Changes
6
+
7
+ - @module-federation/sdk@0.21.1
8
+
9
+ ## 0.21.0
10
+
11
+ ### Minor Changes
12
+
13
+ - d225658: feat: Add React Router v7 Support to Module Federation Bridge
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [d1e90a4]
18
+ - @module-federation/sdk@0.21.0
19
+
3
20
  ## 0.20.0
4
21
 
5
22
  ### Patch Changes
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "react-router",
3
+ "version": "7.0.0",
4
+ "description": "Declarative routing for React applications",
5
+ "keywords": [
6
+ "react",
7
+ "router",
8
+ "route",
9
+ "routing",
10
+ "history",
11
+ "link"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/remix-run/react-router",
16
+ "directory": "packages/react-router"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Remix Software <hello@remix.run>",
20
+ "sideEffects": false,
21
+ "main": "./dist/main.js",
22
+ "unpkg": "./dist/umd/react-router.production.min.js",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "dependencies": {
26
+ "@remix-run/router": "2.0.0"
27
+ },
28
+ "devDependencies": {
29
+ "react": "^18.3.1",
30
+ "react-dom": "^18.3.1"
31
+ },
32
+ "peerDependencies": {
33
+ "react": ">=18.0.0",
34
+ "react-dom": ">=18.0.0"
35
+ },
36
+ "files": [
37
+ "dist/",
38
+ "CHANGELOG.md",
39
+ "LICENSE.md",
40
+ "README.md"
41
+ ],
42
+ "engines": {
43
+ "node": ">=18.0.0"
44
+ }
45
+ }
@@ -1,9 +1,6 @@
1
1
  import path from 'node:path';
2
- import {
3
- checkVersion,
4
- findPackageJson,
5
- getBridgeRouterAlias,
6
- } from '../src/utis';
2
+ import { checkVersion, findPackageJson } from '../src/utils';
3
+ import { getBridgeRouterAlias } from '../src/router-alias';
7
4
 
8
5
  const resolveRouterV5 = path.resolve(
9
6
  __dirname,
@@ -21,6 +18,14 @@ const resolveRouterV6_PkgPath = path.resolve(
21
18
  __dirname,
22
19
  '../__tests__/mockRouterDir/router-v6/react-router-dom/package.json',
23
20
  );
21
+ const resolveRouterV7 = path.resolve(
22
+ __dirname,
23
+ '../__tests__/mockRouterDir/router-v7/react-router',
24
+ );
25
+ const resolveRouterV7_PkgPath = path.resolve(
26
+ __dirname,
27
+ '../__tests__/mockRouterDir/router-v7/react-router/package.json',
28
+ );
24
29
 
25
30
  describe('test checkVersion: should return the correct major version for react-router-dom', () => {
26
31
  it('should return 5', () => {
@@ -42,16 +47,29 @@ describe('test checkVersion: should return the correct major version for react-r
42
47
  it('should return 6', () => {
43
48
  expect(checkVersion('^6.0.0')).toBe(6);
44
49
  });
50
+
51
+ it('should return 7', () => {
52
+ expect(checkVersion('7.0.0')).toBe(7);
53
+ });
54
+
55
+ it('should return 7', () => {
56
+ expect(checkVersion('~7.0.0')).toBe(7);
57
+ });
58
+
59
+ it('should return 7', () => {
60
+ expect(checkVersion('^7.0.0')).toBe(7);
61
+ });
45
62
  });
46
63
 
47
- describe('test findPackageJson: should return the correct package.json path for react-router-dom v5 and v6', () => {
64
+ describe('test findPackageJson: should return the correct package.json path for react-router-dom v5, v6 and react-router v7', () => {
48
65
  it('should return the package.json path', () => {
49
66
  expect(findPackageJson(resolveRouterV5)).toBe(resolveRouterV5_PkgPath);
50
67
  expect(findPackageJson(resolveRouterV6)).toBe(resolveRouterV6_PkgPath);
68
+ expect(findPackageJson(resolveRouterV7)).toBe(resolveRouterV7_PkgPath);
51
69
  });
52
70
  });
53
71
 
54
- describe('test getBridgeRouterAlias: should return the correct alias for react-router-dom v5 and v6', () => {
72
+ describe('test getBridgeRouterAlias: should return the correct alias for react-router-dom v5, v6 and react-router v7', () => {
55
73
  it('should return the correct alias for router v5', () => {
56
74
  const res = getBridgeRouterAlias(resolveRouterV5);
57
75
  expect(res).toEqual({
@@ -66,6 +84,19 @@ describe('test getBridgeRouterAlias: should return the correct alias for react-r
66
84
  expect(res).toEqual({
67
85
  'react-router-dom$':
68
86
  '@module-federation/bridge-react/dist/router-v6.es.js',
87
+ 'react-router-dom/dist/index.js': resolveRouterV6,
88
+ });
89
+ });
90
+
91
+ it('should return the correct alias for router v7', () => {
92
+ const res = getBridgeRouterAlias(resolveRouterV7);
93
+ expect(res).toEqual({
94
+ 'react-router$': '@module-federation/bridge-react/dist/router-v7.es.js',
95
+ 'react-router-dom$':
96
+ '@module-federation/bridge-react/dist/router-v7.es.js',
97
+ 'react-router/dist/development/index.js': resolveRouterV7,
98
+ 'react-router/dist/production/index.js': resolveRouterV7,
99
+ 'react-router-dom/dist/index.js': resolveRouterV7,
69
100
  });
70
101
  });
71
102
  });
package/dist/index.cjs.js CHANGED
@@ -1,27 +1,6 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
3
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
18
- // If the importer is in node compatibility mode or this is not an ESM
19
- // file that has been converted to a CommonJS file using a Babel-
20
- // compatible transform (i.e. "__esModule" has not been set), then set
21
- // "default" to the CommonJS "module.exports" for node compatibility.
22
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
- mod
24
- ));
25
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
26
5
  const fs = require("node:fs");
27
6
  const path = require("node:path");
@@ -1625,8 +1604,10 @@ const checkVersion = (version) => {
1625
1604
  const cleanVersion = versionMatch[0];
1626
1605
  if (semver$1.gte(cleanVersion, "5.0.0") && semver$1.lt(cleanVersion, "6.0.0")) {
1627
1606
  return 5;
1628
- } else if (semver$1.gte(cleanVersion, "6.0.0")) {
1607
+ } else if (semver$1.gte(cleanVersion, "6.0.0") && semver$1.lt(cleanVersion, "7.0.0")) {
1629
1608
  return 6;
1609
+ } else if (semver$1.gte(cleanVersion, "7.0.0")) {
1610
+ return 7;
1630
1611
  }
1631
1612
  return 0;
1632
1613
  };
@@ -1657,72 +1638,95 @@ const getDependencies = () => {
1657
1638
  };
1658
1639
  const reactRouterDomV5AliasPath = "@module-federation/bridge-react/dist/router-v5.es.js";
1659
1640
  const reactRouterDomV6AliasPath = "@module-federation/bridge-react/dist/router-v6.es.js";
1641
+ const reactRouterDomV7AliasPath = "@module-federation/bridge-react/dist/router-v7.es.js";
1642
+ const createReactRouterV7Alias = (reactRouterDomPath) => {
1643
+ const baseAlias = {
1644
+ "react-router$": reactRouterDomV7AliasPath,
1645
+ "react-router-dom$": reactRouterDomV7AliasPath
1646
+ // Keep compatibility for old imports
1647
+ };
1648
+ const resolvedDistPaths = {
1649
+ "react-router/dist/development/index.js": reactRouterDomPath,
1650
+ "react-router/dist/production/index.js": reactRouterDomPath
1651
+ };
1652
+ const legacyCompatibility = {
1653
+ "react-router-dom/dist/index.js": reactRouterDomPath
1654
+ };
1655
+ return {
1656
+ ...baseAlias,
1657
+ ...resolvedDistPaths,
1658
+ ...legacyCompatibility
1659
+ };
1660
+ };
1661
+ const createReactRouterV5Alias = (reactRouterDomPath) => {
1662
+ return {
1663
+ "react-router-dom$": reactRouterDomV5AliasPath,
1664
+ "react-router-dom/index.js": reactRouterDomPath
1665
+ };
1666
+ };
1667
+ const createReactRouterV6Alias = (reactRouterDomPath) => {
1668
+ return {
1669
+ "react-router-dom$": reactRouterDomV6AliasPath,
1670
+ "react-router-dom/dist/index.js": reactRouterDomPath
1671
+ };
1672
+ };
1660
1673
  const setRouterAlias = (majorVersion, reactRouterDomPath) => {
1661
- let bridgeRouterAlias = {};
1662
- if (majorVersion === 5) {
1663
- bridgeRouterAlias = {
1664
- "react-router-dom$": reactRouterDomV5AliasPath
1665
- };
1666
- try {
1667
- require.resolve("react-router-dom/index.js");
1668
- } catch (error) {
1669
- bridgeRouterAlias = {
1670
- ...bridgeRouterAlias,
1671
- "react-router-dom/index.js": reactRouterDomPath
1672
- };
1673
- }
1674
- } else if (majorVersion === 6) {
1675
- bridgeRouterAlias = {
1676
- "react-router-dom$": reactRouterDomV6AliasPath
1677
- };
1678
- try {
1679
- require.resolve("react-router-dom/dist/index.js");
1680
- } catch (error) {
1681
- bridgeRouterAlias = {
1682
- ...bridgeRouterAlias,
1683
- "react-router-dom/dist/index.js": reactRouterDomPath
1684
- };
1685
- }
1674
+ switch (majorVersion) {
1675
+ case 5:
1676
+ return createReactRouterV5Alias(reactRouterDomPath);
1677
+ case 6:
1678
+ return createReactRouterV6Alias(reactRouterDomPath);
1679
+ case 7:
1680
+ return createReactRouterV7Alias(reactRouterDomPath);
1681
+ default:
1682
+ console.warn(
1683
+ `Unsupported React Router version: ${majorVersion}. Defaulting to v7.`
1684
+ );
1685
+ return createReactRouterV7Alias(reactRouterDomPath);
1686
1686
  }
1687
- return bridgeRouterAlias;
1688
1687
  };
1689
1688
  const getBridgeRouterAlias = (originalAlias) => {
1690
1689
  const userDependencies = getDependencies();
1691
1690
  let reactRouterDomPath = "";
1692
1691
  if (originalAlias) {
1693
1692
  reactRouterDomPath = originalAlias;
1693
+ } else if (userDependencies["react-router"]) {
1694
+ reactRouterDomPath = path.resolve(
1695
+ process.cwd(),
1696
+ "node_modules/react-router"
1697
+ );
1694
1698
  } else if (userDependencies["react-router-dom"]) {
1695
- try {
1696
- reactRouterDomPath = path.resolve(
1697
- process.cwd(),
1698
- "node_modules/react-router-dom"
1699
- );
1700
- } catch (error) {
1701
- console.log(error);
1702
- }
1699
+ reactRouterDomPath = path.resolve(
1700
+ process.cwd(),
1701
+ "node_modules/react-router-dom"
1702
+ );
1703
1703
  }
1704
1704
  if (reactRouterDomPath) {
1705
- const packageJsonPath = findPackageJson(reactRouterDomPath) || "";
1706
- const packageJsonContent = JSON.parse(
1707
- fs.readFileSync(packageJsonPath, "utf-8")
1708
- );
1709
- const majorVersion = checkVersion(packageJsonContent.version);
1710
- const bridgeRouterAlias = setRouterAlias(majorVersion, reactRouterDomPath);
1711
- console.log(
1712
- "<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>",
1713
- bridgeRouterAlias
1714
- );
1715
- return bridgeRouterAlias;
1716
- } else {
1717
- const bridgeRouterAlias = {
1718
- "react-router-dom$": reactRouterDomV6AliasPath
1719
- };
1720
- console.log(
1721
- "<<<<<<<<<<<<< default bridgeRouterAlias >>>>>>>>>>>>>",
1722
- bridgeRouterAlias
1723
- );
1724
- return bridgeRouterAlias;
1705
+ const packageJsonPath = findPackageJson(reactRouterDomPath);
1706
+ if (packageJsonPath) {
1707
+ const packageJsonContent = JSON.parse(
1708
+ fs.readFileSync(packageJsonPath, "utf-8")
1709
+ );
1710
+ const majorVersion = checkVersion(packageJsonContent.version);
1711
+ const bridgeRouterAlias2 = setRouterAlias(
1712
+ majorVersion,
1713
+ reactRouterDomPath
1714
+ );
1715
+ console.log(
1716
+ "<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>",
1717
+ bridgeRouterAlias2
1718
+ );
1719
+ return bridgeRouterAlias2;
1720
+ }
1725
1721
  }
1722
+ const bridgeRouterAlias = {
1723
+ "react-router-dom$": reactRouterDomV6AliasPath
1724
+ };
1725
+ console.log(
1726
+ "<<<<<<<<<<<<< default bridgeRouterAlias >>>>>>>>>>>>>",
1727
+ bridgeRouterAlias
1728
+ );
1729
+ return bridgeRouterAlias;
1726
1730
  };
1727
1731
  class ReactBridgeAliasChangerPlugin {
1728
1732
  constructor(info) {
package/dist/index.es.js CHANGED
@@ -1603,8 +1603,10 @@ const checkVersion = (version) => {
1603
1603
  const cleanVersion = versionMatch[0];
1604
1604
  if (semver$1.gte(cleanVersion, "5.0.0") && semver$1.lt(cleanVersion, "6.0.0")) {
1605
1605
  return 5;
1606
- } else if (semver$1.gte(cleanVersion, "6.0.0")) {
1606
+ } else if (semver$1.gte(cleanVersion, "6.0.0") && semver$1.lt(cleanVersion, "7.0.0")) {
1607
1607
  return 6;
1608
+ } else if (semver$1.gte(cleanVersion, "7.0.0")) {
1609
+ return 7;
1608
1610
  }
1609
1611
  return 0;
1610
1612
  };
@@ -1635,72 +1637,95 @@ const getDependencies = () => {
1635
1637
  };
1636
1638
  const reactRouterDomV5AliasPath = "@module-federation/bridge-react/dist/router-v5.es.js";
1637
1639
  const reactRouterDomV6AliasPath = "@module-federation/bridge-react/dist/router-v6.es.js";
1640
+ const reactRouterDomV7AliasPath = "@module-federation/bridge-react/dist/router-v7.es.js";
1641
+ const createReactRouterV7Alias = (reactRouterDomPath) => {
1642
+ const baseAlias = {
1643
+ "react-router$": reactRouterDomV7AliasPath,
1644
+ "react-router-dom$": reactRouterDomV7AliasPath
1645
+ // Keep compatibility for old imports
1646
+ };
1647
+ const resolvedDistPaths = {
1648
+ "react-router/dist/development/index.js": reactRouterDomPath,
1649
+ "react-router/dist/production/index.js": reactRouterDomPath
1650
+ };
1651
+ const legacyCompatibility = {
1652
+ "react-router-dom/dist/index.js": reactRouterDomPath
1653
+ };
1654
+ return {
1655
+ ...baseAlias,
1656
+ ...resolvedDistPaths,
1657
+ ...legacyCompatibility
1658
+ };
1659
+ };
1660
+ const createReactRouterV5Alias = (reactRouterDomPath) => {
1661
+ return {
1662
+ "react-router-dom$": reactRouterDomV5AliasPath,
1663
+ "react-router-dom/index.js": reactRouterDomPath
1664
+ };
1665
+ };
1666
+ const createReactRouterV6Alias = (reactRouterDomPath) => {
1667
+ return {
1668
+ "react-router-dom$": reactRouterDomV6AliasPath,
1669
+ "react-router-dom/dist/index.js": reactRouterDomPath
1670
+ };
1671
+ };
1638
1672
  const setRouterAlias = (majorVersion, reactRouterDomPath) => {
1639
- let bridgeRouterAlias = {};
1640
- if (majorVersion === 5) {
1641
- bridgeRouterAlias = {
1642
- "react-router-dom$": reactRouterDomV5AliasPath
1643
- };
1644
- try {
1645
- require.resolve("react-router-dom/index.js");
1646
- } catch (error) {
1647
- bridgeRouterAlias = {
1648
- ...bridgeRouterAlias,
1649
- "react-router-dom/index.js": reactRouterDomPath
1650
- };
1651
- }
1652
- } else if (majorVersion === 6) {
1653
- bridgeRouterAlias = {
1654
- "react-router-dom$": reactRouterDomV6AliasPath
1655
- };
1656
- try {
1657
- require.resolve("react-router-dom/dist/index.js");
1658
- } catch (error) {
1659
- bridgeRouterAlias = {
1660
- ...bridgeRouterAlias,
1661
- "react-router-dom/dist/index.js": reactRouterDomPath
1662
- };
1663
- }
1673
+ switch (majorVersion) {
1674
+ case 5:
1675
+ return createReactRouterV5Alias(reactRouterDomPath);
1676
+ case 6:
1677
+ return createReactRouterV6Alias(reactRouterDomPath);
1678
+ case 7:
1679
+ return createReactRouterV7Alias(reactRouterDomPath);
1680
+ default:
1681
+ console.warn(
1682
+ `Unsupported React Router version: ${majorVersion}. Defaulting to v7.`
1683
+ );
1684
+ return createReactRouterV7Alias(reactRouterDomPath);
1664
1685
  }
1665
- return bridgeRouterAlias;
1666
1686
  };
1667
1687
  const getBridgeRouterAlias = (originalAlias) => {
1668
1688
  const userDependencies = getDependencies();
1669
1689
  let reactRouterDomPath = "";
1670
1690
  if (originalAlias) {
1671
1691
  reactRouterDomPath = originalAlias;
1692
+ } else if (userDependencies["react-router"]) {
1693
+ reactRouterDomPath = path.resolve(
1694
+ process.cwd(),
1695
+ "node_modules/react-router"
1696
+ );
1672
1697
  } else if (userDependencies["react-router-dom"]) {
1673
- try {
1674
- reactRouterDomPath = path.resolve(
1675
- process.cwd(),
1676
- "node_modules/react-router-dom"
1677
- );
1678
- } catch (error) {
1679
- console.log(error);
1680
- }
1698
+ reactRouterDomPath = path.resolve(
1699
+ process.cwd(),
1700
+ "node_modules/react-router-dom"
1701
+ );
1681
1702
  }
1682
1703
  if (reactRouterDomPath) {
1683
- const packageJsonPath = findPackageJson(reactRouterDomPath) || "";
1684
- const packageJsonContent = JSON.parse(
1685
- fs.readFileSync(packageJsonPath, "utf-8")
1686
- );
1687
- const majorVersion = checkVersion(packageJsonContent.version);
1688
- const bridgeRouterAlias = setRouterAlias(majorVersion, reactRouterDomPath);
1689
- console.log(
1690
- "<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>",
1691
- bridgeRouterAlias
1692
- );
1693
- return bridgeRouterAlias;
1694
- } else {
1695
- const bridgeRouterAlias = {
1696
- "react-router-dom$": reactRouterDomV6AliasPath
1697
- };
1698
- console.log(
1699
- "<<<<<<<<<<<<< default bridgeRouterAlias >>>>>>>>>>>>>",
1700
- bridgeRouterAlias
1701
- );
1702
- return bridgeRouterAlias;
1704
+ const packageJsonPath = findPackageJson(reactRouterDomPath);
1705
+ if (packageJsonPath) {
1706
+ const packageJsonContent = JSON.parse(
1707
+ fs.readFileSync(packageJsonPath, "utf-8")
1708
+ );
1709
+ const majorVersion = checkVersion(packageJsonContent.version);
1710
+ const bridgeRouterAlias2 = setRouterAlias(
1711
+ majorVersion,
1712
+ reactRouterDomPath
1713
+ );
1714
+ console.log(
1715
+ "<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>",
1716
+ bridgeRouterAlias2
1717
+ );
1718
+ return bridgeRouterAlias2;
1719
+ }
1703
1720
  }
1721
+ const bridgeRouterAlias = {
1722
+ "react-router-dom$": reactRouterDomV6AliasPath
1723
+ };
1724
+ console.log(
1725
+ "<<<<<<<<<<<<< default bridgeRouterAlias >>>>>>>>>>>>>",
1726
+ bridgeRouterAlias
1727
+ );
1728
+ return bridgeRouterAlias;
1704
1729
  };
1705
1730
  class ReactBridgeAliasChangerPlugin {
1706
1731
  constructor(info) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/bridge-react-webpack-plugin",
3
- "version": "0.20.0",
3
+ "version": "0.21.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -25,7 +25,7 @@
25
25
  "dependencies": {
26
26
  "semver": "7.6.3",
27
27
  "@types/semver": "7.5.8",
28
- "@module-federation/sdk": "0.20.0"
28
+ "@module-federation/sdk": "0.21.1"
29
29
  },
30
30
  "devDependencies": {
31
31
  "typescript": "^5.2.2",
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import type { moduleFederationPlugin } from '@module-federation/sdk';
4
- import { getBridgeRouterAlias } from './utis';
4
+ import { getBridgeRouterAlias } from './router-alias';
5
5
 
6
6
  class ReactBridgeAliasChangerPlugin {
7
7
  alias: string;
@@ -0,0 +1,124 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { checkVersion, findPackageJson, getDependencies } from './utils';
4
+
5
+ const reactRouterDomV5AliasPath =
6
+ '@module-federation/bridge-react/dist/router-v5.es.js';
7
+ const reactRouterDomV6AliasPath =
8
+ '@module-federation/bridge-react/dist/router-v6.es.js';
9
+ const reactRouterDomV7AliasPath =
10
+ '@module-federation/bridge-react/dist/router-v7.es.js';
11
+
12
+ const createReactRouterV7Alias = (
13
+ reactRouterDomPath: string,
14
+ ): Record<string, string> => {
15
+ const baseAlias = {
16
+ 'react-router$': reactRouterDomV7AliasPath,
17
+ 'react-router-dom$': reactRouterDomV7AliasPath, // Keep compatibility for old imports
18
+ };
19
+
20
+ const resolvedDistPaths: Record<string, string> = {
21
+ 'react-router/dist/development/index.js': reactRouterDomPath,
22
+ 'react-router/dist/production/index.js': reactRouterDomPath,
23
+ };
24
+
25
+ const legacyCompatibility: Record<string, string> = {
26
+ 'react-router-dom/dist/index.js': reactRouterDomPath,
27
+ };
28
+
29
+ return {
30
+ ...baseAlias,
31
+ ...resolvedDistPaths,
32
+ ...legacyCompatibility,
33
+ };
34
+ };
35
+
36
+ const createReactRouterV5Alias = (
37
+ reactRouterDomPath: string,
38
+ ): Record<string, string> => {
39
+ return {
40
+ 'react-router-dom$': reactRouterDomV5AliasPath,
41
+ 'react-router-dom/index.js': reactRouterDomPath,
42
+ };
43
+ };
44
+
45
+ const createReactRouterV6Alias = (
46
+ reactRouterDomPath: string,
47
+ ): Record<string, string> => {
48
+ return {
49
+ 'react-router-dom$': reactRouterDomV6AliasPath,
50
+ 'react-router-dom/dist/index.js': reactRouterDomPath,
51
+ };
52
+ };
53
+
54
+ const setRouterAlias = (
55
+ majorVersion: number,
56
+ reactRouterDomPath: string,
57
+ ): Record<string, string> => {
58
+ switch (majorVersion) {
59
+ case 5:
60
+ return createReactRouterV5Alias(reactRouterDomPath);
61
+ case 6:
62
+ return createReactRouterV6Alias(reactRouterDomPath);
63
+ case 7:
64
+ return createReactRouterV7Alias(reactRouterDomPath);
65
+ default:
66
+ console.warn(
67
+ `Unsupported React Router version: ${majorVersion}. Defaulting to v7.`,
68
+ );
69
+ return createReactRouterV7Alias(reactRouterDomPath);
70
+ }
71
+ };
72
+
73
+ export const getBridgeRouterAlias = (
74
+ originalAlias: string,
75
+ ): Record<string, string> => {
76
+ const userDependencies = getDependencies();
77
+ let reactRouterDomPath = '';
78
+
79
+ if (originalAlias) {
80
+ reactRouterDomPath = originalAlias;
81
+ } else if (userDependencies['react-router']) {
82
+ // React Router v7 uses 'react-router' package name
83
+ reactRouterDomPath = path.resolve(
84
+ process.cwd(),
85
+ 'node_modules/react-router',
86
+ );
87
+ } else if (userDependencies['react-router-dom']) {
88
+ // React Router v5/v6 uses 'react-router-dom' package name
89
+ reactRouterDomPath = path.resolve(
90
+ process.cwd(),
91
+ 'node_modules/react-router-dom',
92
+ );
93
+ }
94
+
95
+ // Generate alias based on detected router package
96
+ if (reactRouterDomPath) {
97
+ const packageJsonPath = findPackageJson(reactRouterDomPath);
98
+ if (packageJsonPath) {
99
+ const packageJsonContent = JSON.parse(
100
+ fs.readFileSync(packageJsonPath, 'utf-8'),
101
+ );
102
+ const majorVersion = checkVersion(packageJsonContent.version);
103
+ const bridgeRouterAlias = setRouterAlias(
104
+ majorVersion,
105
+ reactRouterDomPath,
106
+ );
107
+ console.log(
108
+ '<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>',
109
+ bridgeRouterAlias,
110
+ );
111
+ return bridgeRouterAlias;
112
+ }
113
+ }
114
+
115
+ // Default to v6 which uses 'react-router-dom'
116
+ const bridgeRouterAlias = {
117
+ 'react-router-dom$': reactRouterDomV6AliasPath,
118
+ };
119
+ console.log(
120
+ '<<<<<<<<<<<<< default bridgeRouterAlias >>>>>>>>>>>>>',
121
+ bridgeRouterAlias,
122
+ );
123
+ return bridgeRouterAlias;
124
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,52 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import semver from 'semver';
4
+
5
+ export const checkVersion = (version: string) => {
6
+ // Extract the version number starting from the first digit
7
+ const versionMatch = version.match(/\d.*/);
8
+ if (!versionMatch) return 0;
9
+
10
+ const cleanVersion = versionMatch[0];
11
+
12
+ if (semver.gte(cleanVersion, '5.0.0') && semver.lt(cleanVersion, '6.0.0')) {
13
+ return 5;
14
+ } else if (
15
+ semver.gte(cleanVersion, '6.0.0') &&
16
+ semver.lt(cleanVersion, '7.0.0')
17
+ ) {
18
+ return 6;
19
+ } else if (semver.gte(cleanVersion, '7.0.0')) {
20
+ return 7;
21
+ }
22
+
23
+ return 0;
24
+ };
25
+
26
+ export const findPackageJson = (startPath: string): string | null => {
27
+ let currentPath = startPath;
28
+ while (currentPath !== path.parse(currentPath).root) {
29
+ const packageJsonPath = path.join(currentPath, 'package.json');
30
+ if (fs.existsSync(packageJsonPath)) {
31
+ return packageJsonPath;
32
+ }
33
+ currentPath = path.dirname(currentPath);
34
+ }
35
+ return null;
36
+ };
37
+
38
+ export const getDependencies = () => {
39
+ const userPackageJsonPath = path.resolve(process.cwd(), 'package.json');
40
+ let userDependencies: Record<string, string> = {};
41
+
42
+ if (fs.existsSync(userPackageJsonPath)) {
43
+ const userPackageJson = JSON.parse(
44
+ fs.readFileSync(userPackageJsonPath, 'utf-8'),
45
+ );
46
+ userDependencies = {
47
+ ...userPackageJson.dependencies,
48
+ ...userPackageJson.devDependencies,
49
+ };
50
+ }
51
+ return userDependencies;
52
+ };
package/src/utis.ts DELETED
@@ -1,128 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import semver from 'semver';
4
-
5
- export const checkVersion = (version: string) => {
6
- // Extract the version number starting from the first digit
7
- const versionMatch = version.match(/\d.*/);
8
- if (!versionMatch) return 0;
9
-
10
- const cleanVersion = versionMatch[0];
11
-
12
- if (semver.gte(cleanVersion, '5.0.0') && semver.lt(cleanVersion, '6.0.0')) {
13
- return 5;
14
- } else if (semver.gte(cleanVersion, '6.0.0')) {
15
- return 6;
16
- }
17
-
18
- return 0;
19
- };
20
-
21
- export const findPackageJson = (startPath: string): string | null => {
22
- let currentPath = startPath;
23
- while (currentPath !== path.parse(currentPath).root) {
24
- const packageJsonPath = path.join(currentPath, 'package.json');
25
- if (fs.existsSync(packageJsonPath)) {
26
- return packageJsonPath;
27
- }
28
- currentPath = path.dirname(currentPath);
29
- }
30
- return null;
31
- };
32
-
33
- const getDependencies = () => {
34
- const userPackageJsonPath = path.resolve(process.cwd(), 'package.json');
35
- let userDependencies: Record<string, string> = {};
36
-
37
- if (fs.existsSync(userPackageJsonPath)) {
38
- const userPackageJson = JSON.parse(
39
- fs.readFileSync(userPackageJsonPath, 'utf-8'),
40
- );
41
- userDependencies = {
42
- ...userPackageJson.dependencies,
43
- ...userPackageJson.devDependencies,
44
- };
45
- }
46
- return userDependencies;
47
- };
48
-
49
- const reactRouterDomV5AliasPath =
50
- '@module-federation/bridge-react/dist/router-v5.es.js';
51
- const reactRouterDomV6AliasPath =
52
- '@module-federation/bridge-react/dist/router-v6.es.js';
53
-
54
- const setRouterAlias = (majorVersion: number, reactRouterDomPath: string) => {
55
- let bridgeRouterAlias = {};
56
- if (majorVersion === 5) {
57
- bridgeRouterAlias = {
58
- 'react-router-dom$': reactRouterDomV5AliasPath,
59
- };
60
- try {
61
- require.resolve('react-router-dom/index.js');
62
- } catch (error) {
63
- // if react-router-dom/index.js cannot be resolved, set the alias to origin reactRouterDomPath
64
- bridgeRouterAlias = {
65
- ...bridgeRouterAlias,
66
- 'react-router-dom/index.js': reactRouterDomPath,
67
- };
68
- }
69
- } else if (majorVersion === 6) {
70
- bridgeRouterAlias = {
71
- 'react-router-dom$': reactRouterDomV6AliasPath,
72
- };
73
- try {
74
- require.resolve('react-router-dom/dist/index.js');
75
- } catch (error) {
76
- // if react-router-dom/dist/index.js cannot be resolved, set the alias to origin reactRouterDomPath
77
- bridgeRouterAlias = {
78
- ...bridgeRouterAlias,
79
- 'react-router-dom/dist/index.js': reactRouterDomPath,
80
- };
81
- }
82
- }
83
- return bridgeRouterAlias;
84
- };
85
-
86
- export const getBridgeRouterAlias = (
87
- originalAlias: string,
88
- ): Record<string, string> => {
89
- const userDependencies = getDependencies();
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
-
105
- // if find react-router-dom in package.json
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);
112
- const bridgeRouterAlias = setRouterAlias(majorVersion, reactRouterDomPath);
113
- console.log(
114
- '<<<<<<<<<<<<< bridgeRouterAlias >>>>>>>>>>>>>',
115
- bridgeRouterAlias,
116
- );
117
- return bridgeRouterAlias;
118
- } else {
119
- const bridgeRouterAlias = {
120
- 'react-router-dom$': reactRouterDomV6AliasPath,
121
- };
122
- console.log(
123
- '<<<<<<<<<<<<< default bridgeRouterAlias >>>>>>>>>>>>>',
124
- bridgeRouterAlias,
125
- );
126
- return bridgeRouterAlias;
127
- }
128
- };