-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Webpack module federation breaks with multiple entrypoints #2692
Comments
|
Feel free to send a fix |
If I had a clue what the problem is, i'd send a PR right away. Spent a few hours with this, don't have the slightest idea 🤔 |
/cc @sokra Any ideas on this? |
Okay, so the issue here is that WDS is likely appending its own entrypoint to the array / injecting a "require" to the hmr client. That typically serves as the entry module of an application. When ModuleFederation emits a remoteEnty with the same name as the entrypoint, they are merged. This lets us attach initialization code, to federated remotes. something like changing publicPath. webpack_public_path = new URL(document.currentScript.src).origin + "/"; module-federation/module-federation-examples#277 The remote becomes 400kb big (usually like 5kb) and the module returned to the global is not @sokra i believe that either the WDS entry needs to return the container - which may lead to HMR capabilities? or the WDS entry module should not be applied to federated entry points when combined. module.exports = {
entry: {
app1: "./src/setPublicPath",
main: "./src/index",
},
plugins: [
new ModuleFederationPlugin({
name: "app1",
remotes: {
app2: "app2@http://localhost:3002/remoteEntry.js",
},
shared: { react: { singleton: true }, "react-dom": { singleton: true } },
}),
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
], |
The wds entrypoint should be prepended instead of appended. The last value is exported. |
Will take a look, thanks @sokra |
Looks like we want to modify this const prependEntry = (originalEntry, additionalEntries) => {
if (typeof originalEntry === 'function') {
return () =>
Promise.resolve(originalEntry()).then((entry) =>
prependEntry(entry, additionalEntries)
);
}
if (typeof originalEntry === 'object' && !Array.isArray(originalEntry)) {
/** @type {Object<string,string>} */
const clone = {};
Object.keys(originalEntry).forEach((key) => {
// entry[key] should be a string here
const entryDescription = originalEntry[key];
if (typeof entryDescription === 'object' && entryDescription.import) {
clone[key] = Object.assign({}, entryDescription, {
import: prependEntry(entryDescription.import, additionalEntries),
});
} else {
clone[key] = prependEntry(entryDescription, additionalEntries);
}
}); |
It already prepends... So there is somethings else wrong |
The bug is here: webpack-dev-server/lib/utils/addEntries.js Lines 86 to 92 in 4ab1f21
Desprite the function name Change it to: /** @type {Entry} */
const entriesClone = [].concat(originalEntry);
additionalEntries.forEach((newEntry) => {
if (!entriesClone.includes(newEntry)) {
entriesClone.push(newEntry);
}
}); Could somebody send a PR for that? As test case you can use |
Great, feel free to send a PR |
I will send a PR. |
I'm not sure this will actually fix HMR with module federation - but i'm looking forward to try! |
Opening PR, will need to work on / help on test case |
this resolves an issue with module federation re webpack#2692
this resolves an issue with module federation re webpack#2692
I looked into this more closely, and here is the full problem: In most cases, the dev server is trying to add some variation of these 2 entries (unless you disable their injection): Current behavior (webpack 5):Our config has a single entry: The 2 dev server entries are appended to the entry list (as explained above), resulting in something like:
Then we call the
This results in an entry list on the webpack side that looks like this: So it would seem that prepending the 2 entries would fix the issue: However, it is not that simple because webpack essentially filters out the duplicate entries, taking only the first of the duplicates (https://github.com/webpack/webpack/blob/bdf4a2b942bed9d78815af828f7935ddfcd3d567/lib/Compilation.js#L1764), so we get: Thus, the solution is to either change how this filtering works, or change the selection of entries by webpack such that the very first entry from the I think the latter is preferred, because I realize now that it is not ideal to be pushing duplicate entries into the |
Why does webpack-dev-server do that? The hooks is not owned by webpack-dev-server. It should not call it. It's already called by webpack itself. That's probably also the reason why the entries are added twice... Seems like this seem to be some kind of hack to reapply the A plugin to add these entries for webpack-dev-server could be like that: compiler.hooks.make.tapAsync({
name: "webpack-dev-server",
stage: -100
}, (compilation, callback) => {
const options = {
name: undefined // global entry, added before all entries
};
const dep = EntryPlugin.createDependency("webpack-dev-server/client/...", options);
compilation.addEntry(context, dep, options, err => {
callback(err);
});
}); Note the hack works in webpack 4 as each entrypoint can only have a single module (arrays are wrapped in a artificial module), so reapplying the entryOption also adds it twice, but the second one overrides the first one. There is no |
Thanks everyone for chiming in. What confuses me a little about this is that I don't understand a lot of the details everyone wrote in this issue, but i'm glad this problem is being ironed out and fixed. |
i have updated repo demo here please check it .
you can see the page infinite loop reload now |
Thanks, I will look at this in near time |
oh, you have multiple hmr with different hashes, it is weird usage, to be honestly, ideally you should run dev server on each compiler |
we plan for [email protected] implement plugin support so you will put |
Or use multiple entries... |
i.e. you have only one |
Can you clarify why do you use single dev server for multi compiler mode? What is use case? Why do not run two dev servers? |
yes, we have huge complex project with multiple
|
for this, do |
Sorry, it is impossible by design, you can't have the same HMR code for multi compilers, it is invalid and weird, before you don't have bug due our invalid logic for initial, in fact you should have gotten this problem much earlier, we support multi compiler mode, but your case has multiple independent builds with the same HMR, even more new features like |
Even more, you have only one web socket connection, therefore hash from one build will be already rewrite hash from other build, in theory we can pass unique name of compiler and implement multiple statuses, but it is more work then you think, also many plugins (for dev server) can break your HMR |
in this case you should use multi entry mode (it will optimize your code even better) or module federation |
ok, i have rewrite my codebase to startup multi dev-server with multi BTW it seems that we can't disabled logger.info() message via |
And does |
No, but it is in our roadmap. |
@tianyingchun for logger we have the special option https://webpack.js.org/configuration/other-options/#level, we decide don't have a lot of different logger options as were before and focus only on one |
This time I'll be that guy... do we have any updates on this? Have one question regarding Hot Module Replacement. Seems it doesn’t work properly. I used CRA with react-scripts v 5.0.0 and CRACO to override the webpack config in order to set up my module federation and that’s everything is fine (build, test, start commands)… Thought when it comes to the local development I need manually hard reload pages to reflect changes, and it’s really inconvenient. I'm thinking to build React app from a scratch and playing around with webpack config but that is not what I wanted to do as we already have a pretty well config provided by CRA only need to override it for Module Federation config is that correct? Does somebody know whether CRA (latest version 5) is able to work with hot module reload? What configuration do I miss? |
This is a very hard problem to solve. An easy workaround I use is I'll fetch all remote urls on a poll and hash them. If any hash based on the contents changes, I window reload the page. Simple but easy way to get live reloading. HMR is not easy since webpack needs to know both where something exists and where it's consumed. All graphs would need to be interlinked |
@ScriptedAlchemy Hello! Thank you for your reply. Wouldn't you mind sharing a piece of that config where you "fetch all remote URLs on a poll and hash them"? I'm not super aware of how to set up such config in webpack and it sounds a bit difficult to me to implement that so maybe some samples would be really helpful to me to understand that configuration and make live reloading in my case. Thank you again! |
Fetch(jsUrl).then(text).then(md5hashFronString) Store it on some object and put the fetch in a setInterval. |
any updates, i face the same issue when use module federation |
Since this issue is still open, I'll link to my (pretty damn hacky) "solution" for HMR I posted in another thread: webpack/webpack#11240 (comment) It's very far from perfect, but it does the job in a pinch |
20 October 2023 👀 |
For CRA, the reason HMR is not working is due to the issue mentioned in #3038 (comment) The solution would be to override the webpack config using
|
Correct. The issue is collision in the runtime. Loading the remote of the hot binds to the second chunk loading global and the host no longer gets the refresh updates |
Note. Federation v2 supports hmr and fast refresh of remotes as well. |
@ScriptedAlchemy can you point how do you resolve it? |
Which issue? |
This time it's @sokra who told me this is probably the place to open an issue.
Code
https://github.com/codepunkt/module-federation-examples/tree/dynamic-host-remote
Expected Behavior
Using
webpack-dev-server
instead ofwebpack
should still support module federation with additional content tacked onto the remoteEntry by defining it as an additionalentry
.Actual Behavior
Running "yarn start" to start
webpack-dev-server
breaks module federation and thus breaks the app in development mode.For Bugs; How can we reproduce the behavior?
yarn
on repo rootyarn start
in "dynamic-host-remote" directorylocalhost:3001
in the browserwebpack-dev-server
yarn build && yarn serve
and revisitlocalhost:3001
to see production build working just fine.The text was updated successfully, but these errors were encountered: