CaseSensitiveModulesWarning.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. /** @typedef {import("./Module")} Module */
  8. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  9. /**
  10. * @param {Module[]} modules the modules to be sorted
  11. * @returns {Module[]} sorted version of original modules
  12. */
  13. const sortModules = modules => {
  14. return modules.sort((a, b) => {
  15. const aIdent = a.identifier();
  16. const bIdent = b.identifier();
  17. /* istanbul ignore next */
  18. if (aIdent < bIdent) return -1;
  19. /* istanbul ignore next */
  20. if (aIdent > bIdent) return 1;
  21. /* istanbul ignore next */
  22. return 0;
  23. });
  24. };
  25. /**
  26. * @param {Module[]} modules each module from throw
  27. * @param {ModuleGraph} moduleGraph the module graph
  28. * @returns {string} each message from provided modules
  29. */
  30. const createModulesListMessage = (modules, moduleGraph) => {
  31. return modules
  32. .map(m => {
  33. let message = `* ${m.identifier()}`;
  34. const validReasons = Array.from(
  35. moduleGraph.getIncomingConnectionsByOriginModule(m).keys()
  36. ).filter(x => x);
  37. if (validReasons.length > 0) {
  38. message += `\n Used by ${validReasons.length} module(s), i. e.`;
  39. message += `\n ${validReasons[0].identifier()}`;
  40. }
  41. return message;
  42. })
  43. .join("\n");
  44. };
  45. class CaseSensitiveModulesWarning extends WebpackError {
  46. /**
  47. * Creates an instance of CaseSensitiveModulesWarning.
  48. * @param {Iterable<Module>} modules modules that were detected
  49. * @param {ModuleGraph} moduleGraph the module graph
  50. */
  51. constructor(modules, moduleGraph) {
  52. const sortedModules = sortModules(Array.from(modules));
  53. const modulesList = createModulesListMessage(sortedModules, moduleGraph);
  54. super(`There are multiple modules with names that only differ in casing.
  55. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
  56. Use equal casing. Compare these module identifiers:
  57. ${modulesList}`);
  58. this.name = "CaseSensitiveModulesWarning";
  59. this.module = sortedModules[0];
  60. }
  61. }
  62. module.exports = CaseSensitiveModulesWarning;