core icon indicating copy to clipboard operation
core copied to clipboard

external-remotes-plugin not working with @module-federation/enhanced/webpack

Open GonchuB opened this issue 1 year ago • 2 comments

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

GonchuB avatar Feb 24 '25 17:02 GonchuB

You dont need it. just write your own runtime plugin. https://module-federation.io/plugin/dev/index.html

ScriptedAlchemy avatar Feb 25 '25 02:02 ScriptedAlchemy

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;
        },
    };
}

GonchuB avatar Feb 25 '25 09:02 GonchuB