Skip to content

Commit 7abe212

Browse files
committed
fix(@angular-devkit/build-angular): correctly resolve custom service worker configuration file
Paths within the `angular.json` file should be relative to the location of the `angular.json` file. The `ngswConfigPath` option was incorrectly using the current working directory for a base path when a relative configuration path was specified. Most of the time this would work as a build command usually is executed from the root of the workspace. However, this may not always be the case and for those cases the actual workspace root is now used to resolve the full path for the service worker configuration file.
1 parent 586f393 commit 7abe212

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

packages/angular_devkit/build_angular/src/builders/app-shell/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ async function _renderUniversal(
115115
if (browserOptions.serviceWorker) {
116116
await augmentAppWithServiceWorker(
117117
projectRoot,
118+
root,
118119
outputPath,
119120
browserOptions.baseHref || '/',
120121
browserOptions.ngswConfigPath,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ export function buildWebpackBrowser(
348348
try {
349349
await augmentAppWithServiceWorker(
350350
projectRoot,
351+
context.workspaceRoot,
351352
outputPath,
352353
getLocaleBaseHref(i18n, locale) || options.baseHref || '/',
353354
options.ngswConfigPath,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ describe('Browser Builder service worker', () => {
5252
await run.stop();
5353
});
5454

55+
it('supports specifying a custom service worker configuration file', async () => {
56+
host.writeMultipleFiles({
57+
'src/configs/ngsw.json': JSON.stringify(manifest),
58+
'src/assets/folder-asset.txt': 'folder-asset.txt',
59+
'src/styles.css': `body { background: url(./spectrum.png); }`,
60+
});
61+
62+
const overrides = { serviceWorker: true, ngswConfigPath: 'src/configs/ngsw.json' };
63+
64+
const run = await architect.scheduleTarget(target, overrides);
65+
66+
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
67+
68+
await run.stop();
69+
70+
expect(host.scopedSync().exists(normalize('dist/ngsw.json'))).toBeTrue();
71+
});
72+
5573
it('works with service worker', async () => {
5674
host.writeMultipleFiles({
5775
'src/ngsw-config.json': JSON.stringify(manifest),

packages/angular_devkit/build_angular/src/utils/service-worker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ class CliFilesystem implements Filesystem {
6262

6363
export async function augmentAppWithServiceWorker(
6464
appRoot: string,
65+
workspaceRoot: string,
6566
outputPath: string,
6667
baseHref: string,
6768
ngswConfigPath?: string,
6869
): Promise<void> {
6970
// Determine the configuration file path
7071
const configPath = ngswConfigPath
71-
? path.normalize(ngswConfigPath)
72+
? path.join(workspaceRoot, ngswConfigPath)
7273
: path.join(appRoot, 'ngsw-config.json');
7374

7475
// Read the configuration file

0 commit comments

Comments
 (0)