Skip to content

Commit 44b4017

Browse files
committed
fix(@angular/build): ensure input index HTML file triggers rebuilds when changed
When using the `application` builder, the input index HTML file will now correctly trigger rebuilds of the application. The rebuilds are not currently optimized for the input index file only case but changes will be reflected in the output. (cherry picked from commit 2708b18)
1 parent 95a4d6e commit 44b4017

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

packages/angular/build/src/builders/application/execute-build.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ export async function executeBuild(
141141
);
142142
}
143143

144+
// Watch input index HTML file if configured
145+
if (options.indexHtmlOptions) {
146+
executionResult.extraWatchFiles.push(options.indexHtmlOptions.input);
147+
}
148+
144149
// Perform i18n translation inlining if enabled
145150
if (i18nOptions.shouldInline) {
146151
const result = await inlineI18n(options, executionResult, initialFiles);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { concatMap, count, take, timeout } from 'rxjs';
10+
import { buildApplication } from '../../index';
11+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
12+
13+
/**
14+
* Maximum time in milliseconds for single build/rebuild
15+
* This accounts for CI variability.
16+
*/
17+
export const BUILD_TIMEOUT = 30_000;
18+
19+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
20+
describe('Behavior: "Rebuilds when input index HTML changes"', () => {
21+
beforeEach(async () => {
22+
// Application code is not needed for styles tests
23+
await harness.writeFile('src/main.ts', 'console.log("TEST");');
24+
});
25+
26+
it('rebuilds output index HTML', async () => {
27+
harness.useTarget('build', {
28+
...BASE_OPTIONS,
29+
watch: true,
30+
});
31+
32+
const buildCount = await harness
33+
.execute({ outputLogsOnFailure: false })
34+
.pipe(
35+
timeout(30000),
36+
concatMap(async ({ result }, index) => {
37+
switch (index) {
38+
case 0:
39+
expect(result?.success).toBe(true);
40+
harness.expectFile('dist/browser/index.html').content.toContain('charset="utf-8"');
41+
42+
await harness.modifyFile('src/index.html', (content) =>
43+
content.replace('charset="utf-8"', 'abc'),
44+
);
45+
break;
46+
case 1:
47+
expect(result?.success).toBe(true);
48+
harness
49+
.expectFile('dist/browser/index.html')
50+
.content.not.toContain('charset="utf-8"');
51+
52+
await harness.modifyFile('src/index.html', (content) =>
53+
content.replace('abc', 'charset="utf-8"'),
54+
);
55+
break;
56+
case 2:
57+
expect(result?.success).toBe(true);
58+
harness.expectFile('dist/browser/index.html').content.toContain('charset="utf-8"');
59+
60+
break;
61+
}
62+
}),
63+
take(3),
64+
count(),
65+
)
66+
.toPromise();
67+
68+
expect(buildCount).toBe(3);
69+
});
70+
});
71+
});

packages/angular/build/src/tools/esbuild/bundler-execution-result.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class ExecutionResult {
4242
warnings: (Message | PartialMessage)[] = [];
4343
logs: string[] = [];
4444
externalMetadata?: ExternalResultMetadata;
45+
extraWatchFiles: string[] = [];
4546

4647
constructor(
4748
private rebuildContexts: BundlerContext[],
@@ -138,7 +139,7 @@ export class ExecutionResult {
138139
files.push(...this.codeBundleCache.loadResultCache.watchFiles);
139140
}
140141

141-
return files;
142+
return files.concat(this.extraWatchFiles);
142143
}
143144

144145
createRebuildState(fileChanges: ChangedFiles): RebuildState {

0 commit comments

Comments
 (0)