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

refactor(utils): Extract packages - use forEach instead of reduce #4288

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
134 changes: 63 additions & 71 deletions packages/utils/src/webpack/extract/modules-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,79 +79,71 @@ export const extractModulesPackages = (
currentExtractedData?: MetricsModules,
): MetricsPackages => {
const modules = Object.entries(currentExtractedData?.metrics?.modules || {});
const packages: Record<string, Package> = {};

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<string, Package>,
);

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 } };
};