Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error message if resolve.extensions is invalid #16807

Merged
merged 3 commits into from Mar 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/NormalModuleFactory.js
Expand Up @@ -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(/^[^.]/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hoist this RegExp literal into a well-named variable. This reduces memory usage and GC.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See

const METACHARACTERS_REGEXP = /[-[\]\\/{}()*+?.^$|]/g;
const CONTENT_HASH_DETECT_REGEXP = /\[contenthash(:\w+)?\]/;
const CSS_AND_JS_MODULE_EXTENSIONS_REGEXP = /\.((c|m)?js|css)($|\?)/i;
const CSS_EXTENSION_DETECT_REGEXP = /\.css($|\?)/i;
const MAP_URL_COMMENT_REGEXP = /\[map\]/g;
const URL_COMMENT_REGEXP = /\[url\]/g;
const URL_FORMATTING_REGEXP = /^\n\/\/(.*)$/;
for example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also use RegExp.prototype.test instead of String.prototype.match since .match is stateful and we don't need that state, rather just the boolean return on if a match occurs.

err.message += `\nDid you miss the leading dot in 'resolve.extensions'? Did you mean '.${extension}' instead of '${extension}'?`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's improve this and try to write everything in a line, I saw project with a lot of them and it will be un readable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait Updated 👍🏻

Screenshot 2023-03-12 at 4 15 17 PM

}
});

callback(err);
}
);
Expand Down