Skip to content

Commit 6649483

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

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

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

+62-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
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,
1818
lazyModuleFnImport,
19+
outputPath,
1920
} from '../utils';
2021

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

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)