Skip to content

Commit e11d556

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/build-angular): generate service worker manifest when running build in watch mode
Closes #16883
1 parent 35bc2bc commit e11d556

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ export function buildWebpackBrowser(
747747
}
748748
}
749749

750-
if (!options.watch && options.serviceWorker) {
750+
if (options.serviceWorker) {
751751
for (const [locale, outputPath] of outputPaths.entries()) {
752752
let localeBaseHref;
753753
if (i18n.locales[locale] && i18n.locales[locale].baseHref !== '') {

packages/angular_devkit/build_angular/src/browser/specs/service-worker_spec.ts

+56
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
*/
88
import { Architect } from '@angular-devkit/architect';
99
import { normalize, virtualFs } from '@angular-devkit/core';
10+
import { debounceTime, take, tap } from 'rxjs/operators';
1011
import { createArchitect, host } from '../../test-utils';
1112

13+
// tslint:disable-next-line: no-big-function
1214
describe('Browser Builder service worker', () => {
1315
const manifest = {
1416
index: '/index.html',
@@ -124,6 +126,60 @@ describe('Browser Builder service worker', () => {
124126
await run.stop();
125127
});
126128

129+
it('works in watch mode', async () => {
130+
host.writeMultipleFiles({
131+
'src/ngsw-config.json': JSON.stringify(manifest),
132+
'src/assets/folder-asset.txt': 'folder-asset.txt',
133+
'src/styles.css': `body { background: url(./spectrum.png); }`,
134+
});
135+
136+
const overrides = { serviceWorker: true, watch: true };
137+
const run = await architect.scheduleTarget(target, overrides);
138+
let buildNumber = 0;
139+
140+
await run.output
141+
.pipe(
142+
debounceTime(3000),
143+
tap(buildEvent => {
144+
expect(buildEvent.success).toBeTrue();
145+
146+
const ngswJsonPath = normalize('dist/ngsw.json');
147+
expect(host.scopedSync().exists(ngswJsonPath)).toBeTrue();
148+
const ngswJson = JSON.parse(virtualFs.fileBufferToString(host.scopedSync().read(ngswJsonPath)));
149+
150+
const hashTableEntries = Object.keys(ngswJson.hashTable);
151+
152+
buildNumber += 1;
153+
switch (buildNumber) {
154+
case 1:
155+
expect(hashTableEntries).toEqual([
156+
'/assets/folder-asset.txt',
157+
'/favicon.ico',
158+
'/index.html',
159+
'/spectrum.png',
160+
]);
161+
162+
host.copyFile('src/assets/folder-asset.txt', 'src/assets/folder-new-asset.txt');
163+
break;
164+
165+
case 2:
166+
expect(hashTableEntries).toEqual([
167+
'/assets/folder-asset.txt',
168+
'/assets/folder-new-asset.txt',
169+
'/favicon.ico',
170+
'/index.html',
171+
'/spectrum.png',
172+
]);
173+
break;
174+
}
175+
}),
176+
take(2),
177+
)
178+
.toPromise();
179+
180+
await run.stop();
181+
});
182+
127183
it('works with service worker and baseHref', async () => {
128184
host.writeMultipleFiles({
129185
'src/ngsw-config.json': JSON.stringify(manifest),

0 commit comments

Comments
 (0)