Repository: webpack/webpack (JavaScript)

Issue:
Error: "undefined is not an object" when using "globalObject: this" and "runtimeChunk: single"
<!-- Please don't delete this template because we'll close your issue -->
<!-- Before creating an issue please make sure you are using the latest version of webpack. -->

# Bug report

<!-- Please ask questions on StackOverflow or the webpack Gitter. -->
<!-- https://stackoverflow.com/questions/ask?tags=webpack -->
<!-- https://gitter.im/webpack/webpack -->
<!-- Issues which contain questions or support requests will be closed. -->

**What is the current behavior?**

Using a combination of `globalObject: 'this'` and `runtimeChunk: 'single'` in `webpack.config.js` causes the following error when including the generated code in a browser:
```
TypeError: undefined is not an object (evaluating 'this["webpackChunkMyLibrary"]')
```

This issue started on Webpack v5.47.0. I believe it was caused likely caused by this commit:
https://github.com/webpack/webpack/commit/0bd1e789d48f9b3e3ca38d6c48db7c7be5380bcd#diff-820a04017e95cec05fd58ca44cd7f6c9cfa5b698a144471d5a0f523932486c56R619

**If the current behavior is a bug, please provide the steps to reproduce.**

See https://github.com/jluxenberg/webpack-use-strict-bug-report for full reproduction instructions.

My `webpack.config.js`:
```
const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    library: 'MyLibrary',
    libraryTarget: 'umd',
    globalObject: 'this',
  },
  optimization: {
    minimize: false,
    runtimeChunk: 'single',
  },
};
```

**What is the expected behavior?**

I see the text "hello world" printed to the console when including the generated `runtime.js` and `main.js` files.

**Other relevant information:**
webpack version:  bug occurs starting with v5.47.0. I tested with latest (v5.70.0) as well.
Node.js version: N/A
Operating System: MacOS 12.1 Monterey 
Additional tools:



Hints:
the same

Patch:
diff --git a/lib/library/UmdLibraryPlugin.js b/lib/library/UmdLibraryPlugin.js
--- a/lib/library/UmdLibraryPlugin.js
+++ b/lib/library/UmdLibraryPlugin.js
@@ -310,9 +310,11 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
 								: "		var a = factory();\n") +
 						  "		for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
 						  "	}\n") +
-					`})(${
-						runtimeTemplate.outputOptions.globalObject
-					}, function(${externalsArguments(externals)}) {\nreturn `,
+					`})(${runtimeTemplate.outputOptions.globalObject}, ${
+						runtimeTemplate.supportsArrowFunction()
+							? `(${externalsArguments(externals)}) =>`
+							: `function(${externalsArguments(externals)})`
+					} {\nreturn `,
 				"webpack/universalModuleDefinition"
 			),
 			source,


Test patch:
diff --git a/test/ConfigTestCases.template.js b/test/ConfigTestCases.template.js
--- a/test/ConfigTestCases.template.js
+++ b/test/ConfigTestCases.template.js
@@ -619,7 +619,12 @@ const describeCases = config => {
 													const fn = runInNewContext
 														? vm.runInNewContext(code, globalContext, p)
 														: vm.runInThisContext(code, p);
-													fn.call(m.exports, ...argValues);
+													fn.call(
+														testConfig.nonEsmThis
+															? testConfig.nonEsmThis(module)
+															: m.exports,
+														...argValues
+													);
 													document.currentScript = oldCurrentScript;
 													return m.exports;
 												}
diff --git a/test/configCases/umd/issue-15545/index.js b/test/configCases/umd/issue-15545/index.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/umd/issue-15545/index.js
@@ -0,0 +1,5 @@
+it("should compile and run", () => {
+	expect(hello()).toBe("hello");
+});
+
+export function hello() { return "hello"; }
diff --git a/test/configCases/umd/issue-15545/test.config.js b/test/configCases/umd/issue-15545/test.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/umd/issue-15545/test.config.js
@@ -0,0 +1,9 @@
+const CONTEXT = {};
+module.exports = {
+	nonEsmThis(module) {
+		return CONTEXT;
+	},
+	findBundle() {
+		return ["./runtime.js", "./main.js"];
+	}
+};
diff --git a/test/configCases/umd/issue-15545/webpack.config.js b/test/configCases/umd/issue-15545/webpack.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/umd/issue-15545/webpack.config.js
@@ -0,0 +1,19 @@
+/** @type {import("../../../../").Configuration} */
+module.exports = {
+	mode: "production",
+	entry: {
+		main: "./index.js"
+	},
+	output: {
+		filename: "[name].js",
+		library: "MyLibrary",
+		libraryTarget: "umd",
+		chunkLoading: "jsonp",
+		chunkFormat: "array-push",
+		globalObject: "this"
+	},
+	optimization: {
+		minimize: false,
+		runtimeChunk: "single"
+	}
+};