compile method

Future<DartSourceOutput?> compile(
  1. LibraryElement element
)

Given a .dart library target element, returns .template.dart.

May return Future(null) to signify the compilation should result in no output (such as there were no @Component() annotations found in the target element).

Implementation

Future<DartSourceOutput?> compile(LibraryElement element) async {
  final exceptionHandler = ComponentVisitorExceptionHandler();

  // Parse Dart code for @Components and @Directives
  final compileComponentsData = findComponentsAndDirectives(
    LibraryReader(element),
    exceptionHandler,
  );

  // Some uncaught exceptions are handled asynchronously, so we need to await
  // and let those be resolved and emitted before continuing. If at least one
  // error occurs, this function will throw and not continue further in the
  // compiler.
  await exceptionHandler.maybeReportErrors(_resolver);

  final noRelevantAnnotationsFound = compileComponentsData.isEmpty;
  if (noRelevantAnnotationsFound) return null;

  // Convert the Components into an intermediate representation
  final components = <ir.Component>[];
  for (final component in compileComponentsData.components) {
    final normalizedComponent = await _normalizeComponent(component);
    final componentIR = _convertToIR(normalizedComponent);
    components.add(componentIR);
  }

  // Convert the Directives into an intermediate representation
  final directives = <ir.Directive>[];
  for (final directive in compileComponentsData.directives) {
    final directiveIR = _directiveConverter.convertDirectiveToIR(directive);
    directives.add(directiveIR);
  }

  // Compile and return intermediate representation into Dart source code.
  return _templateCompiler.compile(
    ir.Library(components, directives),
    _moduleUrlFor(compileComponentsData),
  );
}