resolveImport method

  1. @override
Uri resolveImport(
  1. Uri target,
  2. Uri source,
  3. String extension
)

Resolves an import of a generated Dart file.

Both source and target are .proto files, where source imports target.

The returned URI can be used within one of the source's dart files to import the target's generated file with the given extension.

Implementation

@override
Uri resolveImport(Uri target, Uri source, String extension) {
  final targetBase = p.withoutExtension(target.path);
  final targetUri = _packageUriFor('$targetBase$extension');
  final sourceUri = _packageUriFor(source.path);

  if (targetUri == null && sourceUri != null) {
    // We can't reach outside of the lib/ directory of a package without
    // using a package: import. Using a relative import for [target] could
    // break anyone who uses a package: import to load [source].
    throw 'ERROR: cannot generate import for $target from $source.';
  }

  if (targetUri != null && sourceUri?.packageName != targetUri.packageName) {
    return targetUri.uri;
  }

  return super.resolveImport(target, source, extension);
}