fix(swap-widget): guard import.meta.env so non-Vite bundlers can import the widget - #12521
fix(swap-widget): guard import.meta.env so non-Vite bundlers can import the widget#12521reallybeard wants to merge 1 commit into
Conversation
…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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe API client now safely reads ChangesAPI configuration
Estimated code review effort: 1 (Trivial) | ~5 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Description
import.meta.envonly exists after a Vite-style bundler substitutes it. Every other bundler — webpack, Turbopack, Rollup — evaluates the module withimport.meta.envstillundefined. The existing??guards the property, not the object, so reading.VITE_SWAP_WIDGET_API_URLoffundefinedthrows at module evaluation, before the fallback can apply.The practical effect is that
@shapeshiftoss/swap-widgetcannot be imported at all outside Vite. It is not a degraded API URL — it is a hardTypeErrorthat takes down the importing component tree. In a Next.js 16 (Turbopack) app on0.5.2:One optional chain fixes it. Under Vite the behaviour is identical; elsewhere it now falls back to the documented
https://api.shapeshift.comdefault instead of throwing.This is the only
import.meta.envreachable from the published entry — the other two occurrences are insrc/demo/, whichtsupdoes 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.envis always defined, so the expression evaluates exactly as before.None.
Testing
Engineering
pnpm devinpackages/swap-widget): unchanged —VITE_SWAP_WIDGET_API_URLis still honoured when set, and the default applies when it is not.SwapWidgetinto a Next.js 16 (Turbopack) app. Before this change it throws theTypeErrorabove at import time; after, it mounts and uses the default base URL. I verified this by applying the identical change to0.5.2's publisheddistin such an app, where the widget then rendered and returned quotes normally.tscor 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
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