Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix split module from main package component in sub-packages optimization #2208

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fixed moving main package component's JS module to sub-packages commo…
…n/vender in optimization.
  • Loading branch information
huadong committed Oct 22, 2020
commit 8c4ad386930e782de1b80bcf5074d4bfb8ccf442
38 changes: 35 additions & 3 deletions packages/vue-cli-plugin-uni/lib/split-chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,18 @@ module.exports = function getSplitChunks () {
if (!baseTest(module)) {
return false
}
const matchSubPackagesCount = findSubPackages(chunks).size
const matchSubPackages = findSubPackages(chunks)
const matchSubPackagesCount = matchSubPackages.size
const isMainPackage = ( // 非分包 或 两个及以上分包 或 主包内有使用
matchSubPackagesCount === 0 ||
matchSubPackagesCount > 1 ||
(
matchSubPackagesCount === 1 &&
hasMainPackage(chunks)
) ||
(
matchSubPackagesCount === 1 &&
hasMainPackageComponent(module, matchSubPackages.values().next().value)
)
)
if (isMainPackage && process.env.UNI_OPT_TRACE) {
Expand Down Expand Up @@ -157,6 +162,31 @@ module.exports = function getSplitChunks () {
return chunks.find(item => !subPackageRoots.find(root => item.name.indexOf(root) === 0))
}

const hasMainPackageComponent = function (module, subPackageRoot) {
if (module.resource && module.reasons) {
for (let index = 0; index < module.reasons.length; index++) {
const m = module.reasons[index]

if (m.module && m.module.resource) {
const resource = normalizePath(m.module.resource)
if (
resource.indexOf('.vue') !== -1 ||
resource.indexOf('.nvue') !== -1
) {
if (resource.indexOf(subPackageRoot) === -1) {
if (process.env.UNI_OPT_TRACE) {
console.log('move module to main chunk:', module.resource,
'from', subPackageRoot, 'for component in main package:', resource)
}
return true
}
}
}
}
}
return false
}

const subPackageRoots = Object.keys(process.UNI_SUBPACKAGES).map(root => root + '/')

Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
Expand All @@ -170,8 +200,10 @@ module.exports = function getSplitChunks () {
if (
matchSubPackages.size === 1 &&
matchSubPackages.has(root + '/') &&
!hasMainPackage(chunks)
!hasMainPackage(chunks) &&
!hasMainPackageComponent(module, matchSubPackages.values().next().value)
) {

if (process.env.UNI_OPT_TRACE) {
console.log(root, module.resource, chunks.map(chunk => chunk.name))
}
Expand All @@ -192,4 +224,4 @@ module.exports = function getSplitChunks () {
},
cacheGroups
}
}
}