-
Notifications
You must be signed in to change notification settings - Fork 29
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
HMR Does not work #183
Comments
Hi, you can use this plugin to update your remotes in the host application when something is changing |
Hi, I am not sure how that helps? I thought this package was suppose to work with HMR by default? I didn't see anything in the Documentation that I needed to use a separate plugin to get HMR. That being said, I am not sure what your are suggesting what I do with that plugin, would it be putting something like this in my host/vite.cofnig.ts? ViteRestart({
restart: [`${env.parsed?.VITE_CORE_URL}/assets/remoteEntry.js`]
}) If so, that doesn't work. |
Yep, you need to add this plugin in the host vite configuration. It takes the file system paths to watch |
Okay... That is a touch disappointing as I was hoping there would be a way to have the reset just happens. The way your describing would require me to have every developer edit the path so that it points to their location on their machine. I guess to elaborate a bit more, my hope was to have my host app allow connections from other teams and be hosted on an AWS S3 Bucket. Then we could point the Host at the remote localhost and we could develop that way so other teams didn't have to have access to the host app. But I am assuming that is not possible, correct? (Just a note, I did test that concept with @originjs/vite-plugin-federation, it works, just couldn't do HMR.) |
Im not sure if vite ever natively supported hmr, as far as i know fast refresh and hmr only work in webpack and rspack based builds as a built-in solution. |
The built-in HMR in Vite works well. You can see it in action by starting both the host and remote in dev mode. |
Your host is in build mode, and the remote is in dev mode. For this scenario, additional configuration is required. |
|
Does my chrome dev tools for federation work with this package? |
yes Can it bypass the rules of React Refresh? |
When you enable hmr switch a runtime plugin is added that uses resolve share hook and injects react runtime and dev versions from unpkg |
Then it should theoretically be supported already. I’ll check the shared configuration soon and see how to adapt it to work with @vitejs/plugin-react. @harrisKAsher Have you tried the Module Federation Chrome extension? |
@ScriptedAlchemy Didn’t see unpkg. |
Does a prod and dev mix work tho? Give it a try. What i know is the devtool implements a dev react to bypass mixing issues. |
@zhangHongEn I just installed the Module Federation Chrome extension to try it out and no matter what I do I get told "No ModuleInfo Detected". |
Did you use the json remote protocol. Not the js one |
No, I am using the Error: [ Federation Runtime ]: [ Federation Runtime ]: [ Federation Runtime ]: Failed to get manifest.
args: {"manifestUrl":"http://localhost:3110/assets/remoteEntry.json","moduleName":"globalStore"}
https://module-federation.io/guide/troubleshooting/runtime/RUNTIME-003
Original Error Message:
SyntaxError: JSON.parse: unexpected character at line 2 column 3 of the JSON data |
The docs state you have to use the json protocol. A js file should emit alongside something like mf-manifest.json |
@harrisKAsher mf-manifest.json |
Okay, I think I am making progress. Here is what my host one looks like now, the core and globalStore one looks really similar.
But when I try to run this it gets stuck in an infinite loop stating And in case it helps, this is what mf-manifest.json looks like on host. {
"id": "host",
"name": "host",
"metaData": {
"name": "host",
"type": "app",
"buildInfo": {
"buildVersion": "1.0.0",
"buildName": "host"
},
"remoteEntry": {
"name": "remoteEntry-[hash]",
"path": "",
"type": "module"
},
"ssrRemoteEntry": {
"name": "remoteEntry-[hash]",
"path": "",
"type": "module"
},
"types": {
"path": "",
"name": ""
},
"globalName": "host",
"pluginVersion": "0.2.5",
"publicPath": "/"
},
"shared": [
{
"id": "host:react",
"name": "react",
"version": "18.3.1",
"requiredVersion": "^18.3.1",
"assets": {
"js": {
"async": [],
"sync": []
},
"css": {
"async": [],
"sync": []
}
}
},
{
"id": "host:react-dom",
"name": "react-dom",
"version": "18.3.1",
"requiredVersion": "^18.3.1",
"assets": {
"js": {
"async": [],
"sync": []
},
"css": {
"async": [],
"sync": []
}
}
}
],
"remotes": [
{
"federationContainerName": "http://localhost:3110/mf-manifest.json",
"moduleName": "Store",
"alias": "globalStore",
"entry": "*"
},
{
"federationContainerName": "http://localhost:3100/mf-manifest.json",
"moduleName": "App",
"alias": "core",
"entry": "*"
}
],
"exposes": []
} |
As far as i know, vite doesnt support circular imports, like remote importing host exposed modules, its one-way - currently rspack/webpack implementations have omnidirectional support Is a remote trying to import something from host? |
No, it shouldn't be. Host loads GlobalStore and Core When I was using @originjs/vite-plugin-federation I was able to have core load itself to get the GlobalStore, but I separated it out for this as it was clearly wasn't working, but when I just use |
@harrisKAsher Could you provide a reproducible example? I plan to resolve the circular references and other related issues that are causing the loop. |
For example, I found that @module-federation/bridge-vue3 also causes Vite to call init multiple times. I need to make it more robust. |
Multiple initializations aren't a problem. It's a warning, but there's no real danger. Next.js does it too. It's probably circular remote imports, which I'm not sure if Vite just doesn't work with cyclic host remotes or if it doesn't work with cyclic remotes at all. |
vite/src/virtualModules/virtualRemoteEntry.ts Line 133 in 1660524
Here’s the faulty code; it leads to an infinite loop. |
Okay so in our bundlers we track if it was already initialized, so circular remotes do not infinitely initialize |
I haven’t yet thought carefully about how to log it without polluting the global scope. |
@zhangHongEn Do you still need a reproducible example? |
I also export a createContainer function from my packages which just uses webpacks actual mechanics directly for initialization. we do not need to do it on global scope // handling circular init calls
var initToken = initTokens[shareScopeName];
if (!initToken)
initToken = initTokens[shareScopeName] = { from: mfInstance.name };
if (initScope.indexOf(initToken) >= 0) return;
initScope.push(initToken); Note that create container works in other bundlers. This was how remote entry generation in esbuild and I think rollup work |
I haven’t reproduced the infinite loop myself, but it indeed needs optimization. No example is needed. |
Okay, well I was pretty close to done, so I just finished it up. Here it is in case it helps. |
The init token is the main aspect that most miss in the runtime implementation. The init scope is passed through all remotes as another argument so everyone knows what everyone else has so far. |
Sorry, not sure I follow, what is the init token? I can't find anything in the docs about such a thing. |
Speaking to maintainers in thread. Sorry! |
Oh, okay. Thought it was a direct response to me. 😅 Also, another thing I am encountering (not sure its related or not) is in a remote app when I import a component from a package (ComponentA) that internally imports another component (ComponentB) from a dependent package, it fails to load the entire federated remote app. If I already import ComponentB in the remote app then everything works. This issue doesn't occur when running I go to the remote apps endpoint and view it outside the federated instance. It also doesn't happen if I do a build and preview it in the Federated Instance. I have tried a bunch of different adjustments to make ComponentA and/or ComponentB shared with the vite config but none that changes anything. |
I'm not sure about that one. Good litmus test is to try it with rspack or rsbuild (our vite-like experience). If the same setup works with rspack/rsbuild, then it's a problem with vite implementation. If it doesn't, then it's a bug in our runtime. |
Thanks for sharing, I'll copy your example in the repo to show how to do it |
So I was trying to reproduce this and I cannot do it without one thing: getting from a private npm repository. My organization has an internal package of components to make things uniform across the various web applications we make. Its kind of similar to HeadlessUI. Anyway, that is what is breaking the app, and some of them are pretty simple. Literally some of them are just div's with styles on them. But it seems that if an internal package relies on another internal package that is when it breaks. My only theory is it is breaking because of how the federation in dev mode can't get the internal package for some reason? Not really sure how to verify that though as I am not that familiar with internal packages. I tried with other packages that are similar such as HeadlessUI but I could not get it to break in the same way. |
I was reading intently thus far 👀 waiting to see a solution on this. Everything @harrisKAsher described is happening to me as well. Line by line. Word for word. Here's is my example, which is very much similar to his: vite-micro-frontends-react. Like him, I kept things very basic & simple (although my case has 1 parent host & 2 child remotes, no grand-children, 1 child is on build (appears on host's "remotes"), and the other child is dynamically imported). |
I will prioritize solving this problem as the top priority when I have time
…---- Replied Message ----
| From | ***@***.***> |
| Date | 11/19/2024 07:20 |
| To | module-federation/vite ***@***.***> |
| Cc | zhn ***@***.***>,
Mention ***@***.***> |
| Subject | Re: [module-federation/vite] HMR Does not work (Issue #183) |
I was reading intently thus far 👀 waiting to see a solution on this.
***@***.*** described is happening to me as well. Line by line. Word for word.
Here's is my example, which is very much similar to his: vite-micro-frontends-react. Like him, I kept things very basic & simple (although my case has 1 parent host & 2 child remotes, no grand-children, 1 child is on build (appears on host's "remotes"), and the other child is dynamically imported).
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
So for me to support. I'll require a rspack or webpack based attempt. Then i can help. Otherwise we will need to rely on our community partners who maintain vite to proxy the needs back to me from a runtime perspective. Core team only allocated resources to
We can wait for @zhangHongEn to take a look and contact Core team, but just letting yall know where my support kicks in. 🥰🥰 |
Is it like missing css or is there something else like missing JS modules. Missing css would be tailwind tree shake classes it can't see. That can be solved with allow list. If it's missing modules on the other hand. That's something we need to investigate etc |
This issue mentions two problems:
|
Just a heads up. You can contact me on Twitter / bluesky I have a LLC dedicated to NDA signing operations for such cases. Would allow you to send me a zip of your node modules and all that I can just extract and run without any liabilities on either end. Pain in the ass but we can pull that never IF it's needed. On my end it's not a problem. Sign a few hundred a year haha 😄 |
Thanks to @ScriptedAlchemy for providing a solution approach, which allows me to implement it without checking the pack code; the only thing missing is time. 1: Solution is here |
Ahhh time. The great evil of all mankind 🫠 |
Thanks @ScriptedAlchemy for your core help. this means a lot for this project. |
Alright guys, here's the news. HMR does work for I've created a small project that illustrates the 2 bugs: one is the infinite loop, and the other is the HMR not working. The project: module-federation-with-vite-vs-rsbuild Steps to reproduce: HMR works with Rsbuild
pnpm install
pnpm rsdev
pnpm install
pnpm rsdev the app will open on
HMR doesn't works with Vite
pnpm dev
pnpm dev the app will open on The result: an infinite loop! the app will never appear, and you'll see a white screen. Different Approach (where vite's wepabb works but HMR doesn't):
Result: try running both projects now with |
I am assuming this in response to my importing issue from a private repository for packages? I have realized thats probably a separate issue so I have made a new issue here. But just for the record, there is missing css, but that's not the problem as the components my organization makes uses styled components. So I believe it is missing the module. |
@harrisKAsher did you get the HMR to work in the host with the example? the css is working, but not the JSX file, i can see in the network both App.css and App.jsx is received in the host, but not reloaded. |
@jamalsoueidan In Host if you comment out the GlobalStore and Core App code then the host app will run and update with HMR. But keep in mind you have to run it in dev mode, and dev mode breaks with GlobalStore or Core App imported right now. |
Hi, I am not sure what your are suggesting I do? what global store or core app code? I'm using this example, running in dev mode, hmr not working. What should i uncomment? Thank you |
Oh, I was talking about my reproduction of the bug example. Not sure what to tell you about the example they have. |
Hi, I am trying to migrate from @originjs/vite-plugin-federation to this as it seems better supported and has HMR support. But I cannot get the HMR to work from the remotes (though the app loads). Currently I am just trying to get this set up between 3 different apps.
This is my host/vite.config.ts
This is my core/vite.config.ts
My globalStore/vite.config.ts
The globalStore shouldn't matter as right now I am just trying to get the HMR working between Host and Core and have basically made my App.tsx to look like this in core. I have to keep globalStore in my config though as otherwise it will break as other components need it even though they aren't imported anywhere.
And in Host its this
So as you can see, just about as barebones as it gets, and yet HMR does not work. Though in the console I do see this
[vite] hot updated: /src/App.tsx [client:223:18](http://localhost:3100/@vite/client)
And in the network tab I can see this, which even includes the changes I made to core/App.tsx! Which just baffels me that they are there, yet it will not update them on my host.
Either I am missing or something is broken. If you need further information from me please let me know. Thank you for the support.
The text was updated successfully, but these errors were encountered: