question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maybe a problem of webpack-5's libraryTarget:'window'

See original GitHub issue

Bug report

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: image

using webpack-5: image

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
JonesClynecommented, Oct 28, 2020

Can you fix this issue quickly, i got the same problem.

1reaction
toulonducommented, Oct 21, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack 4 output.library failed to add object to window
I think it may be you need to add a libraryExport property to the ... I located the issue, which is caused by...
Read more >
Output - webpack
libraryTarget , this option allows users to insert comments within the export wrapper. To insert the same comment for each libraryTarget type, ...
Read more >
Output
The top-level output key contains set of options instructing webpack on how and where it should output your bundles, assets and anything else...
Read more >
Webpack's TemplatePlugin - Tan Li Hau
js export default 'foo';. And if you build it with webpack just like that, out-of-the-box, you may be surprised that ...
Read more >
webpack/webpack - Gitter
jQuery , window.$ via expose-loader . The issue is that there are inline <script> tags in the page which are running before the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found