Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/build-angular): use URLs for absolute import paths with ESM #21804

Merged
merged 1 commit into from
Sep 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/angular_devkit/build_angular/src/utils/load-esm.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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