Skip to content

Conversation

1oo1
Copy link
Contributor

@1oo1 1oo1 commented Aug 27, 2025

这个 PR 做了什么? (简要描述所做更改)
允许加载虚拟 css,如 unocss vite 插件生成的虚拟样式,现在会因为找不到文件导致编译失败。

这个 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
    • 样式加载器在处理样式请求时增加文件存在性校验:仅在目标文件确实存在时才加入监听并读取其内容;若文件缺失则跳过并返回空内容,从而避免因缺失文件导致的异常、构建中断或多余错误日志。对外行为与公开 API 无变更。

Copy link

coderabbitai bot commented Aug 27, 2025

Walkthrough

在 Harmony 样式加载流程中新增文件存在性检查:处理样式请求时先判断原始路径是否存在,存在则加入监视并读取内容;不存在则跳过监听与读取并不返回内容,避免因缺失文件抛出异常。

Changes

Cohort / File(s) Change Summary
Harmony 样式加载守护
packages/taro-vite-runner/src/harmony/style.ts
在读取与监听样式文件前增加 fs.existsSync(rawId) 判断;若文件不存在则跳过监听与读取并返回空结果,改变了缺失文件时的控制流与错误处理。

Sequence Diagram(s)

sequenceDiagram
    participant Client as 调用方
    participant Loader as Harmony 样式 Loader
    participant FS as 文件系统

    Client->>Loader: 请求处理样式 (rawId)
    Loader->>FS: 检查是否存在 (fs.existsSync(rawId))
    alt 文件存在
        Loader->>FS: 读取文件内容
        Loader->>Loader: 将文件加入 watch 列表
        Loader-->>Client: 返回内容
    else 文件不存在
        note right of Loader: 跳过读取与监听\n不返回内容以避免异常
        Loader-->>Client: 返回空结果 / 无内容
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • ZakaryCode
  • yoturg

Poem

小兔轻步入代码巷,
先问文件是否在旁。
无影处不惊不闹,
省去报错静悄然。
监视名单更从简,
风里代码笑开颜。

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c448a0a and 8a72f2c.

📒 Files selected for processing (1)
  • packages/taro-vite-runner/src/harmony/style.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/taro-vite-runner/src/harmony/style.ts
✨ 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.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit 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:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit 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/Issue comments)

Type @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/taro-vite-runner/src/harmony/style.ts (1)

236-266: 统一 cssCache 缓存键的规范化
当前在写入缓存时使用:

id = stripVirtualModulePrefix(id).replace(STYLE_SUFFIX, '')
cssCache.set(id, css)

而读取缓存时用到的键是:

const rawId = stripVirtualModulePrefix(cssId)
  .replace(STYLE_SUFFIX_RE, '')
  .replace(usedRE, '')
return cssCache.get(rawId) || ''

两者对 id 的处理逻辑不一致(写入时未去除 ?used 查询参数,且使用了不同的后缀替换方式),会导致缓存未命中(例如 *.module.css?used 场景)。请在写入缓存前同样执行 .replace(STYLE_SUFFIX_RE, '').replace(usedRE, ''),示例修改:

@@ packages/taro-vite-runner/src/harmony/style.ts:236
-      id = stripVirtualModulePrefix(id).replace(STYLE_SUFFIX, '')
+      // 规范化 id:移除虚拟前缀、样式后缀及 '?used' 查询参数
+      const normalizedId = stripVirtualModulePrefix(id)
+        .replace(STYLE_SUFFIX_RE, '')
+        .replace(usedRE, '')
+      id = normalizedId

@@ packages/taro-vite-runner/src/harmony/style.ts:263
-      cssCache.set(id, css)
+      cssCache.set(normalizedId, css)

请务必保证 cssCache.setcssCache.get 使用同样的键规范化策略,否则会造成缓存命中失败。

  • 影响文件:packages/taro-vite-runner/src/harmony/style.ts(第 236–266 行)
  • 可用命令检验:
    rg -n -C1 'cssCache\.set\('
    rg -n -C1 'cssCache\.get\('
🧹 Nitpick comments (1)
packages/taro-vite-runner/src/harmony/style.ts (1)

63-75: 预加载阶段遗漏 virtual:*.css:入口全局样式可能未被提前加载

getCssIdSets 仅在 resolveSync 成功时加入 cssIdSet。对 virtual:uno.css 这类虚拟样式,resolveSync 会失败,导致未加入集合,后续“确保 CSS 文件已经加载”的逻辑可能错过该样式(尽管最终由下游插件加载,但这里的预加载与缓存填充会缺席)。

建议在未解析且判断为虚拟 CSS 时,直接把原始 id 纳入集合,让后续 this.load({ id }) 触发下游插件处理。

@@ function getCssIdSets (raw: string, id: string, sourceDir: string) {
-                if (resolveId && CSS_LANGS_RE.test(rawId)) {
+                if (resolveId && CSS_LANGS_RE.test(rawId)) {
                   // Note: 预加载依赖的 CSS 文件
                   const cssId = appendVirtualModulePrefix(resolveId + STYLE_SUFFIX)
                   cssIdSet.add(cssId)
+                } else if (CSS_LANGS_RE.test(rawId) && rawId.startsWith('virtual:')) {
+                  // 支持虚拟样式:交由后续 load 钩子与下游插件处理
+                  cssIdSet.add(rawId)
                 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1d2aead and 6ef46fa.

📒 Files selected for processing (1)
  • packages/taro-vite-runner/src/harmony/style.ts (1 hunks)
🔇 Additional comments (1)
packages/taro-vite-runner/src/harmony/style.ts (1)

146-149: 方向正确:为样式加载增加存在性检查以兼容虚拟 CSS

当文件不存在时不再读盘,可让下游(如 unocss)插件接管加载,避免异常,符合 PR 目标。

@Single-Dancer Single-Dancer added this to the 4.1.6 milestone Aug 27, 2025
@Single-Dancer Single-Dancer force-pushed the feat/harmony_cpp-virtual-css branch from 6ef46fa to c448a0a Compare August 28, 2025 05:41
@coderabbitai coderabbitai bot requested review from yoturg and ZakaryCode August 28, 2025 05:42
@Single-Dancer Single-Dancer force-pushed the feat/harmony_cpp-virtual-css branch from c448a0a to 8a72f2c Compare August 28, 2025 05:43
Copy link

codecov bot commented Aug 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.06%. Comparing base (d1f3db8) to head (8a72f2c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #18253   +/-   ##
=======================================
  Coverage   55.06%   55.06%           
=======================================
  Files         416      416           
  Lines       21563    21563           
  Branches     5278     5293   +15     
=======================================
  Hits        11873    11873           
- Misses       8163     8173   +10     
+ Partials     1527     1517   -10     
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 59.87% <ø> (ø)
taro-web 53.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 12 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.

@Single-Dancer Single-Dancer merged commit 9ec6b7f into NervJS:main Aug 28, 2025
24 checks passed
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