-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix(taro-platform-h5): 修复windows开发环境Taro对象上等方法不可用 #18151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Walkthrough本次变更对 Vite 配置中的 Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 分钟 Suggested reviewers
Poem
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 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis 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: 验证路径匹配边界已通过
- 在
/srcvs/src2、带查询串和大小写差异场景下,新逻辑(给sourceDir添加尾部斜杠再做startsWith)表现符合预期;- 原有正常匹配场景(如
C:\proj\src\app.ts)未受影响。无需进一步修改。
| const normalizedSourceDir = viteCompilerContext.sourceDir.replace(/\\/g, '/') // 👈 替换斜杠方向 | ||
| if (cleanId.startsWith(normalizedSourceDir) && exts.some((ext) => id.includes(ext))) { |
There was a problem hiding this comment.
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.
这个 PR 做了什么? (简要描述所做更改)
解决windows开发环境Taro.request, Taro.setStorageSync, Taro.getStorageSync,Taro.showToast等方法不可用
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit