Skip to content

Commit 2cbb5a3

Browse files
committed
fix(@angular/build): handle relative @ng/components
This update serves as a preparatory step to address #29248. The change involves modifying the line 'urlPartial' in [r3_hmr_compiler.ts](https://github.com/angular/angular/blob/4e6017a9f5cda389c5fbf4f2c1519ce1bba23e11/packages/compiler/src/render3/r3_hmr_compiler.ts#L57) to start with `./` instead of `/`.
1 parent d3648f2 commit 2cbb5a3

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

packages/angular/build/src/tools/vite/middlewares/component-middleware.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import type { Connect } from 'vite';
9+
import type { Connect, ViteDevServer } from 'vite';
10+
import { pathnameWithoutBasePath } from '../utils';
1011

1112
const ANGULAR_COMPONENT_PREFIX = '/@ng/component';
1213

1314
export function createAngularComponentMiddleware(
15+
server: ViteDevServer,
1416
templateUpdates: ReadonlyMap<string, string>,
1517
): Connect.NextHandleFunction {
1618
return function angularComponentMiddleware(req, res, next) {
1719
if (req.url === undefined || res.writableEnded) {
1820
return;
1921
}
2022

21-
if (!req.url.startsWith(ANGULAR_COMPONENT_PREFIX)) {
23+
const pathname = pathnameWithoutBasePath(req.url, server.config.base);
24+
if (!pathname.includes(ANGULAR_COMPONENT_PREFIX)) {
2225
next();
2326

2427
return;

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ export async function createAngularMemoryPlugin(
4646
// Vite will resolve these these files example:
4747
// `file:///@ng/component?c=src%2Fapp%2Fapp.component.ts%40AppComponent&t=1737017253850`
4848
const sourcePath = fileURLToPath(source);
49-
const { root } = parse(sourcePath);
50-
const sourceWithoutRoot = normalizePath('/' + sourcePath.slice(root.length));
49+
const sourceWithoutRoot = sourcePath.startsWith(virtualProjectRoot)
50+
? normalizePath('/' + relative(virtualProjectRoot, sourcePath))
51+
: // TODO: remove once https://github.com/angular/angular/blob/4e6017a9f5cda389c5fbf4f2c1519ce1bba23e11/packages/compiler/src/render3/r3_hmr_compiler.ts#L57
52+
// is changed from `/@ng` to `./@ng/`
53+
normalizePath('/' + sourcePath.slice(parse(sourcePath).root.length));
5154

5255
if (sourceWithoutRoot.startsWith(ANGULAR_PREFIX)) {
5356
const [, query] = source.split('?', 2);

packages/angular/build/src/tools/vite/plugins/setup-middlewares-plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function createAngularSetupMiddlewaresPlugin(
8888

8989
// Headers, assets and resources get handled first
9090
server.middlewares.use(createAngularHeadersMiddleware(server));
91-
server.middlewares.use(createAngularComponentMiddleware(templateUpdates));
91+
server.middlewares.use(createAngularComponentMiddleware(server, templateUpdates));
9292
server.middlewares.use(
9393
createAngularAssetsMiddleware(
9494
server,

0 commit comments

Comments
 (0)