Repository: parcel-bundler/parcel
File: packages/core/core/src/AssetGraphBuilder.js (JavaScript)

Code before:
    return defer;
  }

  handleCompletedDepPathRequest(
    requestNode: DepPathRequestNode,
    assetGroup: AssetRequest | null
  ) {
    if (!assetGroup) {
      return;
    }
    let dependency = requestNode.value;

    let defer = this.shouldDeferDependency(dependency, assetGroup.sideEffects);

    let assetGroupNode = nodeFromAssetGroup(assetGroup, defer);
    let existingAssetGroupNode = this.assetGraph.getNode(assetGroupNode.id);
    if (existingAssetGroupNode) {
      // Don't overwrite non-deferred asset groups with deferred ones
      invariant(existingAssetGroupNode.type === 'asset_group');
      assetGroupNode.deferred = existingAssetGroupNode.deferred && defer;
    }
    this.assetGraph.resolveDependency(dependency, assetGroupNode);
    if (existingAssetGroupNode) {
      // Node already existed, that asset might have deferred dependencies,
      // recheck all dependencies of all assets of this asset group
      let assetNodes = this.assetGraph
        .getNodesConnectedFrom(assetGroupNode)
        .map(v => {
          invariant(v.type === 'asset');
          return v;
        });
      for (let assetNode of assetNodes) {
        let dependencyNodes = this.assetGraph
          .getNodesConnectedFrom(assetNode)
          .map(v => {
            invariant(v.type === 'dependency');
            return v;
          });
        for (let depNode of dependencyNodes) {
          let assetGroupNodes = this.assetGraph
            .getNodesConnectedFrom(depNode)
            .map(v => {
              invariant(v.type === 'asset_group');
              return v;
            });
          if (assetGroupNodes.length == 0) {
            // Dependency might not be resolved yet
            continue;
          }
          invariant(assetGroupNodes.length === 1);
          let assetGroupNode = assetGroupNodes[0];

Review comment (suggestion):
Maybe use `traverse` for this? You can use `skipChildren` to stop going any further once you hit the asset group.

Code after:
    return defer;
  }

  handleCompletedDepPathRequest(
    requestNode: DepPathRequestNode,
    assetGroup: AssetRequest | null
  ) {
    if (!assetGroup) {
      return;
    }
    let dependency = requestNode.value;

    let defer = this.shouldDeferDependency(dependency, assetGroup.sideEffects);

    let assetGroupNode = nodeFromAssetGroup(assetGroup, defer);
    let existingAssetGroupNode = this.assetGraph.getNode(assetGroupNode.id);
    if (existingAssetGroupNode) {
      // Don't overwrite non-deferred asset groups with deferred ones
      invariant(existingAssetGroupNode.type === 'asset_group');
      assetGroupNode.deferred = existingAssetGroupNode.deferred && defer;
    }
    this.assetGraph.resolveDependency(dependency, assetGroupNode);
    if (existingAssetGroupNode) {
      // Node already existed, that asset might have deferred dependencies,
      // recheck all dependencies of all assets of this asset group
      this.assetGraph.traverse((node, parent, actions) => {
        if (node == assetGroupNode) {
          return;
        }

        if (node.type == 'asset_group') {
          invariant(parent && parent.type === 'dependency');
          if (
            node.deferred &&
            !this.shouldDeferDependency(parent.value, node.value.sideEffects)
          ) {
            node.deferred = false;
            this.requestGraph.addAssetRequest(node.id, node.value);
          }

          actions.skipChildren();
        }

        return node;
      }, assetGroupNode);
    }
  }

  respondToFSEvents(events: Array<Event>) {
    return this.requestGraph.respondToFSEvents(events);
  }