Repository: webpack/enhanced-resolve (js)

Issue:
Ignore `Default condition should be last one`
I am looking for a way to more closely mimic the nodejs resolution as in both https://github.com/eslint-community/eslint-plugin-n/issues/182 and https://github.com/eslint-community/eslint-plugin-n/issues/242 we have packages that don't correctly implement the exports order. However, they do resolve correctly when called in either `import` or `require`.

When debugging this initially I discoved the error was throw here: https://github.com/webpack/enhanced-resolve/blob/main/lib/util/entrypoints.js#L474-L476

What I am hoping for is that we can (via a passed in option) disable the throw?

---

I am very willing to submit code if that would help!

Pull request description:
feat: allow default condition to be anywhere
### Description
This aligns the "default" condition behaviour with Node.Js. Specifically, this allows the "default" condition to be found anywhere in the "exports" without throwing an error.

### Related Issues:
- https://github.com/webpack/enhanced-resolve/issues/421
- https://github.com/eslint-community/eslint-plugin-n/issues/242
- https://github.com/oxc-project/oxc-resolver/issues/160

---

fixes #421 

Patch:
diff --git a/lib/util/entrypoints.js b/lib/util/entrypoints.js
index 392898c..bba26a6 100644
--- a/lib/util/entrypoints.js
+++ b/lib/util/entrypoints.js
@@ -464,17 +464,11 @@ function conditionalMapping(conditionalMapping_, conditionNames) {
 
 	loop: while (lookup.length > 0) {
 		const [mapping, conditions, j] = lookup[lookup.length - 1];
-		const last = conditions.length - 1;
 
 		for (let i = j; i < conditions.length; i++) {
 			const condition = conditions[i];
 
-			// assert default. Could be last only
-			if (i !== last) {
-				if (condition === "default") {
-					throw new Error("Default condition should be last one");
-				}
-			} else if (condition === "default") {
+			if (condition === "default") {
 				const innerMapping = mapping[condition];
 				// is nested
 				if (isConditionalMapping(innerMapping)) {


Test patch:
diff --git a/test/exportsField.test.js b/test/exportsField.test.js
index 057f55d..b8a0375 100644
--- a/test/exportsField.test.js
+++ b/test/exportsField.test.js
@@ -543,7 +543,7 @@ describe("Process exports field", function exportsField() {
 		},
 		{
 			name: "Direct mapping #7",
-			expect: new Error(), // Default is first one
+			expect: ["./src/index.js"], // Default is first one
 			suite: [
 				{
 					".": {
diff --git a/test/importsField.test.js b/test/importsField.test.js
index a57055f..ea6702f 100644
--- a/test/importsField.test.js
+++ b/test/importsField.test.js
@@ -451,7 +451,7 @@ describe("Process imports field", function exportsField() {
 		},
 		{
 			name: "Direct mapping #7",
-			expect: new Error(), // Default is first one
+			expect: ["./src/index.js"], // Default is first one
 			suite: [
 				{
 					"#a": {
@@ -1309,7 +1309,6 @@ describe("ImportsFieldPlugin", () => {
 				fs.rmdirSync(dir);
 				if (err) return done(err);
 				if (!result) return done(new Error("No result"));
-				console.log(result);
 				expect(result).toEqual(file);
 				done();
 			});