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
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:
| Variant | Initial bundle (raw) | Estimated transfer |
|---|---|---|
| App without any query | 485.6 kB | 125.9 kB |
injectQuery used, shim as published | 649.1 kB | 171.2 kB |
Same, with pending-tasks-compat.mjs replaced by a static import { PendingTasks } from '@angular/core' | 497.8 kB | 129.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#
npx @angular/cli@22 new repro --style=css --ssr=falsenpm i @tanstack/angular-query-experimental@5.103.2- Add
provideTanStackQuery(new QueryClient())toapp.config.tsand oneinjectQuery(() => ({ queryKey: ['x'], queryFn: () => Promise.resolve(1) }))toApp. ng build --stats-json, then inspectdist/repro/stats.json(outputs[…].inputsfor@angular/core/fesm2022/*), or just compare the "Initial total" with and without step 3.- Edit
node_modules/@tanstack/angular-query-experimental/pending-tasks-compat.mjstoimport { 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 >= 16only for the experimental package and the upcoming@tanstack/angular-queryalready requires>= 20.1(and usesutils/inject-pending-tasks-lifecyclewith a static import), the experimental package could do the same static import guarded byVERSION.major >= 19(likecreate-base-queryalready does withVERSION.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 atry/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.
dinnes-dev02(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
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.