Skip to content

Commit 1c14b3c

Browse files
committed
fix(nf): respect nested entry points in package.json
1 parent f3f1bfe commit 1c14b3c

File tree

1 file changed

+57
-38
lines changed

1 file changed

+57
-38
lines changed

libs/native-federation-core/src/lib/utils/package-info.ts

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ export type VersionMap = Record<string, string>;
2020
export type PackageJsonInfo = {
2121
content: any;
2222
directory: string;
23-
}
23+
};
2424

2525
const packageCache: Record<string, PackageJsonInfo[]> = {};
2626

27-
export function findPackageJsonFiles(project: string, workspace: string): string[] {
27+
export function findPackageJsonFiles(
28+
project: string,
29+
workspace: string
30+
): string[] {
2831
return expandFolders(project, workspace)
29-
.map(f => path.join(f, 'package.json'))
30-
.filter(f => fs.existsSync(f));
32+
.map((f) => path.join(f, 'package.json'))
33+
.filter((f) => fs.existsSync(f));
3134
}
3235

3336
export function expandFolders(child: string, parent: string): string[] {
@@ -36,30 +39,30 @@ export function expandFolders(child: string, parent: string): string[] {
3639
child = normalize(child, true);
3740

3841
if (!child.startsWith(parent)) {
39-
throw new Error(`Workspace folder ${path} needs to be a parent of the project folder ${child}`);
42+
throw new Error(
43+
`Workspace folder ${path} needs to be a parent of the project folder ${child}`
44+
);
4045
}
4146

4247
let current = child;
4348

4449
while (current !== parent) {
4550
result.push(current);
46-
51+
4752
const cand = normalize(path.dirname(current), true);
4853
if (cand === current) {
49-
break
50-
};
54+
break;
55+
}
5156
current = cand;
5257
}
5358
result.push(parent);
5459
return result;
5560
}
5661

57-
5862
export function getPackageInfo(
5963
packageName: string,
6064
workspaceRoot: string
6165
): PackageInfo | null {
62-
6366
workspaceRoot = normalize(workspaceRoot, true);
6467

6568
const packageJsonInfos = getPackageJsonFiles(workspaceRoot, workspaceRoot);
@@ -79,14 +82,19 @@ function getVersionMapCacheKey(project: string, workspace: string): string {
7982
return `${project}**${workspace}`;
8083
}
8184

82-
export function getVersionMaps(project: string, workspace: string): VersionMap[] {
83-
return getPackageJsonFiles(project, workspace)
84-
.map(json => ({
85-
...json.content['dependencies']
86-
}));
85+
export function getVersionMaps(
86+
project: string,
87+
workspace: string
88+
): VersionMap[] {
89+
return getPackageJsonFiles(project, workspace).map((json) => ({
90+
...json.content['dependencies'],
91+
}));
8792
}
8893

89-
export function getPackageJsonFiles(project: string, workspace: string): PackageJsonInfo[] {
94+
export function getPackageJsonFiles(
95+
project: string,
96+
workspace: string
97+
): PackageJsonInfo[] {
9098
const cacheKey = getVersionMapCacheKey(project, workspace);
9199

92100
let maps = packageCache[cacheKey];
@@ -95,21 +103,24 @@ export function getPackageJsonFiles(project: string, workspace: string): Package
95103
return maps;
96104
}
97105

98-
maps = findPackageJsonFiles(project, workspace)
99-
.map(f => {
100-
const content = JSON.parse(fs.readFileSync(f, 'utf-8'));
101-
const directory = normalize(path.dirname(f), true);
102-
const result: PackageJsonInfo = {
103-
content, directory
104-
};
105-
return result;
106-
});
106+
maps = findPackageJsonFiles(project, workspace).map((f) => {
107+
const content = JSON.parse(fs.readFileSync(f, 'utf-8'));
108+
const directory = normalize(path.dirname(f), true);
109+
const result: PackageJsonInfo = {
110+
content,
111+
directory,
112+
};
113+
return result;
114+
});
107115

108116
packageCache[cacheKey] = maps;
109117
return maps;
110118
}
111119

112-
export function findDepPackageJson(packageName: string, projectRoot: string): string | null {
120+
export function findDepPackageJson(
121+
packageName: string,
122+
projectRoot: string
123+
): string | null {
113124
const mainPkgName = getPkgFolder(packageName);
114125

115126
let mainPkgPath = path.join(projectRoot, 'node_modules', mainPkgName);
@@ -118,7 +129,6 @@ export function findDepPackageJson(packageName: string, projectRoot: string): st
118129
let directory = projectRoot;
119130

120131
while (path.dirname(directory) !== directory) {
121-
122132
if (fs.existsSync(mainPkgJsonPath)) {
123133
break;
124134
}
@@ -132,19 +142,19 @@ export function findDepPackageJson(packageName: string, projectRoot: string): st
132142
if (!fs.existsSync(mainPkgJsonPath)) {
133143
// TODO: Add logger
134144
// context.logger.warn('No package.json found for ' + packageName);
135-
logger.verbose('No package.json found for ' + packageName + ' in ' + mainPkgPath);
145+
logger.verbose(
146+
'No package.json found for ' + packageName + ' in ' + mainPkgPath
147+
);
136148

137149
return null;
138150
}
139151
return mainPkgJsonPath;
140-
141152
}
142153

143154
export function _getPackageInfo(
144155
packageName: string,
145-
directory: string,
156+
directory: string
146157
): PackageInfo | null {
147-
148158
const mainPkgName = getPkgFolder(packageName);
149159
const mainPkgJsonPath = findDepPackageJson(packageName, directory);
150160

@@ -177,13 +187,13 @@ export function _getPackageInfo(
177187

178188
if (typeof cand === 'string') {
179189
return {
180-
entryPoint: path.join(mainPkgPath, cand),
181-
packageName,
182-
version,
183-
esm,
190+
entryPoint: path.join(mainPkgPath, cand),
191+
packageName,
192+
version,
193+
esm,
184194
};
185195
}
186-
196+
187197
cand = mainPkgJson?.exports?.[relSecondaryPath]?.import;
188198

189199
if (typeof cand === 'object') {
@@ -197,6 +207,16 @@ export function _getPackageInfo(
197207
}
198208

199209
if (cand) {
210+
if (typeof cand === 'object') {
211+
if (cand.module) {
212+
cand = cand.module;
213+
} else if (cand.default) {
214+
cand = cand.default;
215+
} else {
216+
cand = null;
217+
}
218+
}
219+
200220
return {
201221
entryPoint: path.join(mainPkgPath, cand),
202222
packageName,
@@ -228,7 +248,6 @@ export function _getPackageInfo(
228248

229249
cand = mainPkgJson?.exports?.[relSecondaryPath]?.default;
230250
if (cand) {
231-
232251
if (typeof cand === 'object') {
233252
if (cand.module) {
234253
cand = cand.module;
@@ -238,7 +257,7 @@ export function _getPackageInfo(
238257
cand = null;
239258
}
240259
}
241-
260+
242261
return {
243262
entryPoint: path.join(mainPkgPath, cand),
244263
packageName,

0 commit comments

Comments
 (0)