Skip to content

Conversation

@ZakaryCode
Copy link
Contributor

@ZakaryCode ZakaryCode commented Feb 9, 2026

这个 PR 做了什么? (简要描述所做更改)

支持 harmony context 的 async 使用

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

  • 代码重构 (Refactor)

这个 PR 涉及以下平台:

  • 鸿蒙(Harmony)

Summary by CodeRabbit

发布说明

  • 改进
    • 构建/打包插件的初始化与变换钩子(如构建开始/结束、transform 等)改为支持异步等待编译上下文,提升初始化稳定性并减少竞态与异常风险。
    • 在非生产/生产模式下对编译上下文的获取进行了更稳健的处理,增强在不同环境下的可靠性。
  • 杂项
    • 多个包的版本统一提升到 4.1.12-alpha.4(仅版本元数据更新,无功能变更)。

@coderabbitai
Copy link

coderabbitai bot commented Feb 9, 2026

Walkthrough

将多个 Vite/Rollup 插件钩子(如 buildStart、buildEnd、transform)和工具函数 getViteHarmonyCompilerContext 从同步改为异步,插件中改为 await 获取 vite 编译器上下文;同时统一将大量 package.json 的版本提升到 4.1.12-alpha.4。

Changes

Cohort / File(s) Summary
框架层 Vite 插件
packages/taro-framework-react/src/vite.harmony.ts, packages/taro-framework-solid/src/vite.harmony.ts, packages/taro-framework-vue3/src/vite.harmony.ts
injectLoaderMeta 返回对象的 buildStart 标记为 async,并将内部 getViteHarmonyCompilerContext(this) 改为 await
Harmony C++ 平台 — 插件入口/页面/渲染
packages/taro-platform-harmony-cpp/src/program/vite/app.ts, packages/taro-platform-harmony-cpp/src/program/vite/page.ts, packages/taro-platform-harmony-cpp/src/program/vite/render.ts
各插件的 buildStart 改为 async,内部对 getViteHarmonyCompilerContext(pluginContext) 使用 await
Harmony C++ 平台 — 注入与样式
packages/taro-platform-harmony-cpp/src/program/vite/inject-env.ts, packages/taro-platform-harmony-cpp/src/program/vite/style.ts
transformbuildStartbuildEnd 等钩子改为 asyncawait 编译器上下文检索。
平台 ArkTS / 其他插件
packages/taro-platform-harmony/src/program/arkTS.ts
loader meta 注入中的编译器上下文检索改为异步 await getViteHarmonyCompilerContext(...)
工具函数异步化
packages/taro-runner-utils/src/vite.ts, packages/taro-vite-runner/src/harmony/pipeline.ts
导出 getViteHarmonyCompilerContext 改为 async,返回 `Promise<ViteHarmonyCompilerContext
Manifest / 版本提升
package.json, crates/native_binding/package.json, npm/*/package.json, packages/*/package.json
大量 package.json 的 version4.1.11 升级为 4.1.12-alpha.4(仅 manifest 版本变更)。

Sequence Diagram(s)

sequenceDiagram
    participant Plugin as Vite Plugin Hook
    participant Utils as runner-utils.getViteHarmonyCompilerContext()
    participant Module as Vite/Rollup ModuleStore
    Plugin->>Utils: await getViteHarmonyCompilerContext(this)
    Utils->>Module: (prod) getModuleInfo(VITE_COMPILER_LABEL)\n(dev) load({ id: VITE_COMPILER_LABEL })
    Module-->>Utils: module info (包含 meta.viteCompilerContext)
    Utils-->>Plugin: 返回 viteCompilerContext
    Plugin->>Plugin: 根据 viteCompilerContext 初始化 loaderMeta / 继续构建逻辑
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • luckyadam
  • tutuxxx
  • yoturg
  • Single-Dancer

Poem

🐰 我是园中小白兔,await 轻踏草间路,
钩子披上异步袍,编译上下文慢慢来,
版本齐声轻声唱,胡萝卜舞动庆新芽 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了主要变更内容:多个文件中的buildStart、buildEnd和transform hooks从同步改为异步,以及getViteHarmonyCompilerContext函数的异步化改造,以支持Harmony平台的异步上下文处理。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/harmony-watch

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.

@ZakaryCode ZakaryCode requested a review from yoturg February 9, 2026 07:31
@ZakaryCode ZakaryCode added A-loader Area - Webpack loader 相关 A-runner Area - Webpack runner 相关 T-harmony Target - 编译到 Harmony labels Feb 9, 2026
@coderabbitai coderabbitai bot requested a review from Single-Dancer February 9, 2026 07:31
@ZakaryCode ZakaryCode added this to the 4.1.12 milestone Feb 9, 2026
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

🤖 Fix all issues with AI agents
In `@packages/taro-platform-harmony-cpp/src/program/vite/page.ts`:
- Around line 53-56: The code uses a non-null assertion on the result of
getViteHarmonyCompilerContext which can be undefined and causes a runtime crash
when accessing compiler.taroConfig; change the flow to await
getViteHarmonyCompilerContext into a possibly-undefined compiler and guard
accesses with if (compiler) (or if (compiler && (compiler.pages instanceof Array
|| compiler.components instanceof Array))) before reading compiler.taroConfig or
other properties to match the pattern used elsewhere (e.g., in app.ts,
render.ts, vue3/vite.harmony.ts).
🧹 Nitpick comments (1)
packages/taro-platform-harmony-cpp/src/program/vite/style.ts (1)

73-79: buildEnd 缺少 this: PluginContext 类型注解。

buildStart(第 34 行)显式声明了 this: PluginContext,但 buildEnd 没有。虽然运行时 Rollup 会正确绑定 this,但为了类型安全和一致性,建议补上。

建议补充类型注解
-    async buildEnd() {
+    async buildEnd(this: PluginContext) {

Comment on lines +53 to 56
const compiler = (await getViteHarmonyCompilerContext(pluginContext))!
const taroConfig = compiler.taroConfig

if (compiler?.pages instanceof Array || compiler?.components instanceof Array) {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

非空断言 ! 存在运行时崩溃风险。

getViteHarmonyCompilerContext 的返回类型为 Promise<ViteHarmonyCompilerContext | void>,若返回 undefined,第 54 行 compiler.taroConfig 将抛出 TypeError。其他所有调用点(如 app.tsrender.tsvue3/vite.harmony.ts 等)均使用 if (compiler) 守卫,建议此处保持一致。

建议修复
-      const compiler = (await getViteHarmonyCompilerContext(pluginContext))!
-      const taroConfig = compiler.taroConfig
-
-      if (compiler?.pages instanceof Array || compiler?.components instanceof Array) {
+      const compiler = await getViteHarmonyCompilerContext(pluginContext)
+      if (!compiler) return
+      const taroConfig = compiler.taroConfig
+
+      if (compiler.pages instanceof Array || compiler.components instanceof Array) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const compiler = (await getViteHarmonyCompilerContext(pluginContext))!
const taroConfig = compiler.taroConfig
if (compiler?.pages instanceof Array || compiler?.components instanceof Array) {
const compiler = await getViteHarmonyCompilerContext(pluginContext)
if (!compiler) return
const taroConfig = compiler.taroConfig
if (compiler.pages instanceof Array || compiler.components instanceof Array) {
🤖 Prompt for AI Agents
In `@packages/taro-platform-harmony-cpp/src/program/vite/page.ts` around lines 53
- 56, The code uses a non-null assertion on the result of
getViteHarmonyCompilerContext which can be undefined and causes a runtime crash
when accessing compiler.taroConfig; change the flow to await
getViteHarmonyCompilerContext into a possibly-undefined compiler and guard
accesses with if (compiler) (or if (compiler && (compiler.pages instanceof Array
|| compiler.components instanceof Array))) before reading compiler.taroConfig or
other properties to match the pattern used elsewhere (e.g., in app.ts,
render.ts, vue3/vite.harmony.ts).

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.66%. Comparing base (3317bb2) to head (91423d2).

❌ Your project check has failed because the head coverage (52.66%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #18876      +/-   ##
==========================================
- Coverage   56.31%   52.66%   -3.65%     
==========================================
  Files         447      252     -195     
  Lines       23352    11682   -11670     
  Branches     5776     2756    -3020     
==========================================
- Hits        13150     6152    -6998     
+ Misses       8372     4563    -3809     
+ Partials     1830      967     -863     
Flag Coverage Δ
taro-runtime 60.27% <ø> (ø)

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

2 similar comments
@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.66%. Comparing base (3317bb2) to head (91423d2).

❌ Your project check has failed because the head coverage (52.66%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #18876      +/-   ##
==========================================
- Coverage   56.31%   52.66%   -3.65%     
==========================================
  Files         447      252     -195     
  Lines       23352    11682   -11670     
  Branches     5776     2756    -3020     
==========================================
- Hits        13150     6152    -6998     
+ Misses       8372     4563    -3809     
+ Partials     1830      967     -863     
Flag Coverage Δ
taro-runtime 60.27% <ø> (ø)

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

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.66%. Comparing base (3317bb2) to head (91423d2).

❌ Your project check has failed because the head coverage (52.66%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #18876      +/-   ##
==========================================
- Coverage   56.31%   52.66%   -3.65%     
==========================================
  Files         447      252     -195     
  Lines       23352    11682   -11670     
  Branches     5776     2756    -3020     
==========================================
- Hits        13150     6152    -6998     
+ Misses       8372     4563    -3809     
+ Partials     1830      967     -863     
Flag Coverage Δ
taro-runtime 60.27% <ø> (ø)

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

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.31%. Comparing base (3317bb2) to head (f7ff48d).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
packages/taro-framework-vue3/src/vite.harmony.ts 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #18876   +/-   ##
=======================================
  Coverage   56.31%   56.31%           
=======================================
  Files         447      447           
  Lines       23352    23352           
  Branches     5776     5794   +18     
=======================================
  Hits        13150    13150           
+ Misses       8372     8371    -1     
- Partials     1830     1831    +1     
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 60.27% <ø> (ø)
taro-web 53.12% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-framework-vue3/src/vite.harmony.ts 0.00% <0.00%> (ø)

... and 10 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.

@coderabbitai coderabbitai bot requested review from tutuxxx and yoturg February 9, 2026 08:00
@coderabbitai coderabbitai bot requested a review from luckyadam February 9, 2026 08:27
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

🤖 Fix all issues with AI agents
In `@packages/taro-vite-runner/src/harmony/pipeline.ts`:
- Around line 14-19: In buildStart, the production branch currently drops the
Promise from this.load so getModuleInfo(VITE_COMPILER_LABEL) may see null;
change the ternary so the production path uses await this.load({ id:
VITE_COMPILER_LABEL }) and the non-production/watch path calls this.load({ id:
VITE_COMPILER_LABEL }) without awaiting, ensuring
this.getModuleInfo(VITE_COMPILER_LABEL) runs only after the module is loaded in
production; update the ternary in the buildStart method accordingly.

Comment on lines +14 to 19
async buildStart () {
const isProd = getMode(taroConfig) === 'production'
isProd
? this.load({ id: VITE_COMPILER_LABEL })
: await this.load({ id: VITE_COMPILER_LABEL })
const info = this.getModuleInfo(VITE_COMPILER_LABEL)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

cat -n packages/taro-vite-runner/src/harmony/pipeline.ts | head -40

Repository: NervJS/taro

Length of output: 1520


🏁 Script executed:

# Search for other buildStart implementations in harmony-related files
rg -n "buildStart" --type=ts -C 5 packages/taro-vite-runner/src/harmony/ packages/taro-platform-harmony-cpp/

Repository: NervJS/taro

Length of output: 12792


🏁 Script executed:

# Check if there are related harmony plugin files
fd -e ts -g '*harmony*' packages/ | head -20

Repository: NervJS/taro

Length of output: 1238


生产模式下 this.load() 的 Promise 被丢弃,getModuleInfo() 无法获取正确的模块信息。

当前代码在 lines 16-18 的三元表达式将 await 放在了错误的分支:

  • 生产模式isProd = true):this.load() 不 await → Promise 被丢弃
  • 非生产模式isProd = false):await this.load() → 等待加载完成

但 line 19 的 this.getModuleInfo(VITE_COMPILER_LABEL) 依赖模块已加载。在生产模式下不 await,该方法可能返回 null,导致 line 20-23 的后续操作被跳过(模块元信息设置和配置文件监听失效)。

整个 harmony 生态中其他 async buildStart 方法都正确地 await 其异步操作(如 taro-platform-harmony-cpp/src/program/vite/ 下的 app.ts、render.ts、page.ts 等),这里应该保持一致。

三元分支应交换:生产模式同步等待加载完成,开发/watch 模式可以不阻塞。

建议修复:交换三元分支
     async buildStart () {
       const isProd = getMode(taroConfig) === 'production'
-      isProd
-        ? this.load({ id: VITE_COMPILER_LABEL })
-        : await this.load({ id: VITE_COMPILER_LABEL })
+      isProd
+        ? await this.load({ id: VITE_COMPILER_LABEL })
+        : this.load({ id: VITE_COMPILER_LABEL })
       const info = this.getModuleInfo(VITE_COMPILER_LABEL)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async buildStart () {
const isProd = getMode(taroConfig) === 'production'
isProd
? this.load({ id: VITE_COMPILER_LABEL })
: await this.load({ id: VITE_COMPILER_LABEL })
const info = this.getModuleInfo(VITE_COMPILER_LABEL)
async buildStart () {
const isProd = getMode(taroConfig) === 'production'
isProd
? await this.load({ id: VITE_COMPILER_LABEL })
: this.load({ id: VITE_COMPILER_LABEL })
const info = this.getModuleInfo(VITE_COMPILER_LABEL)
🤖 Prompt for AI Agents
In `@packages/taro-vite-runner/src/harmony/pipeline.ts` around lines 14 - 19, In
buildStart, the production branch currently drops the Promise from this.load so
getModuleInfo(VITE_COMPILER_LABEL) may see null; change the ternary so the
production path uses await this.load({ id: VITE_COMPILER_LABEL }) and the
non-production/watch path calls this.load({ id: VITE_COMPILER_LABEL }) without
awaiting, ensuring this.getModuleInfo(VITE_COMPILER_LABEL) runs only after the
module is loaded in production; update the ternary in the buildStart method
accordingly.

@ZakaryCode ZakaryCode merged commit af08244 into main Feb 11, 2026
46 checks passed
@ZakaryCode ZakaryCode deleted the feat/harmony-watch branch February 11, 2026 02:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-loader Area - Webpack loader 相关 A-runner Area - Webpack runner 相关 T-harmony Target - 编译到 Harmony

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants