webpack icon indicating copy to clipboard operation
webpack copied to clipboard

After turning on lazyCompilation, the first load of externals will fail

Open nanianlisao opened this issue 1 year ago • 13 comments

Bug report

Enable lazyCompilation, and because of the micro frontend, I need to set output.library.type to umd. At this time, an error will appear during the first load. After refreshing the page again, it will become normal.

What is the current behavior?

Image

If the current behavior is a bug, please provide the steps to reproduce.

https://github.com/nanianlisao/webpack-repoduce

Run the following command:

pnpm i pnpm run dev

What is the expected behavior?

no mistakes

Other relevant information: webpack version: 5.97.1 Node.js version: 18.17.0 Operating System: Additional tools:

nanianlisao avatar Jan 16 '25 02:01 nanianlisao

relatived #16436 ,but I didn't find any solution

nanianlisao avatar Jan 16 '25 02:01 nanianlisao

The reason is that, external modules should only be present in the main chunk, not async chunks, normally this is done by a plugin called EnsureChunkCondition.

However hot update chunk is a special case, imaging that we don't have any external module in the first build, but we import an external module in the second build, we should indeed insert external module into al modules, so the external module should be placed in hot update chunk, but it can't as it is an external module which cannot be present at async chunks

JSerFeng avatar Feb 11 '25 05:02 JSerFeng

The temporary solution is that reload the page. Once reload, the browser fetch the new main chunk which contains updated external modules.

JSerFeng avatar Feb 11 '25 05:02 JSerFeng

When building an asynchronous chunks, external modules need to be imported, and the contents of the main chunk will be updated. So, can’t the browser actively notify the update of the main chunk to reload?

nanianlisao avatar Feb 11 '25 06:02 nanianlisao

So, can’t the browser actively notify the update of the main chunk to reload?

Yeah, that's exact way that I want to solve this for now, when external modules present at async chunks, notify browser to reload

JSerFeng avatar Feb 11 '25 06:02 JSerFeng

If you are using Webpack 5, setting libraryTarget: "umd" might help instead of library.type: "umd".

Niharika201 avatar Mar 15 '25 18:03 Niharika201

@JSerFeng Have you tried to solve this yet? If not, I will look, just to not waste time if someone is already working on it.

alexander-akait avatar Mar 27 '25 00:03 alexander-akait

No, I just found the reason but do not know how to better fix this, not familiar with hmr part T-T

JSerFeng avatar Mar 28 '25 06:03 JSerFeng

solution for this in my mind is implement a option where they can consider the to make the external module for lazy condition and other might be implementing force reload of browser so it can include that in main chunk any opinions on implementing this

Vansh5632 avatar Apr 14 '25 12:04 Vansh5632

hi there can i contribute ??

shivammSharma avatar May 12 '25 10:05 shivammSharma

yupp you can contribute you can check previous prs and then makeout your solution

Vansh5632 avatar May 12 '25 15:05 Vansh5632

We only generate the UMD wrapper for entry chunk. If UMD output is intended for micro-frontend (e.g., Qiankun), I recommend setting externalTypes: 'global' | 'window' | 'var' as a workaround.

hai-x avatar Jul 05 '25 09:07 hai-x

hey!!! can i contribute?

im-shubhamm avatar Oct 22 '25 17:10 im-shubhamm