Skip to content

Commit cc4fec1

Browse files
committed
fix(@ngtools/webpack): invalidate all the files changed
Webpack only passes the first file that was changed. If there are multiple, we need to validate all of them. For that we result to listening to Watchpack directly.
1 parent 63eaaee commit cc4fec1

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

packages/@ngtools/webpack/src/plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,14 @@ export class AotPlugin implements Tapable {
237237
apply(compiler: any) {
238238
this._compiler = compiler;
239239

240-
compiler.plugin('invalid', (fileName: string) => {
240+
compiler.plugin('invalid', () => {
241241
// Turn this off as soon as a file becomes invalid and we're about to start a rebuild.
242242
this._firstRun = false;
243243
this._diagnoseFiles = {};
244244

245-
this._compilerHost.invalidate(fileName);
245+
compiler.watchFileSystem.watcher.once('aggregated', (changes: string[]) => {
246+
changes.forEach((fileName: string) => this._compilerHost.invalidate(fileName));
247+
});
246248
});
247249

248250
// Add lazy modules to the context module for @angular/core/src/linker

tests/e2e/tests/build/rebuild.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
silentExecAndWaitForOutputToMatch,
66
ng,
77
} from '../../utils/process';
8-
import {writeFile} from '../../utils/fs';
8+
import {writeFile, writeMultipleFiles, appendToFile, expectFileToMatch} from '../../utils/fs';
99
import {wait} from '../../utils/utils';
10+
import {request} from '../../utils/http';
1011

1112

1213
export default function() {
@@ -69,6 +70,33 @@ export default function() {
6970
throw new Error('Expected webpack to create a new chunk, but did not.');
7071
}
7172
})
73+
.then(() => wait(1000))
74+
// Change multiple files and check that all of them are invalidated and recompiled.
75+
.then(() => writeMultipleFiles({
76+
'src/app/app.module.ts': `
77+
console.log('$$_E2E_GOLDEN_VALUE_1');
78+
export let X = '$$_E2E_GOLDEN_VALUE_2';
79+
`,
80+
'src/main.ts': `
81+
import * as m from './app/app.module';
82+
console.log(m.X);
83+
console.log('$$_E2E_GOLDEN_VALUE_3');
84+
`
85+
}))
86+
.then(() => waitForAnyProcessOutputToMatch(
87+
/webpack: bundle is now VALID|webpack: Compiled successfully./, 10000))
88+
.then(() => request('http://localhost:4200/main.bundle.js'))
89+
.then((body) => {
90+
if (!body.match(/\$\$_E2E_GOLDEN_VALUE_1/)) {
91+
throw new Error('Expected golden value 1.');
92+
}
93+
if (!body.match(/\$\$_E2E_GOLDEN_VALUE_2/)) {
94+
throw new Error('Expected golden value 2.');
95+
}
96+
if (!body.match(/\$\$_E2E_GOLDEN_VALUE_3/)) {
97+
throw new Error('Expected golden value 3.');
98+
}
99+
})
72100
.then(() => killAllProcesses(), (err: any) => {
73101
killAllProcesses();
74102
throw err;

tests/e2e/utils/fs.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ export function copyFile(from: string, to: string) {
8282
}
8383

8484

85-
export function writeMultipleFiles(fs: any) {
86-
return Object.keys(fs)
87-
.reduce((previous, curr) => {
88-
return previous.then(() => writeFile(curr, fs[curr]));
89-
}, Promise.resolve());
85+
export function writeMultipleFiles(fs: { [path: string]: string }) {
86+
return Promise.all(Object.keys(fs).map(fileName => writeFile(fileName, fs[fileName])));
9087
}
9188

9289

0 commit comments

Comments
 (0)