Skip to content

Commit a63cfab

Browse files
committed
feat(nf-core): add externals config option to manually add externals
1 parent f34e134 commit a63cfab

File tree

10 files changed

+90
-53
lines changed

10 files changed

+90
-53
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "@softarc/native-federation",
3-
"version": "2.0.18",
3+
"version": "2.0.19",
44
"type": "commonjs",
55
"license": "MIT",
66
"dependencies": {
77
"json5": "^2.2.0",
88
"npmlog": "^6.0.2",
9-
"@softarc/native-federation-runtime": "2.0.18"
9+
"@softarc/native-federation-runtime": "2.0.19"
1010
}
1111
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface FederationConfig {
2323
shared?: Record<string, SharedConfig>;
2424
sharedMappings?: Array<string>;
2525
skip?: SkipList;
26+
externals?: string[];
2627
}
2728

2829
export interface NormalizedSharedConfig {
@@ -46,4 +47,5 @@ export interface NormalizedFederationConfig {
4647
shared: Record<string, NormalizedSharedConfig>;
4748
sharedMappings: Array<MappedPath>;
4849
skip: PreparedSkipList;
50+
externals: string[];
4951
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ function readConfiguredSecondaries(
209209
key != '.' &&
210210
key != './package.json' &&
211211
key.startsWith('./') &&
212-
(exports[key]['default'] || exports[key]['import'] || typeof exports[key] === 'string')
212+
(exports[key]['default'] ||
213+
exports[key]['import'] ||
214+
typeof exports[key] === 'string')
213215
);
214216

215217
const result = {} as Record<string, SharedConfig>;
@@ -276,8 +278,8 @@ function getDefaultEntry(
276278
let entry = '';
277279
if (typeof exports[key] === 'string') {
278280
entry = exports[key] as unknown as string;
279-
}
280-
281+
}
282+
281283
if (!entry) {
282284
entry = exports[key]?.['default'];
283285
if (typeof entry === 'object') {
@@ -289,7 +291,7 @@ function getDefaultEntry(
289291
entry = exports[key]?.['import'];
290292
if (typeof entry === 'object') {
291293
entry = entry['import'];
292-
}
294+
}
293295
}
294296

295297
return entry;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function withNativeFederation(
2323
exposes: config.exposes ?? {},
2424
shared: normalizeShared(config, skip),
2525
sharedMappings: normalizeSharedMappings(config, skip),
26-
skip
26+
skip,
27+
externals: config.externals ?? [],
2728
};
2829
}
2930

@@ -54,7 +55,7 @@ function normalizeShared(
5455
includeSecondaries: shared[cur].includeSecondaries,
5556
packageInfo: shared[cur].packageInfo,
5657
platform: shared[cur].platform ?? getDefaultPlatform(cur),
57-
build: shared[cur].build ?? 'default'
58+
build: shared[cur].build ?? 'default',
5859
},
5960
}),
6061
{}
@@ -87,8 +88,7 @@ function normalizeSharedMappings(
8788
sharedMappings: config.sharedMappings,
8889
});
8990

90-
const result = paths
91-
.filter(
91+
const result = paths.filter(
9292
(p) => !isInSkipList(p.key, skip) && !p.key.includes('*')
9393
);
9494

@@ -100,10 +100,9 @@ function normalizeSharedMappings(
100100
}
101101

102102
function getDefaultPlatform(cur: string): 'browser' | 'node' {
103-
if (DEFAULT_SERVER_DEPS_LIST.find(e => cur.startsWith(e))) {
103+
if (DEFAULT_SERVER_DEPS_LIST.find((e) => cur.startsWith(e))) {
104104
return 'node';
105-
}
106-
else {
105+
} else {
107106
return 'browser';
108107
}
109108
}

libs/native-federation-core/src/lib/core/build-for-federation.ts

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { NormalizedFederationConfig, NormalizedSharedConfig } from '../config/federation-config';
1+
import {
2+
NormalizedFederationConfig,
3+
NormalizedSharedConfig,
4+
} from '../config/federation-config';
25
import { FederationInfo, SharedInfo } from '@softarc/native-federation-runtime';
36
import { FederationOptions } from './federation-options';
47
import { writeImportMap } from './write-import-map';
@@ -19,14 +22,15 @@ export const defaultBuildParams: BuildParams = {
1922
skipMappingsAndExposed: false,
2023
};
2124

22-
2325
export async function buildForFederation(
2426
config: NormalizedFederationConfig,
2527
fedOptions: FederationOptions,
2628
externals: string[],
2729
buildParams = defaultBuildParams
2830
) {
31+
2932
let artefactInfo: ArtefactInfo | undefined;
33+
3034
if (!buildParams.skipMappingsAndExposed) {
3135
artefactInfo = await bundleExposedAndMappings(
3236
config,
@@ -39,23 +43,46 @@ export async function buildForFederation(
3943
? describeExposed(config, fedOptions)
4044
: artefactInfo.exposes;
4145

42-
const {
43-
sharedBrowser,
44-
sharedServer,
45-
separateBrowser,
46-
separateServer
47-
} = splitShared(config.shared);
46+
const { sharedBrowser, sharedServer, separateBrowser, separateServer } =
47+
splitShared(config.shared);
48+
49+
const sharedPackageInfoBrowser = await bundleShared(
50+
sharedBrowser,
51+
config,
52+
fedOptions,
53+
externals,
54+
'browser'
55+
);
4856

49-
const sharedPackageInfoBrowser = await bundleShared(sharedBrowser, config, fedOptions, externals, 'browser');
50-
const sharedPackageInfoServer = await bundleShared(sharedServer, config, fedOptions, externals, 'node');
51-
const separatePackageInfoBrowser = await bundleSeparate(separateBrowser, externals, config, fedOptions, 'browser');
52-
const separatePackageInfoServer = await bundleSeparate(separateServer, externals, config, fedOptions, 'node');
57+
const sharedPackageInfoServer = await bundleShared(
58+
sharedServer,
59+
config,
60+
fedOptions,
61+
externals,
62+
'node'
63+
);
64+
65+
const separatePackageInfoBrowser = await bundleSeparate(
66+
separateBrowser,
67+
externals,
68+
config,
69+
fedOptions,
70+
'browser'
71+
);
72+
73+
const separatePackageInfoServer = await bundleSeparate(
74+
separateServer,
75+
externals,
76+
config,
77+
fedOptions,
78+
'node'
79+
);
5380

5481
const sharedPackageInfo = [
55-
...sharedPackageInfoBrowser,
82+
...sharedPackageInfoBrowser,
5683
...sharedPackageInfoServer,
5784
...separatePackageInfoBrowser,
58-
...separatePackageInfoServer
85+
...separatePackageInfoServer,
5986
];
6087

6188
const sharedMappingInfo = !artefactInfo
@@ -86,25 +113,41 @@ type SplitSharedResult = {
86113
function inferPackageFromSecondary(secondary: string): string {
87114
const parts = secondary.split('/');
88115
if (secondary.startsWith('@') && parts.length >= 2) {
89-
return parts[0] + '/' + parts[1];
116+
return parts[0] + '/' + parts[1];
90117
}
91118
return parts[0];
92119
}
93120

94-
async function bundleSeparate(separateBrowser: Record<string, NormalizedSharedConfig>, externals: string[], config: NormalizedFederationConfig, fedOptions: FederationOptions, platform: 'node' | 'browser') {
121+
async function bundleSeparate(
122+
separateBrowser: Record<string, NormalizedSharedConfig>,
123+
externals: string[],
124+
config: NormalizedFederationConfig,
125+
fedOptions: FederationOptions,
126+
platform: 'node' | 'browser'
127+
) {
95128
const result: SharedInfo[] = [];
96129
for (const key in separateBrowser) {
97130
const shared = separateBrowser[key];
98131
const packageName = inferPackageFromSecondary(key);
99-
const filteredExternals = externals.filter(e => !e.startsWith(packageName));
132+
const filteredExternals = externals.filter(
133+
(e) => !e.startsWith(packageName)
134+
);
100135
const record = { [key]: shared };
101-
const buildResult = await bundleShared(record, config, fedOptions, filteredExternals, platform);
102-
buildResult.forEach(item => result.push(item));
136+
const buildResult = await bundleShared(
137+
record,
138+
config,
139+
fedOptions,
140+
filteredExternals,
141+
platform
142+
);
143+
buildResult.forEach((item) => result.push(item));
103144
}
104145
return result;
105146
}
106147

107-
function splitShared(shared: Record<string, NormalizedSharedConfig>): SplitSharedResult {
148+
function splitShared(
149+
shared: Record<string, NormalizedSharedConfig>
150+
): SplitSharedResult {
108151
const sharedServer: Record<string, NormalizedSharedConfig> = {};
109152
const sharedBrowser: Record<string, NormalizedSharedConfig> = {};
110153
const separateBrowser: Record<string, NormalizedSharedConfig> = {};
@@ -114,24 +157,19 @@ function splitShared(shared: Record<string, NormalizedSharedConfig>): SplitShare
114157
const obj = shared[key];
115158
if (obj.platform === 'node' && obj.build === 'default') {
116159
sharedServer[key] = obj;
117-
}
118-
else if (obj.platform === 'node' && obj.build === 'separate') {
160+
} else if (obj.platform === 'node' && obj.build === 'separate') {
119161
separateServer[key] = obj;
120-
}
121-
else if (obj.platform === 'browser' && obj.build === 'default') {
162+
} else if (obj.platform === 'browser' && obj.build === 'default') {
122163
sharedBrowser[key] = obj;
123-
}
124-
else {
164+
} else {
125165
separateBrowser[key] = obj;
126166
}
127167
}
128168

129169
return {
130-
sharedBrowser,
170+
sharedBrowser,
131171
sharedServer,
132172
separateBrowser,
133173
separateServer,
134174
};
135-
136175
}
137-

libs/native-federation-core/src/lib/core/get-externals.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { NormalizedFederationConfig } from '../config/federation-config';
33
export function getExternals(config: NormalizedFederationConfig) {
44
const shared = Object.keys(config.shared);
55
const sharedMappings = config.sharedMappings.map((m) => m.key);
6-
7-
// TODO: Also handle deps that match RegExps and functions
8-
const depsToSkip = config.skip.strings;
9-
const externals = [...shared, ...sharedMappings, ...depsToSkip];
10-
6+
const externals = [...shared, ...sharedMappings, ...config.externals];
117
return externals;
128
}

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": "2.0.18",
3+
"version": "2.0.19",
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,4 +1,4 @@
11
{
22
"name": "@softarc/native-federation-node",
3-
"version": "2.0.18"
3+
"version": "2.0.19"
44
}

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": "2.0.18",
3+
"version": "2.0.19",
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": "19.0.5",
3+
"version": "19.0.6",
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": "2.0.18",
24-
"@softarc/native-federation-runtime": "2.0.18",
23+
"@softarc/native-federation": "2.0.19",
24+
"@softarc/native-federation-runtime": "2.0.19",
2525
"@types/browser-sync": "^2.29.0",
2626
"@chialab/esbuild-plugin-commonjs": "^0.18.0",
2727
"browser-sync": "^3.0.2",

0 commit comments

Comments
 (0)