Skip to content

Commit

Permalink
fix: 🩹 优化分包编译判断逻辑,适配只被子包和node包引用的模块的编译
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanisper committed Jan 10, 2025
1 parent aaacf57 commit 644cbd8
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function UniappSubPackagesOptimization(enableLogger: boolean): Plugin {
const name = moduleIdProcessor(m.id)
// 判断是否为组件
if (name.includes('.vue') || name.includes('.nvue')) {
// 判断存在跨包引用的情况(该组件的引用路径不包含子包路径,就说明跨包引用了)
// 判断存在跨包引用的情况(该组件的引用路径不包含子包路径,就说明跨包引用了)
if (subPackageRoot && !name.includes(subPackageRoot)) {
if (process.env.UNI_OPT_TRACE) {
console.log('move module to main chunk:', moduleInfo.id, 'from', subPackageRoot, 'for component in main package:', name)
Expand Down Expand Up @@ -129,7 +129,7 @@ export function UniappSubPackagesOptimization(enableLogger: boolean): Plugin {
const originalOutput = config?.build?.rollupOptions?.output

const existingManualChunks
= (Array.isArray(originalOutput) ? originalOutput[0]?.manualChunks : originalOutput?.manualChunks) as GetManualChunk
= (Array.isArray(originalOutput) ? originalOutput[0]?.manualChunks : originalOutput?.manualChunks) as GetManualChunk

// 合并已有的 manualChunks 配置
const mergedManualChunks: GetManualChunk = (id, meta) => {
Expand Down Expand Up @@ -177,6 +177,39 @@ export function UniappSubPackagesOptimization(enableLogger: boolean): Plugin {
if (result?.[0] && !(hasNoSubPackage(importers) && !hasNodeModules(importers))) {
return `${result[0]}common/vendor`
}

// #region 判断是否只被子包和 node_modules 的包引用
// 排除子包和node_modules的importers | 剩下的都是主包的模块
const mainPackageImporters = importers.filter((item) => {
return !subPackageRoots.some(root => moduleIdProcessor(item).indexOf(root) === 0) && !moduleIdProcessor(item).includes('node_modules')
})
if ((!mainPackageImporters || !mainPackageImporters.length) && subPackageRoot && hasNodeModules(importers)) {
const nodeModulesInfo = importers.filter(item => item.includes('node_modules')).map(item => meta.getModuleInfo(item))
for (let index = 0; index < nodeModulesInfo.length; index++) {
const info = nodeModulesInfo[index]
if (info?.importers) {
const matchSubPackages = findSubPackages(info.importers)
let _subPackageRoot: string | undefined = matchSubPackages.values().next().value
const matchSubpackageModules = PackageModulesInstance.findModuleInImporters(importers) || {}
const matchSubpackage = Object.keys(matchSubpackageModules)[0] // 当前仅支持一个子包引用

if (((matchSubPackages.size === 1 && !hasNoSubPackage(info.importers))
|| (matchSubpackage && hasNodeModules(info.importers)
))
&& !hasMainPackageComponent(info, _subPackageRoot)) {
if (!_subPackageRoot) {
_subPackageRoot = matchSubpackage
}

if (_subPackageRoot === subPackageRoot) {
PackageModulesInstance.addModuleRecord(_subPackageRoot, moduleInfo) // 模块引入子包记录,用于链式依赖的索引
return `${_subPackageRoot}common/vendor`
}
}
}
}
}
// #endregion
}
}
}
Expand Down

0 comments on commit 644cbd8

Please sign in to comment.