Skip to content

Commit

Permalink
only warn when there is really a casing difference
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Apr 12, 2021
1 parent 21bc6e7 commit 57f0426
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/CaseSensitiveModulesWarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const WebpackError = require("./WebpackError");
* @returns {Module[]} sorted version of original modules
*/
const sortModules = modules => {
return modules.slice().sort((a, b) => {
return modules.sort((a, b) => {
const aIdent = a.identifier();
const bIdent = b.identifier();
/* istanbul ignore next */
Expand Down Expand Up @@ -52,11 +52,11 @@ const createModulesListMessage = (modules, moduleGraph) => {
class CaseSensitiveModulesWarning extends WebpackError {
/**
* Creates an instance of CaseSensitiveModulesWarning.
* @param {Module[]} modules modules that were detected
* @param {Iterable<Module>} modules modules that were detected
* @param {ModuleGraph} moduleGraph the module graph
*/
constructor(modules, moduleGraph) {
const sortedModules = sortModules(modules);
const sortedModules = sortModules(Array.from(modules));
const modulesList = createModulesListMessage(sortedModules, moduleGraph);
super(`There are multiple modules with names that only differ in casing.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Expand Down
24 changes: 15 additions & 9 deletions lib/WarnCaseSensitiveModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const CaseSensitiveModulesWarning = require("./CaseSensitiveModulesWarning");

/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./Module")} Module */

class WarnCaseSensitiveModulesPlugin {
/**
Expand All @@ -20,21 +21,26 @@ class WarnCaseSensitiveModulesPlugin {
"WarnCaseSensitiveModulesPlugin",
compilation => {
compilation.hooks.seal.tap("WarnCaseSensitiveModulesPlugin", () => {
/** @type {Map<string, Map<string, Module>>} */
const moduleWithoutCase = new Map();
for (const module of compilation.modules) {
const identifier = module.identifier().toLowerCase();
const array = moduleWithoutCase.get(identifier);
if (array) {
array.push(module);
} else {
moduleWithoutCase.set(identifier, [module]);
const identifier = module.identifier();
const lowerIdentifier = identifier.toLowerCase();
let map = moduleWithoutCase.get(lowerIdentifier);
if (map === undefined) {
map = new Map();
moduleWithoutCase.set(lowerIdentifier, map);
}
map.set(identifier, module);
}
for (const pair of moduleWithoutCase) {
const array = pair[1];
if (array.length > 1) {
const map = pair[1];
if (map.size > 1) {
compilation.warnings.push(
new CaseSensitiveModulesWarning(array, compilation.moduleGraph)
new CaseSensitiveModulesWarning(
map.values(),
compilation.moduleGraph
)
);
}
}
Expand Down

0 comments on commit 57f0426

Please sign in to comment.