Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[runtime] styled-components loads twice without shared.lib specified, but Vite SPA breaks with it #3473

Open
5 tasks done
marcinkurka97 opened this issue Jan 30, 2025 · 0 comments

Comments

@marcinkurka97
Copy link

Describe the bug

In my project, I have a remote that serves micro-frontends styled by styled-components, to multiple hosts SPAs in the organization with different bundlers (webpack, rspack and vite)

Lately, I've migrated the way that SPAs consume the remote MFs, from the build plugin, into a runtime approach. It worked great with Webpack, and RsPack bundlers, but I've encountered some issues while using MF Runtime + Vite as a bundler.

Passing lib to MF runtime init function, for the styled-components share module, completely breaks the Vite apps, while it works completely fine for RsPack and Webpack bundlers.

I've noticed that lib is required to be specified in order to properly load a single instance of the library. But I'm not able to do so with the Vite, so I end up with two instances of styled-components being fetched by the browser. Even though the versions match exactly between the host and remote, and the singleton: true option is enabled.


I have prepared a minimal reproduction repo: https://github.com/marcinkurka97/mf-runtime-vite to present the issue. It has following setup:

  1. remote MF remote, bundled with @module-federation/vite@1.1.5
  2. host SPA which consumes MF via @module-federation/runtime@0.8.5, bundled with Vite
  3. host-rspack SPA which consumes MF via @module-federation/runtime@0.8.5, bundled with RsPack

Reproduction steps

  1. Run the following commands to run remote and both hosts
cd remote && npm i && npm run start
cd host && npm i && npm run start
cd host-rspack && npm i && npm run start
  1. Open http://127.0.0.1:8082 with Vite SPA running
    2.1. Check <head> section of the application and observe that two <style data-styled="active" data-styled-version="5.3.11"></style> tags were added
    2.2. Check the Network tab and observe that both index.js and styled-components.browser.esm.js chunks loads the styled-components library
    2.3. Try to adjust the shared['styled-components'] init MF config in: host/src/App.tsx by adding lib with the module

    "styled-components": {
      version: pkg.dependencies["styled-components"],
      scope: "default",
    + lib: () => import("styled-components")
      shareConfig: {
        singleton: true, // changing this does not have any affect on the described issue
        requiredVersion: pkg.dependencies["styled-components"],
      },
    },

    2.4. The Vite app crashes, no matter how we would try to pass the value to lib

  2. Open http://127.0.0.1:8083 with RsPack SPA running
    3.1. Check <head> section of the application and observe that two <style data-styled="active" data-styled-version="5.3.11"></style> tags were added
    3.2. Check the Network tab and observe that both main.js and styled-components.browser.esm.js chunks loads the styled-components library
    3.3. Try to adjust the shared['styled-components'] init MF config in: host-rspack/src/App.tsx by adding lib with the module

    "styled-components": {
      version: pkg.dependencies["styled-components"],
      scope: "default",
    + lib: () => import("styled-components")
      shareConfig: {
        singleton: true, // changing this does not have any affect on the described issue
        requiredVersion: pkg.dependencies["styled-components"],
      },
    },

    3.4. The Rspack works correctly, and the styled-components are loaded once (check <head> and Network) ✅


Summary

There seem to be two issues:
1.@module-federation/runtime loads the styled-components module twice, unless we pass lib to its shared configuration.
2. Passing lib to shared config while using Vite bundler breaks the app, while it works fine with RsPack

Reproduction

https://github.com/marcinkurka97/mf-runtime-vite

Used Package Manager

npm

System Info

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 84.69 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
    npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
  Browsers:
    Chrome: 132.0.6834.111
    Safari: 17.4.1

Validations

@marcinkurka97 marcinkurka97 marked this as a duplicate of #3469 Jan 30, 2025
@marcinkurka97 marcinkurka97 marked this as a duplicate of #3470 Jan 30, 2025
@marcinkurka97 marcinkurka97 marked this as a duplicate of #3471 Jan 30, 2025
@marcinkurka97 marcinkurka97 marked this as a duplicate of #3472 Jan 30, 2025
@marcinkurka97 marcinkurka97 marked this as a duplicate of #3468 Jan 30, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant