rsbuild icon indicating copy to clipboard operation
rsbuild copied to clipboard

[Feature]: Federated Modules: Dynamic Remotes with synchronous imports

Open zimo888 opened this issue 2 years ago • 6 comments

What problem does this feature solve?

Can module federation's config support dynamic url and replace it on runtime ?

like external-remotes-plugin

What does the proposed API look like?

 moduleFederation: {
    options: {
      name: 'host',
      remotes: {
        remote: 'remote@[window.entry.remote]',
        antd: 'antd@[window.entry.base]',
      },
    },
  }

Host source somewhere before loading main entry file

window.entry=  {
   remote: "[cdn_url]",
   base: "[cdn_url]",
}

import("./bootstrap");

Then I wish another plugin can replace some module when compile start

for example replace all antd in project

import {Button} from 'antd'

to

import {Button} from 'base/antd'

In this way, some modules can be pre compiled , and some basic modules version can be easily managed.

Thank you!

zimo888 avatar Mar 19 '24 08:03 zimo888

Rsbuild's moduleFederation.options config is the same as rspack.container.ModuleFederationPlugin, we will try not to change the meaning of this config.

Can we just use the external-remotes-plugin in an Rsbuild project?

chenjiahan avatar Mar 21 '24 03:03 chenjiahan

This is the source code for the external-remotes-plugin. I noticed that there are some Webpack hooks that have not been implemented in Rspack, and I’m not sure if this kind of implementation will be supported by Rsbuild.

/*!
 * ExternalTemplateRemotesPlugin
 * License: MIT (https://mit-license.org/)
 */

const extractUrlAndGlobal = require('webpack/lib/util/extractUrlAndGlobal');
const { RawSource } = require('webpack-sources');

const PLUGIN_NAME = 'ExternalTemplateRemotesPlugin';

class ExternalTemplateRemotesPlugin {
    apply(compiler) {
        compiler.hooks.make.tap(PLUGIN_NAME, compilation => {
            const scriptExternalModules = [];

            compilation.hooks.buildModule.tap(PLUGIN_NAME, module => {
                if (module.constructor.name === 'ExternalModule' && module.externalType === 'script') {
                    scriptExternalModules.push(module);
                }
            });

            compilation.hooks.afterCodeGeneration.tap(PLUGIN_NAME, function() {
                scriptExternalModules.map(module => {
                    const urlTemplate = extractUrlAndGlobal(module.request)[0];
                    const urlExpression = toExpression(urlTemplate);
                    const sourceMap = compilation.codeGenerationResults.get(module).sources;
                    const rawSource = sourceMap.get('javascript');
                    sourceMap.set(
                        'javascript',
                        new RawSource(rawSource.source().replace(`"${urlTemplate}"`, urlExpression))
                    );
                });
            });
        });
    }
}

function toExpression(templateUrl) {
    const result = [];
    const current = [];
    let isExpression = false;
    let invalid = false;
    for (const c of templateUrl) {
        if (c === '[') {
            if (isExpression) {
                invalid = true;
                break;
            }
            isExpression = true;
            if (current.length) {
                result.push(`"${current.join('')}"`);
                current.length = 0;
            }
        } else if (c === ']') {
            if (!isExpression) {
                invalid = true;
                break;
            }
            isExpression = false;
            if (current.length) {
                result.push(`${current.join('')}`);
                current.length = 0;
            }
            current.length = 0;
        } else {
            current.push(c);
        }
    }
    if (isExpression || invalid) {
        throw new Error(`Invalid template URL "${templateUrl}"`);
    }
    if (current.length) {
        result.push(`"${current.join('')}"`);
    }
    return result.join(' + ');
}

module.exports = ExternalTemplateRemotesPlugin;

zimo888 avatar Mar 21 '24 06:03 zimo888

Setting the remote URL at runtime is a very convenient feature:

  1. When modules are published, new archive url can be published to a configuration center, which provides a restful url to return a new window.entry.
  2. Developers can add an item to the LocalStorage setting window.entry = {"module": "localhost:3000/remoteEntry.js"}, which overrides the window.entry at runtime. This module will then be proxied to the local development environment for easy debugging.

zimo888 avatar Mar 21 '24 06:03 zimo888

This seems to be a very good feature, and we should improve its support from the bottom up.

I suggest promoting this in two ways:

  1. Create an issue in the rspack repository and request support for the required hooks.
  2. Create an issue in the module-federation/universe repository, and discuss whether there is a need to provide builtin support for this feature.

chenjiahan avatar Mar 28 '24 13:03 chenjiahan

This seems to be a very good feature, and we should improve its support from the bottom up.

I suggest promoting this in two ways:

  1. Create an issue in the rspack repository and request support for the required hooks.
  2. Create an issue in the module-federation/universe repository, and discuss whether there is a need to provide builtin support for this feature.

Thank you! I’ve moved the discussion to the corresponding issue and close this issue for now.

https://github.com/web-infra-dev/rspack/issues/6077

zimo888 avatar Apr 01 '24 05:04 zimo888

This seems to be a very good feature, and we should improve its support from the bottom up.

I suggest promoting this in two ways:

  1. Create an issue in the rspack repository and request support for the required hooks.
  2. Create an issue in the module-federation/universe repository, and discuss whether there is a need to provide builtin support for this feature.

Can I currently use the rspack mf v1 version to set the dynamic remotes ?

@chenjiahan

dxhsmwqcwelllee avatar May 21 '25 01:05 dxhsmwqcwelllee