Skip to content

Commit 1efe198

Browse files
committed
perf(core): cache package.json files for dependency lookup
1 parent 3c84960 commit 1efe198

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

libs/native-federation-core/src/lib/config/share-utils.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
prepareSkipList,
1010
SkipList,
1111
} from '../core/default-skip-list';
12-
import { findPackageJsonFiles } from '../utils/package-info';
12+
import { findPackageJsonFiles, getVersionMaps, VersionMap } from '../utils/package-info';
1313
import { getConfigContext } from './configuration-context';
1414

1515
let inferVersion = false;
@@ -19,7 +19,6 @@ export const DEFAULT_SECONARIES_SKIP_LIST = [
1919
'@angular/common/upgrade',
2020
];
2121

22-
type VersionMap = Record<string, string>;
2322
type IncludeSecondariesOptions = { skip: string | string[] } | boolean;
2423
type CustomSharedConfig = SharedConfig & {
2524
includeSecondaries?: IncludeSecondariesOptions;
@@ -72,11 +71,10 @@ function readVersionMap(packagePath: string): VersionMap {
7271

7372
function lookupVersion(key: string, projectRoot: string, workspaceRoot: string): string {
7473

75-
const packageJsonList = findPackageJsonFiles(projectRoot, workspaceRoot);
74+
const versionMaps = getVersionMaps(projectRoot, workspaceRoot);
7675

77-
for (const packagePath of packageJsonList) {
76+
for (const versionMap of versionMaps) {
7877

79-
const versionMap = readVersionMap(packagePath);
8078
const version = lookupVersionInMap(key, versionMap);
8179

8280
if (version) {
@@ -258,11 +256,10 @@ export function shareAll(
258256
workspacePath = projectPath;
259257
}
260258

261-
const packageJsonList = findPackageJsonFiles(projectPath, workspacePath);
259+
const versionMaps = getVersionMaps(projectPath, workspacePath);
262260
const share: Record<string, unknown> = {};
263261

264-
for(const packagePath of packageJsonList) {
265-
const versions = readVersionMap(packagePath);
262+
for(const versions of versionMaps) {
266263

267264
const preparedSkipList = prepareSkipList(skip);
268265

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

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ export interface PartialPackageJson {
1515
main: string;
1616
}
1717

18+
export type VersionMap = Record<string, string>;
19+
20+
export type PackageJsonInfo = {
21+
content: any;
22+
directory: string;
23+
}
24+
25+
const packageCache: Record<string, PackageJsonInfo[]> = {};
26+
1827
export function findPackageJsonFiles(project: string, workspace: string): string[] {
1928
return expandFolders(project, workspace)
2029
.map(f => path.join(f, 'package.json'))
@@ -58,37 +67,62 @@ export function getPackageInfo(
5867
throw new Error(`Workspace folder ${workspaceRoot} needs to be a parent of the project folder ${projectRoot}`);
5968
}
6069

61-
let currentPath = projectRoot;
70+
const packageJsonInfos = getPackageJsonFiles(projectRoot, workspaceRoot);
6271

63-
while (workspaceRoot !== currentPath) {
64-
65-
const cand = _getPackageInfo(packageName, currentPath);
72+
for (const info of packageJsonInfos) {
73+
const cand = _getPackageInfo(packageName, info);
6674
if (cand) {
6775
return cand;
6876
}
69-
70-
currentPath = normalize(path.dirname(currentPath), true);
7177
}
7278

73-
const result = _getPackageInfo(packageName, currentPath);
79+
logger.warn('No meta data found for shared lib ' + packageName);
80+
return null;
81+
}
82+
83+
function getVersionMapCacheKey(project: string, workspace: string): string {
84+
return `${project}**${workspace}`;
85+
}
7486

75-
if (!result) {
76-
logger.warn('No meta data found for shared lib ' + packageName);
77-
}
87+
export function getVersionMaps(project: string, workspace: string): VersionMap[] {
88+
return getPackageJsonFiles(project, workspace)
89+
.map(json => ({
90+
...json.content['dependencies']
91+
}));
92+
}
7893

79-
return result;
94+
export function getPackageJsonFiles(project: string, workspace: string): PackageJsonInfo[] {
95+
const cacheKey = getVersionMapCacheKey(project, workspace);
96+
97+
let maps = packageCache[cacheKey];
98+
99+
if (maps) {
100+
return maps;
101+
}
80102

103+
maps = findPackageJsonFiles(project, workspace)
104+
.map(f => {
105+
const content = fs.readFileSync(f, 'utf-8');
106+
const directory = path.dirname(f);
107+
const result: PackageJsonInfo = {
108+
content, directory
109+
};
110+
return result;
111+
});
112+
113+
packageCache[cacheKey] = maps;
114+
return maps;
81115
}
82116

83117
export function _getPackageInfo(
84118
packageName: string,
85-
currentPath: string,
119+
packageJsonInfo: PackageJsonInfo,
86120
): PackageInfo | null {
87121

88122
const mainPkgName = getPkgFolder(packageName);
89123

90-
const mainPkgPath = path.join(currentPath, 'node_modules', mainPkgName);
91-
const mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
124+
const mainPkgPath = path.join(packageJsonInfo.directory, 'node_modules', mainPkgName);
125+
// const mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
92126

93127
if (!fs.existsSync(mainPkgPath)) {
94128
// TODO: Add logger
@@ -98,7 +132,8 @@ export function _getPackageInfo(
98132
return null;
99133
}
100134

101-
const mainPkgJson = readJson(mainPkgJsonPath);
135+
//const mainPkgJson = readJson(mainPkgJsonPath);
136+
const mainPkgJson = packageJsonInfo.content;
102137

103138
const version = mainPkgJson['version'] as string;
104139
const esm = mainPkgJson['type'] === 'module';
@@ -171,7 +206,7 @@ export function _getPackageInfo(
171206
};
172207
}
173208

174-
const secondaryPgkPath = path.join(currentPath, 'node_modules', packageName);
209+
const secondaryPgkPath = path.join(packageJsonInfo.directory, 'node_modules', packageName);
175210
const secondaryPgkJsonPath = path.join(secondaryPgkPath, 'package.json');
176211
let secondaryPgkJson: PartialPackageJson | null = null;
177212
if (fs.existsSync(secondaryPgkJsonPath)) {
1.76 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)