Repository: webpack/webpack (JavaScript)

Issue:
external hash url break amd library 
<!-- 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 GitHub Discussions. -->
<!-- https://github.com/webpack/webpack/discussions -->
<!-- https://stackoverflow.com/questions/ask?tags=webpack -->
<!-- Issues which contain questions or support requests will be closed. -->

**What is the current behavior?**

### Input
``` ts
new URL("#hash", import.meta.url)
```
### Output
``` ts
define(["#hash"], function(){})
```
The amd output will generate '#hash' as exteranl module, it is is error behavior.


<!-- 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?**

Don't external hash url at amd library. I think https://github.com/webpack/webpack/pull/17116 is a bad fix, maybe we should debugger into original error and fix it.

<!-- "It should work" is not a helpful explanation -->
<!-- Explain exactly how it should behave -->

**Other relevant information:**
webpack version:  
Node.js version:
Operating System:
Additional tools:



Hints:

Patch:
diff --git a/lib/library/AmdLibraryPlugin.js b/lib/library/AmdLibraryPlugin.js
--- a/lib/library/AmdLibraryPlugin.js
+++ b/lib/library/AmdLibraryPlugin.js
@@ -87,7 +87,11 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
 		const modern = runtimeTemplate.supportsArrowFunction();
 		const modules = chunkGraph
 			.getChunkModules(chunk)
-			.filter(m => m instanceof ExternalModule);
+			.filter(
+				m =>
+					m instanceof ExternalModule &&
+					(m.externalType === "amd" || m.externalType === "amd-require")
+			);
 		const externals = /** @type {ExternalModule[]} */ (modules);
 		const externalsDepsArray = JSON.stringify(
 			externals.map(m =>


Test patch:
diff --git a/test/configCases/externals/non-amd-externals-amd/index.js b/test/configCases/externals/non-amd-externals-amd/index.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/externals/non-amd-externals-amd/index.js
@@ -0,0 +1,33 @@
+var fs = require("fs");
+var path = require("path");
+
+var dependencyArrayRegex = /define\((\[[^\]]*\]), (function)?\(/;
+var source = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
+var [, deps] = dependencyArrayRegex.exec(source);
+
+it("should correctly import a AMD external", function() {
+	var external = require("external0");
+	expect(external).toBe("module 0");
+});
+
+it("should contain the AMD external in the dependency array", function() {
+	expect(deps).toContain("\"external0\"");
+});
+
+it("should correctly import a non-AMD external", function() {
+	var external = require("external1");
+	expect(external).toBe("abc");
+});
+
+it("should not contain the non-AMD external in the dependency array", function() {
+	expect(deps).not.toContain("\"external1\"");
+});
+
+it("should correctly import a asset external", function() {
+	var asset = new URL("#hash", import.meta.url);
+	expect(asset.href).toBe(__webpack_base_uri__ + "#hash");
+});
+
+it("should not contain asset external in the dependency array", function() {
+	expect(deps).not.toContain("\"#hash\"");
+});
diff --git a/test/configCases/externals/non-amd-externals-amd/test.config.js b/test/configCases/externals/non-amd-externals-amd/test.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/externals/non-amd-externals-amd/test.config.js
@@ -0,0 +1,5 @@
+module.exports = {
+	modules: {
+		external0: "module 0"
+	}
+};
diff --git a/test/configCases/externals/non-amd-externals-amd/webpack.config.js b/test/configCases/externals/non-amd-externals-amd/webpack.config.js
new file mode 100644
--- /dev/null
+++ b/test/configCases/externals/non-amd-externals-amd/webpack.config.js
@@ -0,0 +1,26 @@
+const webpack = require("../../../../");
+/** @type {import("../../../../").Configuration} */
+module.exports = {
+	output: {
+		libraryTarget: "amd"
+	},
+	externals: {
+		external0: "external0",
+		external1: "var 'abc'"
+	},
+	node: {
+		__dirname: false,
+		__filename: false
+	},
+	target: "web",
+	externalsPresets: {
+		node: true
+	},
+	plugins: [
+		new webpack.BannerPlugin({
+			raw: true,
+			banner:
+				"function define(deps, fn) { fn(...deps.map(dep => require(dep))); }\n"
+		})
+	]
+};
diff --git a/test/configCases/target/amd-container-unnamed/index.js b/test/configCases/target/amd-container-unnamed/index.js
--- a/test/configCases/target/amd-container-unnamed/index.js
+++ b/test/configCases/target/amd-container-unnamed/index.js
@@ -4,5 +4,5 @@ it("should name define", function() {
 	var fs = require("fs");
 	var source = fs.readFileSync(__filename, "utf-8");
 
-	expect(source).toMatch(/window\['clientContainer'\]\.define\(\[[^\]]*\], (function)?\(/);
+	expect(source).toMatch(/window\['clientContainer'\]\.define\((function)?\(/);
 });
diff --git a/test/configCases/target/amd-container-unnamed/webpack.config.js b/test/configCases/target/amd-container-unnamed/webpack.config.js
--- a/test/configCases/target/amd-container-unnamed/webpack.config.js
+++ b/test/configCases/target/amd-container-unnamed/webpack.config.js
@@ -15,7 +15,7 @@ module.exports = {
 		new webpack.BannerPlugin({
 			raw: true,
 			banner:
-				"function define(deps, fn) { fn(); }\nconst window = {};\nwindow['clientContainer'] = { define };\n"
+				"function define(fn) { fn(); }\nconst window = {};\nwindow['clientContainer'] = { define };\n"
 		})
 	]
 };
diff --git a/test/configCases/target/amd-unnamed/index.js b/test/configCases/target/amd-unnamed/index.js
--- a/test/configCases/target/amd-unnamed/index.js
+++ b/test/configCases/target/amd-unnamed/index.js
@@ -4,5 +4,5 @@ it("should name define", function() {
 	var fs = require("fs");
 	var source = fs.readFileSync(__filename, "utf-8");
 
-	expect(source).toMatch(/define\(\[[^\]]*\], (function)?\(/);
+	expect(source).toMatch(/define\((function)?\(/);
 });
diff --git a/test/configCases/target/amd-unnamed/webpack.config.js b/test/configCases/target/amd-unnamed/webpack.config.js
--- a/test/configCases/target/amd-unnamed/webpack.config.js
+++ b/test/configCases/target/amd-unnamed/webpack.config.js
@@ -11,7 +11,7 @@ module.exports = {
 	plugins: [
 		new webpack.BannerPlugin({
 			raw: true,
-			banner: "function define(deps, fn) { fn(); }\n"
+			banner: "function define(fn) { fn(); }\n"
 		})
 	]
 };