Skip to content

Conversation

@Adamlyu-hub
Copy link
Contributor

@Adamlyu-hub Adamlyu-hub commented Aug 8, 2025

这个 PR 做了什么? (简要描述所做更改)
解决windows开发环境Taro.request, Taro.setStorageSync, Taro.getStorageSync,Taro.showToast等方法不可用

这个 PR 是什么类型? (至少选择一个)

  • [ ✅] 错误修复 (Bugfix) issue: fix #
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe):

这个 PR 涉及以下平台:

  • 所有平台
  • [ ✅] Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 钉钉小程序
  • QQ 小程序
  • 飞书小程序
  • 快手小程序
  • 头条小程序

Summary by CodeRabbit

  • 修复问题
    • 优化了路径格式的处理,提升了跨操作系统的兼容性,避免因路径分隔符差异导致的问题。

@coderabbitai
Copy link

coderabbitai bot commented Aug 8, 2025

Walkthrough

本次变更对 Vite 配置中的 apiPlugin transform 函数进行了路径归一化处理。通过对文件路径和源目录路径进行统一格式化,增强了跨操作系统的兼容性,确保只对正确的文件应用 Babel 转换。未涉及导出或公共实体的声明变更。

Changes

Cohort / File(s) Change Summary
路径归一化与 transform 条件增强
packages/taro-platform-h5/src/program.ts
优化 transform 函数中的路径判断逻辑:先去除路径中的前导空字符和查询参数,再将反斜杠替换为正斜杠,对比时统一格式,提升跨平台一致性。

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 分钟

Suggested reviewers

  • Single-Dancer

Poem

小兔子今日舞键盘,
路径归一细又宽。
反斜杠正斜杠,
系统差异不再烦。
transform 判断更精准,
代码世界乐无边!
🐇✨

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai bot requested a review from Single-Dancer August 8, 2025 06:10
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
packages/taro-platform-h5/src/program.ts (3)

237-237: 扩展名匹配更稳健:对“去查询串后的路径”使用 endsWith

当前用 id.includes(ext) 可能被诸如 ?lang.ts 等查询参数误触发,也可能命中非文件后缀的中间片段。建议基于 cleanId(已去查询串)使用 endsWith,提高命中精度。

-              if (cleanId.startsWith(normalizedSourceDir) && exts.some((ext) => id.includes(ext))) {
+              if (inSourceDir && exts.some((ext) => cleanId.endsWith(ext))) {

若需进一步严谨,可先取真正的文件后缀再比对(注意要同时覆盖 .vue 等框架文件场景)。


236-237: Windows 大小写不敏感:建议在 win32 平台做不区分大小写的匹配

在大小写可能不一致的情况下,Windows 文件系统不区分大小写,startsWith 仍可能受大小写影响。可在 win32 下将两侧统一为小写再比较,增强鲁棒性。

与上一条边界处理合并后的建议:

-              let normalizedSourceDir = viteCompilerContext.sourceDir.replace(/\\/g, '/') // 👈 替换斜杠方向
-              normalizedSourceDir = normalizedSourceDir.replace(/\/+$/, '') + '/'
-              const inSourceDir = cleanId.startsWith(normalizedSourceDir)
+              let normalizedSourceDir = viteCompilerContext.sourceDir.replace(/\\/g, '/')
+              normalizedSourceDir = normalizedSourceDir.replace(/\/+$/, '') + '/'
+              const isWin = process.platform === 'win32'
+              const a = isWin ? cleanId.toLowerCase() : cleanId
+              const b = isWin ? normalizedSourceDir.toLowerCase() : normalizedSourceDir
+              const inSourceDir = a.startsWith(b)

239-241: 建议使用 cleanId 作为 Babel 的 filename/sourceFileName

传入带查询串或虚拟前缀的 id 可能影响 sourcemap 或下游插件基于 filename 的逻辑。既然已得到 cleanId,建议用于 Babel 选项,提升一致性。

-                const result = await transformAsync(code, {
-                  filename: id,
+                const result = await transformAsync(code, {
+                  filename: cleanId,
+                  sourceFileName: cleanId,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ae19ce5 and 30e5a48.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (1)
  • packages/taro-platform-h5/src/program.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: ianzone
PR: NervJS/taro#17993
File: .github/advanced-issue-labeler.yml:20-63
Timestamp: 2025-07-11T14:04:05.557Z
Learning: 在 NervJS/taro 项目的 .github/advanced-issue-labeler.yml 中,平台标签被设计为可以多个共存。当用户选择"所有平台"时,系统会同时应用多个平台标签(如 T-h5, T-rn, T-weapp 等),这是期望的行为而非冲突。同样,"所有小程序"也会为小程序相关的标签应用多个标签。这种设计有助于跨平台问题的管理和维护者关注。
Learnt from: ianzone
PR: NervJS/taro#18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.207Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。
📚 Learning: 2025-08-08T02:32:58.207Z
Learnt from: ianzone
PR: NervJS/taro#18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.207Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。

Applied to files:

  • packages/taro-platform-h5/src/program.ts
📚 Learning: 2025-05-25T18:02:31.387Z
Learnt from: ianzone
PR: NervJS/taro#17746
File: packages/taro-runtime/tsdown.config.ts:10-16
Timestamp: 2025-05-25T18:02:31.387Z
Learning: 在 taro-runtime 包的 tsdown 配置中,必须禁用 treeshake 来保留 dom-external/index.js 文件。

Applied to files:

  • packages/taro-platform-h5/src/program.ts
🔇 Additional comments (2)
packages/taro-platform-h5/src/program.ts (2)

227-237: 肯定:路径归一化修复直击问题本质,能在 Windows 下恢复 Taro API 转换

处理了三类常见 Vite id 场景:移除前缀空字符、去掉查询串、将反斜杠统一为正斜杠,并同步规范 sourceDir。这样 startsWith 判定在 win32 不再失效,有助于确保 babel-plugin-transform-taroapi 被正确应用,从而解决 Taro.request 等 API 在 Windows 开发环境不可用的问题。此处改动方向正确且与 PR 目标一致。


230-237: 验证路径匹配边界已通过

  • /src vs /src2、带查询串和大小写差异场景下,新逻辑(给 sourceDir 添加尾部斜杠再做 startsWith)表现符合预期;
  • 原有正常匹配场景(如 C:\proj\src\app.ts)未受影响。

无需进一步修改。

Comment on lines +236 to +237
const normalizedSourceDir = viteCompilerContext.sourceDir.replace(/\\/g, '/') // 👈 替换斜杠方向
if (cleanId.startsWith(normalizedSourceDir) && exts.some((ext) => id.includes(ext))) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

避免路径前缀误匹配:建议加“目录边界”判定

当前使用 startsWith 可能将 /src 与 /src2 误判为命中。建议在归一化后为 sourceDir 补尾随斜杠,或计算相对路径/使用边界判断,降低误伤范围。

可考虑最小变更如下:

-              const normalizedSourceDir = viteCompilerContext.sourceDir.replace(/\\/g, '/') // 👈 替换斜杠方向
-              if (cleanId.startsWith(normalizedSourceDir) && exts.some((ext) => id.includes(ext))) {
+              let normalizedSourceDir = viteCompilerContext.sourceDir.replace(/\\/g, '/') // 👈 替换斜杠方向
+              normalizedSourceDir = normalizedSourceDir.replace(/\/+$/, '') + '/'
+              const inSourceDir = cleanId.startsWith(normalizedSourceDir)
+              if (inSourceDir && exts.some((ext) => id.includes(ext))) {
🤖 Prompt for AI Agents
In packages/taro-platform-h5/src/program.ts around lines 236 to 237, the current
use of startsWith to check if cleanId begins with normalizedSourceDir can cause
false positives when paths like <sourceDir>/src2 are mistaken for
<sourceDir>/src. To fix this, ensure normalizedSourceDir ends with a trailing
slash after normalization, then check if cleanId starts with this adjusted path
to enforce directory boundary matching and avoid incorrect matches.

This was referenced Aug 9, 2025
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.

2 participants