You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
remote MF remote, bundled with @module-federation/vite@1.1.5
host SPA which consumes MF via @module-federation/runtime@0.8.5, bundled with Vite
host-rspack SPA which consumes MF via @module-federation/runtime@0.8.5, bundled with RsPack
Reproduction steps
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
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 ❌
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
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 thestyled-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 ofstyled-components
being fetched by the browser. Even though the versions match exactly between the host and remote, and thesingleton: 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:
remote
MF remote, bundled with@module-federation/vite@1.1.5
host
SPA which consumes MF via@module-federation/runtime@0.8.5
, bundled with Vitehost-rspack
SPA which consumes MF via@module-federation/runtime@0.8.5
, bundled with RsPackReproduction steps
Open
http://127.0.0.1:8082
with Vite SPA running2.1. Check
<head>
section of the application and observe that two<style data-styled="active" data-styled-version="5.3.11"></style>
tags were added2.2. Check the Network tab and observe that both
index.js
andstyled-components.browser.esm.js
chunks loads thestyled-components
library2.3. Try to adjust the
shared['styled-components']
init MF config in:host/src/App.tsx
by addinglib
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
❌Open
http://127.0.0.1:8083
with RsPack SPA running3.1. Check
<head>
section of the application and observe that two<style data-styled="active" data-styled-version="5.3.11"></style>
tags were added3.2. Check the Network tab and observe that both
main.js
andstyled-components.browser.esm.js
chunks loads thestyled-components
library3.3. Try to adjust the
shared['styled-components']
init MF config in:host-rspack/src/App.tsx
by addinglib
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>
andNetwork
) ✅Summary
There seem to be two issues:
1.
@module-federation/runtime
loads thestyled-components
module twice, unless we passlib
to its shared configuration.2. Passing
lib
to shared config while using Vite bundler breaks the app, while it works fine with RsPackReproduction
https://github.com/marcinkurka97/mf-runtime-vite
Used Package Manager
npm
System Info
Validations
The text was updated successfully, but these errors were encountered: