core icon indicating copy to clipboard operation
core copied to clipboard

rspack module federation not firing onLoad and other hooks when exposed modules from other remotes are loaded

Open MadaraUchiha-314 opened this issue 9 months ago • 10 comments

Describe the bug

rspack rewrites the import for an exposed module from a remote to a static import rather than a dynamic import using loadRemote

This is causing 2 issues:

  1. The lifecycle hooks like onLoad and other hooks are not triggered for the exposed modules loaded
  2. Once the parent remote entry is loaded, all child remote entries are loaded by default because they are static imports and not dynamic.
    • This is a cause of concern as it has serious performance impact for large projects

Example:

I have 2 remotes:

  • project-a
  • project-b

project-a imports the button (which is an exposed module) from project-b.

import Button, { someThingElse } from 'project-b/button';

rspack converts it to this:

import * as __WEBPACK_EXTERNAL_MODULE_webpack_container_reference_project_b__ from 'https://rollup-plugin-module-federation-project-b.vercel.app/rspack/esm/my-remote-entry.js';

My rollup plugin rollup-plugin-module-federation converts it to:

const Button = (await loadRemote('project-b/button')).default;
const { someThingElse } = (await loadRemote('project-b/button'));

My implementation fixes both the bugs I mentioned above.

  1. Since exposed modules are loaded using loadRemote it fires all the hooks
  2. Since the exposed module is actually loaded only when it's required, it doesn't trigger additional network calls when the remote for project-a is loaded

Working example:

  • rspack: https://rollup-plugin-module-federation-project-a.vercel.app/rspack/esm/
  • rollup: https://rollup-plugin-module-federation-project-a.vercel.app/rollup/esm/

Just click on the buttons from left to right and check the console logs and the network tabs.

Reproduction

Provided above.

Used Package Manager

npm

System Info

NA

Validations

MadaraUchiha-314 avatar May 23 '24 20:05 MadaraUchiha-314