Skip to content
Merged
Show file tree
Hide file tree
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 @@ -114,7 +114,6 @@ async function _renderUniversal(

if (browserOptions.serviceWorker) {
await augmentAppWithServiceWorker(
normalize(root),
projectRoot,
normalize(outputPath),
browserOptions.baseHref || '/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '/',
Expand Down
31 changes: 9 additions & 22 deletions packages/angular_devkit/build_angular/src/utils/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -63,34 +62,17 @@ class CliFilesystem implements Filesystem {
}

export async function augmentAppWithServiceWorker(
projectRoot: Path,
appRoot: Path,
outputPath: Path,
baseHref: string,
ngswConfigPath?: string,
): Promise<void> {
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;
Expand All @@ -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<typeof import('@angular/service-worker/config')>(swConfigPath)
await loadEsmModule<typeof import('@angular/service-worker/config')>(
'@angular/service-worker/config',
)
).Generator;

// Generate the manifest
Expand All @@ -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,
Expand Down