Skip to content

Commit 3c84960

Browse files
committed
feat(core): make share respect package-based monorepos
1 parent a3e65d0 commit 3c84960

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

.vscode/settings.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,15 @@
2222
"commandCenter.border": "#e7e7e799"
2323
},
2424
"peacock.color": "#832561",
25-
"eslint.validate": ["json"]
25+
"eslint.validate": [
26+
"json"
27+
],
28+
"files.exclude": {
29+
"**/.git": true,
30+
"**/.svn": true,
31+
"**/.hg": true,
32+
"**/CVS": true,
33+
"**/.DS_Store": true,
34+
"**/Thumbs.db": true
35+
}
2636
}

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

Lines changed: 42 additions & 16 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 { expandFolders, findPackageJsonFiles } from '../utils/package-info';
12+
import { findPackageJsonFiles } from '../utils/package-info';
1313
import { getConfigContext } from './configuration-context';
1414

1515
let inferVersion = false;
@@ -70,7 +70,27 @@ function readVersionMap(packagePath: string): VersionMap {
7070
return versions;
7171
}
7272

73-
function lookupVersion(key: string, versions: VersionMap): string {
73+
function lookupVersion(key: string, projectRoot: string, workspaceRoot: string): string {
74+
75+
const packageJsonList = findPackageJsonFiles(projectRoot, workspaceRoot);
76+
77+
for (const packagePath of packageJsonList) {
78+
79+
const versionMap = readVersionMap(packagePath);
80+
const version = lookupVersionInMap(key, versionMap);
81+
82+
if (version) {
83+
return version;
84+
}
85+
86+
}
87+
88+
throw new Error(
89+
`Shared Dependency ${key} has requiredVersion:'auto'. However, this dependency is not found in your package.json`
90+
);
91+
}
92+
93+
function lookupVersionInMap(key: string, versions: VersionMap): string | null {
7494
const parts = key.split('/');
7595
if (parts.length >= 2 && parts[0].startsWith('@')) {
7696
key = parts[0] + '/' + parts[1];
@@ -83,9 +103,7 @@ function lookupVersion(key: string, versions: VersionMap): string {
83103
}
84104

85105
if (!versions[key]) {
86-
throw new Error(
87-
`Shared Dependency ${key} has requiredVersion:'auto'. However, this dependency is not found in your package.json`
88-
);
106+
return null;
89107
}
90108
return versions[key];
91109
}
@@ -225,17 +243,16 @@ function readConfiguredSecondaries(
225243
export function shareAll(
226244
config: CustomSharedConfig = {},
227245
skip: SkipList = DEFAULT_SKIP_LIST,
228-
projectPath = '',
229-
workspacePath = '',
246+
projectPath = ''
230247
): Config | null {
231248

249+
let workspacePath: string | undefined = undefined;
250+
232251
if (!projectPath) {
233252
projectPath = cwd();
234253
}
235254

236-
if (!workspacePath) {
237-
workspacePath = getConfigContext().workspaceRoot ?? '';
238-
}
255+
workspacePath = getConfigContext().workspaceRoot ?? '';
239256

240257
if (!workspacePath) {
241258
workspacePath = projectPath;
@@ -272,14 +289,22 @@ export function setInferVersion(infer: boolean): void {
272289
inferVersion = infer;
273290
}
274291

275-
export function share(shareObjects: Config, packageJsonPath = ''): Config {
276-
if (!packageJsonPath) {
277-
packageJsonPath = cwd();
292+
export function share(shareObjects: Config, projectPath = ''): Config {
293+
if (!projectPath) {
294+
projectPath = cwd();
278295
}
279296

280-
const packagePath = findPackageJson(packageJsonPath);
297+
let workspacePath: string | undefined = undefined;
298+
299+
workspacePath = getConfigContext().workspaceRoot ?? '';
300+
301+
if (!workspacePath) {
302+
workspacePath = projectPath;
303+
}
304+
305+
const packagePath = findPackageJson(projectPath);
281306

282-
const versions = readVersionMap(packagePath);
307+
// const versions = readVersionMap(packagePath);
283308
const result: any = {};
284309
let includeSecondaries;
285310

@@ -291,7 +316,8 @@ export function share(shareObjects: Config, packageJsonPath = ''): Config {
291316
shareObject.requiredVersion === 'auto' ||
292317
(inferVersion && typeof shareObject.requiredVersion === 'undefined')
293318
) {
294-
const version = lookupVersion(key, versions);
319+
const version = lookupVersion(key, projectPath, workspacePath);
320+
295321
shareObject.requiredVersion = version;
296322
shareObject.version = version.replace(/^\D*/, '');
297323
}

0 commit comments

Comments
 (0)