-
-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathModuleFederationPlugin.ts
More file actions
141 lines (125 loc) · 4.67 KB
/
Copy pathModuleFederationPlugin.ts
File metadata and controls
141 lines (125 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
bindLoggerToCompiler,
infrastructureLogger,
type moduleFederationPlugin,
} from '@module-federation/sdk';
import type IModuleFederationPlugin from '../lib/container/ModuleFederationPlugin';
import path from 'node:path';
import fs from 'node:fs';
import ReactBridgePlugin from '@module-federation/bridge-react-webpack-plugin';
import BaseWrapperPlugin from './BaseWrapperPlugin';
export const PLUGIN_NAME = 'ModuleFederationPlugin';
export default class ModuleFederationPlugin extends BaseWrapperPlugin {
private _mfPlugin?: IModuleFederationPlugin;
constructor(options: moduleFederationPlugin.ModuleFederationPluginOptions) {
super(options, PLUGIN_NAME, '../lib/container/ModuleFederationPlugin');
}
protected override createCorePluginInstance(
CorePlugin: any,
compiler: any,
): void {
bindLoggerToCompiler(
infrastructureLogger,
compiler,
'EnhancedModuleFederationPlugin',
);
this._mfPlugin = new CorePlugin(this._options);
this._mfPlugin!.apply(compiler);
const checkBridgeReactInstalled = () => {
try {
const userPackageJsonPath = path.resolve(
compiler.context,
'package.json',
);
if (fs.existsSync(userPackageJsonPath)) {
const userPackageJson = JSON.parse(
fs.readFileSync(userPackageJsonPath, 'utf-8'),
);
const userDependencies = {
...userPackageJson.dependencies,
...userPackageJson.devDependencies,
};
return !!userDependencies['@module-federation/bridge-react'];
}
return false;
} catch (error) {
return false;
}
};
const hasBridgeReact = checkBridgeReactInstalled();
const shouldEnableBridgePlugin = () => {
// Priority 1: Explicit enableBridgeRouter configuration
if (this._options?.bridge?.enableBridgeRouter === true) {
return true;
}
// Priority 2: Explicit disable via enableBridgeRouter:false or disableAlias:true
if (
this._options?.bridge?.enableBridgeRouter === false ||
this._options?.bridge?.disableAlias === true
) {
if (this._options?.bridge?.disableAlias === true) {
infrastructureLogger.warn(
'⚠️ [ModuleFederationPlugin] The `disableAlias` option is deprecated and will be removed in a future version.\n' +
' Please use `enableBridgeRouter: false` instead:\n' +
' {\n' +
' bridge: {\n' +
' enableBridgeRouter: false // Use this instead of disableAlias: true\n' +
' }\n' +
' }',
);
}
return false;
}
// Priority 3: Automatic detection based on bridge-react installation
if (hasBridgeReact) {
infrastructureLogger.info(
'💡 [ModuleFederationPlugin] Detected @module-federation/bridge-react in your dependencies.\n' +
' For better control and to avoid future breaking changes, please explicitly set:\n' +
' {\n' +
' bridge: {\n' +
' enableBridgeRouter: true // Explicitly enable bridge router\n' +
' }\n' +
' }',
);
return true;
}
return false;
};
const enableBridgePlugin = shouldEnableBridgePlugin();
// When bridge plugin is disabled (router disabled), alias to /base entry
if (!enableBridgePlugin && hasBridgeReact) {
compiler.hooks.afterPlugins.tap('BridgeReactBaseAliasPlugin', () => {
try {
const path = require('path');
const fs = require('fs');
const bridgeReactBasePath = path.resolve(
compiler.context,
'node_modules/@module-federation/bridge-react/dist/base.es.js',
);
if (!fs.existsSync(bridgeReactBasePath)) {
infrastructureLogger.warn(
'⚠️ [ModuleFederationPlugin] bridge-react /base entry not found, falling back to default entry',
);
return;
}
compiler.options.resolve.alias = {
...compiler.options.resolve.alias,
'@module-federation/bridge-react$': bridgeReactBasePath,
};
infrastructureLogger.info(
'✅ [ModuleFederationPlugin] Router disabled - using /base entry (no react-router-dom)',
);
} catch (error) {
infrastructureLogger.warn(
'⚠️ [ModuleFederationPlugin] Failed to set /base alias, using default entry',
);
}
});
}
if (enableBridgePlugin) {
new ReactBridgePlugin({
moduleFederationOptions: this._options,
}).apply(compiler);
}
}
}