Skip to content

Commit 48963fc

Browse files
committed
fix(@angular-devkit/build-angular): several windows fixes to application builder prerendering
This commit fixes several Windows issues in the prerendering pipeline. Primarily due to path normalization and other Windows only constraints.
1 parent eb5a075 commit 48963fc

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/angular_devkit/build_angular/src/utils/server-rendering/esm-in-memory-file-loader.ts

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

9-
import { join } from 'node:path';
9+
import { join, relative } from 'node:path';
10+
import { pathToFileURL } from 'node:url';
1011
import { workerData } from 'node:worker_threads';
1112
import { fileURLToPath } from 'url';
1213
import { JavaScriptTransformer } from '../../tools/esbuild/javascript-transformer';
@@ -23,7 +24,7 @@ const { outputFiles, workspaceRoot } = workerData as {
2324

2425
const TRANSFORMED_FILES: Record<string, string> = {};
2526
const CHUNKS_REGEXP = /file:\/\/\/(main\.server|chunk-\w+)\.mjs/;
26-
const WORKSPACE_ROOT_FILE = new URL(join(workspaceRoot, 'index.mjs'), 'file:').href;
27+
const WORKSPACE_ROOT_FILE = pathToFileURL(join(workspaceRoot, 'index.mjs')).href;
2728

2829
const JAVASCRIPT_TRANSFORMER = new JavaScriptTransformer(
2930
// Always enable JIT linking to support applications built with and without AOT.
@@ -44,7 +45,9 @@ export function resolve(
4445
return {
4546
format: 'module',
4647
shortCircuit: true,
47-
url: new URL(normalizedSpecifier, 'file:').href,
48+
// File URLs need to absolute. In Windows these also need to include the drive.
49+
// The `/` will be resolved to the drive letter.
50+
url: pathToFileURL('/' + normalizedSpecifier).href,
4851
};
4952
}
5053
}
@@ -60,8 +63,8 @@ export function resolve(
6063
export async function load(url: string, context: { format?: string | null }, nextLoad: Function) {
6164
if (isFileProtocol(url)) {
6265
const filePath = fileURLToPath(url);
63-
let source =
64-
outputFiles[filePath.slice(1)] /* Remove leading slash */ ?? TRANSFORMED_FILES[filePath];
66+
// Remove '/' or drive letter for Windows that was added in the above 'resolve'.
67+
let source = outputFiles[relative('/', filePath)] ?? TRANSFORMED_FILES[filePath];
6568

6669
if (source === undefined) {
6770
source = TRANSFORMED_FILES[filePath] = Buffer.from(

packages/angular_devkit/build_angular/src/utils/server-rendering/prerender.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import { OutputFile } from 'esbuild';
1010
import { readFile } from 'node:fs/promises';
11-
import { extname, posix } from 'node:path';
11+
import { extname, join, posix } from 'node:path';
12+
import { pathToFileURL } from 'node:url';
1213
import Piscina from 'piscina';
1314
import type { RenderResult, ServerContext } from './render-page';
1415
import type { WorkerData } from './render-worker';
@@ -73,7 +74,7 @@ export async function prerenderPages(
7374
execArgv: [
7475
'--no-warnings', // Suppress `ExperimentalWarning: Custom ESM Loaders is an experimental feature...`.
7576
'--loader',
76-
require.resolve('./esm-in-memory-file-loader.js'),
77+
pathToFileURL(join(__dirname, 'esm-in-memory-file-loader.js')).href, // Loader cannot be an absolute path on Windows.
7778
],
7879
});
7980

0 commit comments

Comments
 (0)