Repository: parcel-bundler/parcel
File: packages/bundlers/default/src/DefaultBundler.js (JavaScript)

Code before:
          // If there is now only a single bundle group that contains this bundle,
          // merge it into the remaining source bundles. If it is orphaned entirely, remove it.
          let incomingNodeCount =
            bundleGraph.getNodeIdsConnectedTo(bundleIdToRemove).length;

          if (
            incomingNodeCount <= 2 &&
            //Never fully remove reused bundles
            bundleToRemove.mainEntryAsset == null
          ) {
            // If one bundle group removes a shared bundle, but the other *can* keep it, still remove because that shared bundle is pointless (only one source bundle)
            removeBundle(bundleGraph, bundleIdToRemove, assetReference);
            // Stop iterating through bundleToRemove's sourceBundles as the bundle has been removed.
            break;
          } else {
            bundleGraph.removeEdge(sourceBundleId, bundleIdToRemove);
          }
        }
        numBundlesInGroup--;
      }
    }
  }

  // Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
  if (modifiedSourceBundles.size > 0) {
    let assetOrderMap = new Map(assets.map(a => [a, assets.indexOf(a)]));

    for (let bundle of modifiedSourceBundles) {
      bundle.assets = new Set(
        [...bundle.assets].sort((a, b) => {
          let aIndex = nullthrows(assetOrderMap.get(a));
          let bIndex = nullthrows(assetOrderMap.get(b));

          return aIndex - bIndex;
        }),
      );
    }
  }

  function deleteBundle(bundleRoot: BundleRoot) {
    bundleGraph.removeNode(nullthrows(bundles.get(bundleRoot.id)));
    bundleRoots.delete(bundleRoot);
    bundles.delete(bundleRoot.id);
    if (bundleRootGraph.hasContentKey(bundleRoot.id)) {
      bundleRootGraph.removeNode(
        bundleRootGraph.getNodeIdByContentKey(bundleRoot.id),
      );
    }
  }
  function getBundleGroupsForBundle(nodeId: NodeId) {
    let bundleGroupBundleIds = new Set();

Review comment (suggestion):
```suggestion
    let assetOrderMap = new Map(assets.map((a, index) => [a, index]));
```

Code after:
          // If there is now only a single bundle group that contains this bundle,
          // merge it into the remaining source bundles. If it is orphaned entirely, remove it.
          let incomingNodeCount =
            bundleGraph.getNodeIdsConnectedTo(bundleIdToRemove).length;

          if (
            incomingNodeCount <= 2 &&
            //Never fully remove reused bundles
            bundleToRemove.mainEntryAsset == null
          ) {
            // If one bundle group removes a shared bundle, but the other *can* keep it, still remove because that shared bundle is pointless (only one source bundle)
            removeBundle(bundleGraph, bundleIdToRemove, assetReference);
            // Stop iterating through bundleToRemove's sourceBundles as the bundle has been removed.
            break;
          } else {
            bundleGraph.removeEdge(sourceBundleId, bundleIdToRemove);
          }
        }
        numBundlesInGroup--;
      }
    }
  }

  // Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
  if (modifiedSourceBundles.size > 0) {
    let assetOrderMap = new Map(assets.map((a, index) => [a, index]));

    for (let bundle of modifiedSourceBundles) {
      bundle.assets = new Set(
        [...bundle.assets].sort((a, b) => {
          let aIndex = nullthrows(assetOrderMap.get(a));
          let bIndex = nullthrows(assetOrderMap.get(b));

          return aIndex - bIndex;
        }),
      );
    }
  }

  function deleteBundle(bundleRoot: BundleRoot) {
    bundleGraph.removeNode(nullthrows(bundles.get(bundleRoot.id)));
    bundleRoots.delete(bundleRoot);
    bundles.delete(bundleRoot.id);
    if (bundleRootGraph.hasContentKey(bundleRoot.id)) {
      bundleRootGraph.removeNode(
        bundleRootGraph.getNodeIdByContentKey(bundleRoot.id),
      );
    }
  }
  function getBundleGroupsForBundle(nodeId: NodeId) {
    let bundleGroupBundleIds = new Set();