Skip to content

Commit

Permalink
Merge pull request #16807 from snitin315/improve-resolve-extention-error
Browse files Browse the repository at this point in the history
fix: improve error message if `resolve.extensions` is invalid
  • Loading branch information
TheLarkInn committed Mar 27, 2023
2 parents eac5d8c + d9604e9 commit a3ab4e7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/NormalModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const EMPTY_GENERATOR_OPTIONS = {};
const EMPTY_ELEMENTS = [];

const MATCH_RESOURCE_REGEX = /^([^!]+)!=!/;
const LEADING_DOT_EXTENSION_REGEX = /^[^.]/;

const loaderToIdent = data => {
if (!data.options) {
Expand Down Expand Up @@ -852,6 +853,25 @@ ${err2.stack}`;
err.message += `
${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
);
const expectedExtensions = specifiedExtensions.map(extension => {
if (LEADING_DOT_EXTENSION_REGEX.test(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);
}
);
Expand Down

0 comments on commit a3ab4e7

Please sign in to comment.