fix(@angular/build): handle external `@angular/` packages during SSR by alan-agius4 · Pull Request #29094 · angular/angular-cli · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions packages/angular/build/src/tools/esbuild/application-code-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { pathToFileURL } from 'node:url';
import { fileURLToPath } from 'url';
import { JavaScriptTransformer } from '../../../tools/esbuild/javascript-transformer';

/**
* @note For some unknown reason, setting `globalThis.ngServerMode = true` does not work when using ESM loader hooks.
*/
const NG_SERVER_MODE_INIT_BYTES = new TextEncoder().encode('var ngServerMode=true;');

/**
* Node.js ESM loader to redirect imports to in memory files.
* @see: https://nodejs.org/api/esm.html#loaders for more information about loaders.
Expand Down Expand Up @@ -133,7 +138,12 @@ export async function load(url: string, context: { format?: string | null }, nex
// need linking are ESM only.
if (format === 'module' && isFileProtocol(url)) {
const filePath = fileURLToPath(url);
const source = await javascriptTransformer.transformFile(filePath);
let source = await javascriptTransformer.transformFile(filePath);

if (filePath.includes('@angular/')) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the name does not include '@angular/'?

@alan-agius4 alan-agius4 Dec 9, 2024

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ngServerMode is not added. And the resolved module will be returned with the standard transformations.

// Prepend 'var ngServerMode=true;' to the source.
source = Buffer.concat([NG_SERVER_MODE_INIT_BYTES, source]);
}

return {
format,
Expand Down