Skip to content

feat(mobile): 根据 __DEV__ 自动切换 API 地址 - #57

Merged
hinson0 merged 1 commit into
mainfrom
worktree-fix-envfile
Apr 17, 2026
Merged

hinson0 merged 1 commit into
mainfrom
worktree-fix-envfile

Conversation

@hinson0

@hinson0 hinson0 commented Apr 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • 新建 apps/mobile/lib/config.ts:用 __DEV__ 在 EXPO_PUBLIC_API_URL_DEV(开发)与 EXPO_PUBLIC_API_URL(生产)之间切换,变量缺失时启动即抛错
  • apps/mobile/lib/api.ts、apps/mobile/lib/auth.ts 改为从 ./config 导入 API_BASE
  • apps/mobile/.env.example 补齐双变量,并加注释说明真机调试不能用 localhost

动机

之前本地开发和线上发布共用 EXPO_PUBLIC_API_URL,每次切换环境都要改 .env,容易忘改提交。Metro 会把 __DEV__ 和 process.env.EXPO_PUBLIC_* 内联为字面常量,所以运行时零开销。

Test plan

  • cp apps/mobile/.env.example apps/mobile/.env 并把 EXPO_PUBLIC_API_URL_DEV 改为开发机局域网 IP
  • pnpm dev 启动 Expo,真机登录 / 拉交易列表,确认走 DEV URL
  • 临时注释掉 .env 里的 EXPO_PUBLIC_API_URL_DEV,确认启动时抛出"API_BASE 未配置"错误
  • 本地用 pnpm --filter mobile exec expo start --no-dev --minify 启动,真机/模拟器连上确认走生产 URL(119.45.41.158)

Co-Authored-By: Claude noreply@anthropic.com

抽出 lib/config.ts,用 __DEV__ 在 EXPO_PUBLIC_API_URL_DEV 和
EXPO_PUBLIC_API_URL 之间切换,缺失时启动即抛错。
.env 从此一次写死,不再手动来回改。

Co-Authored-By: Claude <noreply@anthropic.com>
@hinson0
hinson0 merged commit a6da8fe into main Apr 17, 2026
1 check passed
@hinson0
hinson0 deleted the worktree-fix-envfile branch April 17, 2026 13:53
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