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(utils): Extract package - skip module federation modules #4286

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/utils/src/webpack/extract/__tests__/modules-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ describe('Webpack/extract', () => {
path: './node_modules/.pnpm/github.com+org-a+repo-a@abcd1234/node_modules/repo-a',
});
});

test('should skip module federation module paths', () => {
expect(
getPackageMetaFromModulePath(
'provide shared module (default) react@17.0.2 = ../node_modules/react/index.js',
),
).toEqual(null);
expect(
getPackageMetaFromModulePath(
'consume shared module (default) react@^17.0.2 (singleton) (fallback: ../node_modules/react/index.js) (eager)',
),
).toEqual(null);
});
});
});

Expand Down
135 changes: 74 additions & 61 deletions packages/utils/src/webpack/extract/modules-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const uniqLast = (data: Array<unknown>) => {
* Heuristics to extract package id, name, and path from a module path
*/
export const getPackageMetaFromModulePath = (modulePath: string) => {
// Skip module federation entries
if (modulePath.match(/^(provide|consume) shared module/)) {
return null;
}

const paths = modulePath.match(MODULE_PATH_PACKAGES);

// No package paths found, skip
Expand All @@ -41,7 +46,11 @@ export const getPackageMetaFromModulePath = (modulePath: string) => {
return [];
}

return [...found].flat().slice(1).filter(Boolean).map((name) => name.replace(/\+/g, '/'));
return [...found]
.flat()
.slice(1)
.filter(Boolean)
.map((name) => name.replace(/\+/g, '/'));
})
.flat(),
);
Expand Down Expand Up @@ -71,74 +80,78 @@ export const extractModulesPackages = (
): MetricsPackages => {
const modules = Object.entries(currentExtractedData?.metrics?.modules || {});

const packages = modules.reduce((agg, [modulePath, { value }]) => {
const packageMeta = getPackageMetaFromModulePath(modulePath);

if (!packageMeta) {
return agg;
}

const existingPackageData = agg[packageMeta.id];
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);

// New package data
if (!existingPackageData) {
return {
...agg,
[packageMeta.id]: {
[packageName]: {
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,
},
};
}, {} as Record<string, Package>);
},
{} as Record<string, Package>,
);

return { metrics: { packages } };
};