DependencyReference.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Florent Cailhol @ooflorent
  4. */
  5. "use strict";
  6. /** @typedef {import("../Module")} Module */
  7. class DependencyReference {
  8. // TODO webpack 5: module must be dynamic, you must pass a function returning a module
  9. // This is needed to remove the hack in ConcatenatedModule
  10. // The problem is that the `module` in Dependency could be replaced i. e. because of Scope Hoisting
  11. /**
  12. *
  13. * @param {Module} module the referenced module
  14. * @param {string[] | boolean} importedNames imported named from the module
  15. * @param {boolean=} weak if this is a weak reference
  16. * @param {number} order the order information or NaN if don't care
  17. */
  18. constructor(module, importedNames, weak = false, order = NaN) {
  19. // TODO webpack 5: make it a getter
  20. this.module = module;
  21. // true: full object
  22. // false: only sideeffects/no export
  23. // array of strings: the exports with this names
  24. this.importedNames = importedNames;
  25. this.weak = !!weak;
  26. this.order = order;
  27. }
  28. /**
  29. * @param {DependencyReference[]} array an array (will be modified)
  30. * @returns {DependencyReference[]} the array again
  31. */
  32. static sort(array) {
  33. /** @type {WeakMap<DependencyReference, number>} */
  34. const originalOrder = new WeakMap();
  35. let i = 0;
  36. for (const ref of array) {
  37. originalOrder.set(ref, i++);
  38. }
  39. return array.sort((a, b) => {
  40. const aOrder = a.order;
  41. const bOrder = b.order;
  42. if (isNaN(aOrder)) {
  43. if (!isNaN(bOrder)) {
  44. return 1;
  45. }
  46. } else {
  47. if (isNaN(bOrder)) {
  48. return -1;
  49. }
  50. if (aOrder !== bOrder) {
  51. return aOrder - bOrder;
  52. }
  53. }
  54. const aOrg = originalOrder.get(a);
  55. const bOrg = originalOrder.get(b);
  56. return aOrg - bOrg;
  57. });
  58. }
  59. }
  60. module.exports = DependencyReference;