-
Notifications
You must be signed in to change notification settings - Fork 7
fix: 修复多平台运行读取不到 pages.json 的问题 #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
总体说明此更改通过集成新的Vite插件和配置调整,引入了对Uni小程序子包支持。更新了依赖项,重构了页面JSON解析逻辑,并添加了TypeScript类型声明和新的页面组件文件。 变更内容
预估代码审查工作量🎯 3 (中等) | ⏱️ ~20-25 分钟 需要特别关注的区域:
诗歌
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
|
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
examples/uni-pages.d.ts (1)
17-19: 空接口可以优化Biome 正确指出空接口等同于
{}。虽然这是生成的文件,但建议在vite-plugin-uni-pages插件中改进生成逻辑。根据 Biome 的建议,可以改为:
-interface SwitchTabOptions { - -} +type SwitchTabOptions = Record<string, never>;或者如果未来可能添加属性:
-interface SwitchTabOptions { - -} +interface SwitchTabOptions { + // Reserved for future use +}不过由于这是生成的文件,该改进应该在插件源码中实现,而不是手动修改此文件。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (8)
.npmrc(1 hunks)examples/package.json(1 hunks)examples/src/pages-sub/index.vue(1 hunks)examples/src/pages.json(1 hunks)examples/uni-pages.d.ts(1 hunks)examples/vite.config.ts(2 hunks)package.json(1 hunks)src/utils.ts(2 hunks)
🧰 Additional context used
🪛 Biome (2.1.2)
examples/src/pages.json
[error] 3-3: Expected an array, an object, or a literal but instead found '// GENERATED BY UNI-PAGES, PLATFORM: H5 || MP-WEIXIN'.
Expected an array, an object, or a literal here.
(parse)
[error] 4-7: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 7-7: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 8-11: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 11-11: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 12-15: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 15-16: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 17-20: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 21-22: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 23-23: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 23-23: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 27-27: Expected an array, an object, or a literal but instead found '// GENERATED BY UNI-PAGES, PLATFORM: H5 || MP-WEIXIN'.
Expected an array, an object, or a literal here.
(parse)
[error] 23-26: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 27-27: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
[error] 28-31: End of file expected
Use an array for a sequence of values: [1, 2]
(parse)
examples/uni-pages.d.ts
[error] 16-19: An empty interface is equivalent to {}.
Safe fix: Use a type alias instead.
(lint/suspicious/noEmptyInterface)
🔇 Additional comments (10)
examples/src/pages.json (3)
16-21: 条件编译语法的正确使用
// #ifdef H5和// #endif是 uni-app 的条件编译语法,用于平台特定的配置。这解释了为什么需要使用官方的parseMiniProgramPagesJson解析器而不是普通的 JSON 解析器。Biome 报告的 JSON 语法错误是误报,因为 uni-app 的 pages.json 支持 JSONC 格式和条件编译注释。
建议在 Biome 配置中排除
pages.json文件的检查:{ "linter": { "ignore": ["**/pages.json"] } }
23-34: subPackages 配置正确实现了分包功能新增的
subPackages配置正确定义了pages-sub子包,这与:
examples/vite.config.ts中的UniPages({ subPackages: ['src/pages-sub'] })配置一致examples/src/pages-sub/index.vue子包页面相对应examples/uni-pages.d.ts中的"/pages-sub/index"类型定义匹配实现符合 uni-app 分包规范。
3-3: No action needed. The fileexamples/src/pages.jsonis a source configuration file required by the build process and should remain committed to version control. It does not contain auto-generated content markers and is not produced by any generation plugin.examples/uni-pages.d.ts (2)
1-4: 生成的类型定义文件配置正确禁用 ESLint、Prettier 和 TypeScript 检查对于生成的文件是正确的做法,避免了不必要的代码风格警告。
6-10: 类型定义正确反映了页面路由结构
_LocationUrl联合类型准确地包含了所有页面路径:
- 主包页面:
/pages/index,/pages/about,/pages/excluded- 子包页面:
/pages-sub/index这与
pages.json中的配置完全一致,提供了类型安全的路由跳转。examples/src/pages-sub/index.vue (1)
1-3: 简洁的子包页面组件这是一个用于演示分包功能的简单组件,内容足够用于测试
pages-sub子包的加载和路由跳转功能。examples/vite.config.ts (2)
4-4: 正确引入 UniPages 插件引入
@uni-helper/vite-plugin-uni-pages插件是实现 uni-app 分包功能的正确方式,该插件能够:
- 自动解析 pages.json 配置
- 生成类型定义文件(uni-pages.d.ts)
- 支持子包(subPackages)配置
13-15: Code snippet in review does not match actual file contentThe review comment references a
UniPagesplugin configuration at lines 13-15 ofexamples/vite.config.ts, but the actual file containsUniKuRootplugin configuration at those lines. The claimed plugin order (UniPlatform → UniPages → UniKuRoot → Uni) does not match the actual configuration (UniPlatform → UniKuRoot → Uni).Additionally,
examples/src/pages.jsondoes not contain anysubPackagesconfiguration. TheUniPagesplugin is not referenced anywhere in the codebase.Likely an incorrect or invalid review comment.
src/utils.ts (2)
29-31: Simplified parsing logic improves code qualityUsing
parseMiniProgramPagesJsoninstead of manual JSONC parsing is the correct approach for better handling of:
- Platform conditional compilation (
// #ifdef,// #endif)- Unified pages and subPackages parsing
- Various edge cases
The code logic is concise and clear. Verify that the parser correctly handles all page paths including subpackages by reviewing the function's return structure and testing with sample configurations containing subpackages.
27-27: Line number correction needed: Review points to line 27, but the actual code is at line 109The concern about
process.env.UNI_PLATFORMbeingundefinedis valid in principle, but the current implementation includes a guard:normalizePlatformPathis only called whenhasPlatformPluginistrue(line 89 in src/index.ts), and the plugin setsUNI_PLATFORMwhen detected.However, adding a defensive default value is still reasonable for robustness:
-const platform = process.env.UNI_PLATFORM +const platform = process.env.UNI_PLATFORM || 'h5'This prevents potential edge cases where the guard logic might fail or the function is called unexpectedly, causing the string template to convert
undefinedto"undefined"and produce incorrect path matching.
#32
Summary by CodeRabbit
发布说明
新增功能
更新
✏️ Tip: You can customize this high-level summary in your review settings.