Repository navigation
Description
Command
serve
Is this a regression?
- Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
Description
The conditions option of @angular/build:application is applied to ng build, but not to packages that ng serve prebundles. With prebundling enabled (the default), the dev server builds the app with external packages and Vite's dependency optimizer resolves them. getDepOptimizationConfig (packages/angular/build/src/tools/vite/utils.ts) passes no conditions to the optimizer, and the Vite resolve config in dev-server/vite/server.ts only sets mainFields. Prebundled packages resolve their default exports, so ng serve runs different code than ng build. This affects both the client bundle and SSR.
Setting prebundle: false or adding the package to prebundle.exclude works around it, because the package is then bundled by esbuild with the build conditions.
Minimal Reproduction
Repository: https://github.com/damienwebdev/angular-cli-conditions-repro/tree/serve-prebundle
-
ng new repro-app --ssr --defaults -
Add a local package with a conditional export, for example
node_modules/cond-pkg:package.json{ "name": "cond-pkg", "version": "1.0.0", "type": "module", "exports": { ".": { "my-condition": { "types": "./custom.d.ts", "default": "./custom.mjs" }, "types": "./default.d.ts", "default": "./default.mjs" } } }custom.mjs:export const VALUE = 'RESOLVED_CUSTOM';
default.mjs:export const VALUE = 'RESOLVED_DEFAULT';
custom.d.ts:export declare const VALUE: string; export declare const customOnly: string;
default.d.ts:export declare const VALUE: string; -
Use it in
src/app/app.ts:import { Component } from '@angular/core'; import { VALUE } from 'cond-pkg'; @Component({ selector: 'app-root', template: '<p id="value">{{ value }}</p>', }) export class App { protected readonly value = VALUE; }
-
In
angular.json, set"conditions": ["my-condition"]in the build target'soptionsand in itsdevelopmentconfiguration. -
ng build --configuration development: the browser and server bundles containRESOLVED_CUSTOM. -
ng serve, thencurl http://localhost:4200/: the page rendersRESOLVED_DEFAULT. The client imports.angular/cache/<version>/<project>/dev-server/vite/deps/cond-pkg.js, which also containsRESOLVED_DEFAULT.
Expected: ng serve renders RESOLVED_CUSTOM, the same as ng build.
Exception or Error
Your Environment
Angular CLI : 22.3.0
Angular : 22.3.2
Node.js : 24.19.0
Package Manager : npm 11.17.0
Operating System : linux x64
@angular/build 22.3.0
@angular/cli 22.3.0
@angular/core 22.3.2
@angular/ssr 22.3.0
typescript 6.0.3
vitest 5.1.2
Anything else relevant?
The same code is on main (637f4b6).
Activity