Skip to content
Source
Sign inSign up
TanStack/query

angular-query-experimental: pending-tasks-compat namespace import (`import * as ng`) defeats tree-shaking of @angular/core (+~150 kB raw / +42 kB gzip in the initial bundle) #11614

Closeddinnes-dev02(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 plus Reflect.get:

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

A namespace object accessed dynamically cannot be tree-shaken, so esbuild (Angular CLI @angular/build:application) has to keep all of @angular/core in the bundle as soon as any injectQuery / injectInfiniteQuery / injectMutation is used (they all import PENDING_TASKS through create-base-query / inject-mutation).

On an Angular 22 app, measured with ng build --configuration production --stats-json and comparing the esbuild metafile before/after the first injectQuery usage:

VariantInitial bundle (raw)Estimated transfer
App without any query485.6 kB125.9 kB
injectQuery used, shim as published649.1 kB171.2 kB
Same, with pending-tasks-compat.mjs replaced by a static import { PendingTasks } from '@angular/core'497.8 kB129.2 kB

The metafile shows the growth is almost entirely @angular/core/fesm2022/_debug_node-chunk.mjs (142 → 268 kB in output) and core.mjs (10 → 63 kB), i.e. Angular internals that the app never references. @tanstack/query-core + the adapter themselves only add ~12 kB raw / ~3 kB gzip.

Your minimal, reproducible example

  1. npx @angular/cli@22 new repro --style=css --ssr=false
  2. npm i @tanstack/angular-query-experimental@5.103.2
  3. Add provideTanStackQuery(new QueryClient()) to app.config.ts and one injectQuery(() => ({ queryKey: ['x'], queryFn: () => Promise.resolve(1) })) to App.
  4. ng build --stats-json, then inspect dist/repro/stats.json (outputs[…].inputs for @angular/core/fesm2022/*), or just compare the "Initial total" with and without step 3.
  5. Edit node_modules/@tanstack/angular-query-experimental/pending-tasks-compat.mjs to import { InjectionToken, PendingTasks, inject } from "@angular/core" + inject(PendingTasks, { optional: true }), rebuild: the ~150 kB are gone.

Steps to reproduce

See above.

Expected behavior

Using the adapter should cost roughly query-core + adapter size (~3 kB gzip), not the whole of @angular/core.

Possible fixes, in order of preference:

  • Since the peer dependency is @angular/core >= 16 only for the experimental package and the upcoming @tanstack/angular-query already requires >= 20.1 (and uses utils/inject-pending-tasks-lifecycle with a static import), the experimental package could do the same static import guarded by VERSION.major >= 19 (like create-base-query already does with VERSION.major < '19'), keeping the no-op fallback for older versions.
  • Or keep the dynamic lookup but without a namespace import, e.g. import { PendingTasks } from '@angular/core' inside a try/optional chain — any pattern that lets the bundler resolve the binding statically.

Happy to open a PR with the static import + version guard if you agree with the direction.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Angular 22.1.6, @angular/build 22.1.8 (esbuild-based application builder), Node 24, Linux.

Tanstack Query adapter

angular-query

TanStack Query version

@tanstack/angular-query-experimental 5.103.2 (query-core 5.103.2)

TypeScript version

6.0.2

Additional context

Workaround we use meanwhile: a postinstall script that replaces pending-tasks-compat.mjs with the static-import version after checking the file's sha256, so a package update fails the install instead of silently re-introducing the cost.

  1. Closing on our side for now; we are handling this with a local postinstall patch. Feel free to reopen if you want to track it here.

Sign in to join this conversation.