From 36eb009de2dcb8796291b51cd7f83c35e2ba8960 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 6 Aug 2020 14:46:28 +0200 Subject: [PATCH] fix(@angular-devkit/build-angular): generate service worker manifest when running build in watch mode Closes #16883 --- .../build_angular/src/browser/index.ts | 2 +- .../src/browser/specs/service-worker_spec.ts | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/angular_devkit/build_angular/src/browser/index.ts b/packages/angular_devkit/build_angular/src/browser/index.ts index 839961e10af4..6652c083d9f7 100644 --- a/packages/angular_devkit/build_angular/src/browser/index.ts +++ b/packages/angular_devkit/build_angular/src/browser/index.ts @@ -747,7 +747,7 @@ export function buildWebpackBrowser( } } - if (!options.watch && options.serviceWorker) { + if (options.serviceWorker) { for (const [locale, outputPath] of outputPaths.entries()) { let localeBaseHref; if (i18n.locales[locale] && i18n.locales[locale].baseHref !== '') { diff --git a/packages/angular_devkit/build_angular/src/browser/specs/service-worker_spec.ts b/packages/angular_devkit/build_angular/src/browser/specs/service-worker_spec.ts index ede66e571a1c..03fbdfc46cf1 100644 --- a/packages/angular_devkit/build_angular/src/browser/specs/service-worker_spec.ts +++ b/packages/angular_devkit/build_angular/src/browser/specs/service-worker_spec.ts @@ -7,8 +7,10 @@ */ import { Architect } from '@angular-devkit/architect'; import { normalize, virtualFs } from '@angular-devkit/core'; +import { debounceTime, take, tap } from 'rxjs/operators'; import { createArchitect, host } from '../../test-utils'; +// tslint:disable-next-line: no-big-function describe('Browser Builder service worker', () => { const manifest = { index: '/index.html', @@ -124,6 +126,60 @@ describe('Browser Builder service worker', () => { await run.stop(); }); + it('works in watch mode', async () => { + host.writeMultipleFiles({ + 'src/ngsw-config.json': JSON.stringify(manifest), + 'src/assets/folder-asset.txt': 'folder-asset.txt', + 'src/styles.css': `body { background: url(./spectrum.png); }`, + }); + + const overrides = { serviceWorker: true, watch: true }; + const run = await architect.scheduleTarget(target, overrides); + let buildNumber = 0; + + await run.output + .pipe( + debounceTime(3000), + tap(buildEvent => { + expect(buildEvent.success).toBeTrue(); + + const ngswJsonPath = normalize('dist/ngsw.json'); + expect(host.scopedSync().exists(ngswJsonPath)).toBeTrue(); + const ngswJson = JSON.parse(virtualFs.fileBufferToString(host.scopedSync().read(ngswJsonPath))); + + const hashTableEntries = Object.keys(ngswJson.hashTable); + + buildNumber += 1; + switch (buildNumber) { + case 1: + expect(hashTableEntries).toEqual([ + '/assets/folder-asset.txt', + '/favicon.ico', + '/index.html', + '/spectrum.png', + ]); + + host.copyFile('src/assets/folder-asset.txt', 'src/assets/folder-new-asset.txt'); + break; + + case 2: + expect(hashTableEntries).toEqual([ + '/assets/folder-asset.txt', + '/assets/folder-new-asset.txt', + '/favicon.ico', + '/index.html', + '/spectrum.png', + ]); + break; + } + }), + take(2), + ) + .toPromise(); + + await run.stop(); + }); + it('works with service worker and baseHref', async () => { host.writeMultipleFiles({ 'src/ngsw-config.json': JSON.stringify(manifest),