Skip to content
Closed
Changes from 2 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
21 changes: 20 additions & 1 deletion src/utils/normalizeModuleFederationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type RemoteEntryType =
| 'system'
| string;

import { existsSync } from 'fs';
import * as path from 'pathe';
import { warn } from './logUtils';

Expand Down Expand Up @@ -117,6 +118,24 @@ function removePathFromNpmPackage(packageString: string): string {
return match ? match[0] : packageString;
}

function findPackageJson(moduleName: string) {
const mainFilePath = require.resolve(moduleName);

let currentDir = path.dirname(mainFilePath);
while (
path.parse(currentDir).base !== 'node_modules' &&
currentDir !== path.parse(currentDir).root
) {
const potentialPackageJsonPath = path.join(currentDir, 'package.json');
if (existsSync(potentialPackageJsonPath)) {
return require(potentialPackageJsonPath);
}
currentDir = path.dirname(currentDir);
}

throw new Error(`Unable to find package.json for the module "${moduleName}"`);
Copy link
Collaborator

@gioboa gioboa Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it @nicotu01 👍
A good error message is always valuable

}

function normalizeShareItem(
key: string,
shareItem:
Expand All @@ -132,7 +151,7 @@ function normalizeShareItem(
): ShareItem {
let version: string | undefined;
try {
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
version = findPackageJson(removePathFromNpmPackage(key)).version;
} catch (e) {
console.log(e);
}
Expand Down
Loading