mainPackageBudget.test.mjs
5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'
import { areaList } from '../../packageWorkspace/utils/area-data.js'
import { analyzeMainPackage } from '../../scripts/main-package-size.mjs'
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
const migrationHeadroomBytes = 1_500_000
test('physical main package stays below the guarded upload budget', () => {
assert.equal(fs.existsSync(analyzerPath), true, 'main package analyzer must exist')
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('work-order subpackage migration preserves the 1.5 MB main-package headroom target', () => {
const report = analyzeMainPackage({ rootDir: projectRoot })
assert.equal(report.totalBytes <= migrationHeadroomBytes, true, JSON.stringify(report))
})
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/Goods.js', 'tabWorkOrder/components/SkuGoods'],
['components/TechnicianSelectPopup', 'tabWorkOrder/components/TechnicianSelectPopup'],
['components/tipsBottom', 'tabWorkOrder/components/tipsBottom'],
['components/areaOperationsModal', 'tabWorkOrder/components/areaOperationsModal'],
]
for (const [oldPath, newPath] of movedPaths) {
const oldEntry = fs.statSync(path.join(projectRoot, oldPath), { throwIfNoEntry: false })
if (oldEntry?.isDirectory()) {
assert.equal(
fs.readdirSync(path.join(projectRoot, oldPath)).length,
0,
`${oldPath} must not contain main-package files`,
)
} else {
assert.equal(oldEntry, undefined, 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 projectConfig = JSON.parse(
fs.readFileSync(path.join(projectRoot, 'project.config.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(
projectConfig.packOptions.ignore.some(
item =>
item.type === 'folder' &&
item.value === 'miniprogram_npm/@vant/area-data',
),
true,
)
assert.equal(
supervisorConfig.usingComponents['gl-cust-form'],
'../../components/GLCustForm/GLCustForm',
)
})
test('workspace-local area data preserves the Vant area payload contract', () => {
const licensePath = path.join(projectRoot, 'packageWorkspace/utils/area-data.LICENSE')
assert.deepEqual(Object.keys(areaList).sort(), [
'city_list',
'county_list',
'province_list',
])
assert.equal(areaList.province_list[110000], '北京市')
assert.equal(areaList.city_list[440300], '深圳市')
assert.equal(areaList.county_list[440305], '南山区')
assert.match(fs.readFileSync(licensePath, 'utf8'), /MIT License/)
})
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/)
})