Skip to content

Commit 80c7480

Browse files
authored
fix(@angular/build): align lazy route entry names with workspace root for SSR modulepreload
Override `compilerOptions.basePath` with `build.initialOptions.absWorkingDir` when initializing the Angular compilation so `lazyRoutesTransformer` computes `ɵentryName` relative to the workspace root rather than the tsconfig directory. This matches the workspace-relative keys in `entryPointToBrowserMapping` used for SSR `modulepreload` link generation when the application's `tsconfig` is not located at the workspace root.
1 parent 7d9ba62 commit 80c7480

4 files changed

Lines changed: 21 additions & 7 deletions

File tree

‎packages/angular/build/src/builders/application/tests/behavior/chunk-optimization-server_spec.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,19 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
151151
.not.toBeNull();
152152

153153
const mapping = JSON.parse(mappingSource![1]) as Record<string, string[]>;
154+
const mainServerContent = harness.readFile('dist/server/main.server.mjs');
154155

155-
// Every lazy route entry point must retain a mapping entry after optimization.
156+
// Every lazy route entry point must retain a mapping entry after optimization
157+
// that matches the ɵentryName emitted into the server bundle.
156158
for (const name of LAZY_ROUTE_NAMES) {
157-
const key = Object.keys(mapping).find((entryPoint) =>
158-
entryPoint.endsWith(`${name}.component.ts`),
159-
);
160-
expect(key)
161-
.withContext(`mapping entry for lazy route '${name}' should exist`)
159+
const expectedEntryPoint = `src/app/${name}.component.ts`;
160+
expect(mapping[expectedEntryPoint])
161+
.withContext(`mapping entry for '${expectedEntryPoint}' should exist`)
162162
.toBeDefined();
163+
164+
expect(mainServerContent)
165+
.withContext(`main.server.mjs should contain ɵentryName '${expectedEntryPoint}'`)
166+
.toContain(expectedEntryPoint);
163167
}
164168

165169
// Every browser file referenced by the mapping must exist on disk.

‎packages/angular/build/src/tools/angular/compilation/angular-compilation_spec.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,23 @@ describe('AngularCompilation', () => {
326326
it('applies override options correctly', () => {
327327
const { compilerOptions } = transformCompilerOptions(
328328
ts,
329-
{ target: ts.ScriptTarget.ES2022, isolatedModules: true },
329+
{
330+
target: ts.ScriptTarget.ES2022,
331+
isolatedModules: true,
332+
basePath: '/workspace/projects/app',
333+
},
330334
{
331335
sourcemap: true,
332336
preserveSymlinks: true,
333337
externalRuntimeStyles: true,
334338
enableHmr: true,
335339
instrumentForCoverage: true,
336340
includeTestMetadata: true,
341+
basePath: '/workspace',
337342
},
338343
);
339344

345+
expect(compilerOptions.basePath).toBe('/workspace');
340346
expect(compilerOptions.inlineSources).toBe(true);
341347
expect(compilerOptions.inlineSourceMap).toBe(true);
342348
expect(compilerOptions.preserveSymlinks).toBe(true);

‎packages/angular/build/src/tools/angular/compilation/compiler-options.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type * as ng from '@angular/compiler-cli';
1010
import type { PartialMessage } from 'esbuild';
1111
import * as path from 'node:path';
1212
import type ts from 'typescript';
13+
import { toPosixPath } from '../../../utils/path';
1314

1415
export interface CompilerOptionOverrides {
1516
sourcemap?: boolean;
@@ -21,6 +22,7 @@ export interface CompilerOptionOverrides {
2122
includeTestMetadata?: boolean;
2223
customConditions?: string[];
2324
rootFiles?: string[];
25+
basePath?: string;
2426
}
2527

2628
export function transformCompilerOptions(
@@ -113,6 +115,7 @@ export function transformCompilerOptions(
113115
return {
114116
compilerOptions: {
115117
...compilerOptions,
118+
basePath: overrides?.basePath ? toPosixPath(overrides.basePath) : compilerOptions.basePath,
116119
noEmitOnError: false,
117120
composite: false,
118121
inlineSources: !!overrides?.sourcemap,

‎packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ export function createCompilerPlugin(
331331
includeTestMetadata: !!pluginOptions.includeTestMetadata,
332332
customConditions: build.initialOptions.conditions,
333333
rootFiles: pluginOptions.rootFiles,
334+
basePath: build.initialOptions.absWorkingDir,
334335
},
335336
);
336337

0 commit comments

Comments
 (0)