Issue 1:
Ehen using build with libraryTarget: 'umd', targeted build in node enviroment; getting:

AR:

Node give "ReferenceError: window is not defined"

ER:

no issues in node & browser

Not possible to set target param to match both platforms;

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

Webpack version 4.1.1

It's due to webpackUniversalModuleDefinition - "this" should be as param instead of window

Issue 2:
Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

We are upgrading a giant codebase from Webpack 3 to 4 and are blocked on one issue. We are seeing significant code duplication in final bundles when the same JS module is required synchronously and asynchronously in the route code.

The JS module referenced from the entry point file contains synchronous require to the page code like this:

// entrypoints.js
"HomeDesktopPage":"HomeDesktopEntry.js"

HomeDesktopEntry.js requires the relevant page module HomeDesktop.js synchronously in order to bundle all dependencies for the initial page load:

// HomeDesktopEntry.js:

require("./logging");
// make sure component is part of the entry point
require("./HomeDesktop");

In another JS module in the HomeDesktopEntry dependency tree, we require HomeDesktop asynchronously:

require.ensure([], () => {
returnValue.resolve({
page: require("./pageTypes/ActivishDetailsPage/HomeDesktop"),
});
});

Webpack will generate additional bundles for "HomeDesktopLazy" and load them on demand when this code is executed. The lazy loaded chunks contain duplicated JS modules that were already bundled and loaded initially when we navigated to HomeDesktopEntry. Even though we required HomeDesktop in the entry point file and bundled all its dependencies, Webpack runtime generates and loads additional lazy chunks for the asynchronous require of that same module. I've built a little tool to measure the duplication and on our giant codebase, this amounted to additional 3MB of just duplicated JS modules. This creates some additional problems with page performance too since we need to wait for those async chunks to load in order to boot up the page client side.

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

The behavior happens only with some splitChunks configs. For example, with the below config we get 68 JS bundles for the HomeDesktopPage route but no duplication happens:

optimization: {
splitChunks: {
chunks: "all",
minSize: 0,
maxInitialRequests: Infinity,
maxAsyncRequests: Infinity,

Trying to reduce the number of bundles and initial requests, however, does cause the above bug and significant JS duplication:

optimization: {
splitChunks: {
chunks: "all",
minSize: 500000,
maxInitialRequests: 5,
maxAsyncRequests: 5,

Our codebase is huge, and I am not sure how easy it will be to create a minimal repro example. I am working on it, but wanted to post the problem in the meantime just in case there is a known solution or an explanation for it. Thanks so much for any help!

What is the expected behavior?

No additional bundles are generated and loaded on demand when the same JS module is required asynchronously if it was previously required synchronously.

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

Node 8 and 10, we tried on both

webpack 4.28

MacOS

Chome