Skip to content

Commit 1c2f6a2

Browse files
committed
perf(mp): 将子包依赖的 node_modules 内容打包到子包 vendor.js 中 (question/185955)
1 parent d3e6edb commit 1c2f6a2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/uni-mp-vite/src/plugin/build.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '@dcloudio/uni-cli-shared'
2323
import type { GetManualChunk, GetModuleInfo, PreRenderedChunk } from 'rollup'
2424
import {
25+
getSubPackages,
2526
isUniComponentUrl,
2627
isUniPageUrl,
2728
parseVirtualComponentPath,
@@ -193,6 +194,29 @@ function createMoveToVendorChunkFn(): GetManualChunk | undefined {
193194
}
194195
return
195196
}
197+
const { hasOptimizationSubPackages, subPackages } = getSubPackages()
198+
// 处理子包引用的 node_modules 中的文件
199+
if (
200+
hasOptimizationSubPackages &&
201+
subPackages.length &&
202+
filename.startsWith(inputDir) &&
203+
filename.includes('node_modules')
204+
) {
205+
const moduleInfo = getModuleInfo(id)
206+
if (!moduleInfo || !moduleInfo.importers.length) {
207+
return
208+
}
209+
const matchSubPackages = new Set(
210+
subPackages.filter((subPackagePath) =>
211+
moduleInfo.importers.some((importer) =>
212+
importer.startsWith(inputDir + '/' + subPackagePath)
213+
)
214+
)
215+
)
216+
if (matchSubPackages.size === 1) {
217+
return `${matchSubPackages.values().next().value}common/vendor`
218+
}
219+
}
196220
// 非项目内的 js 资源,均打包到 vendor
197221
debugChunk('common/vendor', normalizedId)
198222
return 'common/vendor'

packages/uni-mp-vite/src/plugins/entry.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
normalizeMiniProgramFilename,
1212
normalizePath,
1313
parseManifestJsonOnce,
14+
parseMiniProgramPagesJson,
1415
removeExt,
1516
} from '@dcloudio/uni-cli-shared'
1617
import type { Plugin } from 'vite'
@@ -53,6 +54,30 @@ function parseComponentStyleIsolation(file: string) {
5354
}
5455
}
5556

57+
let hasOptimizationSubPackages = false // 是否开启分包优化配置
58+
let subPackages: string[] = []
59+
function initSubPackages() {
60+
const inputDir = normalizePath(process.env.UNI_INPUT_DIR)
61+
const platform = process.env.UNI_PLATFORM
62+
const manifestJson = parseManifestJsonOnce(inputDir)
63+
hasOptimizationSubPackages =
64+
platform && manifestJson[platform]?.optimization?.subPackages
65+
const { appJson } = parseMiniProgramPagesJson(
66+
fs.readFileSync(path.resolve(inputDir, 'pages.json'), 'utf8'),
67+
platform,
68+
{ subpackages: true }
69+
)
70+
subPackages = Object.values(appJson.subPackages || appJson.subpackages || {})
71+
.filter(Boolean)
72+
.map(({ root }) => `${root.replace(/\/$/, '')}/`)
73+
}
74+
export function getSubPackages() {
75+
return {
76+
hasOptimizationSubPackages,
77+
subPackages,
78+
}
79+
}
80+
5681
export function uniEntryPlugin({
5782
global,
5883
template,
@@ -72,6 +97,7 @@ export function uniEntryPlugin({
7297
},
7398
buildStart() {
7499
easycomEncryptComponentPaths.clear()
100+
initSubPackages()
75101
},
76102
load(id) {
77103
if (isUniPageUrl(id)) {

0 commit comments

Comments
 (0)