Skip to content

Commit 2985067

Browse files
committedJan 16, 2025·
fix(@angular/build): resolve HMR-prefixed files in SSR with Vite
Improved handling of Angular prefixed files when using Vite dev-server with SSR. Updated logic ensures proper resolution of `file://` files paths with query strings. Closes #29364 (cherry picked from commit 48385bd)
1 parent f78c140 commit 2985067

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed
 

‎packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import assert from 'node:assert';
1010
import { readFile } from 'node:fs/promises';
11-
import { basename, dirname, join, relative } from 'node:path';
11+
import { basename, dirname, join, parse, relative } from 'node:path';
12+
import { fileURLToPath } from 'node:url';
1213
import type { Plugin } from 'vite';
1314
import { loadEsmModule } from '../../../utils/load-esm';
1415
import { AngularMemoryOutputFiles } from '../utils';
@@ -23,6 +24,7 @@ interface AngularMemoryPluginOptions {
2324

2425
const ANGULAR_PREFIX = '/@ng/';
2526
const VITE_FS_PREFIX = '/@fs/';
27+
const FILE_PROTOCOL = 'file:';
2628

2729
export async function createAngularMemoryPlugin(
2830
options: AngularMemoryPluginOptions,
@@ -40,8 +42,18 @@ export async function createAngularMemoryPlugin(
4042
}
4143

4244
// For SSR with component HMR, pass through as a virtual module
43-
if (ssr && source.startsWith(ANGULAR_PREFIX)) {
44-
return '\0' + source;
45+
if (ssr && source.startsWith(FILE_PROTOCOL) && source.includes(ANGULAR_PREFIX)) {
46+
// Vite will resolve these these files example:
47+
// `file:///@ng/component?c=src%2Fapp%2Fapp.component.ts%40AppComponent&t=1737017253850`
48+
const sourcePath = fileURLToPath(source);
49+
const { root } = parse(sourcePath);
50+
const sourceWithoutRoot = normalizePath('/' + sourcePath.slice(root.length));
51+
52+
if (sourceWithoutRoot.startsWith(ANGULAR_PREFIX)) {
53+
const [, query] = source.split('?', 2);
54+
55+
return `\0${sourceWithoutRoot}?${query}`;
56+
}
4557
}
4658

4759
// Prevent vite from resolving an explicit external dependency (`externalDependencies` option)

0 commit comments

Comments
 (0)
Please sign in to comment.