Skip to content

Commit 240c69f

Browse files
committed
perf(nf-core): less calls to esbuild
1 parent f7adfdb commit 240c69f

29 files changed

+851
-536
lines changed
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.1.2",
3+
"version": "2.0.0",
44
"type": "commonjs",
55
"dependencies": {
66
"json5": "^2.2.0",
77
"npmlog": "^6.0.2",
8-
"@softarc/native-federation-runtime": "1.1.2"
8+
"@softarc/native-federation-runtime": "2.0.0"
99
}
1010
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ export { FederationOptions } from './lib/core/federation-options';
33
export { setBuildAdapter } from './lib/core/build-adapter';
44
export { writeImportMap } from './lib/core/write-import-map';
55
export { writeFederationInfo } from './lib/core/write-federation-info';
6-
export { bundleShared } from './lib/core/bundle-shared';
7-
export { bundleSharedMappings } from './lib/core/bundle-shared-mappings';
8-
export { bundleExposed } from './lib/core/bundle-exposed';
96
export { getExternals } from './lib/core/get-externals';
107
export { loadFederationConfig } from './lib/core/load-federation-config';
118
export { MappedPath } from './lib/utils/mapped-paths';
12-
export { BuildAdapter, BuildAdapterOptions } from './lib/core/build-adapter';
9+
export {
10+
BuildAdapter,
11+
BuildAdapterOptions,
12+
BuildResult,
13+
BuildKind,
14+
} from './lib/core/build-adapter';
1315
export { withNativeFederation } from './lib/config/with-native-federation';
1416
export { buildForFederation } from './lib/core/build-for-federation';
1517
export {
@@ -23,3 +25,4 @@ export {
2325
} from './lib/core/federation-builder';
2426
export { logger, setLogLevel } from './lib/utils/logger';
2527
export { hashFile } from './lib/utils/hash-file';
28+
export * from './lib/utils/build-result-map';
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { MappedPath } from '../utils/mapped-paths';
2-
export type BuildKind = 'shared-package' | 'shared-mapping' | 'exposed';
2+
export type BuildKind = 'shared-package' | 'shared-mapping' | 'exposed' | 'mapping-or-exposed';
3+
export interface EntryPoint {
4+
fileName: string;
5+
outName: string;
6+
}
37
export interface BuildAdapterOptions {
4-
entryPoint: string;
5-
tsConfigPath?: string;
6-
external: Array<string>;
7-
outfile: string;
8-
mappedPaths: MappedPath[];
9-
packageName?: string;
10-
esm?: boolean;
11-
dev?: boolean;
12-
watch?: boolean;
13-
kind: BuildKind;
8+
entryPoints: EntryPoint[];
9+
tsConfigPath?: string;
10+
external: Array<string>;
11+
outdir: string;
12+
mappedPaths: MappedPath[];
13+
packageName?: string;
14+
esm?: boolean;
15+
dev?: boolean;
16+
watch?: boolean;
17+
kind: BuildKind;
18+
hash: boolean;
19+
}
20+
export interface BuildResult {
21+
fileName: string;
1422
}
15-
export type BuildAdapter = (options: BuildAdapterOptions) => Promise<void>;
23+
export type BuildAdapter = (options: BuildAdapterOptions) => Promise<BuildResult[]>;
1624
export declare function setBuildAdapter(buildAdapter: BuildAdapter): void;
1725
export declare function getBuildAdapter(): BuildAdapter;

libs/native-federation-core/src/lib/core/build-adapter.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/native-federation-core/src/lib/core/build-adapter.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,41 @@ import { MappedPath } from '../utils/mapped-paths';
44
let _buildAdapter: BuildAdapter = async () => {
55
// TODO: add logger
66
logger.error('Please set a BuildAdapter!');
7+
return [];
78
};
89

9-
export type BuildKind = 'shared-package' | 'shared-mapping' | 'exposed';
10+
export type BuildKind =
11+
| 'shared-package'
12+
| 'shared-mapping'
13+
| 'exposed'
14+
| 'mapping-or-exposed';
15+
16+
export interface EntryPoint {
17+
fileName: string;
18+
outName: string;
19+
}
1020

1121
export interface BuildAdapterOptions {
12-
entryPoint: string;
22+
entryPoints: EntryPoint[];
1323
tsConfigPath?: string;
1424
external: Array<string>;
15-
outfile: string;
25+
outdir: string;
1626
mappedPaths: MappedPath[];
1727
packageName?: string;
1828
esm?: boolean;
1929
dev?: boolean;
2030
watch?: boolean;
2131
kind: BuildKind;
32+
hash: boolean;
33+
}
34+
35+
export interface BuildResult {
36+
fileName: string;
2237
}
2338

24-
export type BuildAdapter = (options: BuildAdapterOptions) => Promise<void>;
39+
export type BuildAdapter = (
40+
options: BuildAdapterOptions
41+
) => Promise<BuildResult[]>;
2542

2643
export function setBuildAdapter(buildAdapter: BuildAdapter): void {
2744
_buildAdapter = buildAdapter;

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ import { writeImportMap } from './write-import-map';
55
import { writeFederationInfo } from './write-federation-info';
66
import { bundleShared } from './bundle-shared';
77
import {
8-
bundleSharedMappings,
8+
ArtefactInfo,
9+
bundleExposedAndMappings,
10+
describeExposed,
911
describeSharedMappings,
10-
} from './bundle-shared-mappings';
11-
import { bundleExposed, describeExposed } from './bundle-exposed';
12+
} from './bundle-exposed-and-mappings';
1213

1314
export interface BuildParams {
14-
skipMappings: boolean;
15-
skipExposed: boolean;
15+
skipMappingsAndExposed: boolean;
1616
}
1717

1818
export const defaultBuildParams: BuildParams = {
19-
skipExposed: false,
20-
skipMappings: false,
19+
skipMappingsAndExposed: false,
2120
};
2221

2322
export async function buildForFederation(
@@ -26,15 +25,24 @@ export async function buildForFederation(
2625
externals: string[],
2726
buildParams = defaultBuildParams
2827
) {
29-
const exposedInfo = buildParams.skipExposed
28+
let artefactInfo: ArtefactInfo | undefined;
29+
if (!buildParams.skipMappingsAndExposed) {
30+
artefactInfo = await bundleExposedAndMappings(
31+
config,
32+
fedOptions,
33+
externals
34+
);
35+
}
36+
37+
const exposedInfo = !artefactInfo
3038
? describeExposed(config, fedOptions)
31-
: await bundleExposed(config, fedOptions, externals);
39+
: artefactInfo.exposes;
3240

3341
const sharedPackageInfo = await bundleShared(config, fedOptions, externals);
3442

35-
const sharedMappingInfo = buildParams.skipMappings
43+
const sharedMappingInfo = !artefactInfo
3644
? describeSharedMappings(config, fedOptions)
37-
: await bundleSharedMappings(config, fedOptions, externals);
45+
: artefactInfo.mappings;
3846

3947
const sharedInfo = [...sharedPackageInfo, ...sharedMappingInfo];
4048

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import path from 'path';
2+
import { NormalizedFederationConfig } from '../config/federation-config';
3+
import { FederationOptions } from './federation-options';
4+
import { bundle } from '../utils/build-utils';
5+
import { ExposesInfo, SharedInfo } from '@softarc/native-federation-runtime';
6+
import {
7+
createBuildResultMap,
8+
lookupInResultMap,
9+
} from '../utils/build-result-map';
10+
import { logger } from '../utils/logger';
11+
12+
export interface ArtefactInfo {
13+
mappings: SharedInfo[];
14+
exposes: ExposesInfo[];
15+
}
16+
17+
export async function bundleExposedAndMappings(
18+
config: NormalizedFederationConfig,
19+
fedOptions: FederationOptions,
20+
externals: string[]
21+
): Promise<ArtefactInfo> {
22+
const shared = config.sharedMappings.map((sm) => {
23+
const entryPoint = sm.path;
24+
const tmp = sm.key.replace(/[^A-Za-z0-9]/g, '_');
25+
const outFilePath = tmp + '.js';
26+
return { fileName: entryPoint, outName: outFilePath, key: sm.key };
27+
});
28+
const exposes = Object.keys(config.exposes).map((key) => {
29+
const entryPoint = config.exposes[key];
30+
const outFilePath = key + '.js';
31+
return { fileName: entryPoint, outName: outFilePath, key };
32+
});
33+
34+
const entryPoints = [...shared, ...exposes];
35+
36+
const hash = !fedOptions.dev;
37+
38+
logger.info('Building federation artefacts');
39+
40+
const result = await bundle({
41+
entryPoints,
42+
outdir: fedOptions.outputPath,
43+
tsConfigPath: fedOptions.tsConfig,
44+
external: externals,
45+
dev: !!fedOptions.dev,
46+
watch: fedOptions.watch,
47+
mappedPaths: config.sharedMappings,
48+
kind: 'mapping-or-exposed',
49+
hash,
50+
});
51+
52+
const resultMap = createBuildResultMap(result, hash);
53+
54+
const sharedResult: Array<SharedInfo> = [];
55+
56+
for (const item of shared) {
57+
sharedResult.push({
58+
packageName: item.key,
59+
outFileName: lookupInResultMap(resultMap, item.outName),
60+
requiredVersion: '',
61+
singleton: true,
62+
strictVersion: false,
63+
version: '',
64+
dev: !fedOptions.dev
65+
? undefined
66+
: {
67+
entryPoint: path.normalize(item.fileName),
68+
},
69+
});
70+
}
71+
72+
const exposedResult: Array<ExposesInfo> = [];
73+
74+
for (const item of exposes) {
75+
exposedResult.push({
76+
key: item.key,
77+
outFileName: lookupInResultMap(resultMap, item.outName),
78+
dev: !fedOptions.dev
79+
? undefined
80+
: {
81+
entryPoint: item.fileName,
82+
},
83+
});
84+
}
85+
86+
return { mappings: sharedResult, exposes: exposedResult };
87+
}
88+
89+
export function describeExposed(
90+
config: NormalizedFederationConfig,
91+
options: FederationOptions
92+
): Array<ExposesInfo> {
93+
const result: Array<ExposesInfo> = [];
94+
95+
for (const key in config.exposes) {
96+
const localPath = path.normalize(
97+
path.join(options.workspaceRoot, config.exposes[key])
98+
);
99+
100+
result.push({
101+
key,
102+
outFileName: '',
103+
dev: !options.dev
104+
? undefined
105+
: {
106+
entryPoint: localPath,
107+
},
108+
});
109+
}
110+
111+
return result;
112+
}
113+
114+
export function describeSharedMappings(
115+
config: NormalizedFederationConfig,
116+
fedOptions: FederationOptions
117+
): Array<SharedInfo> {
118+
const result: Array<SharedInfo> = [];
119+
120+
for (const m of config.sharedMappings) {
121+
result.push({
122+
packageName: m.key,
123+
outFileName: '',
124+
requiredVersion: '',
125+
singleton: true,
126+
strictVersion: false,
127+
version: '',
128+
dev: !fedOptions.dev
129+
? undefined
130+
: {
131+
entryPoint: path.normalize(m.path),
132+
},
133+
});
134+
}
135+
136+
return result;
137+
}

0 commit comments

Comments
 (0)