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

Code before:
import Dependency, {dependencyToInternalDependency} from './Dependency';
import {mapVisitor} from '../Graph';

// Friendly access for other modules within this package that need access
// to the internal bundle.
const _bundleGraphToInternalBundleGraph: WeakMap<
  IBundleGraph<IBundle>,
  InternalBundleGraph,
> = new WeakMap();
export function bundleGraphToInternalBundleGraph(
  bundleGraph: IBundleGraph<IBundle>,
): InternalBundleGraph {
  return nullthrows(_bundleGraphToInternalBundleGraph.get(bundleGraph));
}

type BundleFactory<TBundle: IBundle> = (
  InternalBundle,
  InternalBundleGraph,
  ParcelOptions,
) => TBundle;

export default class BundleGraph<TBundle: IBundle>
  implements IBundleGraph<TBundle> {
  #graph; // InternalBundleGraph
  #options; // ParcelOptions
  _createBundle: BundleFactory<TBundle>;

  constructor(
    graph: InternalBundleGraph,
    createBundle: BundleFactory<TBundle>,
    options: ParcelOptions,
  ) {
    this.#graph = graph;
    this.#options = options;
    this._createBundle = createBundle;
    // $FlowFixMe
    _bundleGraphToInternalBundleGraph.set(this, graph);
  }

  getDependencyResolution(dep: IDependency, bundle: ?IBundle): ?IAsset {
    let resolution = this.#graph.getDependencyResolution(
      dependencyToInternalDependency(dep),
      bundle && bundleToInternalBundle(bundle),
    );
    if (resolution) {
      return assetFromValue(resolution, this.#options);
    }
  }

  getIncomingDependencies(asset: IAsset): Array<IDependency> {
    return this.#graph

Review comment (suggestion):
This doesn't seem to work as a private property (`#createBundle`) with Flow. The constructor is still accessible through the `constructor` property of the wrapper objects externally anyway.

Code after:
import Dependency, {dependencyToInternalDependency} from './Dependency';
import {mapVisitor} from '../Graph';

// Friendly access for other modules within this package that need access
// to the internal bundle.
const _bundleGraphToInternalBundleGraph: WeakMap<
  IBundleGraph<IBundle>,
  InternalBundleGraph,
> = new WeakMap();
export function bundleGraphToInternalBundleGraph(
  bundleGraph: IBundleGraph<IBundle>,
): InternalBundleGraph {
  return nullthrows(_bundleGraphToInternalBundleGraph.get(bundleGraph));
}

type BundleFactory<TBundle: IBundle> = (
  InternalBundle,
  InternalBundleGraph,
  ParcelOptions,
) => TBundle;

export default class BundleGraph<TBundle: IBundle>
  implements IBundleGraph<TBundle> {
  #graph: InternalBundleGraph;
  #options: ParcelOptions;
  // This is invoked as `this.#createBundle.call(null, ...)` below, as private
  // properties aren't currently callable in Flow:
  // https://github.com/parcel-bundler/parcel/pull/4591#discussion_r422661115
  // https://github.com/facebook/flow/issues/7877
  #createBundle: BundleFactory<TBundle>;

  constructor(
    graph: InternalBundleGraph,
    createBundle: BundleFactory<TBundle>,
    options: ParcelOptions,
  ) {
    this.#graph = graph;
    this.#options = options;
    this.#createBundle = createBundle;
    // $FlowFixMe
    _bundleGraphToInternalBundleGraph.set(this, graph);
  }

  isDependencyDeferred(dep: IDependency): boolean {
    return this.#graph.isDependencyDeferred(
      dependencyToInternalDependency(dep),
    );
  }

  getDependencyResolution(dep: IDependency, bundle: ?IBundle): ?IAsset {
    let resolution = this.#graph.getDependencyResolution(