From 6bdd18bc6807a6d74e87167a445a7d5e9111ce4d Mon Sep 17 00:00:00 2001 From: Vio Date: Wed, 28 Feb 2024 23:10:15 +0100 Subject: [PATCH] refactor(utils): Extract packages - use forEach instead of reduce --- .../src/webpack/extract/modules-packages.ts | 134 ++++++++---------- 1 file changed, 63 insertions(+), 71 deletions(-) diff --git a/packages/utils/src/webpack/extract/modules-packages.ts b/packages/utils/src/webpack/extract/modules-packages.ts index 89f411368b..3b41882aeb 100644 --- a/packages/utils/src/webpack/extract/modules-packages.ts +++ b/packages/utils/src/webpack/extract/modules-packages.ts @@ -79,79 +79,71 @@ export const extractModulesPackages = ( currentExtractedData?: MetricsModules, ): MetricsPackages => { const modules = Object.entries(currentExtractedData?.metrics?.modules || {}); + const packages: Record = {}; - const packages = modules.reduce( - (agg, [modulePath, { value }]) => { - const packageMeta = getPackageMetaFromModulePath(modulePath); - - if (!packageMeta) { - return agg; - } - - const existingPackageData = agg[packageMeta.id]; - - // New package data - if (!existingPackageData) { - return { - ...agg, - [packageMeta.id]: { - name: packageMeta.name, - path: packageMeta.path, - value, - }, - }; - } - - // Existing package info - if (existingPackageData.path === packageMeta.path) { - return { - ...agg, - [packageMeta.id]: { - ...existingPackageData, - value: existingPackageData.value + value, - }, - }; - } - - // Same package name, but different paths (eg: symlinks) - const existingPackageWithEqualPath = Object.entries(agg).find( - ([__, packageData]) => packageData.path === packageMeta.path, - ); - - if (existingPackageWithEqualPath) { - const [name, data] = existingPackageWithEqualPath; - - return { - ...agg, - [name]: { - ...data, - value: data.value + value, - }, - }; - } - - // New package name & data - const lastIndex = - max( - Object.keys(agg) - .map((id) => id.split('~')) - .filter(([id]) => id === packageMeta.id) - .map(([__, index]) => parseInt(index, 10)), - ) || 0; - - const packageName = [packageMeta.id, lastIndex + 1].join(PACKAGE_ID_SEPARATOR); - - return { - ...agg, - [packageName]: { - name: packageMeta.name, - path: packageMeta.path, - value, - }, + modules.forEach(([modulePath, entry]) => { + const packageMeta = getPackageMetaFromModulePath(modulePath); + + if (!packageMeta) { + return; + } + + const existingPackageData = packages[packageMeta.id]; + + // New package data + if (!existingPackageData) { + packages[packageMeta.id] = { + name: packageMeta.name, + path: packageMeta.path, + value: entry.value, }; - }, - {} as Record, - ); + + return; + } + + // Existing package info + if (existingPackageData.path === packageMeta.path) { + packages[packageMeta.id] = { + ...existingPackageData, + value: existingPackageData.value + entry.value, + }; + + return; + } + + // Same package name, but different paths (eg: symlinks) + const existingPackageWithEqualPath = Object.entries(packages).find( + ([__, packageData]) => packageData.path === packageMeta.path, + ); + + if (existingPackageWithEqualPath) { + const [name, data] = existingPackageWithEqualPath; + + packages[name] = { + ...data, + value: data.value + entry.value, + }; + + return; + } + + // New package name & data + const lastIndex = + max( + Object.keys(packages) + .map((id) => id.split('~')) + .filter(([id]) => id === packageMeta.id) + .map(([__, index]) => parseInt(index, 10)), + ) || 0; + + const packageName = [packageMeta.id, lastIndex + 1].join(PACKAGE_ID_SEPARATOR); + + packages[packageName] = { + name: packageMeta.name, + path: packageMeta.path, + value: entry.value, + }; + }); return { metrics: { packages } }; };