Skip to content

fix(swap-widget): guard import.meta.env so non-Vite bundlers can import the widget - #12521

Open
reallybeard wants to merge 1 commit into
shapeshift:developfrom
reallybeard:fix/swap-widget-import-meta-env-guard
Open

fix(swap-widget): guard import.meta.env so non-Vite bundlers can import the widget#12521
reallybeard wants to merge 1 commit into
shapeshift:developfrom
reallybeard:fix/swap-widget-import-meta-env-guard

Conversation

@reallybeard

@reallybeard reallybeard commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

import.meta.env only exists after a Vite-style bundler substitutes it. Every other bundler — webpack, Turbopack, Rollup — evaluates the module with import.meta.env still undefined. The existing ?? guards the property, not the object, so reading .VITE_SWAP_WIDGET_API_URL off undefined throws at module evaluation, before the fallback can apply.

The practical effect is that @shapeshiftoss/swap-widget cannot be imported at all outside Vite. It is not a degraded API URL — it is a hard TypeError that takes down the importing component tree. In a Next.js 16 (Turbopack) app on 0.5.2:

TypeError: Cannot read properties of undefined (reading 'VITE_SWAP_WIDGET_API_URL')
    at module evaluation (@shapeshiftoss_swap-widget_dist_index.js)
    at ShapeShiftSwapWidget.tsx

One optional chain fixes it. Under Vite the behaviour is identical; elsewhere it now falls back to the documented https://api.shapeshift.com default instead of throwing.

This is the only import.meta.env reachable from the published entry — the other two occurrences are in src/demo/, which tsup does not include in the bundle.

Issue (if applicable)

closes #

Risk

Very low. No on-chain transactions, wallets, or contract interactions are touched. The only behavioural change is how the default API base URL is resolved, and only in environments where the widget currently cannot load at all. Within the ShapeShift web app (Vite), import.meta.env is always defined, so the expression evaluates exactly as before.

What protocols, transaction types, wallets or contract interactions might be affected by this PR?

None.

Testing

Engineering

  • Under Vite (pnpm dev in packages/swap-widget): unchanged — VITE_SWAP_WIDGET_API_URL is still honoured when set, and the default applies when it is not.
  • Under a non-Vite bundler: import SwapWidget into a Next.js 16 (Turbopack) app. Before this change it throws the TypeError above at import time; after, it mounts and uses the default base URL. I verified this by applying the identical change to 0.5.2's published dist in such an app, where the widget then rendered and returned quotes normally.
  • Note: I could not run the repo's tsc or ESLint locally — this was a dependency-free clone — so please let CI confirm those. Prettier was checked against the inline config in .eslintrc (printWidth 100, no semi, single quotes) and passes.

Operations

  • 🏁 My feature is behind a flag and doesn't require operations testing (yet)

No user-facing change to the ShapeShift web app; this only affects third-party consumers of the published package.

Screenshots (if applicable)

N/A

Made with Cursor

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when the swap widget loads in environments where runtime configuration is unavailable.
    • Preserved the existing fallback API endpoint behavior.

…rt the widget

`import.meta.env` is only defined once a Vite-style bundler substitutes it.
webpack, Turbopack and Rollup evaluate the module with it still undefined, and
the existing `??` guards the property rather than the object, so reading
`.VITE_SWAP_WIDGET_API_URL` throws at module evaluation and takes down the
importing tree. Optional chaining keeps Vite behaviour identical while letting
every other bundler fall back to the documented default base URL.

Co-authored-by: Cursor <cursoragent@cursor.com>
@reallybeard
reallybeard requested a review from a team as a code owner August 4, 2026 16:39
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e8199cfd-b335-4ad8-ac03-33f9a6593fae

📥 Commits

Reviewing files that changed from the base of the PR and between 4a7be21 and 7c79ee9.

📒 Files selected for processing (1)
  • packages/swap-widget/src/api/client.ts

📝 Walkthrough

Walkthrough

The API client now safely reads import.meta.env when it is available. It preserves the existing fallback API base URL when environment metadata is unavailable.

Changes

API configuration

Layer / File(s) Summary
Safe base URL initialization
packages/swap-widget/src/api/client.ts
Optional chaining protects access to import.meta.env before the existing fallback URL is applied.

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

Suggested reviewers: 0xapotheosis, kaladinlight

Poem

A rabbit hops through API hay,
And guards the environment today.
If settings hide, the fallback stays,
Safe paths guide the client’s ways.
Nibble, test, and ship away!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the fix to import.meta.env access and its purpose for non-Vite bundlers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

1 participant