libraryTarget:'window' works differently in webpack-4 and webpack-5.
What is the current behavior?
I'm using the single-spa to create my apps. When I load the micro-frontend app, I did not use SystemJS, but create a script element to load javascript code and save the app object in a window object's property. like this:
export const runScript = async (url: string) => {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = url;
script.onload = resolve;
script.onerror = reject;
const firstScript = document.getElementsByTagName('script')[0];
(firstScript as any).parentNode.insertBefore(script, firstScript);
});
};
const loadApp = async (appPath: string) => {
try {
await runScript(`/${appPath}/index.js`);
console.log(appPath, '=', (window as any)[appPath]);
} catch {
console.log("load child app's javascript file error。");
}
return (window as any)[appPath];
};
singleSpa.registerApplication({
name: 'app1',
app: () => loadApp('app1'),
activeWhen: '/app1,
customProps: {
},
});
So, I use libraryTarget:'window' in the app1's webpack configuration.
In webpack-4, it works. after doing this I can use window.xxx to get my app object, but when I update to webpack-5, the window.xxx becomes to a void object.
using webpack-4:

using webpack-5:

The other difference is that I use web-dev-server to start webpack-4 app, webpack serve to start webpack-5 app, is this a possible problem?
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
works like webpack-4's output code.
Other relevant information:
webpack version: 5.1.3 & 4.44.2
Operating System: macos
Additional tools:single-spa
For maintainers only:
Yes, it is bug on webpack-dev-server, already fixed, hope we will do release in the current week
Maybe you can crate simple example, I want to check it
Maybe you can crate simple example, I want to check it
Of course, I delete some code of my original code, here is a simple example(maybe not simple enough)
https://github.com/toulondu/webpack-bug-demo
I have the exact same issue.
My library becomes undefined or an empty object on the global window in webpack 5 with webpack-dev-server.
Can you fix this issue quickly, i got the same problem.
I have the exact same issue.
My library becomes undefined or an empty object on the global window in webpack 5 with webpack-dev-server.
Have you solved this problem? I found out that not only 'window', seems every 'libraryTarget' does not work on the webpack5's serve.
Possible duplicate of #2484?
Yep, duplicate
I have the same problem
I fixed it for now with injectClient: false, which also disables autoreload but that doesn't matter to me.
Let's close in favor https://github.com/webpack/webpack-dev-server/issues/2484
Most helpful comment
Can you fix this issue quickly, i got the same problem.