core
core copied to clipboard
external-remotes-plugin not working with @module-federation/enhanced/webpack
Describe the bug
I am using the external-remotes-plugin in webpack configuration to determine the final URL of the remote at runtime.
When upgrading to @module-federation/enhanced using the webpack plugin, the remote urls are not replaced anymore.
GET http://localhost:3001/[window.externalRemote] net::ERR_FAILED 404 (Not Found)
Reproduction
https://github.com/GonchuB/module-federation-v2-external-repro
Used Package Manager
npm
System Info
System:
OS: macOS 15.3.1
CPU: (10) arm64 Apple M1 Pro
Memory: 84.39 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.20.4 - ~/.nvm/versions/node/v18.20.4/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v18.20.4/bin/npm
pnpm: 9.5.0 - ~/Library/pnpm/pnpm
Browsers:
Chrome: 133.0.6943.127
Edge: 111.0.1661.62
Safari: 18.3
Validations
- [x] Read the docs.
- [x] Read the common issues list.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Make sure this is a Module federation issue and not a framework-specific issue.
- [x] The provided reproduction is a minimal reproducible example of the bug.
You dont need it. just write your own runtime plugin. https://module-federation.io/plugin/dev/index.html
Something like this is currently working for my use case. Will close:
function ModuleFederationEvaluateRemotePlugin() {
return {
name: 'evaluate-remote-entry',
beforeRequest(args) {
if (args && args.options && args.options.remotes) {
const { remotes } = args.options;
remotes.forEach(remote => {
if (typeof remote.entry === 'string') {
// remote entry format is "@module-name": "module_entry@[window.someFunction('module_entry')]"
const expression = remote.entry.trim();
const bracketMatch = expression.match(/\[(.*?)\]/);
if (bracketMatch) {
const bracketedExpr = bracketMatch[1].trim(); // Content inside brackets
try {
const value = new Function(`return ${bracketedExpr}`)();
// Replace the bracketed portion with the resolved value
remote.entry = typeof value === 'function' ? value() : value;
} catch (error) {
console.error(`Error evaluating bracketed entry for remote ${remote.name}:`, error);
}
}
}
});
}
return args;
},
};
}