WarnCaseSensitiveModulesPlugin.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const PLUGIN_NAME = "WarnCaseSensitiveModulesPlugin";
  11. class WarnCaseSensitiveModulesPlugin {
  12. /**
  13. * Apply the plugin
  14. * @param {Compiler} compiler the compiler instance
  15. * @returns {void}
  16. */
  17. apply(compiler) {
  18. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  19. compilation.hooks.seal.tap(PLUGIN_NAME, () => {
  20. /** @type {Map<string, Map<string, Module>>} */
  21. const moduleWithoutCase = new Map();
  22. for (const module of compilation.modules) {
  23. const identifier = module.identifier();
  24. // Ignore `data:` URLs, because it's not a real path
  25. if (
  26. /** @type {NormalModule} */
  27. (module).resourceResolveData !== undefined &&
  28. /** @type {NormalModule} */
  29. (module).resourceResolveData.encodedContent !== undefined
  30. ) {
  31. continue;
  32. }
  33. const lowerIdentifier = identifier.toLowerCase();
  34. let map = moduleWithoutCase.get(lowerIdentifier);
  35. if (map === undefined) {
  36. map = new Map();
  37. moduleWithoutCase.set(lowerIdentifier, map);
  38. }
  39. map.set(identifier, module);
  40. }
  41. for (const pair of moduleWithoutCase) {
  42. const map = pair[1];
  43. if (map.size > 1) {
  44. compilation.warnings.push(
  45. new CaseSensitiveModulesWarning(
  46. map.values(),
  47. compilation.moduleGraph
  48. )
  49. );
  50. }
  51. }
  52. });
  53. });
  54. }
  55. }
  56. module.exports = WarnCaseSensitiveModulesPlugin;