Skip to content

fix: mark default client creation as pure for tree-shaking - #644

Open
Logic769 wants to merge 1 commit into
unjs:mainfrom
Logic769:fix/pure-default-client
Open

Logic769 wants to merge 1 commit into
unjs:mainfrom
Logic769:fix/pure-default-client

Conversation

@Logic769

@Logic769 Logic769 commented Oct 29, 2026 •

Copy link
Copy Markdown

Summary

When a consumer imports only an export that has no runtime dependency on the default client — e.g. import { FetchError } from "ofetch" — the default createFetch({ fetch }) instance is still evaluated and retained by bundlers, because the call is not marked side-effect free. This inflates the bundle for the common "only use FetchError" case.

This marks the default client creation with /* @__PURE__ */ so bundlers (esbuild / rollup / webpack) can drop it via tree-shaking when the default ofetch/$fetch export is unused.

Fixes #642.

Change

-export const ofetch: $Fetch = createFetch({ fetch });
+// Mark the default client creation as pure so bundlers can drop it via
+// tree-shaking when only other exports (e.g. `FetchError`) are imported.
+export const ofetch: $Fetch = /* @__PURE__ */ createFetch({ fetch });

createFetch is a pure factory: it only reads globalThis.fetch as a default and returns a closure, without mutating any global state. The package already declares "sideEffects": false, so the annotation is safe.

Testing

  • pnpm build (obuild) succeeds.
  • pnpm exec vitest run → 28/28 tests pass.
  • pnpm exec eslint src/index.ts → clean.

Per the issue's reproduction (https://github.com/onmax/repros/tree/repro/ofetch-pure-initializers), a minified bundle for an entry importing only FetchError drops from ~6070 B to ~364 B.

Summary by CodeRabbit

  • Improvements
    • Build tools can now exclude unused fetch functionality from production bundles, which may reduce download size for applications that do not use it. This change affects build output only; it does not alter the functionality available to end users when fetch features are used.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 047bd6a1-caa8-4f7f-8aba-8eaf394ebadf
Commits

Reviewing files that changed from the base of the PR and between 1dbc37f and 166e7ed.

Files selected for processing: 1
  • src/index.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

The default createFetch({ fetch }) initializer now has a pure annotation. The initializer itself is unchanged.

Changes: Tree-shaking annotation

Layer / File(s) Summary
Mark the default initializer as pure
src/index.ts
A pure annotation marks the default createFetch({ fetch }) call, allowing bundlers to remove it when unused.

Priority: Low

Estimated code review effort: 1 (Trivial) | ~3 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • unjs/ofetch#641: It adds a pure annotation to the same createFetch({ fetch }) initialization.

Suggested reviewers: pi0

Merge Risk: Minimal · up to 166e7

No actionable merge-blocking risk remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the default client creation in src/index.ts is now marked with a /* @__PURE__ */ annotation so bundlers can drop it when unused. The title is specific, concise, and ma…
Linked Issues check Passed Issue #642 requires that the unused default client is not retained when a bundle imports only FetchError. The reviewed head marks the default client creation in src/index.ts as pure: `export const ofe…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes only src/index.ts. It adds a two-line explanatory comment and the /* @__PURE__ */ annotation on the ofetch default client. Both changes directly im…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Title check

Explanation

The title names the main change: the default client creation in src/index.ts is now marked with a /* @__PURE__ */ annotation so bundlers can drop it when unused. The title is specific, concise, and matches the raw summary, which describes the same annotation on the createFetch call and its tree-shaking purpose. The PR objectives (PR #644 and issue #642) describe the same goal of removing the unused default fetch client when only FetchError is imported.

Full details: Linked Issues check

Explanation

Issue #642 requires that the unused default client is not retained when a bundle imports only FetchError. The reviewed head marks the default client creation in src/index.ts as pure: export const ofetch: $Fetch = /* @__PURE__ */ createFetch({ fetch });. This is the only coding change needed for the default-client retention. I checked the callee: createFetch in src/fetch.ts declares local functions and closures, assigns properties only to its own returned $fetch function object, and does not mutate globals. The call argument { fetch } is a plain object literal. The annotation is therefore consistent with the code. The byte-size result (6,070 B to 364 B) is the author's reported measurement. I did not rebuild the reproduction, so that figure is unverified here. No automated test was added. The issue does not request one, so this does not fail the requirement. The issue's non-coding instruction to run the reproduction is not a coding requirement.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes only src/index.ts. It adds a two-line explanatory comment and the /* @__PURE__ */ annotation on the ofetch default client. Both changes directly implement the #642 objective. No unrelated files or behavior changes appear in the diff. The $fetch alias and the fetch wrapper are unchanged.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 1 files.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unused default client is retained when importing FetchError

1 participant