Skip to content

Conversation

@Adamlyu-hub
Copy link
Contributor

@Adamlyu-hub Adamlyu-hub commented Sep 11, 2025

这个 PR 做了什么? (简要描述所做更改)
修改了 program.ts 文件,在extraImportForWeb字符串末尾添加了 .replace(/\/g, '/') 方法调用,确保字符串中的反斜杠被替换为正斜杠,从而解决Windows环境下的路径兼容性问题。

这个 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

  • Bug Fixes
    • 规范化 H5 构建中注入代码的路径分隔符,提升跨平台兼容性与稳定性,减少因反斜杠导致的导入异常风险;无需迁移操作,现有行为保持一致。

@coderabbitai
Copy link

coderabbitai bot commented Sep 11, 2025

Walkthrough

在 packages/taro-platform-h5/src/program.ts 中,modifyWebpackConfig 的默认分支在 useHtmlComponents 为 true 时,对插入到 loaderMeta.extraImportForWeb 的导入代码增加了 .replace(/\\/g, '/') 的规范化处理。其他逻辑未变,更改未影响导出/公共声明。

Changes

Cohort / File(s) Summary of Changes
H5 Webpack 导入字符串规范化
packages/taro-platform-h5/src/program.ts
在 modifyWebpackConfig 的默认分支中,当 useHtmlComponents 为 true 时,对将追加的导入片段应用 .replace(/\\\/g, '/'),以将反斜杠规范为正斜杠;不涉及对外 API 或控制流调整。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • tutuxxx
  • Single-Dancer

Pre-merge checks (3 passed)

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 标题 "Fix/Windows路径解析问题" 明确指出了本次变更的主要目的——修复 Windows 环境下的路径解析/兼容性问题,与 PR 在 program.ts 中加入 .replace(/\/g, '/') 以将反斜杠替换为正斜杠的修改一致。标题简短且能让审阅者快速识别为一个 bug 修复,但使用了英文前缀 "Fix/" 与中文主体混合,风格略显不一致。总体上它反映了主要修改点并可被团队识别。
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

Poem

小兔敲键轻轻跳,
斜杠排队不乱跑。
反斜归正路更直,
H5 入口更可靠。
耳尖一抖提个 PR,
代码风顺心里笑。 🐇✨

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.

✨ 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

Comment @coderabbitai help to get the list of available commands and usage tips.

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: 0

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

143-146: 将路径规范化移动到统一出口,避免只覆盖这一处

目前仅对这段新增的 import 片段做 .replace(/\\/g, '/'),而在同一函数内更早追加到 extraImportForWeb 的内容(如 Line 128、133)未被规范化,存在行为不一致的风险;同时 Vite 分支未做同样处理。建议在返回前对整段 loaderMeta.extraImportForWeb 做一次统一规范化,并同步到 Vite 插件,保证 webpack/vite 一致。

建议修改如下:

           default:
             if (this.useHtmlComponents) {
-              args[0].loaderMeta.extraImportForWeb += `import '@tarojs/components-react/dist/index.css'\nimport { PullDownRefresh } from '@tarojs/components'\n`.replace(/\\/g, '/')
+              args[0].loaderMeta.extraImportForWeb += `import '@tarojs/components-react/dist/index.css'\nimport { PullDownRefresh } from '@tarojs/components'\n`
               args[0].loaderMeta.execBeforeCreateWebApp += `config.PullDownRefresh = PullDownRefresh\n`
             }
         }
+        // 统一归一化,覆盖所有之前追加的片段
+        args[0].loaderMeta.extraImportForWeb =
+          (args[0].loaderMeta.extraImportForWeb || '').replace(/\\/g, '/')
         return args

Vite 分支对应位置(injectLoaderMeta 内)也建议追加一行统一处理:

                 default:
                   if (that.useHtmlComponents) {
                     viteCompilerContext.loaderMeta.extraImportForWeb += `import { PullDownRefresh } from '@tarojs/components'\n`
                     viteCompilerContext.loaderMeta.execBeforeCreateWebApp += `config.PullDownRefresh = PullDownRefresh\n`
                   }
               }
+              // 统一归一化,保持与 webpack 行为一致
+              viteCompilerContext.loaderMeta.extraImportForWeb =
+                (viteCompilerContext.loaderMeta.extraImportForWeb || '').replace(/\\/g, '/')

验证点:

  • 请在 Windows 下构建一次,检查最终注入到虚拟入口/编译产物中的 import 语句是否完全使用正斜杠。

143-146: 如需更稳妥,限定只替换 import 源中的反斜杠

若将来注入代码包含字符串/正则字面量中的反斜杠,整串替换可能“误伤”。可以仅替换 import ... from '...'/import '...' 源部分。

可选替代实现(示意):

args[0].loaderMeta.extraImportForWeb =
  (args[0].loaderMeta.extraImportForWeb || '').replace(
    /(import\s+(?:['"][^'"]+['"]|[\s\S]*?\sfrom\s+['"])([^'"]+)(['"]))/g,
    (_, p1, spec, p3) => p1.replace(spec, spec.replace(/\\/g, '/'))
  )

同理应用于 Vite 分支。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e5958e9 and ffa72ae.

📒 Files selected for processing (1)
  • packages/taro-platform-h5/src/program.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
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 文件。
Learnt from: ianzone
PR: NervJS/taro#18150
File: packages/taro-platform-harmony-hybrid/package.json:43-45
Timestamp: 2025-09-05T18:40:45.775Z
Learning: 在 tarojs/plugin-platform-harmony-hybrid 包中,tarojs/components-library-react、tarojs/components-library-solid 和 tarojs/components-library-vue3 必须作为直接依赖(dependencies)而不能作为 peer 依赖,因为插件源码中有对这些包的直接引用,包括 componentAdapter* getter 方法和 webpack 别名配置。
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin

@codecov
Copy link

codecov bot commented Sep 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.05%. Comparing base (3195665) to head (ffa72ae).
⚠️ Report is 38 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #18335      +/-   ##
==========================================
+ Coverage   54.98%   55.05%   +0.07%     
==========================================
  Files         417      416       -1     
  Lines       21693    21560     -133     
  Branches     5352     5267      -85     
==========================================
- Hits        11928    11870      -58     
+ Misses       8245     8038     -207     
- Partials     1520     1652     +132     
Flag Coverage Δ
taro-cli 72.85% <ø> (+0.43%) ⬆️
taro-runtime 59.87% <ø> (ø)
taro-web 53.13% <ø> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 60 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This was referenced Sep 13, 2025
default:
if (this.useHtmlComponents) {
args[0].loaderMeta.extraImportForWeb += `import '@tarojs/components-react/dist/index.css'\nimport { PullDownRefresh } from '@tarojs/components'\n`
args[0].loaderMeta.extraImportForWeb += `import '@tarojs/components-react/dist/index.css'\nimport { PullDownRefresh } from '@tarojs/components'\n`.replace(/\\/g, '/')
Copy link
Contributor

Choose a reason for hiding this comment

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

模版里好像也没有反斜杠呢

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