Skip to content

Commit

Permalink
Merge pull request #4286 from relative-ci/fix-module-federation-packa…
Browse files Browse the repository at this point in the history
…ge-name

fix(utils): Extract package - skip module federation modules
  • Loading branch information
vio committed Feb 28, 2024
2 parents 82c8fac + a1db638 commit 52f66b3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 61 deletions.
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 } };
};

0 comments on commit 52f66b3

Please sign in to comment.