WarnCaseSensitiveModulesPlugin.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const CaseSensitiveModulesWarning = require("./CaseSensitiveModulesWarning");
  7. /** @typedef {import("./Compiler")} Compiler */
  8. /** @typedef {import("./Module")} Module */
  9. /** @typedef {import("./NormalModule")} NormalModule */
  10. class WarnCaseSensitiveModulesPlugin {
  11. /**
  12. * Apply the plugin
  13. * @param {Compiler} compiler the compiler instance
  14. * @returns {void}
  15. */
  16. apply(compiler) {
  17. compiler.hooks.compilation.tap(
  18. "WarnCaseSensitiveModulesPlugin",
  19. compilation => {
  20. compilation.hooks.seal.tap("WarnCaseSensitiveModulesPlugin", () => {
  21. /** @type {Map<string, Map<string, Module>>} */
  22. const moduleWithoutCase = new Map();
  23. for (const module of compilation.modules) {
  24. const identifier = module.identifier();
  25. // Ignore `data:` URLs, because it's not a real path
  26. if (
  27. /** @type {NormalModule} */
  28. (module).resourceResolveData !== undefined &&
  29. /** @type {NormalModule} */
  30. (module).resourceResolveData.encodedContent !== undefined
  31. ) {
  32. continue;
  33. }
  34. const lowerIdentifier = identifier.toLowerCase();
  35. let map = moduleWithoutCase.get(lowerIdentifier);
  36. if (map === undefined) {
  37. map = new Map();
  38. moduleWithoutCase.set(lowerIdentifier, map);
  39. }
  40. map.set(identifier, module);
  41. }
  42. for (const pair of moduleWithoutCase) {
  43. const map = pair[1];
  44. if (map.size > 1) {
  45. compilation.warnings.push(
  46. new CaseSensitiveModulesWarning(
  47. map.values(),
  48. compilation.moduleGraph
  49. )
  50. );
  51. }
  52. }
  53. });
  54. }
  55. );
  56. }
  57. }
  58. module.exports = WarnCaseSensitiveModulesPlugin;