Skip to content

Commit 45c95e1

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. (cherry picked from commit 3b1f109)
1 parent ecc014d commit 45c95e1

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

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

+27-12
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ export class ServiceWorkerPlugin {
2020
constructor(private readonly options: ServiceWorkerPluginOptions) {}
2121

2222
apply(compiler: Compiler) {
23-
compiler.hooks.done.tapPromise('angular-service-worker', async ({ compilation }) => {
23+
compiler.hooks.done.tapPromise('angular-service-worker', async (stats) => {
24+
if (stats.hasErrors()) {
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;
31+
const { compilation } = stats;
2532
// We use the output path from the compilation instead of build options since during
2633
// localization the output path is modified to a temp directory.
2734
// See: https://github.com/angular/angular-cli/blob/7e64b1537d54fadb650559214fbb12707324cd75/packages/angular_devkit/build_angular/src/utils/i18n-options.ts#L251-L252
@@ -31,17 +38,25 @@ export class ServiceWorkerPlugin {
3138
throw new Error('Compilation output path cannot be empty.');
3239
}
3340

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-
);
41+
try {
42+
await augmentAppWithServiceWorker(
43+
projectRoot,
44+
root,
45+
outputPath,
46+
baseHref,
47+
ngswConfigPath,
48+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
49+
(compiler.inputFileSystem as any).promises,
50+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51+
(compiler.outputFileSystem as any).promises,
52+
);
53+
} catch (error) {
54+
compilation.errors.push(
55+
new compilation.compiler.webpack.WebpackError(
56+
`Failed to generate service worker - ${error instanceof Error ? error.message : error}`,
57+
),
58+
);
59+
}
4560
});
4661
}
4762
}

0 commit comments

Comments
 (0)