Skip to content

fix(@angular-devkit/build-angular): change service worker errors to compilation errors #23858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ export class ServiceWorkerPlugin {
constructor(private readonly options: ServiceWorkerPluginOptions) {}

apply(compiler: Compiler) {
compiler.hooks.done.tapPromise('angular-service-worker', async ({ compilation }) => {
compiler.hooks.done.tapPromise('angular-service-worker', async (stats) => {
if (stats.hasErrors()) {
// Don't generate a service worker if the compilation has errors.
// When there are errors some files will not be emitted which would cause other errors down the line such as readdir failures.
return;
}

const { projectRoot, root, baseHref = '', ngswConfigPath } = this.options;
const { compilation } = stats;
// We use the output path from the compilation instead of build options since during
// localization the output path is modified to a temp directory.
// See: https://github.com/angular/angular-cli/blob/7e64b1537d54fadb650559214fbb12707324cd75/packages/angular_devkit/build_angular/src/utils/i18n-options.ts#L251-L252
Expand All @@ -31,17 +38,25 @@ export class ServiceWorkerPlugin {
throw new Error('Compilation output path cannot be empty.');
}

await augmentAppWithServiceWorker(
projectRoot,
root,
outputPath,
baseHref,
ngswConfigPath,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(compiler.inputFileSystem as any).promises,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(compiler.outputFileSystem as any).promises,
);
try {
await augmentAppWithServiceWorker(
projectRoot,
root,
outputPath,
baseHref,
ngswConfigPath,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(compiler.inputFileSystem as any).promises,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(compiler.outputFileSystem as any).promises,
);
} catch (error) {
compilation.errors.push(
new compilation.compiler.webpack.WebpackError(
`Failed to generate service worker - ${error instanceof Error ? error.message : error}`,
),
);
}
});
}
}