resolve function

Future<LibraryElement> resolve(
  1. String dartSource, {
  2. Map<AssetId, String> additionalFiles = const {},
  3. bool includeAngularDeps = true,
})

Resolves dartSource as a library package:test_lib/test_lib.dart.

Example:

lib = await resolveLibrary(
  '''
    @Component(
      selector: 'example',
      template: 'Hello World',
    )
    class ExampleComponent {}
  ''',
);
  • additionalFiles: May provide additional files available to the program:

    resolveLibrary(
      '''
        @Component(
          selector: 'example',
          templateUrl: 'example.html',
        )
        class ExampleComponent {}
      ''',
      additionalFiles: {
        AssetId(
          'test_lib',
          'lib/example.html',
        ): '''
          <div>Hello World</div>
        ''',
      },
    )
    
  • includeAngularDeps: Set false to not include import 'angular.dart'. This may be used to simulate scenarios where the user has forgotten to add an import to Angular, or where you would want the import specified as an alternative entry-point.

Implementation

Future<LibraryElement> resolve(
  String dartSource, {
  Map<AssetId, String> additionalFiles = const {},
  bool includeAngularDeps = true,
}) async {
  // Add library and import directives to the top.
  dartSource = [
    if (includeAngularDeps) "import '$_angularLibPath';",
    '',
    dartSource,
  ].join('\n');
  final sources = {
    // Map<AssetId, String> -> Map<String, String>
    for (final entry in additionalFiles.entries)
      _assetToPath(entry.key): entry.value,

    // Adds an additional file (dartSource).
    _assetToPath(_defaultAssetId): dartSource,
  };
  final config = await _cachedPackageConfig;
  final result = await withEnabledExperiments(
    () => resolveSources(
      sources,
      (resolver) => resolver.libraryFor(_defaultAssetId),
      packageConfig: config,
    ),
    ['non-nullable'],
  );
  return result;
}