diff --git a/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts b/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts index 7891bac64eb3..952d5f392c2e 100644 --- a/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts +++ b/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts @@ -114,7 +114,6 @@ async function _renderUniversal( if (browserOptions.serviceWorker) { await augmentAppWithServiceWorker( - normalize(root), projectRoot, normalize(outputPath), browserOptions.baseHref || '/', diff --git a/packages/angular_devkit/build_angular/src/builders/browser/index.ts b/packages/angular_devkit/build_angular/src/builders/browser/index.ts index 93df213d6f25..bfa288eb0825 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/index.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/index.ts @@ -352,7 +352,6 @@ export function buildWebpackBrowser( for (const [locale, outputPath] of outputPaths.entries()) { try { await augmentAppWithServiceWorker( - root, normalize(projectRoot), normalize(outputPath), getLocaleBaseHref(i18n, locale) || options.baseHref || '/', diff --git a/packages/angular_devkit/build_angular/src/utils/service-worker.ts b/packages/angular_devkit/build_angular/src/utils/service-worker.ts index b75c2a83e99c..fef7d9d45adb 100644 --- a/packages/angular_devkit/build_angular/src/utils/service-worker.ts +++ b/packages/angular_devkit/build_angular/src/utils/service-worker.ts @@ -12,7 +12,6 @@ import * as crypto from 'crypto'; import { createReadStream, promises as fs, constants as fsConstants } from 'fs'; import * as path from 'path'; import { pipeline } from 'stream'; -import { pathToFileURL } from 'url'; import { loadEsmModule } from './load-esm'; class CliFilesystem implements Filesystem { @@ -63,34 +62,17 @@ class CliFilesystem implements Filesystem { } export async function augmentAppWithServiceWorker( - projectRoot: Path, appRoot: Path, outputPath: Path, baseHref: string, ngswConfigPath?: string, ): Promise { const distPath = getSystemPath(normalize(outputPath)); - const systemProjectRoot = getSystemPath(projectRoot); - - // Find the service worker package - const workerPath = require.resolve('@angular/service-worker/ngsw-worker.js', { - paths: [systemProjectRoot], - }); - // Absolute paths on Windows must be `file://` URLs when using ESM. Otherwise, - // `c:` would be interpreted as a protocol instead of a drive letter. - const swConfigPath = pathToFileURL( - require.resolve('@angular/service-worker/config', { - paths: [systemProjectRoot], - }), - ); // Determine the configuration file path - let configPath; - if (ngswConfigPath) { - configPath = getSystemPath(normalize(ngswConfigPath)); - } else { - configPath = path.join(getSystemPath(appRoot), 'ngsw-config.json'); - } + const configPath = ngswConfigPath + ? getSystemPath(normalize(ngswConfigPath)) + : path.join(getSystemPath(appRoot), 'ngsw-config.json'); // Read the configuration file let config: Config | undefined; @@ -113,7 +95,9 @@ export async function augmentAppWithServiceWorker( // Once TypeScript provides support for keeping the dynamic import this workaround can be // changed to a direct dynamic import. const GeneratorConstructor = ( - await loadEsmModule(swConfigPath) + await loadEsmModule( + '@angular/service-worker/config', + ) ).Generator; // Generate the manifest @@ -124,6 +108,9 @@ export async function augmentAppWithServiceWorker( const manifest = JSON.stringify(output, null, 2); await fs.writeFile(path.join(distPath, 'ngsw.json'), manifest); + // Find the service worker package + const workerPath = require.resolve('@angular/service-worker/ngsw-worker.js'); + // Write the worker code await fs.copyFile( workerPath,