Skip to content

Commit fbd4af3

Browse files
committed
feat(core): support package-based monorepo
1 parent b899673 commit fbd4af3

16 files changed

+101
-42
lines changed
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@softarc/native-federation",
3-
"version": "1.0.3",
3+
"version": "1.1.0-beta.0",
44
"type": "commonjs",
55
"dependencies": {
66
"json5": "^2.2.0",
77
"npmlog": "^6.0.2",
8-
"@softarc/native-federation-runtime": "1.0.3"
8+
"@softarc/native-federation-runtime": "1.1.0-beta.0"
99
}
1010
}

libs/native-federation-core/src/lib/core/bundle-exposed.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export async function bundleExposed(
2121
const entryPoint = config.exposes[key];
2222

2323
const localPath = normalize(
24-
path.join(options.workspaceRoot, config.exposes[key])
24+
path.join(
25+
options.projectRoot ?? options.workspaceRoot,
26+
config.exposes[key])
2527
);
2628

2729
logger.info(`Bundling exposed module ${entryPoint}`);
@@ -76,7 +78,9 @@ export function describeExposed(
7678

7779
for (const key in config.exposes) {
7880
const localPath = normalize(
79-
path.join(options.workspaceRoot, config.exposes[key])
81+
path.join(
82+
options.projectRoot ?? options.workspaceRoot,
83+
config.exposes[key])
8084
);
8185

8286
result.push({

libs/native-federation-core/src/lib/core/bundle-shared.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export async function bundleShared(
1818
const result: Array<SharedInfo> = [];
1919
const packageInfos = Object.keys(config.shared)
2020
// .filter((packageName) => !isInSkipList(packageName, PREPARED_DEFAULT_SKIP_LIST))
21-
.map((packageName) => getPackageInfo(packageName, fedOptions.workspaceRoot))
21+
.map((packageName) => getPackageInfo(
22+
packageName,
23+
fedOptions.workspaceRoot,
24+
fedOptions.projectRoot ?? fedOptions.workspaceRoot
25+
))
2226
.filter((pi) => !!pi) as PackageInfo[];
2327

2428
// logger.notice('Shared packages are only bundled once as they are cached');
@@ -106,7 +110,7 @@ export async function bundleShared(
106110
});
107111

108112
const fullOutputPath = path.join(
109-
fedOptions.workspaceRoot,
113+
fedOptions.projectRoot ?? fedOptions.workspaceRoot,
110114
fedOptions.outputPath,
111115
outFileName
112116
);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export interface FederationOptions {
22
workspaceRoot: string;
3+
/**
4+
* Defaults to workspaceRoot
5+
*/
6+
projectRoot?: string;
37
outputPath: string;
48
federationConfig: string;
59
tsConfig?: string;

libs/native-federation-core/src/lib/core/load-federation-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function loadFederationConfig(
77
fedOptions: FederationOptions
88
): Promise<NormalizedFederationConfig> {
99
const fullConfigPath = path.join(
10-
fedOptions.workspaceRoot,
10+
fedOptions.projectRoot ?? fedOptions.workspaceRoot,
1111
fedOptions.federationConfig
1212
);
1313

libs/native-federation-core/src/lib/core/write-federation-info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function writeFederationInfo(
88
fedOptions: FederationOptions
99
) {
1010
const metaDataPath = path.join(
11-
fedOptions.workspaceRoot,
11+
fedOptions.projectRoot ?? fedOptions.workspaceRoot,
1212
fedOptions.outputPath,
1313
'remoteEntry.json'
1414
);

libs/native-federation-core/src/lib/core/write-import-map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function writeImportMap(
1616

1717
const importMap = { imports };
1818
const importMapPath = path.join(
19-
fedOption.workspaceRoot,
19+
fedOption.projectRoot ?? fedOption.workspaceRoot,
2020
fedOption.outputPath,
2121
'importmap.json'
2222
);
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
export function normalize(path: string): string {
2-
return path.replace(/\\/g, '/');
3-
}
1+
export function normalize(path: string, trailingSlash?: boolean): string {
2+
let cand = path.replace(/\\/g, '/');
3+
4+
if (typeof trailingSlash === 'undefined') {
5+
return cand;
6+
}
7+
8+
while (cand.endsWith('/')) {
9+
cand = cand.substring(0, cand.length-1);
10+
}
11+
12+
if (trailingSlash) {
13+
return cand + '/'
14+
}
15+
16+
return cand;
17+
}

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import { logger } from './logger';
4+
import { normalize } from './normalize';
45

56
export interface PackageInfo {
67
packageName: string;
@@ -16,18 +17,50 @@ export interface PartialPackageJson {
1617

1718
export function getPackageInfo(
1819
packageName: string,
19-
workspaceRoot: string
20+
workspaceRoot: string,
21+
projectRoot: string
2022
): PackageInfo | null {
21-
const projectRoot = workspaceRoot;
23+
24+
let currentPath = projectRoot;
25+
26+
workspaceRoot = normalize(path.dirname(workspaceRoot), true);
27+
projectRoot = normalize(path.dirname(projectRoot), true);
28+
29+
while (workspaceRoot !== currentPath) {
30+
31+
const cand = _getPackageInfo(packageName, currentPath);
32+
if (cand) {
33+
return cand;
34+
}
35+
36+
currentPath = normalize(path.dirname(currentPath), true);
37+
}
38+
39+
const result = _getPackageInfo(packageName, currentPath);
40+
41+
if (!result) {
42+
logger.warn('No meta data found for shared lib ' + packageName);
43+
}
44+
45+
return result;
46+
47+
}
48+
49+
export function _getPackageInfo(
50+
packageName: string,
51+
currentPath: string,
52+
): PackageInfo | null {
53+
54+
2255
const mainPkgName = getPkgFolder(packageName);
2356

24-
const mainPkgPath = path.join(projectRoot, 'node_modules', mainPkgName);
57+
const mainPkgPath = path.join(currentPath, 'node_modules', mainPkgName);
2558
const mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
2659

2760
if (!fs.existsSync(mainPkgPath)) {
2861
// TODO: Add logger
2962
// context.logger.warn('No package.json found for ' + packageName);
30-
logger.warn('No package.json found for ' + packageName);
63+
logger.verbose('No package.json found for ' + packageName + ' in ' + mainPkgPath);
3164

3265
return null;
3366
}
@@ -105,7 +138,7 @@ export function getPackageInfo(
105138
};
106139
}
107140

108-
const secondaryPgkPath = path.join(projectRoot, 'node_modules', packageName);
141+
const secondaryPgkPath = path.join(currentPath, 'node_modules', packageName);
109142
const secondaryPgkJsonPath = path.join(secondaryPgkPath, 'package.json');
110143
let secondaryPgkJson: PartialPackageJson | null = null;
111144
if (fs.existsSync(secondaryPgkJsonPath)) {

0 commit comments

Comments
 (0)