Skip to content

Commit

Permalink
fix(@angular/build): handle loaders correctly in SSR bundles for exte…
Browse files Browse the repository at this point in the history
…rnal packages

This update ensures proper handling of loaders in SSR bundles when packages are marked as external dependencies.

Closes #29235

(cherry picked from commit 134f5fe)
  • Loading branch information
alan-agius4 committed Jan 9, 2025
1 parent ce7c4e2 commit 8aa1ce6
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions packages/angular/build/src/tools/esbuild/application-code-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,7 @@ export function createBrowserCodeBundleOptions(
buildOptions.plugins?.push(...options.plugins);
}

if (options.externalPackages) {
// Package files affected by a customized loader should not be implicitly marked as external
if (
options.loaderExtensions ||
options.plugins ||
typeof options.externalPackages === 'object'
) {
// Plugin must be added after custom plugins to ensure any added loader options are considered
buildOptions.plugins?.push(
createExternalPackagesPlugin(
options.externalPackages !== true ? options.externalPackages : undefined,
),
);
} else {
// Safe to use the packages external option directly
buildOptions.packages = 'external';
}
}
appendOptionsForExternalPackages(options, buildOptions);

return buildOptions;
};
Expand Down Expand Up @@ -302,9 +285,7 @@ export function createServerMainCodeBundleOptions(

buildOptions.plugins ??= [];

if (externalPackages) {
buildOptions.packages = 'external';
} else {
if (!externalPackages) {
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
}

Expand Down Expand Up @@ -381,6 +362,8 @@ export function createServerMainCodeBundleOptions(
buildOptions.plugins.push(...options.plugins);
}

appendOptionsForExternalPackages(options, buildOptions);

return buildOptions;
};
}
Expand Down Expand Up @@ -442,9 +425,7 @@ export function createSsrEntryCodeBundleOptions(

buildOptions.plugins ??= [];

if (externalPackages) {
buildOptions.packages = 'external';
} else {
if (!externalPackages) {
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
}

Expand Down Expand Up @@ -516,6 +497,8 @@ export function createSsrEntryCodeBundleOptions(
buildOptions.plugins.push(...options.plugins);
}

appendOptionsForExternalPackages(options, buildOptions);

return buildOptions;
};
}
Expand Down Expand Up @@ -721,3 +704,29 @@ function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string):
.replace(/\\/g, '/')
);
}

function appendOptionsForExternalPackages(
options: NormalizedApplicationBuildOptions,
buildOptions: BuildOptions,
): void {
if (!options.externalPackages) {
return;
}

buildOptions.plugins ??= [];

// Package files affected by a customized loader should not be implicitly marked as external
if (options.loaderExtensions || options.plugins || typeof options.externalPackages === 'object') {
// Plugin must be added after custom plugins to ensure any added loader options are considered
buildOptions.plugins.push(
createExternalPackagesPlugin(
options.externalPackages !== true ? options.externalPackages : undefined,
),
);

buildOptions.packages = undefined;
} else {
// Safe to use the packages external option directly
buildOptions.packages = 'external';
}
}

0 comments on commit 8aa1ce6

Please sign in to comment.