computeTransitiveDependencies method

Future<List<Module>> computeTransitiveDependencies(
  1. BuildStep buildStep, {
  2. bool throwIfUnsupported = false,
})

Returns all Modules in the transitive dependencies of this module in reverse dependency order.

Throws a MissingModulesException if there are any missing modules. This typically means that somebody is trying to import a non-existing file.

If throwIfUnsupported is true, then an UnsupportedModules will be thrown if there are any modules that are not supported.

Implementation

Future<List<Module>> computeTransitiveDependencies(BuildStep buildStep,
    {bool throwIfUnsupported = false}) async {
  final modules = await buildStep.fetchResource(moduleCache);
  var transitiveDeps = <AssetId, Module>{};
  var modulesToCrawl = {primarySource};
  var missingModuleSources = <AssetId>{};
  var unsupportedModules = <Module>{};

  while (modulesToCrawl.isNotEmpty) {
    var next = modulesToCrawl.last;
    modulesToCrawl.remove(next);
    if (transitiveDeps.containsKey(next)) continue;
    var nextModuleId = next.changeExtension(moduleExtension(platform));
    var module = await modules.find(nextModuleId, buildStep);
    if (module == null || module.isMissing) {
      missingModuleSources.add(next);
      continue;
    }
    if (throwIfUnsupported && !module.isSupported) {
      unsupportedModules.add(module);
    }
    // Don't include the root module in the transitive deps.
    if (next != primarySource) transitiveDeps[next] = module;
    modulesToCrawl.addAll(module.directDependencies);
  }

  if (missingModuleSources.isNotEmpty) {
    throw await MissingModulesException.create(missingModuleSources,
        transitiveDeps.values.toList()..add(this), buildStep);
  }
  if (throwIfUnsupported && unsupportedModules.isNotEmpty) {
    throw UnsupportedModules(unsupportedModules);
  }
  var orderedModules = stronglyConnectedComponents<Module>(
      transitiveDeps.values,
      (m) => m.directDependencies.map((s) => transitiveDeps[s]!),
      equals: (a, b) => a.primarySource == b.primarySource,
      hashCode: (m) => m.primarySource.hashCode);
  return orderedModules.map((c) => c.single).toList();
}