Repository: webpack/webpack (JavaScript)

Issue:
Ambiguous behavior of node.global: true when webpack.DefinePlugin is used
<!-- 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?**
A global reference is not replaced when you set node.global: true and define a global object attribute with DefinePlugin
new webpack.DefinePlugin({'global.test': JSON.stringify('test')}). If you remove the DefinePlugin it works as expected

**If the current behavior is a bug, please provide the steps to reproduce.**
```
git clone https://github.com/astroza/webpack-global-concern
cd webpack-global-concern
yarn install
yarn build
cat out.js
```

```
var __webpack_exports__ = {};
var plsReplaceMyRightValue = global;
console.log(__webpack_require__.g, "test");
```

A global reference is still there. This is only an opinion but I think it is not consistent. Also, many people can lose a day figuring out why a global is still there. The output code does not run in a service worker environment because global does not exist.

<!-- A great way to do this is to provide your configuration via a GitHub repository -->
<!-- The most helpful is a minimal reproduction with instructions on how to reproduce -->
<!-- Repositories with too many files or large `webpack.config.js` files are not suitable -->
<!-- Please only add small code snippets directly into this issue -->
<!-- https://gist.github.com is a good place for longer code snippets -->
<!-- If your issue is caused by a plugin or loader, please create an issue on the loader/plugin repository instead -->

**What is the expected behavior?**
```
var __webpack_exports__ = {};
var plsReplaceMyRightValue = __webpack_require__.g;
console.log(plsReplaceMyRightValue, "test");
```

<!-- "It should work" is not a helpful explanation -->
<!-- Explain exactly how it should behave -->

**Other relevant information:**
webpack version:  5.69.0
Node.js version: v16.13.1
Operating System: MacOS 12.2.1
Additional tools: webpack-cli 4.9.2



Hints:

Patch:
diff --git a/lib/NodeStuffPlugin.js b/lib/NodeStuffPlugin.js
--- a/lib/NodeStuffPlugin.js
+++ b/lib/NodeStuffPlugin.js
@@ -69,6 +69,16 @@ class NodeStuffPlugin {
 									);
 								}
 							});
+						parser.hooks.rename.for("global").tap("NodeStuffPlugin", expr => {
+							const dep = new ConstDependency(
+								RuntimeGlobals.global,
+								expr.range,
+								[RuntimeGlobals.global]
+							);
+							dep.loc = expr.loc;
+							parser.state.module.addPresentationalDependency(dep);
+							return false;
+						});
 					}
 
 					const setModuleConstant = (expressionName, fn, warning) => {


Test patch:
diff --git a/test/configCases/web/node-source-global/index.js b/test/configCases/web/node-source-global/index.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/web/node-source-global/index.js
@@ -0,0 +1,9 @@
+it("should disallow rename global", () => {
+	var shouldBeGlobal = global;
+	// prevent optimizations
+	var method = shouldBeGlobal[String.fromCharCode(40, 40, 40)];
+	method && method();
+	eval("expect(shouldBeGlobal.value1).toBe('value1')");
+	expect(shouldBeGlobal.test).toBe("test");
+	expect(global.test).toBe("test");
+});
diff --git a/test/configCases/web/node-source-global/test.config.js b/test/configCases/web/node-source-global/test.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/web/node-source-global/test.config.js
@@ -0,0 +1,8 @@
+module.exports = {
+	moduleScope(scope) {
+		scope.global = scope.global || {};
+		scope.global.value1 = "error";
+		scope.globalThis = scope.globalThis || {};
+		scope.globalThis.value1 = "value1";
+	}
+};
diff --git a/test/configCases/web/node-source-global/webpack.config.js b/test/configCases/web/node-source-global/webpack.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/web/node-source-global/webpack.config.js
@@ -0,0 +1,13 @@
+const { DefinePlugin } = require("../../../../");
+
+/** @type {import("../../../../").Configuration} */
+module.exports = {
+	node: {
+		global: true
+	},
+	plugins: [
+		new DefinePlugin({
+			"global.test": "'test'"
+		})
+	]
+};