Skip to content

Commit f34e134

Browse files
committed
feat(nf): add build flag with options default and separate
before, all deps were build together to improve build performance. now, you can set build to separate for selected shared deps. In this case they will be built separate. This is necessary in rare cases, e. g. where a lib imports parts of it self using it's own package name. As native federation uses externals such imports would not be bundled in when built together with other deps. However, if we build it separately, we can remove itself from the external list for this one build.
1 parent 18c2874 commit f34e134

File tree

9 files changed

+83
-21
lines changed

9 files changed

+83
-21
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.17",
3+
"version": "2.0.18",
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.17"
9+
"@softarc/native-federation-runtime": "2.0.18"
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
@@ -9,6 +9,7 @@ export interface SharedConfig {
99
includeSecondaries?: boolean;
1010
transient?: boolean;
1111
platform?: 'browser' | 'node';
12+
build?: 'default' | 'separate';
1213
packageInfo?: {
1314
entryPoint: string;
1415
version: string;
@@ -31,6 +32,7 @@ export interface NormalizedSharedConfig {
3132
version?: string;
3233
includeSecondaries?: boolean;
3334
platform: 'browser' | 'node';
35+
build: 'default' | 'separate';
3436
packageInfo?: {
3537
entryPoint: string;
3638
version: string;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import { getConfigContext } from './configuration-context';
1919
import { logger } from '../utils/logger';
2020
import { resolveGlobSync } from '../utils/resolve-glob';
21-
import { ShareObject } from '@softarc/native-federation-runtime';
2221

2322
let inferVersion = false;
2423

@@ -210,7 +209,7 @@ function readConfiguredSecondaries(
210209
key != '.' &&
211210
key != './package.json' &&
212211
key.startsWith('./') &&
213-
(exports[key]['default'] || typeof exports[key] === 'string')
212+
(exports[key]['default'] || exports[key]['import'] || typeof exports[key] === 'string')
214213
);
215214

216215
const result = {} as Record<string, SharedConfig>;
@@ -277,13 +276,22 @@ function getDefaultEntry(
277276
let entry = '';
278277
if (typeof exports[key] === 'string') {
279278
entry = exports[key] as unknown as string;
280-
} else {
279+
}
280+
281+
if (!entry) {
281282
entry = exports[key]?.['default'];
282-
283283
if (typeof entry === 'object') {
284284
entry = entry['default'];
285285
}
286286
}
287+
288+
if (!entry) {
289+
entry = exports[key]?.['import'];
290+
if (typeof entry === 'object') {
291+
entry = entry['import'];
292+
}
293+
}
294+
287295
return entry;
288296
}
289297

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ function normalizeShared(
5353
version: shared[cur].version,
5454
includeSecondaries: shared[cur].includeSecondaries,
5555
packageInfo: shared[cur].packageInfo,
56-
platform: shared[cur].platform ?? getDefaultPlatform(cur)
56+
platform: shared[cur].platform ?? getDefaultPlatform(cur),
57+
build: shared[cur].build ?? 'default'
5758
},
5859
}),
5960
{}

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

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NormalizedFederationConfig, NormalizedSharedConfig } from '../config/federation-config';
2-
import { FederationInfo } from '@softarc/native-federation-runtime';
2+
import { FederationInfo, SharedInfo } from '@softarc/native-federation-runtime';
33
import { FederationOptions } from './federation-options';
44
import { writeImportMap } from './write-import-map';
55
import { writeFederationInfo } from './write-federation-info';
@@ -39,12 +39,24 @@ export async function buildForFederation(
3939
? describeExposed(config, fedOptions)
4040
: artefactInfo.exposes;
4141

42-
const { sharedBrowser, sharedServer } = splitShared(config.shared);
42+
const {
43+
sharedBrowser,
44+
sharedServer,
45+
separateBrowser,
46+
separateServer
47+
} = splitShared(config.shared);
4348

4449
const sharedPackageInfoBrowser = await bundleShared(sharedBrowser, config, fedOptions, externals, 'browser');
4550
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');
4653

47-
const sharedPackageInfo = [...sharedPackageInfoBrowser, ...sharedPackageInfoServer];
54+
const sharedPackageInfo = [
55+
...sharedPackageInfoBrowser,
56+
...sharedPackageInfoServer,
57+
...separatePackageInfoBrowser,
58+
...separatePackageInfoServer
59+
];
4860

4961
const sharedMappingInfo = !artefactInfo
5062
? describeSharedMappings(config, fedOptions)
@@ -64,22 +76,61 @@ export async function buildForFederation(
6476
return federationInfo;
6577
}
6678

67-
function splitShared(shared: Record<string, NormalizedSharedConfig>): {sharedServer: Record<string, NormalizedSharedConfig>, sharedBrowser: Record<string, NormalizedSharedConfig>} {
79+
type SplitSharedResult = {
80+
sharedServer: Record<string, NormalizedSharedConfig>;
81+
sharedBrowser: Record<string, NormalizedSharedConfig>;
82+
separateBrowser: Record<string, NormalizedSharedConfig>;
83+
separateServer: Record<string, NormalizedSharedConfig>;
84+
};
85+
86+
function inferPackageFromSecondary(secondary: string): string {
87+
const parts = secondary.split('/');
88+
if (secondary.startsWith('@') && parts.length >= 2) {
89+
return parts[0] + '/' + parts[1];
90+
}
91+
return parts[0];
92+
}
93+
94+
async function bundleSeparate(separateBrowser: Record<string, NormalizedSharedConfig>, externals: string[], config: NormalizedFederationConfig, fedOptions: FederationOptions, platform: 'node' | 'browser') {
95+
const result: SharedInfo[] = [];
96+
for (const key in separateBrowser) {
97+
const shared = separateBrowser[key];
98+
const packageName = inferPackageFromSecondary(key);
99+
const filteredExternals = externals.filter(e => !e.startsWith(packageName));
100+
const record = { [key]: shared };
101+
const buildResult = await bundleShared(record, config, fedOptions, filteredExternals, platform);
102+
buildResult.forEach(item => result.push(item));
103+
}
104+
return result;
105+
}
106+
107+
function splitShared(shared: Record<string, NormalizedSharedConfig>): SplitSharedResult {
68108
const sharedServer: Record<string, NormalizedSharedConfig> = {};
69109
const sharedBrowser: Record<string, NormalizedSharedConfig> = {};
110+
const separateBrowser: Record<string, NormalizedSharedConfig> = {};
111+
const separateServer: Record<string, NormalizedSharedConfig> = {};
70112

71113
for (const key in shared) {
72-
if (shared[key].platform === 'node') {
73-
sharedServer[key] = shared[key];
114+
const obj = shared[key];
115+
if (obj.platform === 'node' && obj.build === 'default') {
116+
sharedServer[key] = obj;
117+
}
118+
else if (obj.platform === 'node' && obj.build === 'separate') {
119+
separateServer[key] = obj;
120+
}
121+
else if (obj.platform === 'browser' && obj.build === 'default') {
122+
sharedBrowser[key] = obj;
74123
}
75124
else {
76-
sharedBrowser[key] = shared[key];
125+
separateBrowser[key] = obj;
77126
}
78127
}
79128

80129
return {
81130
sharedBrowser,
82-
sharedServer
131+
sharedServer,
132+
separateBrowser,
133+
separateServer,
83134
};
84135

85136
}

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.17",
3+
"version": "2.0.18",
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.17"
3+
"version": "2.0.18"
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.17",
3+
"version": "2.0.18",
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.4",
3+
"version": "19.0.5",
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.17",
24-
"@softarc/native-federation-runtime": "2.0.17",
23+
"@softarc/native-federation": "2.0.18",
24+
"@softarc/native-federation-runtime": "2.0.18",
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)