-
-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Federation Runtime CSS asset Link insert hook #2878
Comments
Really interesting idea. Would love to chat through this more and bring in some folks for a design discussion. Want to hop on a meetup sometime next week? |
Would love to sync and discuss this some more with anyone @zackarychapple! Unfortunately we're pretty booked up next week. The week after or beyond should be fine though (September onwards). I just joined the module-federation discord and can maybe reach out to you there if that'd be easier to schedule something? |
Thanks your advice , it's very usefule ,so you want mf to provider a hook which likes insert hook to control the css/js element ? |
I think i shot you a dm. If not can you shoot me one |
Yes, that hook actually isn't even that great because it's run completely out of context from the rest of the page. So being able to pass in some kind of function hook like this alongside a call to The goal of the callback would be the same though; be called with any We might even be able to investigate loading those CSS assets JS-side to initialize them as constructed styles sheets so they could be shared across any repeat instances of a "micro-app", but that'd be a separate investigation and not a requirement. |
@2heal1 here is a super simplified example to showcase the basic idea behind the scenario I outlined above. Imagine logic contained inside const mountApp = async (outlet: Element, route: string) => {
const moduleMetadata = appManifest.get(route);
const shadowRoot = outlet.shadowRoot ?? outlet.attachShadow({ mode: 'open' });
const { default: remoteModuleConfig } = await loadRemote(`${moduleMetadata.scope}/${moduleMetadata.module}`, {
insertLink: (linkTag: HTMLLinkElement) => shadowRoot.append(linkTag)
});
const appContext = remoteModuleConfig();
await appContext.mount(shadowRoot);
} The idea here is that the |
After looking into the docs a bit more, I do see there is a plugin system for the FederationRuntime we might be able to user here it seems? I see a |
CreateLink is techincally controlled by "webpack runtime" not federation runtime. So this would be something in mini-css runtime module. We may be able to provide some way to patch the function and provide a insert hook though, maybe |
|
Mini css has insert option. That's what controls style inject location. My runtime doesn't actually inject the css. It's the bundlers runtime. I only inject the remote, the bundler injects the chunks |
Just go to mini css plugin Readme. It's one of the options. |
We're using Rsbuild/Rspack everywhere for this at the moment. So if I'm understanding correctly you're referring to the CssExtractRspackPlugin right? We have that configured on the "remotes" to log the |
Okay so this is with the manifest specifically? |
Yes, when loaded from We can probably hack something around this by intercepting |
Clear and concise description of the problem
Issue
Today the federation runtime seems to handle the loading and insertion of CSS files on our behalf. Even in projects where we specifically remove and re-append the
CssExtractRspackPlugin
so that we can define our owninsert
callback, that seems to be ignored since the runtime appears to be loading and inserting CSS asset links instead.There are many places where this seems to possibly occur today. Essentially anywhere where
document.head.appendChild
is called to append styles. The location of where JS assets get appended shouldn't really be relevant here though.Why is this an issue?
In an MFE platform we are building out for our wider organization (with MF 2.0 and the federation runtime which are awsome so far!) we need to make use of Shadow DOMs. Each top-level "micro-app" (which we have defined as anything with it's own route/sub-routes) gets loaded into its own isolated shadow root. We need this for two reasons:
The issue here is that the Module Federation Runtime is inserting CSS asset links in the head of the document on our behalf. This leads to these styles now affecting everything except the isolated shadow root where it is needed (the opposite of what we want to have happen 😆).
Suggested solution
If we could get some kind of callback hook that we could pass to
preloadRemote
and hopefully evenloadRemote
that'd be amazing. Ideally this hook works like theinsert
function ofCssExtractRspackPlugin
where it is just called with each link element that needs to be inserted.This approach would allow us to define the implementation for how link elements get appended to the DOM. In our case this means we could append the Link elements to the shadow root created for a given micro-app, but also possibly cache it to reinsert it across multiple instances of a micro-app being initialized across the platform.
Alternative
There is not a good alternative right now. This would block us from being able to use MF manifests and any value they provide.
Eventually I assume we'd consider trying to develop some custom solution for importing CSS assets that allows us to manage them better at build and run time, but just being able to control the insertion of these seems far more simple.
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: