CaseSensitiveModulesWarning.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ${
  40. /** @type {Module[]} */ (validReasons)[0].identifier()
  41. }`;
  42. }
  43. return message;
  44. })
  45. .join("\n");
  46. };
  47. class CaseSensitiveModulesWarning extends WebpackError {
  48. /**
  49. * Creates an instance of CaseSensitiveModulesWarning.
  50. * @param {Iterable<Module>} modules modules that were detected
  51. * @param {ModuleGraph} moduleGraph the module graph
  52. */
  53. constructor(modules, moduleGraph) {
  54. const sortedModules = sortModules(Array.from(modules));
  55. const modulesList = createModulesListMessage(sortedModules, moduleGraph);
  56. super(`There are multiple modules with names that only differ in casing.
  57. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
  58. Use equal casing. Compare these module identifiers:
  59. ${modulesList}`);
  60. this.name = "CaseSensitiveModulesWarning";
  61. this.module = sortedModules[0];
  62. }
  63. }
  64. module.exports = CaseSensitiveModulesWarning;