Skip to content

Commit 8d166c8

Browse files
committed
feat(nf-core): fall back to entrypoint.[m]js when no index.[m]js is found
1 parent 229a79f commit 8d166c8

File tree

9 files changed

+133
-89
lines changed

9 files changed

+133
-89
lines changed

libs/native-federation-core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@softarc/native-federation",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"type": "commonjs",
55
"license": "MIT",
66
"dependencies": {
77
"json5": "^2.2.0",
88
"chalk": "^4.1.2",
9-
"@softarc/native-federation-runtime": "3.1.0"
9+
"@softarc/native-federation-runtime": "3.1.1"
1010
},
1111
"peerDependencies": {
1212
"@softarc/sheriff-core": "^0.18.0"

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ export async function bundleShared(
7676
"Skip packages you don't want to share in your federation config"
7777
);
7878
}
79-
79+
8080
// If we build for the browser and don't remote unused deps from the shared config,
8181
// we need to exclude typical node libs to avoid compilation issues
82-
const useDefaultExternalList = platform === 'browser' && !config.features.ignoreUnusedDeps;
82+
const useDefaultExternalList =
83+
platform === 'browser' && !config.features.ignoreUnusedDeps;
8384

84-
const additionalExternals =
85-
useDefaultExternalList ? DEFAULT_EXTERNAL_LIST : [];
85+
const additionalExternals = useDefaultExternalList
86+
? DEFAULT_EXTERNAL_LIST
87+
: [];
8688

8789
let bundleResult: BuildResult[] | null = null;
8890

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ export async function loadFederationConfig(
1919
const config = (await import(fullConfigPath)) as NormalizedFederationConfig;
2020

2121
if (config.features.ignoreUnusedDeps && !fedOptions.entryPoint) {
22-
throw new Error(`The feature ignoreUnusedDeps needs the application's entry point. Please set it in your federation options!`);
22+
throw new Error(
23+
`The feature ignoreUnusedDeps needs the application's entry point. Please set it in your federation options!`
24+
);
2325
}
2426

2527
if (config.features.ignoreUnusedDeps) {
2628
return removeUnusedDeps(
2729
config,
2830
fedOptions.entryPoint ?? '',
29-
fedOptions.workspaceRoot,
30-
)
31+
fedOptions.workspaceRoot
32+
);
3133
}
3234

3335
return config;

libs/native-federation-core/src/lib/core/remove-unused-deps.ts

Lines changed: 94 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { getExternalImports as extractExternalImports } from '../utils/get-exter
88
import { MappedPath } from '../utils/mapped-paths';
99
import { ProjectData } from '@softarc/sheriff-core/src/lib/api/get-project-data';
1010

11-
export function removeUnusedDeps(config: NormalizedFederationConfig, main: string, workspaceRoot: string): NormalizedFederationConfig {
11+
export function removeUnusedDeps(
12+
config: NormalizedFederationConfig,
13+
main: string,
14+
workspaceRoot: string
15+
): NormalizedFederationConfig {
1216
const fileInfos = getProjectData(main, cwd(), {
1317
includeExternalLibraries: true,
1418
});
@@ -17,7 +21,10 @@ export function removeUnusedDeps(config: NormalizedFederationConfig, main: strin
1721
const usedPackageNames = usedDeps.usedPackageNames;
1822
const usedMappings = usedDeps.usedMappings;
1923

20-
const usedPackageNamesWithTransient = addTransientDeps(usedPackageNames, workspaceRoot);
24+
const usedPackageNamesWithTransient = addTransientDeps(
25+
usedPackageNames,
26+
workspaceRoot
27+
);
2128
const filteredShared = filterShared(config, usedPackageNamesWithTransient);
2229

2330
return {
@@ -27,92 +34,105 @@ export function removeUnusedDeps(config: NormalizedFederationConfig, main: strin
2734
};
2835
}
2936

30-
function filterShared(config: NormalizedFederationConfig, usedPackageNamesWithTransient: Set<string>) {
31-
const filteredSharedNames = Object.keys(config.shared).filter(
32-
(shared) => usedPackageNamesWithTransient.has(shared)
33-
);
34-
35-
const filteredShared = filteredSharedNames.reduce(
36-
(acc, curr) => ({ ...acc, [curr]: config.shared[curr] }),
37-
{}
38-
);
39-
return filteredShared;
37+
function filterShared(
38+
config: NormalizedFederationConfig,
39+
usedPackageNamesWithTransient: Set<string>
40+
) {
41+
const filteredSharedNames = Object.keys(config.shared).filter((shared) =>
42+
usedPackageNamesWithTransient.has(shared)
43+
);
44+
45+
const filteredShared = filteredSharedNames.reduce(
46+
(acc, curr) => ({ ...acc, [curr]: config.shared[curr] }),
47+
{}
48+
);
49+
return filteredShared;
4050
}
4151

42-
function findUsedDeps(fileInfos: ProjectData, workspaceRoot: string, config: NormalizedFederationConfig) {
43-
const usedPackageNames = new Set<string>();
44-
const usedMappings = new Set<MappedPath>();
52+
function findUsedDeps(
53+
fileInfos: ProjectData,
54+
workspaceRoot: string,
55+
config: NormalizedFederationConfig
56+
) {
57+
const usedPackageNames = new Set<string>();
58+
const usedMappings = new Set<MappedPath>();
4559

46-
for (const fileName of Object.keys(fileInfos)) {
47-
const fileInfo = fileInfos[fileName];
60+
for (const fileName of Object.keys(fileInfos)) {
61+
const fileInfo = fileInfos[fileName];
4862

49-
if (!fileInfo || !fileInfo.externalLibraries) {
50-
continue;
51-
}
63+
if (!fileInfo || !fileInfo.externalLibraries) {
64+
continue;
65+
}
5266

53-
for (const pckg of fileInfo.externalLibraries) {
54-
usedPackageNames.add(pckg);
55-
}
67+
for (const pckg of fileInfo.externalLibraries) {
68+
usedPackageNames.add(pckg);
69+
}
5670

57-
const fullFileName = path.join(workspaceRoot, fileName);
58-
const mappings = config.sharedMappings.filter(
59-
(sm) => sm.path === fullFileName
60-
);
71+
const fullFileName = path.join(workspaceRoot, fileName);
72+
const mappings = config.sharedMappings.filter(
73+
(sm) => sm.path === fullFileName
74+
);
6175

62-
for (const mapping of mappings) {
63-
usedMappings.add(mapping);
64-
}
76+
for (const mapping of mappings) {
77+
usedMappings.add(mapping);
6578
}
66-
return { usedPackageNames, usedMappings };
79+
}
80+
return { usedPackageNames, usedMappings };
6781
}
6882

6983
function addTransientDeps(packages: Set<string>, workspaceRoot: string) {
70-
const packagesAndPeers = new Set<string>([...packages]);
71-
const discovered = new Set<string>(packagesAndPeers);
72-
const stack = [...packagesAndPeers];
73-
74-
while (stack.length > 0) {
75-
const dep = stack.pop();
76-
77-
if (!dep) {
78-
continue;
79-
}
80-
81-
const pInfo = getPackageInfo(dep, workspaceRoot);
82-
83-
if (!pInfo) {
84-
continue;
85-
}
86-
87-
const peerDeps = getExternalImports(pInfo, workspaceRoot);
88-
89-
for (const peerDep of peerDeps) {
90-
if (!discovered.has(peerDep)) {
91-
discovered.add(peerDep);
92-
stack.push(peerDep);
93-
packagesAndPeers.add(peerDep);
94-
}
95-
}
96-
}
97-
return packagesAndPeers;
98-
}
84+
const packagesAndPeers = new Set<string>([...packages]);
85+
const discovered = new Set<string>(packagesAndPeers);
86+
const stack = [...packagesAndPeers];
9987

100-
function getExternalImports(pInfo: PackageInfo, workspaceRoot: string) {
101-
const encodedPackageName = pInfo.packageName.replace(/[^A-Za-z0-9]/g, '_');
102-
const cacheFileName = `${encodedPackageName}-${pInfo.version}.deps.json`;
103-
const cachePath = path.join(workspaceRoot, 'node_modules/.cache/native-federation');
104-
const cacheFilePath = path.join(cachePath, cacheFileName);
88+
while (stack.length > 0) {
89+
const dep = stack.pop();
10590

106-
const cacheHit = fs.existsSync(cacheFilePath);
91+
if (!dep) {
92+
continue;
93+
}
94+
95+
const pInfo = getPackageInfo(dep, workspaceRoot);
10796

108-
let peerDeps;
109-
if (cacheHit) {
110-
peerDeps = JSON.parse(fs.readFileSync(cacheFilePath, 'utf-8'));
97+
if (!pInfo) {
98+
continue;
11199
}
112-
else {
113-
peerDeps = extractExternalImports(pInfo.entryPoint);
114-
fs.mkdirSync(cachePath, { recursive: true });
115-
fs.writeFileSync(cacheFilePath, JSON.stringify(peerDeps, undefined, 2), 'utf-8');
100+
101+
const peerDeps = getExternalImports(pInfo, workspaceRoot);
102+
103+
for (const peerDep of peerDeps) {
104+
if (!discovered.has(peerDep)) {
105+
discovered.add(peerDep);
106+
stack.push(peerDep);
107+
packagesAndPeers.add(peerDep);
108+
}
116109
}
117-
return peerDeps;
110+
}
111+
return packagesAndPeers;
112+
}
113+
114+
function getExternalImports(pInfo: PackageInfo, workspaceRoot: string) {
115+
const encodedPackageName = pInfo.packageName.replace(/[^A-Za-z0-9]/g, '_');
116+
const cacheFileName = `${encodedPackageName}-${pInfo.version}.deps.json`;
117+
const cachePath = path.join(
118+
workspaceRoot,
119+
'node_modules/.cache/native-federation'
120+
);
121+
const cacheFilePath = path.join(cachePath, cacheFileName);
122+
123+
const cacheHit = fs.existsSync(cacheFilePath);
124+
125+
let peerDeps;
126+
if (cacheHit) {
127+
peerDeps = JSON.parse(fs.readFileSync(cacheFilePath, 'utf-8'));
128+
} else {
129+
peerDeps = extractExternalImports(pInfo.entryPoint);
130+
fs.mkdirSync(cachePath, { recursive: true });
131+
fs.writeFileSync(
132+
cacheFilePath,
133+
JSON.stringify(peerDeps, undefined, 2),
134+
'utf-8'
135+
);
136+
}
137+
return peerDeps;
118138
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ export function _getPackageInfo(
330330
};
331331
}
332332

333+
cand = secondaryPgkPath + '.js';
334+
if (fs.existsSync(cand)) {
335+
return {
336+
entryPoint: cand,
337+
packageName,
338+
version,
339+
esm,
340+
};
341+
}
342+
343+
cand = secondaryPgkPath + '.mjs';
344+
if (fs.existsSync(cand)) {
345+
return {
346+
entryPoint: cand,
347+
packageName,
348+
version,
349+
esm,
350+
};
351+
}
352+
333353
// TODO: Add logger
334354
logger.warn('No entry point found for ' + packageName);
335355
logger.warn(

libs/native-federation-esbuild/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@softarc/native-federation-esbuild",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"type": "commonjs",
55
"dependencies": {
66
"@rollup/plugin-commonjs": "^22.0.2",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "@softarc/native-federation-node",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"license": "MIT"
55
}

libs/native-federation-runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@softarc/native-federation-runtime",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"dependencies": {
55
"tslib": "^2.3.0"
66
},

libs/native-federation/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-architects/native-federation",
3-
"version": "20.0.3",
3+
"version": "20.0.4",
44
"main": "src/index.js",
55
"generators": "./collection.json",
66
"builders": "./builders.json",
@@ -20,8 +20,8 @@
2020
},
2121
"dependencies": {
2222
"@babel/core": "^7.19.0",
23-
"@softarc/native-federation": "3.1.0",
24-
"@softarc/native-federation-runtime": "3.1.0",
23+
"@softarc/native-federation": "3.1.1",
24+
"@softarc/native-federation-runtime": "3.1.1",
2525
"@chialab/esbuild-plugin-commonjs": "^0.18.0",
2626
"esbuild": "^0.25.1",
2727
"mrmime": "^1.0.1",

0 commit comments

Comments
 (0)