Skip to content
Source
Sign inSign up
TanStack/query

angular-query-experimental: `import * as ng` in pending-tasks-compat pulls all of @angular/core into the initial bundle #12319

Closeddante0x95(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

packages/angular-query-experimental/src/pending-tasks-compat.ts reads PendingTasks through a namespace import:

import * as ng from '@angular/core'
// …
const token = Reflect.get(ng, 'PendingTasks')

Because the namespace object is accessed dynamically, esbuild (Angular's application builder) can't tree-shake @angular/core. create-base-query (injectQuery, injectInfiniteQuery) and inject-mutation import this module, so using them, even only in a lazy-loaded route, puts most of @angular/core into the initial chunk.

This was reported before in #11614. Its author closed it a few minutes later because they were working around it locally. It still reproduces on the latest release (5.104.5, same dist/pending-tasks-compat.mjs), so I'm opening a new issue to track it.

How to reproduce

Angular 22 app with the application builder (esbuild), production build:

  1. provideTanStackQuery(new QueryClient()) in app.config.ts.
  2. A lazy route (loadComponent) whose component calls injectQuery(...).
  3. ng build and compare the initial bundle with and without step 2.

Numbers (our admin app, @tanstack/angular-query-experimental 5.104.1, Angular 22.2)

main-*.jsInitial total
As published512.14 kB515.56 kB
pending-tasks-compat.mjs patched to a named import327.05 kB330.46 kB

Almost all of the difference is @angular/core (raw, from esbuild stats: 140 kB → 409 kB in main). The app code is ~4 kB in both builds.

Expected behavior

Using injectQuery/injectMutation shouldn't stop @angular/core from being tree-shaken.

Notes / possible directions

We know the namespace access is there because the peer range is @angular/core >=16 and PendingTasks only exists from v19, so a plain import { PendingTasks } would break builds on 16–18. Some options:

  • Drop the compat shim when the minimum supported Angular becomes 19 (named import, no Reflect.get).
  • Until then, keep the shim behind a separate entry point or build condition, so apps on ≥19 get the named import.

Workaround we use for now (Angular 22): a pnpm patch that replaces the namespace import with import { InjectionToken, PendingTasks, inject } from "@angular/core" and inject(PendingTasks, { optional: true }).

Versions

  • @tanstack/angular-query-experimental: 5.104.1 (also checked 5.104.5)
  • @angular/core: 22.2.3, application builder (esbuild)
  1. benjavicente(GitHub profile, opens in a new tab) added labelspackage: angular-query

  2. Hey! We are not doing fixes on @tanstack/angular-query-experimental since @tanstack/angular-query will be released soon. That new version does not have that issue.

    You can try it by installing the release candidate version, and follow the migration docs if needed:

    npm install @tanstack/angular-query@rc
    # Run the migration schematic
    npx ng generate @tanstack/angular-query:migrate-from-experimental --dry-run
  3. TkDodo closed this

Sign in to join this conversation.