Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): generate service worker manifest …
Browse files Browse the repository at this point in the history
…when running build in watch mode

Closes #16883
  • Loading branch information
alan-agius4 authored and filipesilva committed Aug 7, 2020
1 parent 9b3369a commit 36eb009
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 36eb009

Please sign in to comment.