Skip to content

Commit cd8333c

Browse files
JoostKatscott
authored andcommitted
fix(ivy): recompile component when template changes in ngc watch mode (#33551)
When the Angular compiler is operated through the ngc binary in watch mode, changing a template in an external file would not cause the component to be recompiled if Ivy is enabled. There was a problem with how a cached compiler host was present that was unaware of the changed resources, therefore failing to trigger a recompilation of a component whenever its template changes. This commit fixes the issue by ensuring that information about modified resources is correctly available to the cached compiler host. Fixes #32869 PR Close #33551
1 parent 854a377 commit cd8333c

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎packages/compiler-cli/src/perform_watch.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function performWatchCompilation(host: PerformWatchHost):
153153
}
154154

155155
// Invoked to perform initial compilation or re-compilation in watch mode
156-
function doCompilation(modifiedResourceFiles?: Set<string>): Diagnostics {
156+
function doCompilation(): Diagnostics {
157157
if (!cachedOptions) {
158158
cachedOptions = host.readConfiguration();
159159
}
@@ -197,8 +197,12 @@ export function performWatchCompilation(host: PerformWatchHost):
197197
return ce.content !;
198198
};
199199
// Provide access to the file paths that triggered this rebuild
200-
cachedCompilerHost.getModifiedResourceFiles =
201-
modifiedResourceFiles !== undefined ? () => modifiedResourceFiles : undefined;
200+
cachedCompilerHost.getModifiedResourceFiles = function() {
201+
if (timerHandleForRecompilation === undefined) {
202+
return undefined;
203+
}
204+
return timerHandleForRecompilation.modifiedResourceFiles;
205+
};
202206
}
203207
ignoreFilesForWatch.clear();
204208
const oldProgram = cachedProgram;
@@ -287,7 +291,7 @@ export function performWatchCompilation(host: PerformWatchHost):
287291
function recompile() {
288292
host.reportDiagnostics(
289293
[createMessageDiagnostic('File change detected. Starting incremental compilation.')]);
290-
doCompilation(timerHandleForRecompilation !.modifiedResourceFiles);
294+
doCompilation();
291295
timerHandleForRecompilation = undefined;
292296
}
293297
}

‎packages/compiler-cli/test/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ ts_library(
109109
":test_utils",
110110
"//packages/compiler",
111111
"//packages/compiler-cli",
112+
"//packages/private/testing",
112113
"@npm//typescript",
113114
],
114115
)

‎packages/compiler-cli/test/perform_watch_spec.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {ivyEnabled} from '@angular/private/testing';
910
import * as fs from 'fs';
1011
import * as path from 'path';
1112
import * as ts from 'typescript';
@@ -24,8 +25,8 @@ describe('perform watch', () => {
2425
outDir = path.resolve(testSupport.basePath, 'outDir');
2526
});
2627

27-
function createConfig(): ng.ParsedConfiguration {
28-
const options = testSupport.createCompilerOptions({outDir});
28+
function createConfig(overrideOptions: ng.CompilerOptions = {}): ng.ParsedConfiguration {
29+
const options = testSupport.createCompilerOptions({outDir, ...overrideOptions});
2930
return {
3031
options,
3132
rootNames: [path.resolve(testSupport.basePath, 'src/index.ts')],
@@ -50,6 +51,33 @@ describe('perform watch', () => {
5051
expect(fs.existsSync(path.resolve(outDir, 'src', 'main.ngfactory.js'))).toBe(true);
5152
});
5253

54+
it('should recompile components when its template changes', () => {
55+
const config = createConfig({enableIvy: ivyEnabled});
56+
const host = new MockWatchHost(config);
57+
58+
testSupport.writeFiles({
59+
'src/main.ts': createModuleAndCompSource('main', './main.html'),
60+
'src/main.html': 'initial',
61+
'src/index.ts': `export * from './main'; `,
62+
});
63+
64+
const watchResult = performWatchCompilation(host);
65+
expectNoDiagnostics(config.options, watchResult.firstCompileResult);
66+
67+
const htmlPath = path.resolve(testSupport.basePath, 'src', 'main.html');
68+
const genPath = ivyEnabled ? path.resolve(outDir, 'src', 'main.js') :
69+
path.resolve(outDir, 'src', 'main.ngfactory.js');
70+
71+
const initial = fs.readFileSync(genPath, {encoding: 'utf8'});
72+
expect(initial).toContain('"initial"');
73+
74+
fs.writeFileSync(htmlPath, 'updated');
75+
host.triggerFileChange(FileChangeEvent.Change, htmlPath);
76+
77+
const updated = fs.readFileSync(genPath, {encoding: 'utf8'});
78+
expect(updated).toContain('"updated"');
79+
});
80+
5381
it('should cache files on subsequent runs', () => {
5482
const config = createConfig();
5583
const host = new MockWatchHost(config);

0 commit comments

Comments
 (0)