Skip to content

Commit 5dbf357

Browse files
clydinjosephperrott
authored andcommitted
feat(compiler-cli): support getting resource dependencies for a source file (angular#38048)
The compiler maintains an internal dependency graph of all resource dependencies for application source files. This information can be useful for tools that integrate the compiler and need to support file watching. This change adds a `getResourceDependencies` method to the `NgCompiler` class that allows compiler integrations to access resource dependencies of files within the compilation. PR Close angular#38048
1 parent 3bbbf39 commit 5dbf357

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

packages/compiler-cli/src/ngtsc/core/src/compiler.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ export class NgCompiler {
162162
this.ignoreForEmit = this.adapter.ignoreForEmit;
163163
}
164164

165+
/**
166+
* Get the resource dependencies of a file.
167+
*
168+
* If the file is not part of the compilation, an empty array will be returned.
169+
*/
170+
getResourceDependencies(file: ts.SourceFile): string[] {
171+
this.ensureAnalyzed();
172+
173+
return this.incrementalDriver.depGraph.getResourceDependencies(file);
174+
}
175+
165176
/**
166177
* Get all Angular-related diagnostics for this compilation.
167178
*

packages/compiler-cli/src/ngtsc/core/test/compiler_test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,40 @@ runInEachFileSystem(() => {
105105
expect(components).toEqual(new Set([CmpA, CmpC]));
106106
});
107107
});
108+
109+
describe('getResourceDependencies', () => {
110+
it('should return resource dependencies of a component source file', () => {
111+
const COMPONENT = _('/cmp.ts');
112+
fs.writeFile(COMPONENT, `
113+
import {Component} from '@angular/core';
114+
@Component({
115+
selector: 'test-cmp',
116+
templateUrl: './template.html',
117+
styleUrls: ['./style.css'],
118+
})
119+
export class Cmp {}
120+
`);
121+
fs.writeFile(_('/template.html'), `<h1>Resource</h1>`);
122+
fs.writeFile(_('/style.css'), `h1 { }`);
123+
124+
const options: NgCompilerOptions = {
125+
strictTemplates: true,
126+
};
127+
const baseHost = new NgtscCompilerHost(getFileSystem(), options);
128+
const host = NgCompilerHost.wrap(baseHost, [COMPONENT], options, /* oldProgram */ null);
129+
const program = ts.createProgram({host, options, rootNames: host.inputFiles});
130+
const compiler = new NgCompiler(
131+
host, options, program, new ReusedProgramStrategy(program, host, options, []),
132+
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false);
133+
134+
const deps = compiler.getResourceDependencies(getSourceFileOrError(program, COMPONENT));
135+
expect(deps.length).toBe(2);
136+
expect(deps).toEqual(jasmine.arrayContaining([
137+
jasmine.stringMatching(/\/template.html$/),
138+
jasmine.stringMatching(/\/style.css$/),
139+
]));
140+
});
141+
});
108142
});
109143
});
110144

packages/compiler-cli/src/ngtsc/incremental/src/dependency_tracking.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export class FileDependencyGraph<T extends {fileName: string} = ts.SourceFile> i
5353
}
5454
}
5555

56+
getResourceDependencies(from: T): AbsoluteFsPath[] {
57+
const node = this.nodes.get(from);
58+
59+
return node ? [...node.usesResources] : [];
60+
}
61+
5662
isStale(sf: T, changedTsPaths: Set<string>, changedResources: Set<AbsoluteFsPath>): boolean {
5763
return isLogicallyChanged(sf, this.nodeFor(sf), changedTsPaths, EMPTY_SET, changedResources);
5864
}

0 commit comments

Comments
 (0)