Skip to content

Commit 5d0141b

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 f4da756 commit 5d0141b

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+
context.workspaceRoot,
118119
normalize(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
@@ -354,6 +354,7 @@ export function buildWebpackBrowser(
354354
try {
355355
await augmentAppWithServiceWorker(
356356
normalize(projectRoot),
357+
context.workspaceRoot,
357358
normalize(outputPath),
358359
getLocaleBaseHref(i18n, locale) || options.baseHref || '/',
359360
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
@@ -63,6 +63,7 @@ class CliFilesystem implements Filesystem {
6363

6464
export async function augmentAppWithServiceWorker(
6565
appRoot: Path,
66+
workspaceRoot: string,
6667
outputPath: Path,
6768
baseHref: string,
6869
ngswConfigPath?: string,
@@ -71,7 +72,7 @@ export async function augmentAppWithServiceWorker(
7172

7273
// Determine the configuration file path
7374
const configPath = ngswConfigPath
74-
? getSystemPath(normalize(ngswConfigPath))
75+
? path.join(workspaceRoot, getSystemPath(normalize(ngswConfigPath)))
7576
: path.join(getSystemPath(appRoot), 'ngsw-config.json');
7677

7778
// Read the configuration file

0 commit comments

Comments
 (0)