Skip to content

refactor(uniapp): 自实现配置解析并补充单测#34

Merged
Vanisper merged 2 commits into
mainfrom
refactor/parse-uniapp-json
May 8, 2026
Merged

refactor(uniapp): 自实现配置解析并补充单测#34
Vanisper merged 2 commits into
mainfrom
refactor/parse-uniapp-json

Conversation

@Vanisper
Copy link
Copy Markdown
Member

@Vanisper Vanisper commented May 7, 2026

Summary by CodeRabbit

发布说明

  • New Features

    • 增加 UniApp/小程序 JSON 解析与平台条件预处理支持,改进对分包与页面声明的处理与校验。
  • Tests

    • 引入并配置 Vitest 测试,添加针对 JSON 解析与分包逻辑的单元测试。
  • Chores

    • 新增/更新测试脚本与开发依赖以支持运行测试。
    • 更新编辑器配置以改进 TypeScript SDK 路径解析。
    • 调整工作区包配置以更新部分依赖条目。

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5e5fbb9f-1110-4717-8a77-74b8bf4942c7

📥 Commits

Reviewing files that changed from the base of the PR and between 5202dd5 and 7a1a7a5.

📒 Files selected for processing (1)
  • src/utils/uniapp/config.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/utils/uniapp/config.ts

Walkthrough

本 PR 将 UniApp JSON 条件编译解析移入本地工具:新增 src/utils/uniapp/config.ts(含 manifest/pages 解析、条件指令预处理、布尔表达式求值、注释/逗号清理等),添加 Vitest 配置与测试用例,并更新相关导出、插件导入与开发配置。

UniApp JSON 解析工具提取

Layer / File(s) Summary
测试框架与依赖配置
vitest.config.ts, package.json, pnpm-workspace.yaml
配置 Vitest、添加 test/test:watch 脚本并在 workspace 中调整 catalog 条目。
开发工具配置
.vscode/settings.json, examples/vue3+vite+ts/package.json
将 VS Code 的 TypeScript SDK key 从 typescript.tsdk 切换为 js/ts.tsdk.path,并调整示例项目的 devDependencies 列表。
类型定义与契约
src/utils/uniapp/config.ts
新增 JSON/页面/子包类型与解析选项接口。
核心 JSON 解析实现
src/utils/uniapp/config.ts
实现 parseManifestJson/parseManifestJsonOnce、parseMiniProgramPagesJson、条件指令预处理、布尔表达式求值、注释与尾逗号清理及路径归一化与验证。
公开 API 导出
src/utils/uniapp/index.ts
通过 export * from './config' 将新解析工具导出。
插件导入更新
src/plugin/subpackages-optimization.ts
将解析函数的导入源从 @dcloudio/uni-cli-shared 改为本地 ../utils
测试覆盖
src/utils/uniapp/config.test.ts
新增 Vitest 测试套件,覆盖缓存、条件编译逻辑、子包处理与重复路径检测。

总体概览

本 PR 将 UniApp JSON 解析功能从外部依赖迁移到内部工具库。新增 src/utils/uniapp/config.ts 模块,提供带条件编译支持的 manifest.json 和 pages.json 解析能力,包含平台特定的指令处理、布尔表达式求值和 JSON 预处理功能。建立 Vitest 测试框架并提供完整的测试覆盖。

变更说明

UniApp JSON 解析工具提取

层级 / 文件 说明
测试框架与依赖配置
vitest.config.ts, package.json, pnpm-workspace.yaml
配置 Vitest 测试环境,添加 test/test:watch 脚本,更新工作区依赖目录。
开发工具配置
.vscode/settings.json, examples/vue3+vite+ts/package.json
更新 VS Code TypeScript SDK 配置和示例项目依赖。
类型定义与契约
src/utils/uniapp/config.ts
定义 JsonObject、MiniProgramAppJson、SubPackage 等类型和解析选项接口。
核心 JSON 解析实现
src/utils/uniapp/config.ts
实现 parseManifestJson/parseManifestJsonOnce、parseMiniProgramPagesJson,以及条件编译预处理、布尔表达式求值、JSON 注释清理等辅助函数。
公开 API 导出
src/utils/uniapp/index.ts
通过 export * from './config' 重新导出新增工具函数。
插件导入更新
src/plugin/subpackages-optimization.ts
将 parseManifestJsonOnce 和 parseMiniProgramPagesJson 导入源从 @dcloudio/uni-cli-shared 改为本地 ../utils 模块。
测试覆盖
src/utils/uniapp/config.test.ts
建立 Vitest 测试套件,验证 manifest.json 解析缓存、pages.json 条件编译、子包结构处理、条件表达式求值和数据验证行为。

📝 代码审查工作量估计

🎯 3 (中等) | ⏱️ ~20 分钟

🐰 诗意庆贺

兔子欣然跳跃,看着 JSON 解析工具,
从外部引入到内部归家,条件编译如魔法般运转,
Vitest 测试帷幕拉开,每行代码都闪闪发光,
本地工具库愈发充实,工程质量稳步向前! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清晰准确地总结了主要变更:自行实现UniApp配置解析功能并添加单元测试覆盖。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/parse-uniapp-json

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.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 7, 2026

Open in StackBlitz

commit: 7a1a7a5

Copy link
Copy Markdown

@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

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/utils/uniapp/config.ts`:
- Around line 41-44: The current check returns an empty object whenever
process.env.UNI_COMPILE_TARGET is truthy, which can hide real missing-manifest
errors; change the logic in the block that checks fs.existsSync(filename) so it
only returns {} for an explicit whitelist of compile targets (e.g.
process.env.UNI_COMPILE_TARGET === 'ext-api' or membership in a small allowed
array) and otherwise throw or re-raise an error for the missing manifest; update
the condition that references process.env.UNI_COMPILE_TARGET and the early
return so only whitelisted targets short-circuit to {} while all other cases
surface the missing-file error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1a655d65-5509-4bfe-a5bc-9b7d13683633

📥 Commits

Reviewing files that changed from the base of the PR and between fd7d363 and 5202dd5.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • .vscode/settings.json
  • examples/vue3+vite+ts/package.json
  • package.json
  • pnpm-workspace.yaml
  • src/plugin/subpackages-optimization.ts
  • src/utils/uniapp/config.test.ts
  • src/utils/uniapp/config.ts
  • src/utils/uniapp/index.ts
  • vitest.config.ts
💤 Files with no reviewable changes (2)
  • examples/vue3+vite+ts/package.json
  • pnpm-workspace.yaml

Comment on lines +41 to +44
if (!fs.existsSync(filename)) {
// 特殊编译目标可能没有完整项目配置,缺失 manifest 时按空配置处理
if (process.env.UNI_COMPILE_TARGET)
return {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

收窄 manifest.json 缺失时的兜底条件,避免误吞配置错误

Line 43 当前只要 UNI_COMPILE_TARGET 有值就返回空对象,范围过大;这会在非特殊目标下把真实配置问题静默掩盖。建议仅对白名单目标(如 ext-api)兜底,其余情况继续抛错。

建议修改
   if (!fs.existsSync(filename)) {
     // 特殊编译目标可能没有完整项目配置,缺失 manifest 时按空配置处理
-    if (process.env.UNI_COMPILE_TARGET)
+    if (process.env.UNI_COMPILE_TARGET === 'ext-api')
       return {}
 
     throw new Error(`[bundle-optimizer] manifest.json not found: ${filename}`)
   }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utils/uniapp/config.ts` around lines 41 - 44, The current check returns
an empty object whenever process.env.UNI_COMPILE_TARGET is truthy, which can
hide real missing-manifest errors; change the logic in the block that checks
fs.existsSync(filename) so it only returns {} for an explicit whitelist of
compile targets (e.g. process.env.UNI_COMPILE_TARGET === 'ext-api' or membership
in a small allowed array) and otherwise throw or re-raise an error for the
missing manifest; update the condition that references
process.env.UNI_COMPILE_TARGET and the early return so only whitelisted targets
short-circuit to {} while all other cases surface the missing-file error.

@Vanisper Vanisper merged commit 6393c8d into main May 8, 2026
3 checks passed
@Vanisper Vanisper deleted the refactor/parse-uniapp-json branch May 8, 2026 09:59
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