perf(mp): 将子包依赖的静态资源内容打包到子包 assets.js 中#5982
Conversation
There was a problem hiding this comment.
Pull request overview
该 PR 旨在优化小程序构建产物的分包资源组织方式:将位于子包目录内的静态资源打包到对应子包下的 common/assets.js,以减少主包 common/assets.js 的内容/体积压力。
Changes:
- 在 Rollup
manualChunks里对静态资源(DEFAULT_ASSETS_RE)按子包根目录进行分桶 - 静态资源位于子包根目录时,chunk 名称改为
${subPackageRoot}common/assets,否则仍为common/assets
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (DEFAULT_ASSETS_RE.test(filename)) { | ||
| debugChunk('common/assets', normalizedId) | ||
| const { subPackages } = getSubPackages() | ||
| if (subPackages.length) { | ||
| const relativePath = normalizePath(path.relative(inputDir, filename)) | ||
| const subPackageRoot = subPackages.find((subPackage) => | ||
| relativePath.startsWith(subPackage) | ||
| ) | ||
| if (subPackageRoot) { | ||
| return subPackageRoot + 'common/assets' |
There was a problem hiding this comment.
debugChunk is always called with 'common/assets', but this branch can now return a subpackage-scoped chunk name (e.g. ${subPackageRoot}common/assets). This makes chunk debug output misleading when investigating bundle layout. Consider computing the actual chunk name first, logging that value, and then returning it.
| relativePath.startsWith(subPackage) | ||
| ) | ||
| if (subPackageRoot) { | ||
| return subPackageRoot + 'common/assets' |
There was a problem hiding this comment.
The returned chunk name is built via subPackageRoot + 'common/assets', which implicitly relies on subPackageRoot always ending with / (currently true because getSubPackages() normalizes roots with a trailing slash). To make this more robust against future changes, build the chunk name in a way that guarantees the separator (e.g. a template using the existing trailing slash, or a posix join).
| return subPackageRoot + 'common/assets' | |
| return path.posix.join(subPackageRoot, 'common/assets') |
| relativePath.startsWith(subPackage) | ||
| ) | ||
| if (subPackageRoot) { | ||
| return subPackageRoot + 'common/assets' |
There was a problem hiding this comment.
New behavior: assets located under a subpackage root are now emitted into a subpackage-specific common/assets chunk. There doesn’t appear to be coverage ensuring the expected output file (e.g. subRoot/common/assets.js) is produced and contains the expected inlined assets. Consider adding/updating an integration test in the playground build tests to cover a project with pages.json subPackages importing an asset within that subpackage.
| return subPackageRoot + 'common/assets' | |
| return normalizePath(path.join(subPackageRoot, 'common/assets')) |
Size report
|
a8a3feb to
65fc40e
Compare
No description provided.