Skip to content

Commit 7cb5d35

Browse files
committed
fix(@angular-devkit/build-angular): disable dependency optimization for SSR
It appears that Vite currently, has a number of limitation/bugs when using `optimizeDeps` for SSR bundles. Currently this causes a number of issues: - Deps are re-optimized everytime the server is started. - Added deps after a rebuild are not optimized. - Breaks RxJs (Unless it is added as external). See: #26235 We should follow up with Vite and try to get this solved as this would be a nice feature to use. Closes #26235 and #26234
1 parent ef2437f commit 7cb5d35

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/angular_devkit/build_angular/src/builders/application/execute-build.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,16 @@ export async function executeBuild(
125125
new BundlerContext(
126126
workspaceRoot,
127127
!!options.watch,
128-
createServerCodeBundleOptions(options, nodeTargets, codeBundleCache),
128+
createServerCodeBundleOptions(
129+
{
130+
...options,
131+
// Disable external deps for server bundles.
132+
// This is because it breaks Vite 'optimizeDeps' for SSR.
133+
externalPackages: false,
134+
},
135+
nodeTargets,
136+
codeBundleCache,
137+
),
129138
() => false,
130139
),
131140
);

packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export async function* serveWithVite(
224224
server = await createServer(serverConfiguration);
225225
await server.listen();
226226

227-
if (browserOptions.ssr) {
227+
if (serverConfiguration.ssr?.optimizeDeps?.disabled === false) {
228228
/**
229229
* Vite will only start dependency optimization of SSR modules when the first request comes in.
230230
* In some cases, this causes a long waiting time. To mitigate this, we call `ssrLoadModule` to
@@ -451,8 +451,19 @@ export async function setupServer(
451451
// Exclude any Node.js built in module and provided dependencies (currently build defined externals)
452452
external: serverExplicitExternal,
453453
optimizeDeps: getDepOptimizationConfig({
454+
/**
455+
* *********************************************
456+
* NOTE: Temporary disable 'optimizeDeps' for SSR.
457+
* *********************************************
458+
*
459+
* Currently this causes a number of issues.
460+
* - Deps are re-optimized everytime the server is started.
461+
* - Added deps after a rebuild are not optimized.
462+
* - Breaks RxJs (Unless it is added as external). See: https://github.com/angular/angular-cli/issues/26235
463+
*/
464+
454465
// Only enable with caching since it causes prebundle dependencies to be cached
455-
disabled: !serverOptions.cacheOptions.enabled,
466+
disabled: true, // !serverOptions.cacheOptions.enabled,
456467
// Exclude any explicitly defined dependencies (currently build defined externals and node.js built-ins)
457468
exclude: serverExplicitExternal,
458469
// Include all implict dependencies from the external packages internal option

0 commit comments

Comments
 (0)