Skip to content

Commit 83e8107

Browse files
committed
feat(core): make shareAll respect package-based monorepos
1 parent fbd4af3 commit 83e8107

File tree

4 files changed

+82
-16
lines changed

4 files changed

+82
-16
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface ConfigurationContext {
2+
workspaceRoot?: string;
3+
}
4+
5+
let _context: ConfigurationContext = {};
6+
7+
export function useWorkspace(workspaceRoot: string): void {
8+
_context = {..._context, workspaceRoot};
9+
}
10+
11+
export function getConfigContext(): ConfigurationContext {
12+
return _context;
13+
}

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
prepareSkipList,
1010
SkipList,
1111
} from '../core/default-skip-list';
12+
import { expandFolders, findPackageJsonFiles } from '../utils/package-info';
13+
import { getConfigContext } from './configuration-context';
1214

1315
let inferVersion = false;
1416

@@ -223,28 +225,47 @@ function readConfiguredSecondaries(
223225
export function shareAll(
224226
config: CustomSharedConfig = {},
225227
skip: SkipList = DEFAULT_SKIP_LIST,
226-
packageJsonPath = ''
228+
projectPath = '',
229+
workspacePath = '',
227230
): Config | null {
228-
if (!packageJsonPath) {
229-
packageJsonPath = cwd();
231+
232+
if (!projectPath) {
233+
projectPath = cwd();
230234
}
231235

232-
const packagePath = findPackageJson(packageJsonPath);
236+
if (!workspacePath) {
237+
workspacePath = getConfigContext().workspaceRoot ?? '';
238+
}
233239

234-
const versions = readVersionMap(packagePath);
235-
const share: any = {};
240+
if (!workspacePath) {
241+
workspacePath = projectPath;
242+
}
236243

237-
const preparedSkipList = prepareSkipList(skip);
244+
const packageJsonList = findPackageJsonFiles(projectPath, workspacePath);
245+
const share: Record<string, unknown> = {};
238246

239-
for (const key in versions) {
240-
if (isInSkipList(key, preparedSkipList)) {
241-
continue;
242-
}
247+
for(const packagePath of packageJsonList) {
248+
const versions = readVersionMap(packagePath);
249+
250+
const preparedSkipList = prepareSkipList(skip);
251+
252+
for (const key in versions) {
253+
if (isInSkipList(key, preparedSkipList)) {
254+
continue;
255+
}
243256

244-
share[key] = { ...config };
257+
const inferVersion = (!config.requiredVersion || config.requiredVersion === 'auto');
258+
const requiredVersion = inferVersion ?
259+
versions[key] :
260+
config.requiredVersion;
261+
262+
if (!share[key]) {
263+
share[key] = { ...config, requiredVersion };
264+
}
265+
}
245266
}
246267

247-
return module.exports.share(share, packageJsonPath);
268+
return module.exports.share(share, projectPath);
248269
}
249270

250271
export function setInferVersion(infer: boolean): void {

libs/native-federation-core/src/lib/core/federation-builder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useWorkspace } from '../config/configuration-context';
12
import { NormalizedFederationConfig } from '../config/federation-config';
23
import { BuildAdapter, setBuildAdapter } from './build-adapter';
34
import { buildForFederation, defaultBuildParams } from './build-for-federation';
@@ -17,6 +18,7 @@ let fedOptions: FederationOptions;
1718
async function init(params: BuildHelperParams): Promise<void> {
1819
setBuildAdapter(params.adapter);
1920
fedOptions = params.options;
21+
useWorkspace(params.options.workspaceRoot);
2022
config = await loadFederationConfig(fedOptions);
2123
externals = getExternals(config);
2224
}

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

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

18+
export function findPackageJsonFiles(project: string, workspace: string): string[] {
19+
return expandFolders(project, workspace)
20+
.map(f => path.join(f, 'package.json'))
21+
.filter(f => fs.existsSync(f));
22+
}
23+
24+
export function expandFolders(child: string, parent: string): string[] {
25+
const result: string[] = [];
26+
parent = normalize(parent, true);
27+
child = normalize(child, true);
28+
29+
if (!child.startsWith(parent)) {
30+
throw new Error(`Workspace folder ${path} needs to be a parent of the project folder ${child}`);
31+
}
32+
33+
let current = child;
34+
35+
while (current !== parent) {
36+
result.push(current);
37+
38+
const cand = normalize(path.dirname(current), true);
39+
if (cand === current) {
40+
break
41+
};
42+
current = cand;
43+
}
44+
result.push(parent);
45+
return result;
46+
}
47+
1848
export function getPackageInfo(
1949
packageName: string,
2050
workspaceRoot: string,
2151
projectRoot: string
2252
): PackageInfo | null {
2353

24-
let currentPath = projectRoot;
54+
workspaceRoot = normalize(workspaceRoot, true);
55+
projectRoot = normalize(projectRoot, true);
2556

26-
workspaceRoot = normalize(path.dirname(workspaceRoot), true);
27-
projectRoot = normalize(path.dirname(projectRoot), true);
57+
let currentPath = projectRoot;
2858

2959
while (workspaceRoot !== currentPath) {
3060

0 commit comments

Comments
 (0)