Skip to content

Commit 13cceab

Browse files
committed
fix(@angular-devkit/build-angular): use URLs for absolute import paths with ESM
Absolute import paths, especially on Windows, must be `file://` URLs when using ESM. Otherwise, Windows drive letters (e.g., `C:`) would be interpreted as a protocol instead of a drive letter when performing the import.
1 parent 18fad63 commit 13cceab

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/angular_devkit/build_angular/src/utils/load-esm.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import { URL } from 'url';
10+
911
/**
1012
* This uses a dynamic import to load a module which may be ESM.
1113
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
@@ -18,7 +20,7 @@
1820
* @param modulePath The path of the module to load.
1921
* @returns A Promise that resolves to the dynamically imported module.
2022
*/
21-
export async function loadEsmModule<T>(modulePath: string): Promise<T> {
23+
export async function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
2224
try {
2325
return (await new Function('modulePath', `return import(modulePath);`)(modulePath)) as T;
2426
} catch (e) {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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';
1516
import { loadEsmModule } from './load-esm';
1617

1718
class CliFilesystem implements Filesystem {
@@ -75,9 +76,13 @@ export async function augmentAppWithServiceWorker(
7576
const workerPath = require.resolve('@angular/service-worker/ngsw-worker.js', {
7677
paths: [systemProjectRoot],
7778
});
78-
const swConfigPath = require.resolve('@angular/service-worker/config', {
79-
paths: [systemProjectRoot],
80-
});
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+
);
8186

8287
// Determine the configuration file path
8388
let configPath;

0 commit comments

Comments
 (0)