Skip to content

Commit df75984

Browse files
committed
fix(@angular-devkit/build-angular): change service worker errors to compilation errors
Previously, when there was an error during a build that had service workers enabled, the dev-server crashed as the promise was rejected instead of emitting a compilation error. With this change we update the logic so that any errors during the SW augmentation phase are changed to a compilation error and also update the logic so that when there are compilation errors we don't try to generate a SW.
1 parent f86b384 commit df75984

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/service-worker-plugin.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import type { Compiler } from 'webpack';
9+
import { Compiler, WebpackError } from 'webpack';
1010
import { augmentAppWithServiceWorker } from '../../utils/service-worker';
1111

1212
export interface ServiceWorkerPluginOptions {
@@ -21,6 +21,12 @@ export class ServiceWorkerPlugin {
2121

2222
apply(compiler: Compiler) {
2323
compiler.hooks.done.tapPromise('angular-service-worker', async ({ compilation }) => {
24+
if (compilation.errors.length) {
25+
// Don't generate a service worker if the compilation has errors.
26+
// When there are errors some files will not be emitted which would cause other errors down the line such as readdir failures.
27+
return;
28+
}
29+
2430
const { projectRoot, root, baseHref = '', ngswConfigPath } = this.options;
2531
// We use the output path from the compilation instead of build options since during
2632
// localization the output path is modified to a temp directory.
@@ -31,17 +37,25 @@ export class ServiceWorkerPlugin {
3137
throw new Error('Compilation output path cannot be empty.');
3238
}
3339

34-
await augmentAppWithServiceWorker(
35-
projectRoot,
36-
root,
37-
outputPath,
38-
baseHref,
39-
ngswConfigPath,
40-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41-
(compiler.inputFileSystem as any).promises,
42-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
43-
(compiler.outputFileSystem as any).promises,
44-
);
40+
try {
41+
await augmentAppWithServiceWorker(
42+
projectRoot,
43+
root,
44+
outputPath,
45+
baseHref,
46+
ngswConfigPath,
47+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
48+
(compiler.inputFileSystem as any).promises,
49+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
50+
(compiler.outputFileSystem as any).promises,
51+
);
52+
} catch (error) {
53+
compilation.errors.push(
54+
new WebpackError(
55+
`Failed to generate service worker - ${error instanceof Error ? error.message : error}`,
56+
),
57+
);
58+
}
4559
});
4660
}
4761
}

0 commit comments

Comments
 (0)