Skip to content

Commit 8b1c1f5

Browse files
alan-agius4vikerman
authored andcommitted
fix(@ngtools/webpack): delete all virtual files for styles on change
Fixes #15143
1 parent aa052c5 commit 8b1c1f5

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

packages/angular_devkit/build_angular/test/browser/rebuild_spec_large.ts

+64-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
// tslint:disable:no-big-function
99
import { Architect } from '@angular-devkit/architect';
1010
import { TestLogger } from '@angular-devkit/architect/testing';
11-
import { logging, normalize, virtualFs } from '@angular-devkit/core';
11+
import { join, logging, normalize, virtualFs } from '@angular-devkit/core';
1212
import { debounceTime, take, takeWhile, tap } from 'rxjs/operators';
1313
import {
1414
createArchitect,
1515
host,
1616
ivyEnabled,
1717
lazyModuleFiles,
18-
lazyModuleStringImport,
18+
lazyModuleFnImport,
19+
lazyModuleStringImport,
20+
outputPath,
1921
} from '../utils';
2022

2123
describe('Browser Builder rebuilds', () => {
@@ -477,4 +479,64 @@ describe('Browser Builder rebuilds', () => {
477479
.toPromise();
478480
await run.stop();
479481
});
482+
483+
it('rebuilds AOT on CSS changes', async () => {
484+
const overrides = { watch: true, aot: true };
485+
486+
let buildCount = 1;
487+
const run = await architect.scheduleTarget(target, overrides);
488+
await run.output.pipe(
489+
debounceTime(1000),
490+
tap(() => {
491+
const content = virtualFs.fileBufferToString(
492+
host.scopedSync().read(join(outputPath, 'main.js')),
493+
);
494+
495+
switch (buildCount) {
496+
case 1:
497+
expect(content).not.toContain('color: green');
498+
host.appendToFile('src/app/app.component.css', 'h1 { color: green; }');
499+
break;
500+
case 2:
501+
expect(content).toContain('color: green');
502+
break;
503+
}
504+
505+
buildCount++;
506+
}),
507+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
508+
take(2),
509+
).toPromise();
510+
await run.stop();
511+
});
512+
513+
it('rebuilds AOT on HTML changes', async () => {
514+
const overrides = { watch: true, aot: true };
515+
516+
let buildCount = 1;
517+
const run = await architect.scheduleTarget(target, overrides);
518+
await run.output.pipe(
519+
debounceTime(1000),
520+
tap(() => {
521+
const content = virtualFs.fileBufferToString(
522+
host.scopedSync().read(join(outputPath, 'main.js')),
523+
);
524+
525+
switch (buildCount) {
526+
case 1:
527+
expect(content).not.toContain('New Updated Content');
528+
host.appendToFile('src/app/app.component.html', 'New Updated Content');
529+
break;
530+
case 2:
531+
expect(content).toContain('New Updated Content');
532+
break;
533+
}
534+
535+
buildCount++;
536+
}),
537+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
538+
take(2),
539+
).toPromise();
540+
await run.stop();
541+
});
480542
});

packages/ngtools/webpack/src/compiler_host.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ export class WebpackCompilerHost implements ts.CompilerHost {
3131
'.js.map',
3232
'.ngfactory.js',
3333
'.ngfactory.js.map',
34-
'.ngstyle.js',
35-
'.ngstyle.js.map',
3634
'.ngsummary.json',
3735
];
3836

37+
private _virtualStyleFileExtensions = [
38+
'.shim.ngstyle.js',
39+
'.shim.ngstyle.js.map',
40+
];
41+
3942
constructor(
4043
private _options: ts.CompilerOptions,
4144
basePath: string,
@@ -110,6 +113,10 @@ export class WebpackCompilerHost implements ts.CompilerHost {
110113
});
111114
}
112115

116+
if (fullPath.endsWith('.ts')) {
117+
return;
118+
}
119+
113120
// In case resolveJsonModule and allowJs we also need to remove virtual emitted files
114121
// both if they exists or not.
115122
if (
@@ -119,6 +126,15 @@ export class WebpackCompilerHost implements ts.CompilerHost {
119126
if (this._memoryHost.exists(fullPath)) {
120127
this._memoryHost.delete(fullPath);
121128
}
129+
130+
return;
131+
}
132+
133+
for (const ext of this._virtualStyleFileExtensions) {
134+
const virtualFile = (fullPath + ext) as Path;
135+
if (this._memoryHost.exists(virtualFile)) {
136+
this._memoryHost.delete(virtualFile);
137+
}
122138
}
123139
}
124140

0 commit comments

Comments
 (0)