Skip to content

Commit

Permalink
refactor(@angular-devkit/build-angular): remove redundant `require.re…
Browse files Browse the repository at this point in the history
…solve` for `@angular/service-worker`

This was mainly done prior to the introduction of optional peer dependencies.

(cherry picked from commit 9a3b637)
  • Loading branch information
alan-agius4 committed Oct 7, 2021
1 parent 4ee9c9a commit 98a6493
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
Expand Up @@ -114,7 +114,6 @@ async function _renderUniversal(

if (browserOptions.serviceWorker) {
await augmentAppWithServiceWorker(
normalize(root),
projectRoot,
normalize(outputPath),
browserOptions.baseHref || '/',
Expand Down
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
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

0 comments on commit 98a6493

Please sign in to comment.