Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): several windows fixes to applicat…
Browse files Browse the repository at this point in the history
…ion builder prerendering

This commit fixes several Windows issues in the prerendering pipeline. Primarily due to path normalization and other Windows only constraints.

(cherry picked from commit 48963fc)
  • Loading branch information
alan-agius4 committed Sep 14, 2023
1 parent 39643be commit d8d116b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

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

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

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

if (source === undefined) {
source = TRANSFORMED_FILES[filePath] = Buffer.from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import { OutputFile } from 'esbuild';
import { readFile } from 'node:fs/promises';
import { extname, posix } from 'node:path';
import { extname, join, posix } from 'node:path';
import { pathToFileURL } from 'node:url';
import Piscina from 'piscina';
import type { RenderResult, ServerContext } from './render-page';
import type { WorkerData } from './render-worker';
Expand Down Expand Up @@ -61,7 +62,7 @@ export async function prerenderPages(
execArgv: [
'--no-warnings', // Suppress `ExperimentalWarning: Custom ESM Loaders is an experimental feature...`.
'--loader',
require.resolve('./esm-in-memory-file-loader.js'),
pathToFileURL(join(__dirname, 'esm-in-memory-file-loader.js')).href, // Loader cannot be an absolute path on Windows.
],
});

Expand Down

0 comments on commit d8d116b

Please sign in to comment.