core icon indicating copy to clipboard operation
core copied to clipboard

Support types consumption with RuntimePlugins

Open arivasnxt opened this issue 10 months ago • 7 comments

Clear and concise description of the problem

As a developer using Module Federation I want that remote types can also be consumed for remotes changed or created at runtime with a RuntimePlugin so that the @mf-types can be generated in my host app for those modules too.

Suggested solution

Refetch types if the remotes change at runtime.

Alternatives

  • Allow setting the hook phase in the DTS configuration.
  • Port this plugin to RsPack: https://github.com/module-federation/external-remotes-plugin/issues/9

Additional context

Module Federation runtime plugins allow developers to manage the module federation lifecycle at runtime. My issue is that we are providing a template URL in the bundlers plugin configuration, and with a plugin at runtime we are replacing the placeholders. This works for fetching the module, but not the types, as they are fetched in another lifecycle.

Examples rspack.config.ts

const MFPlugin = new ModuleFederationPlugin({
  name: 'HostApp',
  remotes: {
    myRemote: 'MyRemote@https://example.org/myRemote/$VERSION/mf-manifest.json',
  },
  runtimePlugins: [resolve(__dirname, './versionInjectionPlugin.ts')],
}),

plugin

export const versionInjectionPlugin: () => FederationRuntimePlugin = () => {
  return {
    name: "mfe-version-injection-plugin",
    beforeRequest(args) {
      args.options.remotes = (args.options.remotes as RemoteWithEntry[]).map((remote) => {
        remote.entry = remote.entry.replace("$VERSION", version);
        return remote;
      });
      return args;
    },
  };
};

The ConsumeTypesPlugin uses my template URL instead of the one with the injection, so the @mf-types folder is not generated in my host proejct. The remote module is fetched correctly.

image

Validations

  • [X] Read the Contributing Guidelines.
  • [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

arivasnxt avatar Dec 19 '24 11:12 arivasnxt