Commit 1781a4ca by 张立顺

docs: gate main package size optimization

1 parent 87171bcd
# 主包体积优化接口清单
本迁移不改变后端 API、请求参数或响应契约,只改变静态资源物理归属:
| 接口/边界 | 迁移前 | 迁移后 |
|---|---|---|
| 学习图表 | `utils/wxcharts.js` | `packageStudy/utils/wxcharts.js` |
| 工单二维码 | `utils/lib/wxqrcode.js` | `tabWorkOrder/utils/wxqrcode.js` |
| 督导表单 | 根全局组件 | `packageWorkspace` 本地组件 |
| 地区数据 | 根 npm 依赖并挂载 `wx.$areaList` | `packageWorkspace/utils/area-data.js` 本地数据 |
| workspace 筛选 | 根组件 | `packageWorkspace/components` |
| 工单独占组件 | 根组件 | `tabWorkOrder/components` |
| 主包预算 | 无 | `npm run size:main`,最大 2,000,000 字节 |
{
"id": "main-package-size-optimization",
"title": "微信小程序主包体积优化",
"sourceProject": "regional-assistant@87171bc",
"affectedPaths": [
"app.js",
"app.json",
"package.json",
"package-lock.json",
"yarn.lock",
"miniprogram_npm",
"utils",
"components",
"packageStudy",
"packageWorkspace",
"tabWorkOrder",
"scripts",
"tests",
"docs"
],
"interfaces": [
"npm run size:main",
"npm run knowledge:build",
"npm test",
"node scripts/wx-upload-guard.js"
],
"tests": [
{
"id": "migration-readiness-contract",
"file": "tests/migration-readiness/checkMigrationReadiness.test.mjs",
"level": "contract",
"expectedBefore": "pass"
},
{
"id": "main-package-budget-contract",
"file": "tests/package-size/mainPackageBudget.test.mjs",
"level": "integration",
"expectedBefore": "fail"
}
],
"baseline": {
"command": "git rev-parse --short 87171bc",
"result": "87171bc"
},
"apiInventory": "docs/migrations/main-package-size-optimization/api-inventory.md",
"acceptance": "docs/migrations/main-package-size-optimization/test-acceptance.md"
}
# 主包体积优化验收
| ID | 验收项 | Pre | Post |
|---|---|---|---|
| SIZE-001 | 物理主包不超过 2,000,000 字节 | fail: 2,360,562 bytes | pending |
| SIZE-002 | wxcharts 仅位于 packageStudy | fail | pending |
| SIZE-003 | wxqrcode 仅位于 tabWorkOrder | fail | pending |
| SIZE-004 | 地区数据与 GLCustForm 仅位于 packageWorkspace | fail | pending |
| SIZE-005 | 工单独占根组件全部迁入 tabWorkOrder | fail | pending |
| SIZE-006 | workspace 独占筛选组件迁入分包 | fail | pending |
| SIZE-007 | 上传依赖、路由和 API 闭包 | pass | pending |
| SIZE-008 | 全仓离线测试 | pass | pending |
# 主包体积优化实施计划
1. 创建迁移 manifest、验收矩阵和 RED 预算/边界测试,通过 `check:migration --phase pre`。
2. 实现通用主包物理字节分析 CLI;在当前布局上确认预算测试失败。
3. 将 `wxcharts` 移到 `packageStudy/utils`;删除无调用的根二维码包装并将 `wxqrcode` 移到 `tabWorkOrder/utils`。
4. 将 `GLCustForm`、`FilterBar`、`TopToolbarPopup` 移到 `packageWorkspace/components`,本地注册消费者;vendoring 地区数据并移除根 npm 依赖/全局挂载。
5. 将 CarBrands、CustomerInfo、DateSelect、Goods、InventoryInfo、Qrbox、RemarkPopup、SkuGoods、TechnicianSelectPopup、tipsBottom、areaOperationsModal 移到 `tabWorkOrder/components`,更新 JSON 注册。
6. 重建知识图谱,运行预算、闭包、工单、知识和全仓离线测试;用 Node 24 执行上传 guard。
7. 更新验收证据,将 manifest 的 RED 基线转为 pass,执行 `check:migration --phase post` 并提交。
# 微信小程序主包体积优化设计
## 目标
将当前物理主包从 2,360,562 字节降到 2,000,000 字节以内,为微信 2 MiB 上传限制保留约 95 KiB 以上余量,并建立可重复的预算与跨包边界检查。
## 已确认事实
- `utils/wxcharts.js` 仅由 `packageStudy/pages/aiCheck/result/result.js` 使用,73,479 字节。
- `utils/lib/wxqrcode.js` 的有效消费者仅在 `tabWorkOrder`;根工具中的 `handleCopy` 无调用方,47,232 字节可移出主包。
- `components/GLCustForm` 仅由 `packageWorkspace/pages/supervisorDaily` 使用;`@vant/area-data` 仅为该组件提供地区数据,却从 `app.js` 全局预加载,主包产物 105,578 字节。
- `FilterBar` 与 `TopToolbarPopup` 仅被 `packageWorkspace` 使用,共 18,394 字节。
- 11 个根组件仅被 `tabWorkOrder` 分包使用,共 117,201 字节。
- `project.private.config.json` 的 `bigPackageSizeSupport` 只影响本地预览能力,不能替代正式上传的 2 MiB 限制,因此不作为验收依据。
## 推荐实现
1. 将学习图表、二维码实现和分包独占组件物理迁移到各自分包,更新相对注册/导入路径。
2. 删除无调用方的根工具二维码包装,避免主包工具通过静态 import 拉回二维码实现。
3. 将 `GLCustForm`、`FilterBar`、`TopToolbarPopup` 移入 `packageWorkspace`;组件本地导入 vendored `@vant/area-data` 1.5.2 数据,并保留 MIT 许可。
4. 从根依赖清单和 `miniprogram_npm` 删除 `@vant/area-data`,同时移除 `app.js` 的全局地区数据挂载和 `app.json` 的全局表单注册。
5. 新增主包分析 CLI,以 `app.json` 分包根和 `project.config.json` 上传排除为边界,预算固定为 2,000,000 字节。
## 参考实践
- 微信官方 demo 使用 `subpackages` 隔离非首屏页面:[miniprogram-demo/app.json](https://github.com/wechat-miniprogram/miniprogram-demo/blob/master/miniprogram/app.json)
- 微信官方 AI demo 同样按功能拆分分包:[ai-mode-demo/app.json](https://github.com/wechat-miniprogram/ai-mode-demo/blob/master/app.json)
- 官方瘦身工具强调从依赖闭包识别无效/跨边界依赖:[miniprogram-slim deps](https://github.com/wechat-miniprogram/miniprogram-slim/blob/master/docs/deps.md?plain=1)
## 风险与回归
- 页面/组件 JSON 相对路径错误会导致运行时找不到组件,必须由依赖闭包和真实路由测试覆盖。
- vendored 地区数据必须与 1.5.2 的 `areaList` 结构一致,并保留许可。
- 根组件中仍被主包页面消费的共享组件不得移动;本次只迁移已证明为单一分包消费者的目录。
- 体积脚本是保守物理边界,不依赖 `ignoreUploadUnusedFiles` 的不可观察优化;真实上传前仍需用 Node 24 上传 guard 和微信开发者工具复核。
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath, pathToFileURL } from 'node:url'
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const analyzerPath = path.join(projectRoot, 'scripts/main-package-size.mjs')
const budgetBytes = 2_000_000
async function loadAnalyzer() {
assert.equal(fs.existsSync(analyzerPath), true, 'main package analyzer must exist')
return import(pathToFileURL(analyzerPath))
}
test('physical main package stays below the guarded upload budget', async () => {
const { analyzeMainPackage } = await loadAnalyzer()
const first = analyzeMainPackage({ rootDir: projectRoot })
const second = analyzeMainPackage({ rootDir: projectRoot })
assert.equal(first.totalBytes <= budgetBytes, true, JSON.stringify(first))
assert.deepEqual(first, second)
assert.ok(first.totalFiles > 0)
assert.equal(first.budgetBytes, budgetBytes)
})
test('heavy libraries and single-subpackage components are physically isolated', () => {
const movedPaths = [
['utils/wxcharts.js', 'packageStudy/utils/wxcharts.js'],
['utils/lib/wxqrcode.js', 'tabWorkOrder/utils/wxqrcode.js'],
['components/GLCustForm', 'packageWorkspace/components/GLCustForm'],
['components/FilterBar', 'packageWorkspace/components/FilterBar'],
['components/TopToolbarPopup', 'packageWorkspace/components/TopToolbarPopup'],
['components/CarBrands', 'tabWorkOrder/components/CarBrands'],
['components/CustomerInfo', 'tabWorkOrder/components/CustomerInfo'],
['components/DateSelect', 'tabWorkOrder/components/DateSelect'],
['components/Goods', 'tabWorkOrder/components/Goods'],
['components/InventoryInfo', 'tabWorkOrder/components/InventoryInfo'],
['components/Qrbox', 'tabWorkOrder/components/Qrbox'],
['components/RemarkPopup', 'tabWorkOrder/components/RemarkPopup'],
['components/SkuGoods', 'tabWorkOrder/components/SkuGoods'],
['components/TechnicianSelectPopup', 'tabWorkOrder/components/TechnicianSelectPopup'],
['components/tipsBottom', 'tabWorkOrder/components/tipsBottom'],
['components/areaOperationsModal', 'tabWorkOrder/components/areaOperationsModal'],
]
for (const [oldPath, newPath] of movedPaths) {
assert.equal(fs.existsSync(path.join(projectRoot, oldPath)), false, oldPath)
assert.equal(fs.existsSync(path.join(projectRoot, newPath)), true, newPath)
}
})
test('root entries no longer pull workspace-only form and area data into main', () => {
const appSource = fs.readFileSync(path.join(projectRoot, 'app.js'), 'utf8')
const appConfig = JSON.parse(fs.readFileSync(path.join(projectRoot, 'app.json'), 'utf8'))
const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'))
const supervisorConfig = JSON.parse(
fs.readFileSync(
path.join(projectRoot, 'packageWorkspace/pages/supervisorDaily/supervisorDaily.json'),
'utf8',
),
)
assert.doesNotMatch(appSource, /@vant\/area-data|\$areaList/)
assert.equal(appConfig.usingComponents['gl-cust-form'], undefined)
assert.equal(packageJson.dependencies['@vant/area-data'], undefined)
assert.equal(
supervisorConfig.usingComponents['gl-cust-form'],
'../../components/GLCustForm/GLCustForm',
)
})
test('subpackage consumers no longer import the former main-package paths', () => {
const studyResult = fs.readFileSync(
path.join(projectRoot, 'packageStudy/pages/aiCheck/result/result.js'),
'utf8',
)
const orderPopup = fs.readFileSync(
path.join(projectRoot, 'tabWorkOrder/components/OrderPopup/OrderPopup.js'),
'utf8',
)
const rootWxUtils = fs.readFileSync(path.join(projectRoot, 'utils/utils_wx.js'), 'utf8')
assert.match(studyResult, /require\(["']\.\.\/\.\.\/\.\.\/utils\/wxcharts["']\)/)
assert.match(orderPopup, /from ["']\.\.\/\.\.\/utils\/wxqrcode["']/)
assert.doesNotMatch(rootWxUtils, /wxqrcode|handleCopy/)
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!