Repository: parcel-bundler/parcel
File: packages/dev/query/src/cli.js (JavaScript)

Code before:
    }
  }
}

function findAsset(v: string) {
  let node = _findAssetNode(v);
  if (node) {
    try {
      console.log(
        `${bundleGraph.getAssetPublicId(
          bundleGraph.getAssetById(node.id),
        )} ${fromProjectPathRelative(node.value.filePath)}`,
      );
    } catch (e) {
      console.log(fromProjectPathRelative(node.value.filePath));
    }
  }
}

function findAssetWithSymbol(local: string) {
  let [, assetId, binding, ref] = nullthrows(
    local.match(/^\$([^$]+)\$([^$]+)\$(.*)$/),
    `symbol ${local} could not be resolved`,
  );

  let asset = bundleGraph._graph.getNodeByContentKey(assetId);

  // If the asset couldn't be found by extracting a content key from the name,
  // search for the local name in asset used symbols.
  if (asset == null) {
    outer: for (let node of assetGraph.nodes.values()) {
      if (node.type === 'asset' && node.value.symbols) {
        for (let symbol of node.value.symbols.values()) {
          if (symbol.local === local) {
            asset = node;
            break outer;
          }
        }
      }
    }
  }

  invariant(asset, `An asset for ${assetId} could not be found`);
  invariant(
    asset.type === 'asset',
    `Expected ${assetId} to be an asset, but found a ${asset.type}`,
  );

  try {
    console.log(
      `${bundleGraph.getAssetPublicId(

Review comment (suggestion):
If you have plugins running after the JS transformer, then `assetId` will be the asset id at the point when the JS transformer ran, and not the final one in the graph. So ideally this would be something like `assetGraph.nodes.find(n => n.type === "asset" && n.value.meta.id === assetId)`

Code after:
          bundleGraph.getAssetById(node.id),
        )} ${fromProjectPathRelative(node.value.filePath)}`,
      );
    } catch (e) {
      console.log(fromProjectPathRelative(node.value.filePath));
    }
  }
}

function findAssetWithSymbol(local: string) {
  let [, assetId, binding, ref] = nullthrows(
    local.match(/^\$([^$]+)\$([^$]+)\$(.*)$/),
    `symbol ${local} could not be resolved`,
  );

  let asset;
  // Search against the id used by the JSTransformer and ScopeHoistingPackager,
  // not the final asset id, as it may have changed with further transformation.
  for (let node of assetGraph.nodes.values()) {
    if (node.type === 'asset' && node.value.meta.id === assetId) {
      asset = node;
      break;
    }
  }

  // If the asset couldn't be found by searching for the id,
  // search for the local name in asset used symbols.
  if (asset == null) {
    outer: for (let node of assetGraph.nodes.values()) {
      if (node.type === 'asset' && node.value.symbols) {
        for (let symbol of node.value.symbols.values()) {
          if (symbol.local === local) {
            asset = node;
            break outer;
          }
        }
      }
    }
  }

  invariant(asset, `An asset for ${assetId} could not be found`);
  invariant(
    asset.type === 'asset',
    `Expected ${assetId} to be an asset, but found a ${asset.type}`,
  );

  try {
    console.log(
      `${bundleGraph.getAssetPublicId(
        bundleGraph.getAssetById(asset.id),
      )} ${fromProjectPathRelative(asset.value.filePath)}`,