Skip to content

Commit 9a3b637

Browse files
committed
refactor(@angular-devkit/build-angular): remove redundant require.resolve for @angular/service-worker
This was mainly done prior to the introduction of optional peer dependencies.
1 parent 5dba8a2 commit 9a3b637

File tree

3 files changed

+9
-24
lines changed

3 files changed

+9
-24
lines changed

‎packages/angular_devkit/build_angular/src/builders/app-shell/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ async function _renderUniversal(
114114

115115
if (browserOptions.serviceWorker) {
116116
await augmentAppWithServiceWorker(
117-
normalize(root),
118117
projectRoot,
119118
normalize(outputPath),
120119
browserOptions.baseHref || '/',

‎packages/angular_devkit/build_angular/src/builders/browser/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ export function buildWebpackBrowser(
352352
for (const [locale, outputPath] of outputPaths.entries()) {
353353
try {
354354
await augmentAppWithServiceWorker(
355-
root,
356355
normalize(projectRoot),
357356
normalize(outputPath),
358357
getLocaleBaseHref(i18n, locale) || options.baseHref || '/',

‎packages/angular_devkit/build_angular/src/utils/service-worker.ts

+9-22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import * as crypto from 'crypto';
1212
import { createReadStream, promises as fs, constants as fsConstants } from 'fs';
1313
import * as path from 'path';
1414
import { pipeline } from 'stream';
15-
import { pathToFileURL } from 'url';
1615
import { loadEsmModule } from './load-esm';
1716

1817
class CliFilesystem implements Filesystem {
@@ -63,34 +62,17 @@ class CliFilesystem implements Filesystem {
6362
}
6463

6564
export async function augmentAppWithServiceWorker(
66-
projectRoot: Path,
6765
appRoot: Path,
6866
outputPath: Path,
6967
baseHref: string,
7068
ngswConfigPath?: string,
7169
): Promise<void> {
7270
const distPath = getSystemPath(normalize(outputPath));
73-
const systemProjectRoot = getSystemPath(projectRoot);
74-
75-
// Find the service worker package
76-
const workerPath = require.resolve('@angular/service-worker/ngsw-worker.js', {
77-
paths: [systemProjectRoot],
78-
});
79-
// Absolute paths on Windows must be `file://` URLs when using ESM. Otherwise,
80-
// `c:` would be interpreted as a protocol instead of a drive letter.
81-
const swConfigPath = pathToFileURL(
82-
require.resolve('@angular/service-worker/config', {
83-
paths: [systemProjectRoot],
84-
}),
85-
);
8671

8772
// Determine the configuration file path
88-
let configPath;
89-
if (ngswConfigPath) {
90-
configPath = getSystemPath(normalize(ngswConfigPath));
91-
} else {
92-
configPath = path.join(getSystemPath(appRoot), 'ngsw-config.json');
93-
}
73+
const configPath = ngswConfigPath
74+
? getSystemPath(normalize(ngswConfigPath))
75+
: path.join(getSystemPath(appRoot), 'ngsw-config.json');
9476

9577
// Read the configuration file
9678
let config: Config | undefined;
@@ -113,7 +95,9 @@ export async function augmentAppWithServiceWorker(
11395
// Once TypeScript provides support for keeping the dynamic import this workaround can be
11496
// changed to a direct dynamic import.
11597
const GeneratorConstructor = (
116-
await loadEsmModule<typeof import('@angular/service-worker/config')>(swConfigPath)
98+
await loadEsmModule<typeof import('@angular/service-worker/config')>(
99+
'@angular/service-worker/config',
100+
)
117101
).Generator;
118102

119103
// Generate the manifest
@@ -124,6 +108,9 @@ export async function augmentAppWithServiceWorker(
124108
const manifest = JSON.stringify(output, null, 2);
125109
await fs.writeFile(path.join(distPath, 'ngsw.json'), manifest);
126110

111+
// Find the service worker package
112+
const workerPath = require.resolve('@angular/service-worker/ngsw-worker.js');
113+
127114
// Write the worker code
128115
await fs.copyFile(
129116
workerPath,

0 commit comments

Comments
 (0)