resolve method

String? resolve(
  1. String scriptUri
)

Returns the absolute path wrt. to the given environment or null, if the import could not be resolved.

Implementation

String? resolve(String scriptUri) {
  final uri = Uri.parse(scriptUri);
  if (uri.scheme == 'dart') {
    final sdkRoot = this.sdkRoot;
    if (sdkRoot == null) {
      // No sdk-root given, do not resolve dart: URIs.
      return null;
    }
    String filePath;
    if (uri.pathSegments.length > 1) {
      var path = uri.pathSegments[0];
      // Drop patch files, since we don't have their source in the compiled
      // SDK.
      if (path.endsWith('-patch')) {
        failed.add('$uri');
        return null;
      }
      // Canonicalize path. For instance: _collection-dev => _collection_dev.
      path = path.replaceAll('-', '_');
      final pathSegments = [
        sdkRoot,
        path,
        ...uri.pathSegments.sublist(1),
      ];
      filePath = p.joinAll(pathSegments);
    } else {
      // Resolve 'dart:something' to be something/something.dart in the SDK.
      final lib = uri.path;
      filePath = p.join(sdkRoot, lib, '$lib.dart');
    }
    return resolveSymbolicLinks(filePath);
  }
  if (uri.scheme == 'package') {
    final packages = _packages;
    if (packages == null) {
      return null;
    }

    final packageName = uri.pathSegments[0];
    final packageUri = packages[packageName];
    if (packageUri == null) {
      failed.add('$uri');
      return null;
    }
    final packagePath = p.fromUri(packageUri);
    final pathInPackage = p.joinAll(uri.pathSegments.sublist(1));
    return resolveSymbolicLinks(p.join(packagePath, pathInPackage));
  }
  if (uri.scheme == 'file') {
    return resolveSymbolicLinks(p.fromUri(uri));
  }
  // We cannot deal with anything else.
  failed.add('$uri');
  return null;
}