Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): use URLs for absolute import path…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
clydin committed Sep 23, 2021
1 parent 18fad63 commit 13cceab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/angular_devkit/build_angular/src/utils/load-esm.ts
Expand Up @@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import { URL } from 'url';

/**
* This uses a dynamic import to load a module which may be ESM.
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
Expand All @@ -18,7 +20,7 @@
* @param modulePath The path of the module to load.
* @returns A Promise that resolves to the dynamically imported module.
*/
export async function loadEsmModule<T>(modulePath: string): Promise<T> {
export async function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
try {
return (await new Function('modulePath', `return import(modulePath);`)(modulePath)) as T;
} catch (e) {
Expand Down
Expand Up @@ -12,6 +12,7 @@ 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 @@ -75,9 +76,13 @@ export async function augmentAppWithServiceWorker(
const workerPath = require.resolve('@angular/service-worker/ngsw-worker.js', {
paths: [systemProjectRoot],
});
const swConfigPath = require.resolve('@angular/service-worker/config', {
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;
Expand Down

0 comments on commit 13cceab

Please sign in to comment.