From 0ecfd7dced28f8f0e749e506b3675904a4585a3e Mon Sep 17 00:00:00 2001 From: Yavor Ivanov Date: Fri, 17 Nov 2023 10:05:19 +0200 Subject: [PATCH] Enable auto split for depCache --- lib/lbt/bundle/AutoSplitter.js | 42 +++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/lbt/bundle/AutoSplitter.js b/lib/lbt/bundle/AutoSplitter.js index 81752d2bb..7de0d9721 100644 --- a/lib/lbt/bundle/AutoSplitter.js +++ b/lib/lbt/bundle/AutoSplitter.js @@ -41,7 +41,9 @@ class AutoSplitter { const numberOfParts = options.numberOfParts; let totalSize = 0; const moduleSizes = Object.create(null); - const depCacheSymbol = Symbol("depCache"); + const depCacheSizes = []; + let depCacheLoaderSize = 0; + let depCacheSet = new Set(); this.optimize = !!options.optimize; // ---- resolve module definition @@ -76,8 +78,8 @@ class AutoSplitter { }); break; case SectionType.DepCache: - moduleSizes[depCacheSymbol] = "sap.ui.loader.config({depCacheUI5:{}});".length; - totalSize += moduleSizes[depCacheSymbol]; + depCacheLoaderSize = "sap.ui.loader.config({depCacheUI5:{}});".length; + totalSize += depCacheLoaderSize; section.modules.forEach( (module) => { promises.push((async () => { @@ -89,8 +91,12 @@ class AutoSplitter { ); if (deps.length > 0) { const depSize = `"${module}": [${deps.map((dep) => `"${dep}"`).join(",")}],`.length; - moduleSizes[depCacheSymbol] += depSize; totalSize += depSize; + + depCacheSizes.push({ + size: depSize, + filters: [module, ...deps] + }); } })()); }); @@ -204,10 +210,34 @@ class AutoSplitter { case SectionType.DepCache: currentSection = { mode: SectionType.DepCache, - filters: section.modules.slice() + filters: [] }; currentModule.sections.push( currentSection ); - totalSize += moduleSizes[depCacheSymbol]; + totalSize += depCacheLoaderSize; + + depCacheSizes.forEach((depCache) => { + if ( part + 1 < numberOfParts && totalSize + depCache.size / 2 > partSize ) { + currentSection.filters = Array.from(depCacheSet); + depCacheSet = new Set(); + part++; + // start a new module + totalSize = depCacheLoaderSize; + currentSection = { + mode: SectionType.DepCache, + filters: [] + }; + currentModule = { + name: moduleNameWithPart.replace(/__part__/, part), + sections: [currentSection] + }; + splittedModules.push(currentModule); + } + + depCache.filters.forEach((moduleName) => depCacheSet.add(moduleName)); + totalSize += depCache.size; + }); + + currentSection.filters = Array.from(depCacheSet); break; default: break;