@module-federation/bridge-react-webpack-plugin 0.0.0-fix-lazy-comile-20250925082726
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 +577 -0
- package/LICENSE +21 -0
- package/README.md +131 -0
- package/__tests__/mockRouterDir/router-v5/react-router-dom/package.json +71 -0
- package/__tests__/mockRouterDir/router-v6/react-router-dom/package.json +49 -0
- package/__tests__/utils.spec.ts +71 -0
- package/dist/index.cjs.d.ts +14 -0
- package/dist/index.cjs.js +1772 -0
- package/dist/index.es.js +1752 -0
- package/package.json +40 -0
- package/project.json +29 -0
- package/src/index.ts +62 -0
- package/src/utis.ts +128 -0
- package/tsconfig.json +42 -0
- package/tsconfig.node.json +11 -0
- package/vite.config.ts +24 -0
- package/vitest.config.ts +22 -0
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/bridge-react-webpack-plugin",
|
|
3
|
+
"version": "0.0.0-fix-lazy-comile-20250925082726",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"author": "zhouxiao <codingzx@gmail.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/module-federation/core.git",
|
|
12
|
+
"directory": "packages/bridge/bridge-react-webpack-plugin"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.cjs.js",
|
|
15
|
+
"module": "./dist/index.es.js",
|
|
16
|
+
"types": "./dist/index.cjs.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.cjs.d.ts",
|
|
20
|
+
"import": "./dist/index.es.js",
|
|
21
|
+
"require": "./dist/index.cjs.js"
|
|
22
|
+
},
|
|
23
|
+
"./*": "./*"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"semver": "7.6.3",
|
|
27
|
+
"@types/semver": "7.5.8",
|
|
28
|
+
"@module-federation/sdk": "0.0.0-fix-lazy-comile-20250925082726"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "^5.2.2",
|
|
32
|
+
"vite": "^5.4.18",
|
|
33
|
+
"vite-plugin-dts": "^4.3.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"dev": "vite",
|
|
37
|
+
"build": "vite build",
|
|
38
|
+
"preview": "vite preview"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bridge-react-webpack-plugin",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/bridge/bridge-react-webpack-plugin/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": ["type:pkg"],
|
|
7
|
+
"targets": {
|
|
8
|
+
"build": {
|
|
9
|
+
"executor": "nx:run-commands",
|
|
10
|
+
"options": {
|
|
11
|
+
"commands": [
|
|
12
|
+
"npm run build --prefix packages/bridge/bridge-react-webpack-plugin"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"test": {
|
|
17
|
+
"executor": "nx:run-commands",
|
|
18
|
+
"options": {
|
|
19
|
+
"parallel": false,
|
|
20
|
+
"commands": [
|
|
21
|
+
{
|
|
22
|
+
"command": "vitest run -c packages/bridge/bridge-react-webpack-plugin/vitest.config.ts",
|
|
23
|
+
"forwardAllArgs": false
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import type { moduleFederationPlugin } from '@module-federation/sdk';
|
|
4
|
+
import { getBridgeRouterAlias } from './utis';
|
|
5
|
+
|
|
6
|
+
class ReactBridgeAliasChangerPlugin {
|
|
7
|
+
alias: string;
|
|
8
|
+
targetFile: string;
|
|
9
|
+
moduleFederationOptions: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
10
|
+
constructor(info: {
|
|
11
|
+
moduleFederationOptions: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
12
|
+
}) {
|
|
13
|
+
this.moduleFederationOptions = info.moduleFederationOptions;
|
|
14
|
+
this.alias = 'react-router-dom$';
|
|
15
|
+
this.targetFile = '@module-federation/bridge-react/dist/router.es.js';
|
|
16
|
+
|
|
17
|
+
if (this.moduleFederationOptions.shared) {
|
|
18
|
+
if (Array.isArray(this.moduleFederationOptions.shared)) {
|
|
19
|
+
if (this.moduleFederationOptions.shared.includes('react-router-dom')) {
|
|
20
|
+
throw Error(
|
|
21
|
+
'React-router-dom cannot be set to shared after react bridge is used',
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
} else {
|
|
25
|
+
if (this.moduleFederationOptions.shared['react-router-dom']) {
|
|
26
|
+
throw Error(
|
|
27
|
+
'React-router-dom cannot be set to shared after react bridge is used',
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
apply(compiler: any) {
|
|
35
|
+
compiler.hooks.afterEnvironment.tap('ReactBridgeAliasPlugin', () => {
|
|
36
|
+
// Gets the path to the node_modules directory
|
|
37
|
+
const nodeModulesPath = path.resolve(compiler.context, 'node_modules');
|
|
38
|
+
const targetFilePath = path.join(nodeModulesPath, this.targetFile);
|
|
39
|
+
|
|
40
|
+
if (fs.existsSync(targetFilePath)) {
|
|
41
|
+
const originalResolve = compiler.options.resolve || {};
|
|
42
|
+
const originalAlias = originalResolve.alias || {};
|
|
43
|
+
|
|
44
|
+
// Update alias
|
|
45
|
+
const updatedAlias = {
|
|
46
|
+
// allow `alias` can be override
|
|
47
|
+
// [this.alias]: targetFilePath,
|
|
48
|
+
...getBridgeRouterAlias(originalAlias['react-router-dom']),
|
|
49
|
+
...originalAlias,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Update the webpack configuration
|
|
53
|
+
compiler.options.resolve = {
|
|
54
|
+
...originalResolve,
|
|
55
|
+
alias: updatedAlias,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default ReactBridgeAliasChangerPlugin;
|
package/src/utis.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
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
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"useDefineForClassFields": true,
|
|
4
|
+
|
|
5
|
+
/* Bundler mode */
|
|
6
|
+
"allowImportingTsExtensions": true,
|
|
7
|
+
"resolveJsonModule": true,
|
|
8
|
+
|
|
9
|
+
"declarationDir": "./dist/types",
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"module": "commonjs",
|
|
12
|
+
"target": "es5",
|
|
13
|
+
|
|
14
|
+
/* Linting */
|
|
15
|
+
"noUnusedLocals": true,
|
|
16
|
+
"noUnusedParameters": true,
|
|
17
|
+
"noFallthroughCasesInSwitch": true,
|
|
18
|
+
"declaration": true,
|
|
19
|
+
"emitDeclarationOnly": true,
|
|
20
|
+
"outDir": "dist",
|
|
21
|
+
"skipLibCheck": true,
|
|
22
|
+
"strict": true,
|
|
23
|
+
"moduleResolution": "node",
|
|
24
|
+
"lib": ["esnext", "dom"],
|
|
25
|
+
"jsx": "preserve",
|
|
26
|
+
"esModuleInterop": true,
|
|
27
|
+
"allowSyntheticDefaultImports": true,
|
|
28
|
+
"sourceMap": true,
|
|
29
|
+
"baseUrl": ".",
|
|
30
|
+
"paths": {
|
|
31
|
+
"@/*": ["src/*"]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"include": [
|
|
35
|
+
"src/**/*.ts",
|
|
36
|
+
"src/**/*.tsx",
|
|
37
|
+
"src/**/*.vue",
|
|
38
|
+
"src/remoteApp.tsx",
|
|
39
|
+
"src/create.ts"
|
|
40
|
+
],
|
|
41
|
+
"references": [{ "path": "./tsconfig.node.json" }]
|
|
42
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import dts from 'vite-plugin-dts';
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
plugins: [
|
|
7
|
+
dts({
|
|
8
|
+
rollupTypes: true,
|
|
9
|
+
}),
|
|
10
|
+
],
|
|
11
|
+
build: {
|
|
12
|
+
lib: {
|
|
13
|
+
entry: {
|
|
14
|
+
index: path.resolve(__dirname, 'src/index.ts'),
|
|
15
|
+
},
|
|
16
|
+
formats: ['cjs', 'es'],
|
|
17
|
+
fileName: (format, entryName) => `${entryName}.${format}.js`,
|
|
18
|
+
},
|
|
19
|
+
rollupOptions: {
|
|
20
|
+
external: ['node:fs', 'node:path'],
|
|
21
|
+
},
|
|
22
|
+
minify: false,
|
|
23
|
+
},
|
|
24
|
+
});
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
define: {
|
|
6
|
+
__DEV__: true,
|
|
7
|
+
__TEST__: true,
|
|
8
|
+
__BROWSER__: false,
|
|
9
|
+
__VERSION__: '"unknown"',
|
|
10
|
+
__APP_VERSION__: '"0.0.0"',
|
|
11
|
+
},
|
|
12
|
+
plugins: [nxViteTsPaths()],
|
|
13
|
+
test: {
|
|
14
|
+
environment: 'jsdom',
|
|
15
|
+
include: [
|
|
16
|
+
path.resolve(__dirname, '__tests__/*.spec.ts'),
|
|
17
|
+
path.resolve(__dirname, '__tests__/*.spec.tsx'),
|
|
18
|
+
],
|
|
19
|
+
globals: true,
|
|
20
|
+
testTimeout: 10000,
|
|
21
|
+
},
|
|
22
|
+
});
|