bundle method

ASTCompilation bundle({
  1. required HTSource source,
  2. Version? version,
})

Parse a string content and generate a library, will import other files.

Implementation

ASTCompilation bundle({
  required HTSource source,
  Version? version,
}) {
  final sourceParseResult = parser.parseSource(source);
  final sw = Stopwatch()..start();
  final sourceParseErrors = sourceParseResult.errors;
  final values = <String, ASTSource>{};
  final sources = <String, ASTSource>{};
  final Set cachedParsingTargets = <String>{};
  void handleImport(ASTSource astSource) {
    cachedParsingTargets.add(astSource.fullName);
    for (final decl in astSource.imports) {
      try {
        if (decl.isPreloadedModule) {
          decl.fullFromPath = decl.fromPath;
          continue;
        }
        late final ASTSource importedSource;
        String importFullName;
        if (config.normalizeImportPath) {
          var currentDir = astSource.fullName
                  .startsWith(InternalIdentifier.anonymousScript)
              ? sourceContext.root
              : path.dirname(astSource.fullName);
          if (!currentDir.endsWith('/')) {
            currentDir += '/';
          }
          decl.fullFromPath = importFullName = sourceContext.getAbsolutePath(
              key: decl.fromPath!, dirName: currentDir);
        } else {
          decl.fullFromPath = importFullName = decl.fromPath!;
        }
        if (sources.keys.contains(importFullName) ||
            cachedParsingTargets.contains(importFullName)) {
          continue;
        }
        final source2 = sourceContext.getResource(importFullName);
        importedSource = parser.parseSource(source2);
        // final parser2 = HTParser(sourceContext: sourceContext);
        // importedSource = parser2.parseSource(source2);
        sourceParseErrors.addAll(importedSource.errors);
        // _cachedParseResults[importFullName] = importedSource;
        if (importedSource.resourceType == HTResourceType.json) {
          values[importFullName] = importedSource;
        } else {
          handleImport(importedSource);
          sources[importFullName] = importedSource;
        }
      } catch (error) {
        if (error is HTError) {
          if (error.code != HTErrorCode.resourceDoesNotExist) {
            sourceParseErrors.add(error);
          } else {
            final convertedError = HTError.sourceProviderError(
                decl.fromPath!, astSource.fullName,
                filename: source.fullName,
                line: decl.line,
                column: decl.column,
                offset: decl.offset,
                length: decl.length);
            sourceParseErrors.add(convertedError);
          }
        } else {
          final convertedError = HTError.extern(error.toString(),
              filename: source.fullName,
              line: decl.line,
              column: decl.column,
              offset: decl.offset,
              length: decl.length);
          sourceParseErrors.add(convertedError);
        }
      }
    }
    cachedParsingTargets.remove(astSource.fullName);
  }

  if (sourceParseResult.resourceType == HTResourceType.json) {
    values[sourceParseResult.fullName] = sourceParseResult;
  } else {
    handleImport(sourceParseResult);
    sources[sourceParseResult.fullName] = sourceParseResult;
  }
  final compilation = ASTCompilation(
    values: values,
    sources: sources,
    entryFullname: source.fullName,
    entryResourceType: source.type,
    errors: sourceParseErrors,
    version: version,
  );
  if (config.printPerformanceStatistics) {
    print(
        'hetu: ${sw.elapsedMilliseconds}ms\tto bundle\t[${source.fullName}]');
  }
  sw.stop();
  return compilation;
}