Skip to content

Commit

Permalink
refactor(compiler-cli): optimize extra import generation in local com…
Browse files Browse the repository at this point in the history
…pilation mode
  • Loading branch information
pmvald committed Apr 25, 2024
1 parent e478180 commit 48e6d74
Show file tree
Hide file tree
Showing 3 changed files with 981 additions and 831 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,12 @@ export class ComponentDecoratorHandler implements
// Dependencies from the `@Component.deferredImports` field.
const explicitlyDeferredDependencies = getExplicitlyDeferredDeps(scope);

// Mark the component is an NgModule-based component with its NgModule in a different file
// then mark this file for extra import generation
if (isModuleScope && context.fileName !== getSourceFile(scope.ngModule).fileName) {
this.localCompilationExtraImportsTracker?.markFileForExtraImportGeneration(context);
}

// Make sure that `@Component.imports` and `@Component.deferredImports` do not have
// the same dependencies.
if (metadata.isStandalone && analysis.rawDeferredImports !== null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,23 @@ export class LocalCompilationExtraImportsTracker {
private readonly localImportsMap = new Map<string, Set<string>>();
private readonly globalImportsSet = new Set<string>();

/** Names of the files marked for extra import generation. */
private readonly markedFilesSet = new Set<string>();

constructor(private readonly typeChecker: ts.TypeChecker) {}

/**
* Marks the source file for extra imports generation.
*
* The extra imports are generated only for the files marked through this method. In other words,
* the method {@link getImportsForFile} returns empty if the file is not marked. This allows the
* consumers of this tool to avoid generating extra imports for unrelated files (e.g., non-Angular
* files)
*/
markFileForExtraImportGeneration(sf: ts.SourceFile) {
this.markedFilesSet.add(sf.fileName);
}

/**
* Adds an extra import to be added to the generated file of a specific source file.
*/
Expand Down Expand Up @@ -91,6 +106,10 @@ export class LocalCompilationExtraImportsTracker {
* Returns the list of all module names that the given file should include as its extra imports.
*/
getImportsForFile(sf: ts.SourceFile): string[] {
if (!this.markedFilesSet.has(sf.fileName)) {
return [];
}

return [
...this.globalImportsSet,
...(this.localImportsMap.get(sf.fileName) ?? []),
Expand Down

0 comments on commit 48e6d74

Please sign in to comment.