CaseSensitiveModulesWarning.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /**
  9. * @param {Module[]} modules the modules to be sorted
  10. * @returns {Module[]} sorted version of original modules
  11. */
  12. const sortModules = modules => {
  13. return modules.slice().sort((a, b) => {
  14. const aIdent = a.identifier();
  15. const bIdent = b.identifier();
  16. /* istanbul ignore next */
  17. if (aIdent < bIdent) return -1;
  18. /* istanbul ignore next */
  19. if (aIdent > bIdent) return 1;
  20. /* istanbul ignore next */
  21. return 0;
  22. });
  23. };
  24. /**
  25. * @param {Module[]} modules each module from throw
  26. * @returns {string} each message from provided moduels
  27. */
  28. const createModulesListMessage = modules => {
  29. return modules
  30. .map(m => {
  31. let message = `* ${m.identifier()}`;
  32. const validReasons = m.reasons.filter(reason => reason.module);
  33. if (validReasons.length > 0) {
  34. message += `\n Used by ${validReasons.length} module(s), i. e.`;
  35. message += `\n ${validReasons[0].module.identifier()}`;
  36. }
  37. return message;
  38. })
  39. .join("\n");
  40. };
  41. class CaseSensitiveModulesWarning extends WebpackError {
  42. /**
  43. * Creates an instance of CaseSensitiveModulesWarning.
  44. * @param {Module[]} modules modules that were detected
  45. */
  46. constructor(modules) {
  47. const sortedModules = sortModules(modules);
  48. const modulesList = createModulesListMessage(sortedModules);
  49. super(`There are multiple modules with names that only differ in casing.
  50. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
  51. Use equal casing. Compare these module identifiers:
  52. ${modulesList}`);
  53. this.name = "CaseSensitiveModulesWarning";
  54. this.origin = this.module = sortedModules[0];
  55. Error.captureStackTrace(this, this.constructor);
  56. }
  57. }
  58. module.exports = CaseSensitiveModulesWarning;