From 68f2598befdfba1f417e1c6d1141aa5c98645b39 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 11 Mar 2023 11:00:12 +0530 Subject: [PATCH 1/3] fix: improve error message if `resolve.extensions` is invalid --- lib/NormalModuleFactory.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/NormalModuleFactory.js b/lib/NormalModuleFactory.js index 4cb2cb65d5d..c3eab62b6d1 100644 --- a/lib/NormalModuleFactory.js +++ b/lib/NormalModuleFactory.js @@ -849,6 +849,17 @@ ${err2.stack}`; err.message += ` ${hints.join("\n\n")}`; } + + // Check if the extension is missing a leading dot (e.g. "js" instead of ".js") + const specifiedExtensions = Array.from( + resolver.options.extensions + ); + specifiedExtensions.forEach(extension => { + if (extension.match(/^[^.]/)) { + err.message += `\nDid you miss the leading dot in 'resolve.extensions'? Did you mean '.${extension}' instead of '${extension}'?`; + } + }); + callback(err); } ); From 482ec6d6c5bf6c039b601ae277ffe528b65835db Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 12 Mar 2023 16:18:01 +0530 Subject: [PATCH 2/3] fix: improve resolve extention hint to be in one line --- lib/NormalModuleFactory.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/NormalModuleFactory.js b/lib/NormalModuleFactory.js index c3eab62b6d1..f269eef6104 100644 --- a/lib/NormalModuleFactory.js +++ b/lib/NormalModuleFactory.js @@ -851,14 +851,22 @@ ${hints.join("\n\n")}`; } // Check if the extension is missing a leading dot (e.g. "js" instead of ".js") + let appendResolveExtensionsHint = false; const specifiedExtensions = Array.from( resolver.options.extensions ); - specifiedExtensions.forEach(extension => { + const expectedExtensions = specifiedExtensions.map(extension => { if (extension.match(/^[^.]/)) { - err.message += `\nDid you miss the leading dot in 'resolve.extensions'? Did you mean '.${extension}' instead of '${extension}'?`; + appendResolveExtensionsHint = true; + return `.${extension}`; } + return extension; }); + if (appendResolveExtensionsHint) { + err.message += `\nDid you miss the leading dot in 'resolve.extensions'? Did you mean '${JSON.stringify( + expectedExtensions + )}' instead of '${JSON.stringify(specifiedExtensions)}'?`; + } callback(err); } From d9604e9003d4bc3800bcaf2a8c63b078f1ff3813 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 27 Mar 2023 12:20:30 +0530 Subject: [PATCH 3/3] refactor: move RegExp to a variable and use RegExp.prototype.test() --- lib/NormalModuleFactory.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/NormalModuleFactory.js b/lib/NormalModuleFactory.js index f269eef6104..d2f222275d8 100644 --- a/lib/NormalModuleFactory.js +++ b/lib/NormalModuleFactory.js @@ -72,6 +72,7 @@ const EMPTY_GENERATOR_OPTIONS = {}; const EMPTY_ELEMENTS = []; const MATCH_RESOURCE_REGEX = /^([^!]+)!=!/; +const LEADING_DOT_EXTENSION_REGEX = /^[^.]/; const loaderToIdent = data => { if (!data.options) { @@ -856,7 +857,7 @@ ${hints.join("\n\n")}`; resolver.options.extensions ); const expectedExtensions = specifiedExtensions.map(extension => { - if (extension.match(/^[^.]/)) { + if (LEADING_DOT_EXTENSION_REGEX.test(extension)) { appendResolveExtensionsHint = true; return `.${extension}`; }