Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): clean up internal Angular state d…
Browse files Browse the repository at this point in the history
…uring rendering SSR

This commit clean ups the compiled components state when the build is being executed in watch mode. This is required as otherwise during development `Component ID generation collision detected` are displayed on the server.

Closes: #25924
  • Loading branch information
alan-agius4 committed Sep 29, 2023
1 parent 0368b23 commit 2d2e799
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ export function createServerCodeBundleOptions(
target: string[],
sourceFileCache: SourceFileCache,
): BuildOptions {
const { jit, serverEntryPoint, workspaceRoot, ssrOptions } = options;
const {
jit,
serverEntryPoint,
workspaceRoot,
ssrOptions,
watch,
externalPackages,
prerenderOptions,
} = options;

assert(
serverEntryPoint,
Expand Down Expand Up @@ -194,7 +202,7 @@ export function createServerCodeBundleOptions(
};

buildOptions.plugins ??= [];
if (options.externalPackages) {
if (externalPackages) {
buildOptions.packages = 'external';
} else {
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
Expand Down Expand Up @@ -225,7 +233,11 @@ export function createServerCodeBundleOptions(
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
];

if (options.prerenderOptions?.discoverRoutes) {
if (watch) {
contents.push(`export { ɵresetCompiledComponents } from '@angular/core';`);
}

if (prerenderOptions?.discoverRoutes) {
// We do not import it directly so that node.js modules are resolved using the correct context.
const routesExtractorCode = await readFile(
join(__dirname, '../../utils/routes-extractor/extractor.js'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ export interface MainServerBundleExports {

/** Method to extract routes from the router config. */
extractRoutes: typeof extractRoutes;

ɵresetCompiledComponents?: () => void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ export async function renderPage({
ɵSERVER_CONTEXT,
renderModule,
renderApplication,
ɵresetCompiledComponents,
} = await loadBundle('./main.server.mjs');

// Need to clean up GENERATED_COMP_IDS map in `@angular/core`.
// Otherwise an incorrect component ID generation collision detected warning will be displayed in development.
// See: https://github.com/angular/angular-cli/issues/25924
ɵresetCompiledComponents?.();

const platformProviders: StaticProvider[] = [
{
provide: ɵSERVER_CONTEXT,
Expand Down

0 comments on commit 2d2e799

Please sign in to comment.