weapp-vite-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseweapp-vite-best-practices
weapp-vite最佳实践
Overview
概述
Build and maintain weapp-vite projects with stable conventions first, then tune performance and build behavior. Prefer minimal config that matches project structure.
首先以稳定的规范构建和维护weapp-vite项目,然后再优化性能和构建行为。优先选择与项目结构匹配的极简配置。
Workflow
工作流
- Confirm baseline
- Use Node and consistent pnpm.
^20.19.0 || >=22.12.0 - Ensure WeChat DevTools service port is enabled before using workflows.
--open - Verify matches real source root (
weapp.srcRootorsrc).miniprogram
- Define project structure
- Keep pages under and subpackages under
src/pages/**.src/packages/** - Keep reusable components in and shared logic in
src/components/**orsrc/shared/**.src/utils/** - Prefer explicit config layering: Vite in , mini-program specifics in
vite.config.tsfield.weapp
- Apply high-value defaults
- Enable when using convention-based pages.
weapp.autoRoutes - Keep auto component import on by default; only add custom when needed.
globs/resolvers - Use Script Setup JSON macros for App/Page/Component config instead of scattered manual JSON edits.
- Prefer matching JSON macros by file type: /
defineAppJson/definePageJson.defineComponentJson - Keep only for legacy compatibility.
<json>
- Choose subpackage strategy intentionally
- Use normal subpackages for shared build context.
- Use independent subpackages only when strict isolation is needed.
- Choose by goal:
weapp.chunks.sharedStrategy- : better subpackage first-open experience.
duplicate - : better total package deduplication/control.
hoist
- Keep build and CI deterministic
- Use stable scripts (,
dev,build, optionalopen).analyze - In CI, separate compile and IDE/preview/upload steps.
- For CLI automation, prefer and non-interactive mode.
weapp-ide-cli
- Debug with framework hooks
- Use for missing output and resolution issues.
weapp.debug.watchFiles/resolveId/load - Use (plugin hook timings) for slow build diagnosis.
inspect - Validate route/component generation artifacts (, auto import outputs).
typed-router.d.ts
- 确认基准环境
- 使用 Node 版本和统一的 pnpm 包管理器。
^20.19.0 || >=22.12.0 - 在使用工作流前,确保微信开发者工具的服务端口已启用。
--open - 验证配置与实际源码根目录(
weapp.srcRoot或src)一致。miniprogram
- 定义项目结构
- 将页面文件放在目录下,分包文件放在
src/pages/**目录下。src/packages/** - 将可复用组件放在目录下,共享逻辑放在
src/components/**或src/shared/**目录下。src/utils/** - 优先采用明确的配置分层:Vite配置写在中,小程序专属配置放在
vite.config.ts字段中。weapp
- 应用高价值默认配置
- 当采用约定式页面结构时,启用配置。
weapp.autoRoutes - 保持自动组件导入默认开启;仅在需要时添加自定义。
globs/resolvers - 使用Script Setup JSON宏定义App/Page/Component配置,替代分散的手动JSON编辑。
- 根据文件类型匹配对应的JSON宏:/
defineAppJson/definePageJson。defineComponentJson - 仅在兼容旧代码时保留纯文件。
<json>
- 谨慎选择分包策略
- 对于共享构建上下文的场景,使用普通分包。
- 仅在需要严格隔离的场景下使用独立分包。
- 根据目标选择:
weapp.chunks.sharedStrategy- :优化分包首次打开体验。
duplicate - :优化整体包体积去重与管控。
hoist
- 确保构建与CI的确定性
- 使用稳定的脚本命令(、
dev、build,可选open)。analyze - 在CI流程中,分离编译步骤与IDE预览/上传步骤。
- 对于CLI自动化,优先使用和非交互模式。
weapp-ide-cli
- 借助框架钩子调试
- 当遇到输出缺失或解析问题时,使用进行调试。
weapp.debug.watchFiles/resolveId/load - 当构建缓慢时,使用(插件钩子计时)诊断问题。
inspect - 验证路由/组件生成的产物(、自动导入输出结果)。
typed-router.d.ts
Guardrails
注意事项
- Avoid customizing config before confirming and output roots are correct.
srcRoot - Avoid mixing many advanced chunk overrides early; start from default strategy and add targeted rules.
- Avoid treating mini-program config as web-only conventions; keep , subpackage roots, and JSON semantics explicit.
usingComponents
- 在确认和输出根目录配置正确前,避免自定义其他配置。
srcRoot - 初期避免混合使用过多高级代码分割覆盖规则;从默认策略开始,逐步添加针对性规则。
- 不要将小程序配置视为纯Web约定;保持、分包根目录和JSON语义的明确性。
usingComponents
Completion Checklist
完成检查清单
- has clear
vite.config.tssection with minimal required options.weapp - source-of-truth strategy is clear (manual or auto routes).
pages/subPackages - Component registration strategy is consistent (auto import + resolver policy documented).
- Subpackage/chunk strategy is chosen and justified.
- Dev/CI workflow is repeatable and does not depend on manual IDE steps.
- 中有清晰的
vite.config.ts配置段,仅包含必要的最少选项。weapp - 的数据源策略明确(手动配置或自动路由)。
pages/subPackages - 组件注册策略一致(自动导入 + 解析器策略已文档化)。
- 已选择并论证分包/代码分割策略的合理性。
- 开发/CI工作流可重复,不依赖手动IDE操作。
References
参考资料
references/config-playbook.mdreferences/debug-playbook.md
references/config-playbook.mdreferences/debug-playbook.md