Skip to content

Conversation

@ianzone
Copy link
Contributor

@ianzone ianzone commented Nov 11, 2025

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

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

  • Chores

    • 升级开发工具链:在根及若干包中引入 Vite、升级 Vitest、替换覆盖率工具为 V8、更新 Rollup 与 PostCSS 版本
    • 调整依赖声明:在多个包中移除或重定位旧依赖(含将 postcss 从 devDependencies 移至 peerDependencies)
  • Tests

    • 统一切换至 Vitest,更新测试脚本并删除旧的 Jest 配置文件
    • 简化覆盖率配置(移除对 Istanbul 的显式指定)并将部分测试迁移为 ES 模块兼容新环境

@coderabbitai
Copy link

coderabbitai bot commented Nov 11, 2025

Walkthrough

将多个包的测试体系从 Jest 迁移到 Vitest:更新/添加根与包内 devDependencies(包括 Vitest、Vite、coverage 适配器与 PostCSS/Rollup 版本调整)、删除部分 Jest 配置、替换测试脚本为 Vitest、新增/修改多包的 vitest.config.ts(多处移除 coverage.provider),并对 Vite 插件 API 做小幅适配。

Changes

Cohort / File(s) 变更摘要
根依赖与工具链
package.json
更新根依赖:替换 @vitest/coverage-istanbul@vitest/coverage-v8,新增/升级 vite、升级 vitest,调整 postcssrollup 版本。
PostCSS 版本调整 / 移除
packages/taro-cli/templates/default/package.json.tmpl, packages/taro-cli-convertor/package.json, packages/taro-platform-harmony-cpp/package.json, packages/taro-rn-style-transformer/package.json, packages/taro/package.json
postcss^8.4.38 升级为 ^8.5.6、或从 devDependencies 移至 peerDependencies、或在某些包中移除。
移除 Jest 配置 & 脚本迁移到 Vitest
Deleted: packages/babel-plugin-transform-solid-jsx/jest.config.js
Modified: packages/babel-plugin-transform-solid-jsx/package.json
删除包级 Jest 配置文件;将测试脚本由 Jest 改为 Vitest(例如 vitest run--coverage 等)。
测试文件 ESM 化
packages/babel-plugin-transform-solid-jsx/test/unique-transform.spec.js
将 CommonJS require 替换为 ESModule import,使用 fileURLToPath(import.meta.url) 重建 __filename/__dirname,并更新测试工具与插件导入路径(指向 ../dist/index.js 等)。
新增/修改 Vitest 配置
packages/babel-plugin-transform-solid-jsx/vitest.config.ts
新增并导出基于 defineConfig 的 Vitest 配置(启用 globals、设置 coverage include 等)。
移除 coverage.provider='istanbul'(多包)
packages/.../vitest.config.ts
packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/vitest.config.ts, packages/babel-plugin-transform-taroapi/vitest.config.ts, packages/babel-preset-taro/vitest.config.ts, packages/css-to-react-native/vitest.config.ts, packages/shared/vitest.config.ts, packages/taro-extend/vitest.config.ts, packages/taro-runtime/vitest.config.ts, packages/taro-framework-vue3/vitest.config.ts, packages/taro-rn-runner/vitest.config.ts, packages/taro-rn-style-transformer/vitest.config.ts
在多个包的 vitest.config.ts 中删除 test.coverage.provider = 'istanbul',仅保留 include,使用默认 coverage provider。
Vite API 与类型适配
packages/taro-vite-runner/src/h5/config.ts, packages/taro-vite-runner/src/h5/mpa.ts, packages/taro-vite-runner/src/mini/style.ts
serverOption.middlewareMode 增加类型/值检查(限定为字符串 `'ssr'
移除 devDependencies(局部)
packages/taro-vite-runner/package.json
从某些包中移除 rollupterservite 等 devDependencies。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Dev as 开发者
  participant Repo as 仓库
  participant CI as CI
  participant Jest as Jest(旧流程)
  participant Vitest as Vitest(新流程)

  rect rgb(250,240,240)
  Note over Dev,Jest: 迁移前(Jest)
  Dev->>Repo: 运行 npm test / CI 触发
  Repo->>Jest: 调用 jest(jest.config.js / package.json scripts)
  Jest->>CI: 运行测试并收集 coverage(istanbul)
  Jest-->>Repo: 返回测试结果与 coverage 报告
  end

  rect rgb(240,250,240)
  Note over Dev,Vitest: 迁移后(Vitest)
  Dev->>Repo: 运行 npm test / CI 触发
  Repo->>Vitest: 调用 vitest(vitest.config.ts / package.json scripts)
  Vitest->>CI: 运行测试并收集 coverage(v8 / 默认 provider)
  Vitest-->>Repo: 返回测试结果与 coverage 报告
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 分钟

  • 重点复核点:
    • 各包 vitest.config.ts 的 coverage include 是否完整、是否需要显式 provider;
    • ESM 测试文件在 CI 与低版本 Node 下兼容性(fileURLToPath(import.meta.url));
    • packages/taro-vite-runner 中从 enforce/transformorder/handler 的语义等价性及 middlewareMode 的边界处理;
    • package.json 依赖变动对工作区/peerDependencies 的影响(尤其 rollup/vite/postcss 位置变更)。

Possibly related PRs

Suggested reviewers

  • luckyadam
  • tutuxxx
  • yoyo837

诗歌

🐰 我从旧跑道轻轻一跃,
换上 Vitest 的新衣裳,
配置整齐脚本排好队,
ESM 小径在月光下闪亮,
小兔欢跳,测试一路歌唱 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清晰准确地反映了主要变化:将babel-plugin-transform-solid-jsx包的测试框架从Jest迁移到Vitest。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 82679d5 and 805cac6.

📒 Files selected for processing (2)
  • packages/taro-vite-runner/src/h5/mpa.ts (3 hunks)
  • packages/taro-vite-runner/src/mini/style.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/taro-vite-runner/src/mini/style.ts
  • packages/taro-vite-runner/src/h5/mpa.ts
⏰ 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). (1)
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc

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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2de8374 and 51ab1dc.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (5)
  • package.json (1 hunks)
  • packages/babel-plugin-transform-solid-jsx/jest.config.js (0 hunks)
  • packages/babel-plugin-transform-solid-jsx/package.json (1 hunks)
  • packages/babel-plugin-transform-solid-jsx/test/unique-transform.spec.js (1 hunks)
  • packages/babel-plugin-transform-solid-jsx/vitest.config.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/babel-plugin-transform-solid-jsx/jest.config.js
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: ianzone
Repo: NervJS/taro PR: 18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.265Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。
Learnt from: ianzone
Repo: NervJS/taro PR: 18150
File: packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json:2356-2356
Timestamp: 2025-08-25T22:16:50.118Z
Learning: The file `packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json` is auto-generated by the post-build script `packages/taro-platform-h5/scripts/post-build.mjs`, which copies content from `taro-platform-h5/dist/definition.json`. This file should not be manually edited.
📚 Learning: 2025-08-08T02:32:58.265Z
Learnt from: ianzone
Repo: NervJS/taro PR: 18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.265Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。

Applied to files:

  • package.json
  • packages/babel-plugin-transform-solid-jsx/package.json
  • packages/babel-plugin-transform-solid-jsx/vitest.config.ts
📚 Learning: 2025-08-25T22:16:50.118Z
Learnt from: ianzone
Repo: NervJS/taro PR: 18150
File: packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json:2356-2356
Timestamp: 2025-08-25T22:16:50.118Z
Learning: The file `packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json` is auto-generated by the post-build script `packages/taro-platform-h5/scripts/post-build.mjs`, which copies content from `taro-platform-h5/dist/definition.json`. This file should not be manually edited.

Applied to files:

  • packages/babel-plugin-transform-solid-jsx/package.json
  • packages/babel-plugin-transform-solid-jsx/vitest.config.ts
  • packages/babel-plugin-transform-solid-jsx/test/unique-transform.spec.js
🧬 Code graph analysis (1)
packages/babel-plugin-transform-solid-jsx/test/unique-transform.spec.js (1)
packages/babel-plugin-transform-solid-jsx/rollup.config.mjs (1)
  • __filename (6-6)
⏰ 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). (5)
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
  • GitHub Check: Build Rust WASM / stable - wasm32-wasi
🔇 Additional comments (3)
packages/babel-plugin-transform-solid-jsx/test/unique-transform.spec.js (1)

1-7: ESM 迁移实现正确

测试文件已正确从 CommonJS 迁移到 ESModule:

  • 使用 import 语法替代 require
  • 通过 fileURLToPath(import.meta.url) 正确实现了 ESM 环境下的 __filename__dirname polyfill
  • 导入路径使用了明确的 .js 扩展名,符合 ESM 规范

该实现模式与项目中其他文件(如 rollup.config.mjs)保持一致。

packages/babel-plugin-transform-solid-jsx/package.json (1)

25-27: 测试脚本已正确迁移到 Vitest

测试脚本从 Jest 迁移到 Vitest 的更改正确:

  • test:ci:从 jest --ci -i 改为 vitest run --coverage
  • test:从 jest --no-cache 改为 vitest run
  • test:coverage:从 jest --coverage --no-cache 改为 vitest run --coverage

符合项目的 monorepo 管理模式,Vitest 依赖在根目录管理。

packages/babel-plugin-transform-solid-jsx/vitest.config.ts (1)

1-13: 需要检查包的具体结构和文件内容,继续验证:

需要检查具体包的结构,执行脚本获取更多信息:

Based on my research, I've identified an issue with the original review comment. Vitest documentation shows that to include uncovered files in the report, you should configure coverage.include with a pattern that will pick your source files, typically using patterns like 'src/**/*.{ts,tsx}'.

The original review comment suggests changing include: ['index.js'] to include: ['dist/index.js'], but this contradicts Vitest best practices. Istanbul coverage tracking is done by instrumenting user's source files, meaning adding additional Javascript in user's files, not in build output.

However, I need to verify the actual package structure to determine the correct configuration. Let me check the package structure:

基于我的验证,原始审查评论存在不准确之处。Vitest 官方文档推荐配置 coverage.include 以选择源文件,例如 'src/**/*.{ts,tsx}' 这样的模式。Istanbul 代码覆盖工具通过对用户源文件进行代码插装来追踪覆盖率,而非构建后的文件。

原始审查评论建议"如果应该覆盖构建后的代码,可能需要改为 include: ['dist/index.js']"与 Vitest 最佳实践相悖。覆盖率配置应该指向源文件,让 Vitest 在测试运行时自动转译。

不要按照原始审查评论的建议改为 dist/ 路径。

根据 Taro monorepo 的标准实践和已发现的文件位置(根目录和 src/ 目录都有 index.js),当前配置应该要么保持 include: ['index.js'](如果入口点在根目录),要么改为 include: ['src/index.ts'] 或类似的源文件模式(如果源代码在 src/ 目录)。

Likely an incorrect or invalid review comment.

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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 51ab1dc and b3c696a.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • package.json (2 hunks)
  • packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/vitest.config.ts (0 hunks)
  • packages/babel-plugin-transform-solid-jsx/vitest.config.ts (1 hunks)
  • packages/babel-plugin-transform-taroapi/vitest.config.ts (0 hunks)
  • packages/babel-preset-taro/vitest.config.ts (0 hunks)
  • packages/css-to-react-native/vitest.config.ts (0 hunks)
  • packages/shared/vitest.config.ts (0 hunks)
  • packages/taro-extend/vitest.config.ts (0 hunks)
  • packages/taro-runtime/vitest.config.ts (0 hunks)
💤 Files with no reviewable changes (7)
  • packages/babel-preset-taro/vitest.config.ts
  • packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/vitest.config.ts
  • packages/css-to-react-native/vitest.config.ts
  • packages/taro-runtime/vitest.config.ts
  • packages/taro-extend/vitest.config.ts
  • packages/babel-plugin-transform-taroapi/vitest.config.ts
  • packages/shared/vitest.config.ts
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: ianzone
Repo: NervJS/taro PR: 18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.265Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。
Learnt from: ianzone
Repo: NervJS/taro PR: 17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 __tests__ 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
📚 Learning: 2025-08-08T02:32:58.265Z
Learnt from: ianzone
Repo: NervJS/taro PR: 18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.265Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。

Applied to files:

  • package.json
  • packages/babel-plugin-transform-solid-jsx/vitest.config.ts
📚 Learning: 2025-08-25T22:16:50.118Z
Learnt from: ianzone
Repo: NervJS/taro PR: 18150
File: packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json:2356-2356
Timestamp: 2025-08-25T22:16:50.118Z
Learning: The file `packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json` is auto-generated by the post-build script `packages/taro-platform-h5/scripts/post-build.mjs`, which copies content from `taro-platform-h5/dist/definition.json`. This file should not be manually edited.

Applied to files:

  • packages/babel-plugin-transform-solid-jsx/vitest.config.ts
⏰ 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). (5)
  • GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
  • GitHub Check: Build Rust WASM / stable - wasm32-wasi
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
🔇 Additional comments (1)
packages/babel-plugin-transform-solid-jsx/vitest.config.ts (1)

1-12: 配置结构正确,coverage 提供程序默认为 v8

Vitest 配置符合 v4 迁移要求:

  • 未指定 coverage.provider 时默认使用 v8,与根目录 package.json 中的 @vitest/coverage-v8 依赖一致
  • environment: 'jsdom' 适用于浏览器环境测试
  • globals: true 启用全局测试 API

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 (1)
packages/taro-vite-runner/src/mini/style.ts (1)

28-28: 类型断言安全。

使用类型断言访问内部 API 时,建议添加运行时检查以提高健壮性。

考虑应用以下改进:

-            const importedCss = (chunk as ExtendedOutputChunk).viteMetadata?.importedCss
+            const extendedChunk = chunk as ExtendedOutputChunk
+            const importedCss = extendedChunk.viteMetadata?.importedCss
             if (importedCss && importedCss.size > 0) {

这样可以提高代码可读性,便于调试。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f5a5737 and 4f423ff.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (5)
  • packages/taro-vite-runner/package.json (1 hunks)
  • packages/taro-vite-runner/src/h5/config.ts (2 hunks)
  • packages/taro-vite-runner/src/h5/mpa.ts (1 hunks)
  • packages/taro-vite-runner/src/mini/style.ts (2 hunks)
  • packages/taro/package.json (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/taro/package.json
🧰 Additional context used
🧠 Learnings (5)
📓 Common learnings
Learnt from: ianzone
Repo: NervJS/taro PR: 18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.265Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。
Learnt from: ianzone
Repo: NervJS/taro PR: 17842
File: packages/jest-helper/tsconfig.json:9-9
Timestamp: 2025-06-23T00:09:31.233Z
Learning: 在 jest-helper 包中,src 目录下没有 __tests__ 测试目录,只包含 resolver.ts、sequencer.ts 和 snapshot 目录。不要假设包中存在测试目录结构。
📚 Learning: 2025-08-25T22:16:50.118Z
Learnt from: ianzone
Repo: NervJS/taro PR: 18150
File: packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json:2356-2356
Timestamp: 2025-08-25T22:16:50.118Z
Learning: The file `packages/babel-plugin-transform-taroapi/tests/__mocks__/h5-definition.json` is auto-generated by the post-build script `packages/taro-platform-h5/scripts/post-build.mjs`, which copies content from `taro-platform-h5/dist/definition.json`. This file should not be manually edited.

Applied to files:

  • packages/taro-vite-runner/src/h5/mpa.ts
  • packages/taro-vite-runner/src/h5/config.ts
📚 Learning: 2025-08-08T02:32:58.265Z
Learnt from: ianzone
Repo: NervJS/taro PR: 18146
File: packages/babel-plugin-transform-react-jsx-to-rn-stylesheet/package.json:12-14
Timestamp: 2025-08-08T02:32:58.265Z
Learning: 在 Taro 项目的 pnpm 工作区中,Vitest 相关依赖(vitest 和 vitest/coverage-istanbul)被管理在根目录的 package.json 中,而不是各个子包的 devDependencies 中。这是 monorepo 中依赖提升的标准做法。

Applied to files:

  • packages/taro-vite-runner/package.json
📚 Learning: 2025-05-25T18:02:31.387Z
Learnt from: ianzone
Repo: NervJS/taro PR: 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-vite-runner/package.json
  • packages/taro-vite-runner/src/h5/config.ts
📚 Learning: 2025-09-05T18:40:45.775Z
Learnt from: ianzone
Repo: NervJS/taro PR: 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 别名配置。

Applied to files:

  • packages/taro-vite-runner/package.json
⏰ 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). (5)
  • GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
  • GitHub Check: Build Rust WASM / stable - wasm32-wasi
  • GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
  • GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
  • GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
🔇 Additional comments (4)
packages/taro-vite-runner/package.json (1)

60-60: 依赖管理合理。

terser 保留在 devDependencies 中是合理的,因为:

  • Line 21 的 @rollup/plugin-terser 依赖于 terser
  • vite 已正确移至 peerDependencies(line 65)

这符合 monorepo 中的依赖提升策略。根据已有的学习记录,Vitest 相关依赖由根目录统一管理。

packages/taro-vite-runner/src/h5/config.ts (2)

131-131: 类型守卫增强了类型安全。

新增的运行时类型检查确保 middlewareMode 只在值为 'ssr''html' 字符串时才被赋值,有效防止了类型错误和潜在的运行时异常。


266-276: transformIndexHtml API 迁移已正确适配 Vite ^4,两处实现保持一致。

Vite v4 及更高版本(包括 v7)使用 orderhandler 替代了 Vite v3 的 enforcetransform 方式。代码审查中的变更符合此要求:

  • peerDependencies 声明 vite: ^4
  • ✓ config.ts(266-276 行):order: 'pre' + handler(html) — Vite v4+ 标准 API
  • ✓ mpa.ts(140-161 行):order: 'pre' + handler(html, ctx) — 两处实现一致

额外发现:这两个文件中还存在其他使用旧 API 的钩子,未进行迁移:

  • config.ts 第 175 行:插件对象上的 enforce: 'pre'(属于 taro:vite-h5-config 插件)
  • mpa.ts 第 72 行:插件对象上的 enforce: 'pre'(属于 taro:vite-h5-mpa 插件)

这些可能是独立的迁移工作项,建议确认是否需要同步更新以保持代码现代化一致性。

packages/taro-vite-runner/src/h5/mpa.ts (1)

140-161: API 迁移保持一致性。

transformIndexHtml 钩子的更新与 packages/taro-vite-runner/src/h5/config.ts (lines 266-276) 中的变更保持一致:

  • enforce: 'pre'order: 'pre'
  • transformhandler

这确保了整个代码库中 Vite API 使用的统一性。请参考前面对 config.ts 的审查意见,确认此 API 变更与目标 Vite 版本兼容。

Comment on lines 7 to 12
// 扩展 OutputChunk 类型以包含 viteMetadata(Vite 内部使用)
interface ExtendedOutputChunk extends OutputChunk {
viteMetadata?: {
importedCss?: Set<string>
}
}
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

验证 Vite 内部 API 的稳定性。

新增的 ExtendedOutputChunk 类型用于访问 Vite 的内部属性 viteMetadata,这是一个未公开的内部 API。虽然当前实现可以工作,但存在以下风险:

  • Vite 内部 API 可能在不同版本间发生变化,导致代码失效
  • 缺乏类型安全保障和官方文档支持

请运行以下脚本验证 Vite 文档中是否有公开的 API 可以替代访问 viteMetadata.importedCss

如果这是必须使用的内部 API,建议:

  1. 添加版本兼容性测试
  2. 在代码注释中明确标注 Vite 版本依赖
  3. 考虑添加防御性检查或降级方案

🌐 Web query:

Vite 4.x OutputChunk viteMetadata importedCss public API documentation

💡 Result:

Short answer: viteMetadata.importedCss is an internal Vite-added property on Rollup OutputChunk (not a Rollup public API). During Vite 4 builds Vite attaches a viteMetadata object to chunks that can include an importedCss array (the CSS asset ids/files associated with that chunk). It's used by Vite internals and many plugins to find which CSS files belong to a chunk (e.g. for injecting CSS in library builds), but it is not documented as a stable public API in the official Vite docs — treat it as internal and likely to change between versions. [1][2][3]

Sources

  • Vite source (build plugin code showing viteMetadata usage). [1]
  • Example plugin README referencing viteMetadata.importedCss and how plugins rely on it. [2]
  • Vite 4 docs (Plugin API / build docs — Vite docs do not list viteMetadata as a public Rollup API). [3]

If you want, I can paste the exact source lines from the Vite repo (file+line) and show a quick example of a plugin using chunk.viteMetadata.importedCss.


我需要检查代码库中使用的 Vite 版本以及是否已有防御性措施。


🏁 Script executed:

#!/bin/bash
# 检查 Vite 版本依赖和相关防御性代码
echo "=== 检查 Vite 版本依赖 ==="
fd -e json "package.json" -x cat {} \; | grep -A 2 -B 2 '"vite"' | head -20

echo ""
echo "=== 检查 style.ts 文件及周围代码 ==="
fd -e ts -p "style\.ts" packages/taro-vite-runner/src/mini | head -5

echo ""
echo "=== 检查是否有版本检查或防御性代码 ==="
rg -n "viteMetadata|importedCss" --type=ts -C 5

Length of output: 3930


需要添加 Vite 版本兼容性声明和更完善的防御机制。

Vite 的 viteMetadata.importedCss 是内部未公开 API,未在官方文档中列出为稳定公开 API。

虽然代码已使用了可选链操作符和空值检查进行基本防御,但存在以下不足:

  1. 缺少版本注释:未在代码中明确标注 Vite 4.2.0+ 的版本依赖
  2. 无版本检查逻辑:缺乏运行时的 Vite 版本验证
  3. 未来升级风险:Vite 5.x+ 可能改变或移除此内部 API

建议改进:

  • 在接口/函数顶部添加注释明确标注 // 依赖 Vite 4.x 内部 API, 升级 Vite 5+ 时需重评
  • 添加运行时版本检查或 try-catch 防御
  • 记录备选方案以便未来迁移
🤖 Prompt for AI Agents
In packages/taro-vite-runner/src/mini/style.ts around lines 7 to 12, the
ExtendedOutputChunk declaration relies on Vite's internal
viteMetadata.importedCss without documenting or defending against breaking
changes; add a top-of-file comment like "// 依赖 Vite 4.x 内部 API, 升级 Vite 5+
时需重评", implement a runtime guard that detects Vite's version (read from
process.env.npm_package_dependencies or require('vite/package.json').version)
and conditionally uses viteMetadata only for >=4.2.0 (fallback otherwise), wrap
accesses to viteMetadata.importedCss in try-catch with a clear logger
warning/information about falling back, and include a short TODO note describing
alternative approaches for Vite 5+ migration.

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.

1 participant