Skip to content

Commit 73761ca

Browse files
committed
feat(nf): delegate chunk splitting to bundler
1 parent 1e43b08 commit 73761ca

File tree

7 files changed

+104
-32
lines changed

7 files changed

+104
-32
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.28",
3+
"version": "3.0.0",
44
"type": "commonjs",
55
"license": "MIT",
66
"dependencies": {
77
"json5": "^2.2.0",
88
"chalk": "^4.1.2",
9-
"@softarc/native-federation-runtime": "2.0.28"
9+
"@softarc/native-federation-runtime": "3.0.0"
1010
}
1111
}

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

Lines changed: 95 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { logger } from '../utils/logger';
1313
import { normalize } from '../utils/normalize';
1414
import crypto from 'crypto';
1515
import { DEFAULT_EXTERNAL_LIST } from './default-external-list';
16+
import { BuildResult } from './build-adapter';
1617

1718
export async function bundleShared(
1819
sharedBundles: Record<string, NormalizedSharedConfig>,
@@ -52,18 +53,7 @@ export async function bundleShared(
5253

5354
const allEntryPoints = packageInfos.map((pi) => {
5455
const encName = pi.packageName.replace(/[^A-Za-z0-9]/g, '_');
55-
// const encVersion = pi.version.replace(/[^A-Za-z0-9]/g, '_');
56-
57-
// const outName = fedOptions.dev
58-
// ? `${encName}-${encVersion}-dev.js`
59-
// : `${encName}-${encVersion}.js`;
60-
61-
const hash = calcHash(pi, configState);
62-
63-
const outName = fedOptions.dev
64-
? `${encName}.${hash}-dev.js`
65-
: `${encName}.${hash}.js`;
66-
56+
const outName = createOutName(pi, configState, fedOptions, encName);
6757
return { fileName: pi.entryPoint, outName };
6858
});
6959

@@ -90,8 +80,10 @@ export async function bundleShared(
9080
const additionalExternals =
9181
platform === 'browser' ? DEFAULT_EXTERNAL_LIST : [];
9282

83+
let bundleResult: BuildResult[] | null = null;
84+
9385
try {
94-
await bundle({
86+
bundleResult = await bundle({
9587
entryPoints,
9688
tsConfigPath: fedOptions.tsConfig,
9789
external: [...additionalExternals, ...externals],
@@ -103,13 +95,9 @@ export async function bundleShared(
10395
platform,
10496
});
10597

106-
for (const fileName of exptedResults) {
107-
const outFileName = path.basename(fileName);
108-
const cachedFile = path.join(cachePath, outFileName);
98+
const cachedFiles = bundleResult.map(br => path.basename(br.fileName));
99+
copyCacheToOutput(cachedFiles, cachePath, fullOutputPath);
109100

110-
copyFileIfExists(cachedFile, fileName);
111-
copySrcMapIfExists(cachedFile, fileName);
112-
}
113101
} catch (e) {
114102
logger.error('Error bundling shared npm package ');
115103
if (e instanceof Error) {
@@ -142,8 +130,73 @@ export async function bundleShared(
142130
throw e;
143131
}
144132

133+
const resultCacheFile = createCacheFileName(
134+
configState,
135+
sharedBundles,
136+
fedOptions,
137+
cachePath,
138+
platform
139+
);
140+
141+
if (fs.existsSync(resultCacheFile)) {
142+
const cachedResult: SharedInfo[] = JSON.parse(fs.readFileSync(resultCacheFile, 'utf-8'));
143+
const cachedFiles = cachedResult.map(cr => cr.outFileName);
144+
copyCacheToOutput(cachedFiles, cachePath, fullOutputPath)
145+
return cachedResult;
146+
}
147+
145148
const outFileNames = [...exptedResults];
146149

150+
const result = buildResult(
151+
packageInfos,
152+
sharedBundles,
153+
outFileNames,
154+
fedOptions);
155+
156+
const chunks = bundleResult.filter(
157+
(br) => !result.find((r) =>
158+
r.outFileName === path.basename(br.fileName))
159+
);
160+
161+
addChunksToResult(chunks, result, fedOptions.dev);
162+
163+
fs.writeFileSync(
164+
resultCacheFile,
165+
JSON.stringify(result, undefined, 2),
166+
'utf-8'
167+
);
168+
169+
return result;
170+
}
171+
172+
function copyCacheToOutput(cachedFiles: string[], cachePath: string, fullOutputPath: string) {
173+
for (const fileName of cachedFiles) {
174+
const cachedFile = path.join(cachePath, fileName);
175+
const distFileName = path.join(fullOutputPath, fileName);
176+
copyFileIfExists(cachedFile, distFileName);
177+
copySrcMapIfExists(cachedFile, distFileName);
178+
}
179+
}
180+
181+
function createOutName(pi: PackageInfo, configState: string, fedOptions: FederationOptions, encName: string) {
182+
const hashBase = pi.version + '_' + pi.entryPoint + '_' + configState;
183+
const hash = calcHash(hashBase);
184+
185+
const outName = fedOptions.dev
186+
? `${encName}.${hash}-dev.js`
187+
: `${encName}.${hash}.js`;
188+
return outName;
189+
}
190+
191+
function createCacheFileName(configState: string, sharedBundles: Record<string, NormalizedSharedConfig>, fedOptions: FederationOptions, cachePath: string, platform: string) {
192+
const resultCacheState = configState + JSON.stringify(sharedBundles);
193+
const resultHash = calcHash(resultCacheState);
194+
const dev = fedOptions.dev ? '-dev' : '';
195+
const resultCacheFile = path.join(cachePath, 'result-' + resultHash + '-' + platform + dev + '.json');
196+
return resultCacheFile;
197+
}
198+
199+
function buildResult(packageInfos: PackageInfo[], sharedBundles: Record<string, NormalizedSharedConfig>, outFileNames: string[], fedOptions: FederationOptions) {
147200
return packageInfos.map((pi) => {
148201
const shared = sharedBundles[pi.packageName];
149202
return {
@@ -156,14 +209,32 @@ export async function bundleShared(
156209
dev: !fedOptions.dev
157210
? undefined
158211
: {
159-
entryPoint: normalize(pi.entryPoint),
160-
},
212+
entryPoint: normalize(pi.entryPoint),
213+
},
161214
} as SharedInfo;
162215
});
163216
}
164217

165-
function calcHash(pi: PackageInfo, configState: string) {
166-
const hashBase = pi.version + '_' + pi.entryPoint + '_' + configState;
218+
function addChunksToResult(chunks: BuildResult[], result: SharedInfo[], dev?: boolean) {
219+
for (const item of chunks) {
220+
const fileName = path.basename(item.fileName);
221+
result.push({
222+
singleton: false,
223+
strictVersion: false,
224+
requiredVersion: '0.0.0',
225+
version: '0.0.0',
226+
packageName: fileName,
227+
outFileName: fileName,
228+
dev: dev
229+
? undefined
230+
: {
231+
entryPoint: normalize(fileName),
232+
},
233+
});
234+
}
235+
}
236+
237+
function calcHash(hashBase: string) {
167238
const hash = crypto
168239
.createHash('sha256')
169240
.update(hashBase)
@@ -175,6 +246,7 @@ function calcHash(pi: PackageInfo, configState: string) {
175246
return hash;
176247
}
177248

249+
178250
function copyFileIfExists(cachedFile: string, fullOutputPath: string) {
179251
fs.mkdirSync(path.dirname(fullOutputPath), { recursive: true });
180252

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.28",
3+
"version": "3.0.0",
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.28"
3+
"version": "3.0.0"
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.28",
3+
"version": "3.0.0",
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.18",
3+
"version": "19.0.19",
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.28",
24-
"@softarc/native-federation-runtime": "2.0.28",
23+
"@softarc/native-federation": "3.0.0",
24+
"@softarc/native-federation-runtime": "3.0.0",
2525
"@chialab/esbuild-plugin-commonjs": "^0.18.0",
2626
"esbuild": "^0.25.1",
2727
"mrmime": "^1.0.1",

libs/native-federation/src/utils/angular-esbuild-adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ async function runEsbuild(
275275
'async-await': false,
276276
'object-rest-spread': false,
277277
},
278-
splitting: kind === 'mapping-or-exposed',
278+
splitting: true, //kind === 'mapping-or-exposed',
279279
platform: platform ?? 'browser',
280280
format: 'esm',
281281
target: target,

0 commit comments

Comments
 (0)