Skip to content

Commit 129dac6

Browse files
authored
feat(managers): add root option (#3730)
1 parent 22107da commit 129dac6

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

.changeset/silent-seas-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/managers': patch
3+
---
4+
5+
feat(managers): add root option

packages/managers/src/BasicPluginOptionsManager.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export class BasicPluginOptionsManager<T> {
22
private _options?: T;
3+
private _root: string;
4+
5+
constructor(root = process.cwd()) {
6+
this._root = root;
7+
}
38

49
get enable(): boolean {
510
return Boolean(this._options);
@@ -9,6 +14,10 @@ export class BasicPluginOptionsManager<T> {
914
return this._options as unknown as T;
1015
}
1116

17+
get root(): string {
18+
return this._root;
19+
}
20+
1221
init(options: T, extraArgs?: Record<string, any>): void {
1322
this._options = options;
1423
}

packages/managers/src/PKGJsonManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ export class PKGJsonManager {
1111
this._pkg = pkg;
1212
}
1313

14-
readPKGJson(ctx = process.cwd()): Record<string, any> {
14+
readPKGJson(root = process.cwd()): Record<string, any> {
1515
if (this._pkg) {
1616
return this._pkg;
1717
}
1818
try {
1919
// eslint-disable-next-line no-restricted-globals
2020
const pkg = JSON.parse(
21-
fs.readFileSync(path.resolve(ctx, 'package.json'), 'utf8'),
21+
fs.readFileSync(path.resolve(root, 'package.json'), 'utf8'),
2222
);
2323
this._pkg = pkg;
2424
return pkg;
2525
} catch (_err) {
2626
try {
27-
const pkg = finder.sync(ctx);
27+
const pkg = finder.sync(root);
2828
this._pkg = pkg;
2929
return pkg;
3030
} catch (err) {
@@ -34,8 +34,8 @@ export class PKGJsonManager {
3434
}
3535
}
3636

37-
getExposeGarfishModuleType(ctx = process.cwd()): string {
38-
const pkg = this.readPKGJson(ctx);
37+
getExposeGarfishModuleType(root = process.cwd()): string {
38+
const pkg = this.readPKGJson(root);
3939
return pkg?.['mf']?.type === MFModuleType.NPM
4040
? MFModuleType.NPM
4141
: MFModuleType.APP;

packages/managers/src/SharedManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ class SharedManager extends BasicPluginOptionsManager<moduleFederationPlugin.Mod
6464
if (path.isAbsolute(shareConfig.import)) {
6565
pkgPath = shareConfig.import;
6666
} else if (shareConfig.import.startsWith('.')) {
67-
pkgPath = path.resolve(process.cwd(), shareConfig.import);
67+
pkgPath = path.resolve(this.root, shareConfig.import);
6868
}
6969
} else {
7070
if (shareConfig.packageName) {
7171
depName = shareConfig.packageName;
7272
}
7373
}
74-
pkgPath = pkgPath || require.resolve(depName, { paths: [process.cwd()] });
74+
pkgPath = pkgPath || require.resolve(depName, { paths: [this.root] });
7575
const pkgJsonPath = findPkg.sync(pkgPath);
7676
return {
7777
pkg: JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')),

0 commit comments

Comments
 (0)