<commit_before>/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import type { Compiler } from 'webpack';
import { augmentAppWithServiceWorker } from '../../utils/service-worker';

export interface ServiceWorkerPluginOptions {
  projectRoot: string;
  root: string;
  baseHref?: string;
  ngswConfigPath?: string;
}

export class ServiceWorkerPlugin {
  constructor(private readonly options: ServiceWorkerPluginOptions) {}

  apply(compiler: Compiler) {
    compiler.hooks.done.tapPromise('angular-service-worker', async ({ compilation }) => {
      const { projectRoot, root, baseHref = '', ngswConfigPath } = this.options;
      // 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
      const outputPath = compilation.outputOptions.path;

      if (!outputPath) {
        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,
      );
    });
  }
}
<commit_msg>fix(@angular-devkit/build-angular): change service worker errors to compilation errors<commit_after>/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import type { Compiler } from 'webpack';
import { augmentAppWithServiceWorker } from '../../utils/service-worker';

export interface ServiceWorkerPluginOptions {
  projectRoot: string;
  root: string;
  baseHref?: string;
  ngswConfigPath?: string;
}

export class ServiceWorkerPlugin {
  constructor(private readonly options: ServiceWorkerPluginOptions) {}

  apply(compiler: Compiler) {
    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
      const outputPath = compilation.outputOptions.path;

      if (!outputPath) {
        throw new Error('Compilation output path cannot be empty.');
      }

      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}`,
          ),
        );
      }
    });
  }
}

{'fraction_non_alphanumeric': 0.0984134853743183, 'fraction_numerical': 0.017848289538919187, 'mean_word_length': 2.9558823529411766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3232083', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1099, 'n_words': 309}