Skip to content

ng serve resolves prebundled packages without the build conditions #34834

Backlog

Description

@damienwebdev

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

  1. ng new repro-app --ssr --defaults

  2. 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;

  3. 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;
    }
  4. In angular.json, set "conditions": ["my-condition"] in the build target's options and in its development configuration.

  5. ng build --configuration development: the browser and server bundles contain RESOLVED_CUSTOM.

  6. ng serve, then curl http://localhost:4200/: the page renders RESOLVED_DEFAULT. The client imports .angular/cache/<version>/<project>/dev-server/vite/deps/cond-pkg.js, which also contains RESOLVED_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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions