You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.3 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isInitialOrHasNoParents = isInitialOrHasNoParents;
  6. exports.isInvalidOrder = isInvalidOrder;
  7. exports.getOrder = getOrder;
  8. exports.getLoaderObject = getLoaderObject;
  9. exports.mergeOptions = mergeOptions;
  10. exports.isString = isString;
  11. exports.isFunction = isFunction;
  12. exports.isType = isType;
  13. /* eslint-disable
  14. no-param-reassign
  15. */
  16. function isInitialOrHasNoParents(chunk) {
  17. let parentCount = 0;
  18. for (const chunkGroup of chunk.groupsIterable) {
  19. parentCount += chunkGroup.getNumberOfParents();
  20. }
  21. return chunk.isOnlyInitial() || parentCount === 0;
  22. }
  23. function isInvalidOrder(a, b) {
  24. // Async chunks' modules don't get turned into ExtractedModule
  25. // instances for some reason. This is a temporary fix that
  26. // moves the isInvalidOrder check inside a condition.
  27. if (a.getPrevModules && b.getPrevModules) {
  28. const bBeforeA = a.getPrevModules().indexOf(b) >= 0;
  29. const aBeforeB = b.getPrevModules().indexOf(a) >= 0;
  30. return aBeforeB && bBeforeA;
  31. }
  32. return false;
  33. }
  34. function getOrder(a, b) {
  35. // Async chunks' modules don't get turned into ExtractedModule
  36. // instances for some reason. This is a temporary fix that
  37. // moves the custom sorting logic inside a condition.
  38. if (a.getOriginalModule && b.getOriginalModule) {
  39. const aOrder = a.getOrder();
  40. const bOrder = b.getOrder();
  41. if (aOrder < bOrder) return -1;
  42. if (aOrder > bOrder) return 1;
  43. // We are trying to use the underlying index2 property
  44. // of the original module, but this property seems
  45. // to be set to null most of the time. It makes
  46. // sorting with it pointless. We should look
  47. // into saving the index, index2 and depth
  48. // props (maybe inside ExtractedModule).
  49. const aIndex = a.getOriginalModule().index2;
  50. const bIndex = b.getOriginalModule().index2;
  51. if (aIndex < bIndex) return -1;
  52. if (aIndex > bIndex) return 1;
  53. const bBeforeA = a.getPrevModules().indexOf(b) >= 0;
  54. const aBeforeB = b.getPrevModules().indexOf(a) >= 0;
  55. if (aBeforeB && !bBeforeA) return -1;
  56. if (!aBeforeB && bBeforeA) return 1;
  57. // Sorting by id is the default behavior of webpack
  58. // and it keeps the modules in the correct order,
  59. // except for async imports. That's the reason
  60. // it is inside the conditional branch
  61. if (a.id < b.id) return -1;
  62. if (a.id > b.id) return 1;
  63. }
  64. // Sorting by identifier breaks the order of async imported
  65. // modules either because webpack sorts them by default,
  66. // or because they are processed in the correct order
  67. // in the first place, or maybe because the modules
  68. // aren't ExtractedModule instances in this case.
  69. // Returning 0 keeps the already correct order.
  70. /*
  71. const ai = a.identifier();
  72. const bi = b.identifier();
  73. if (ai < bi) return -1;
  74. if (ai > bi) return 1;
  75. */
  76. return 0;
  77. }
  78. function getLoaderObject(loader) {
  79. if (isString(loader)) {
  80. return { loader };
  81. }
  82. return loader;
  83. }
  84. function mergeOptions(a, b) {
  85. if (!b) return a;
  86. Object.keys(b).forEach(key => {
  87. a[key] = b[key];
  88. });
  89. return a;
  90. }
  91. function isString(a) {
  92. return typeof a === 'string';
  93. }
  94. function isFunction(a) {
  95. return typeof a === 'function';
  96. }
  97. function isType(type, obj) {
  98. return Object.prototype.toString.call(obj) === `[object ${type}]`;
  99. }