Skip to content

Commit 3afd784

Browse files
alan-agius4clydin
authored andcommittedSep 2, 2022
fix(@angular-devkit/build-angular): watch index file when running build in watch mode
Since the index augmentation happens outside of Webpack previously the index html was not being watched during watch mode. Closes #23851 (cherry picked from commit 130975c)
1 parent 5405a9b commit 3afd784

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/browser/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ export function buildWebpackBrowser(
173173
// Check and warn about IE browser support
174174
checkInternetExplorerSupport(initialization.projectRoot, context.logger);
175175

176+
// Add index file to watched files.
177+
if (options.watch) {
178+
const indexInputFile = path.join(context.workspaceRoot, getIndexInputFile(options.index));
179+
initialization.config.plugins ??= [];
180+
initialization.config.plugins.push({
181+
apply: (compiler: webpack.Compiler) => {
182+
compiler.hooks.thisCompilation.tap('build-angular', (compilation) => {
183+
compilation.fileDependencies.add(indexInputFile);
184+
});
185+
},
186+
});
187+
}
188+
176189
return {
177190
...initialization,
178191
cacheOptions: normalizeCacheOptions(projectMetadata, context.workspaceRoot),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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/operators';
10+
import { BUILD_TIMEOUT, buildWebpackBrowser } from '../../index';
11+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
12+
13+
describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
14+
describe('Behavior: "index is updated during watch mode"', () => {
15+
it('index is watched in watch mode', async () => {
16+
harness.useTarget('build', {
17+
...BASE_OPTIONS,
18+
watch: true,
19+
});
20+
21+
const buildCount = await harness
22+
.execute()
23+
.pipe(
24+
timeout(BUILD_TIMEOUT),
25+
concatMap(async ({ result }, index) => {
26+
expect(result?.success).toBe(true);
27+
28+
switch (index) {
29+
case 0: {
30+
harness.expectFile('dist/index.html').content.toContain('HelloWorldApp');
31+
harness.expectFile('dist/index.html').content.not.toContain('UpdatedPageTitle');
32+
33+
// Trigger rebuild
34+
await harness.modifyFile('src/index.html', (s) =>
35+
s.replace('HelloWorldApp', 'UpdatedPageTitle'),
36+
);
37+
break;
38+
}
39+
case 1: {
40+
harness.expectFile('dist/index.html').content.toContain('UpdatedPageTitle');
41+
break;
42+
}
43+
}
44+
}),
45+
take(2),
46+
count(),
47+
)
48+
.toPromise();
49+
50+
expect(buildCount).toBe(2);
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)
Please sign in to comment.