@module-federation/esbuild 0.0.0-next-20240524225331 → 0.0.0-next-20240528203231
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -35
- package/dist/build.cjs.js +704 -29
- package/dist/build.esm.js +678 -4
- package/dist/esbuild.cjs.js +178 -176
- package/dist/esbuild.esm.js +178 -176
- package/dist/get-externals.cjs.js +20 -0
- package/dist/get-externals.esm.js +18 -0
- package/dist/src/adapters/lib/containerPlugin.d.ts +2 -1
- package/dist/src/adapters/lib/containerReference.d.ts +3 -2
- package/dist/src/adapters/lib/linkRemotesPlugin.d.ts +2 -1
- package/dist/src/adapters/lib/plugin.d.ts +2 -1
- package/package.json +2 -2
- package/dist/federation-builder.cjs.js +0 -741
- package/dist/federation-builder.esm.js +0 -697
package/dist/build.esm.js
CHANGED
|
@@ -1,13 +1,103 @@
|
|
|
1
|
-
import
|
|
2
|
-
export { j as buildForFederation, k as bundleExposedAndMappings, p as createBuildResultMap, i as defaultBuildParams, m as describeExposed, n as describeSharedMappings, f as federationBuilder, c as getBuildAdapter, e as getExternals, h as loadFederationConfig, l as logger, q as lookupInResultMap, s as setBuildAdapter, o as setLogLevel, d as writeFederationInfo, w as writeImportMap } from './federation-builder.esm.js';
|
|
1
|
+
import npmlog from 'npmlog';
|
|
3
2
|
import * as path from 'path';
|
|
4
3
|
import path__default from 'path';
|
|
5
4
|
import * as fs from 'fs';
|
|
6
5
|
import fs__default from 'fs';
|
|
6
|
+
import { g as getExternals } from './get-externals.esm.js';
|
|
7
|
+
export { g as getExternals } from './get-externals.esm.js';
|
|
7
8
|
import process from 'process';
|
|
8
9
|
import * as JSON5 from 'json5';
|
|
9
10
|
import * as crypto from 'crypto';
|
|
10
|
-
|
|
11
|
+
|
|
12
|
+
const levels = npmlog.levels;
|
|
13
|
+
npmlog.addLevel('error', levels.error, {
|
|
14
|
+
fg: 'brightWhite',
|
|
15
|
+
bg: 'red'
|
|
16
|
+
}, ' ERR! ');
|
|
17
|
+
npmlog.addLevel('warn', levels.info, {
|
|
18
|
+
fg: 'brightWhite',
|
|
19
|
+
bg: 'yellow'
|
|
20
|
+
}, ' WARN ');
|
|
21
|
+
npmlog.addLevel('info', levels.warn, {
|
|
22
|
+
fg: 'brightWhite',
|
|
23
|
+
bg: 'green'
|
|
24
|
+
}, ' INFO ');
|
|
25
|
+
npmlog.addLevel('notice', levels.notice, {
|
|
26
|
+
fg: 'black',
|
|
27
|
+
bg: 'brightYellow'
|
|
28
|
+
}, ' NOTE ');
|
|
29
|
+
npmlog.addLevel('verbose', levels.verbose, {
|
|
30
|
+
fg: 'brightWhite',
|
|
31
|
+
bg: 'brightBlue'
|
|
32
|
+
}, ' VRB! ');
|
|
33
|
+
npmlog.addLevel('silly', levels.silly, {
|
|
34
|
+
fg: 'black',
|
|
35
|
+
bg: 'white'
|
|
36
|
+
}, ' DBG! ');
|
|
37
|
+
const logger = {
|
|
38
|
+
error: (msg)=>npmlog.error('', msg),
|
|
39
|
+
warn: (msg)=>npmlog.warn('', msg),
|
|
40
|
+
notice: (msg)=>npmlog.notice('', msg),
|
|
41
|
+
info: (msg)=>npmlog.info('', msg),
|
|
42
|
+
verbose: (msg)=>npmlog.verbose('', msg),
|
|
43
|
+
debug: (msg)=>npmlog.silly('', msg)
|
|
44
|
+
};
|
|
45
|
+
const setLogLevel = (level)=>{
|
|
46
|
+
npmlog.level = level === 'debug' ? 'silly' : level;
|
|
47
|
+
};
|
|
48
|
+
setLogLevel('info');
|
|
49
|
+
|
|
50
|
+
let _buildAdapter = async ()=>{
|
|
51
|
+
logger.error('Please set a BuildAdapter!');
|
|
52
|
+
return [];
|
|
53
|
+
};
|
|
54
|
+
function setBuildAdapter(buildAdapter) {
|
|
55
|
+
_buildAdapter = buildAdapter;
|
|
56
|
+
}
|
|
57
|
+
function getBuildAdapter() {
|
|
58
|
+
return _buildAdapter;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function _extends$3() {
|
|
62
|
+
_extends$3 = Object.assign || function(target) {
|
|
63
|
+
for(var i = 1; i < arguments.length; i++){
|
|
64
|
+
var source = arguments[i];
|
|
65
|
+
for(var key in source){
|
|
66
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
67
|
+
target[key] = source[key];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return target;
|
|
72
|
+
};
|
|
73
|
+
return _extends$3.apply(this, arguments);
|
|
74
|
+
}
|
|
75
|
+
function writeImportMap(sharedInfo, fedOption) {
|
|
76
|
+
const imports = sharedInfo.reduce((acc, cur)=>{
|
|
77
|
+
return _extends$3({}, acc, {
|
|
78
|
+
[cur.packageName]: cur.outFileName
|
|
79
|
+
});
|
|
80
|
+
}, {});
|
|
81
|
+
const importMap = {
|
|
82
|
+
imports
|
|
83
|
+
};
|
|
84
|
+
const importMapPath = path.join(fedOption.workspaceRoot, fedOption.outputPath, 'importmap.json');
|
|
85
|
+
fs.writeFileSync(importMapPath, JSON.stringify(importMap, null, 2));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function writeFederationInfo(federationInfo, fedOptions) {
|
|
89
|
+
const metaDataPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath, 'remoteEntry.json');
|
|
90
|
+
fs.writeFileSync(metaDataPath, JSON.stringify(federationInfo, null, 2));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function loadFederationConfig(fedOptions) {
|
|
94
|
+
const fullConfigPath = path.join(fedOptions.workspaceRoot, fedOptions.federationConfig);
|
|
95
|
+
if (!fs.existsSync(fullConfigPath)) {
|
|
96
|
+
throw new Error('Expected ' + fullConfigPath);
|
|
97
|
+
}
|
|
98
|
+
const config = await import(`${fullConfigPath}`);
|
|
99
|
+
return config;
|
|
100
|
+
}
|
|
11
101
|
|
|
12
102
|
const DEFAULT_SKIP_LIST = [
|
|
13
103
|
'@module-federation/native-federation-runtime',
|
|
@@ -53,6 +143,307 @@ function isInSkipList(entry, skipList) {
|
|
|
53
143
|
return false;
|
|
54
144
|
}
|
|
55
145
|
|
|
146
|
+
function normalize(path, trailingSlash) {
|
|
147
|
+
let cand = path.replace(/\\/g, '/');
|
|
148
|
+
if (typeof trailingSlash === 'undefined') {
|
|
149
|
+
return cand;
|
|
150
|
+
}
|
|
151
|
+
while(cand.endsWith('/')){
|
|
152
|
+
cand = cand.substring(0, cand.length - 1);
|
|
153
|
+
}
|
|
154
|
+
if (trailingSlash) {
|
|
155
|
+
return cand + '/';
|
|
156
|
+
}
|
|
157
|
+
return cand;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function _extends$2() {
|
|
161
|
+
_extends$2 = Object.assign || function(target) {
|
|
162
|
+
for(var i = 1; i < arguments.length; i++){
|
|
163
|
+
var source = arguments[i];
|
|
164
|
+
for(var key in source){
|
|
165
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
166
|
+
target[key] = source[key];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return target;
|
|
171
|
+
};
|
|
172
|
+
return _extends$2.apply(this, arguments);
|
|
173
|
+
}
|
|
174
|
+
const packageCache = {};
|
|
175
|
+
function findPackageJsonFiles(project, workspace) {
|
|
176
|
+
return expandFolders(project, workspace).map((f)=>path.join(f, 'package.json')).filter((f)=>fs.existsSync(f));
|
|
177
|
+
}
|
|
178
|
+
function expandFolders(child, parent) {
|
|
179
|
+
const result = [];
|
|
180
|
+
parent = normalize(parent, true);
|
|
181
|
+
child = normalize(child, true);
|
|
182
|
+
if (!child.startsWith(parent)) {
|
|
183
|
+
throw new Error(`Workspace folder ${path} needs to be a parent of the project folder ${child}`);
|
|
184
|
+
}
|
|
185
|
+
let current = child;
|
|
186
|
+
while(current !== parent){
|
|
187
|
+
result.push(current);
|
|
188
|
+
const cand = normalize(path.dirname(current), true);
|
|
189
|
+
if (cand === current) {
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
current = cand;
|
|
193
|
+
}
|
|
194
|
+
result.push(parent);
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
function getPackageInfo(packageName, workspaceRoot) {
|
|
198
|
+
workspaceRoot = normalize(workspaceRoot, true);
|
|
199
|
+
const packageJsonInfos = getPackageJsonFiles(workspaceRoot, workspaceRoot);
|
|
200
|
+
for (const info of packageJsonInfos){
|
|
201
|
+
const cand = _getPackageInfo(packageName, info.directory);
|
|
202
|
+
if (cand) {
|
|
203
|
+
return cand;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
logger.warn('No meta data found for shared lib ' + packageName);
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
function getVersionMapCacheKey(project, workspace) {
|
|
210
|
+
return `${project}**${workspace}`;
|
|
211
|
+
}
|
|
212
|
+
function getVersionMaps(project, workspace) {
|
|
213
|
+
return getPackageJsonFiles(project, workspace).map((json)=>_extends$2({}, json.content['dependencies']));
|
|
214
|
+
}
|
|
215
|
+
function getPackageJsonFiles(project, workspace) {
|
|
216
|
+
const cacheKey = getVersionMapCacheKey(project, workspace);
|
|
217
|
+
let maps = packageCache[cacheKey];
|
|
218
|
+
if (maps) {
|
|
219
|
+
return maps;
|
|
220
|
+
}
|
|
221
|
+
maps = findPackageJsonFiles(project, workspace).map((f)=>{
|
|
222
|
+
const content = JSON.parse(fs.readFileSync(f, 'utf-8'));
|
|
223
|
+
const directory = normalize(path.dirname(f), true);
|
|
224
|
+
const result = {
|
|
225
|
+
content,
|
|
226
|
+
directory
|
|
227
|
+
};
|
|
228
|
+
return result;
|
|
229
|
+
});
|
|
230
|
+
packageCache[cacheKey] = maps;
|
|
231
|
+
return maps;
|
|
232
|
+
}
|
|
233
|
+
function findDepPackageJson(packageName, projectRoot) {
|
|
234
|
+
const mainPkgName = getPkgFolder(packageName);
|
|
235
|
+
let mainPkgPath = path.join(projectRoot, 'node_modules', mainPkgName);
|
|
236
|
+
let mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
|
|
237
|
+
let directory = projectRoot;
|
|
238
|
+
while(path.dirname(directory) !== directory){
|
|
239
|
+
if (fs.existsSync(mainPkgJsonPath)) {
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
directory = normalize(path.dirname(directory), true);
|
|
243
|
+
mainPkgPath = path.join(directory, 'node_modules', mainPkgName);
|
|
244
|
+
mainPkgJsonPath = path.join(mainPkgPath, 'package.json');
|
|
245
|
+
}
|
|
246
|
+
if (!fs.existsSync(mainPkgJsonPath)) {
|
|
247
|
+
logger.verbose('No package.json found for ' + packageName + ' in ' + mainPkgPath);
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
return mainPkgJsonPath;
|
|
251
|
+
}
|
|
252
|
+
function _getPackageInfo(packageName, directory) {
|
|
253
|
+
var _mainPkgJson_exports, _mainPkgJson_exports_relSecondaryPath, _mainPkgJson_exports1, _mainPkgJson_exports_relSecondaryPath1, _mainPkgJson_exports2, _mainPkgJson_exports_relSecondaryPath2, _mainPkgJson_exports3;
|
|
254
|
+
const mainPkgName = getPkgFolder(packageName);
|
|
255
|
+
const mainPkgJsonPath = findDepPackageJson(packageName, directory);
|
|
256
|
+
if (!mainPkgJsonPath) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
const mainPkgPath = path.dirname(mainPkgJsonPath);
|
|
260
|
+
const mainPkgJson = readJson(mainPkgJsonPath);
|
|
261
|
+
const version = mainPkgJson['version'];
|
|
262
|
+
const esm = mainPkgJson['type'] === 'module';
|
|
263
|
+
if (!version) {
|
|
264
|
+
logger.warn('No version found for ' + packageName);
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
let relSecondaryPath = path.relative(mainPkgName, packageName);
|
|
268
|
+
if (!relSecondaryPath) {
|
|
269
|
+
relSecondaryPath = '.';
|
|
270
|
+
} else {
|
|
271
|
+
relSecondaryPath = './' + relSecondaryPath.replace(/\\/g, '/');
|
|
272
|
+
}
|
|
273
|
+
let cand = mainPkgJson == null ? void 0 : (_mainPkgJson_exports = mainPkgJson.exports) == null ? void 0 : _mainPkgJson_exports[relSecondaryPath];
|
|
274
|
+
if (typeof cand === 'string') {
|
|
275
|
+
return {
|
|
276
|
+
entryPoint: path.join(mainPkgPath, cand),
|
|
277
|
+
packageName,
|
|
278
|
+
version,
|
|
279
|
+
esm
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
cand = mainPkgJson == null ? void 0 : (_mainPkgJson_exports1 = mainPkgJson.exports) == null ? void 0 : (_mainPkgJson_exports_relSecondaryPath = _mainPkgJson_exports1[relSecondaryPath]) == null ? void 0 : _mainPkgJson_exports_relSecondaryPath.import;
|
|
283
|
+
if (typeof cand === 'object') {
|
|
284
|
+
if (cand.module) {
|
|
285
|
+
cand = cand.module;
|
|
286
|
+
} else if (cand.import) {
|
|
287
|
+
cand = cand.import;
|
|
288
|
+
} else if (cand.default) {
|
|
289
|
+
cand = cand.default;
|
|
290
|
+
} else {
|
|
291
|
+
cand = null;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (cand) {
|
|
295
|
+
if (typeof cand === 'object') {
|
|
296
|
+
if (cand.module) {
|
|
297
|
+
cand = cand.module;
|
|
298
|
+
} else if (cand.import) {
|
|
299
|
+
cand = cand.import;
|
|
300
|
+
} else if (cand.default) {
|
|
301
|
+
cand = cand.default;
|
|
302
|
+
} else {
|
|
303
|
+
cand = null;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
entryPoint: path.join(mainPkgPath, cand),
|
|
308
|
+
packageName,
|
|
309
|
+
version,
|
|
310
|
+
esm
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
cand = mainPkgJson == null ? void 0 : (_mainPkgJson_exports2 = mainPkgJson.exports) == null ? void 0 : (_mainPkgJson_exports_relSecondaryPath1 = _mainPkgJson_exports2[relSecondaryPath]) == null ? void 0 : _mainPkgJson_exports_relSecondaryPath1.module;
|
|
314
|
+
if (typeof cand === 'object') {
|
|
315
|
+
if (cand.module) {
|
|
316
|
+
cand = cand.module;
|
|
317
|
+
} else if (cand.import) {
|
|
318
|
+
cand = cand.import;
|
|
319
|
+
} else if (cand.default) {
|
|
320
|
+
cand = cand.default;
|
|
321
|
+
} else {
|
|
322
|
+
cand = null;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (cand) {
|
|
326
|
+
return {
|
|
327
|
+
entryPoint: path.join(mainPkgPath, cand),
|
|
328
|
+
packageName,
|
|
329
|
+
version,
|
|
330
|
+
esm
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
cand = mainPkgJson == null ? void 0 : (_mainPkgJson_exports3 = mainPkgJson.exports) == null ? void 0 : (_mainPkgJson_exports_relSecondaryPath2 = _mainPkgJson_exports3[relSecondaryPath]) == null ? void 0 : _mainPkgJson_exports_relSecondaryPath2.default;
|
|
334
|
+
if (cand) {
|
|
335
|
+
if (typeof cand === 'object') {
|
|
336
|
+
if (cand.module) {
|
|
337
|
+
cand = cand.module;
|
|
338
|
+
} else if (cand.import) {
|
|
339
|
+
cand = cand.import;
|
|
340
|
+
} else if (cand.default) {
|
|
341
|
+
cand = cand.default;
|
|
342
|
+
} else {
|
|
343
|
+
cand = null;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
entryPoint: path.join(mainPkgPath, cand),
|
|
348
|
+
packageName,
|
|
349
|
+
version,
|
|
350
|
+
esm
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
cand = mainPkgJson['module'];
|
|
354
|
+
if (cand && relSecondaryPath === '.') {
|
|
355
|
+
return {
|
|
356
|
+
entryPoint: path.join(mainPkgPath, cand),
|
|
357
|
+
packageName,
|
|
358
|
+
version,
|
|
359
|
+
esm: true
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
const secondaryPgkPath = path.join(mainPkgPath, relSecondaryPath);
|
|
363
|
+
const secondaryPgkJsonPath = path.join(secondaryPgkPath, 'package.json');
|
|
364
|
+
let secondaryPgkJson = null;
|
|
365
|
+
if (fs.existsSync(secondaryPgkJsonPath)) {
|
|
366
|
+
secondaryPgkJson = readJson(secondaryPgkJsonPath);
|
|
367
|
+
}
|
|
368
|
+
if (secondaryPgkJson && secondaryPgkJson.module) {
|
|
369
|
+
return {
|
|
370
|
+
entryPoint: path.join(secondaryPgkPath, secondaryPgkJson.module),
|
|
371
|
+
packageName,
|
|
372
|
+
version,
|
|
373
|
+
esm: true
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
cand = path.join(secondaryPgkPath, 'index.mjs');
|
|
377
|
+
if (fs.existsSync(cand)) {
|
|
378
|
+
return {
|
|
379
|
+
entryPoint: cand,
|
|
380
|
+
packageName,
|
|
381
|
+
version,
|
|
382
|
+
esm: true
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
if (secondaryPgkJson && secondaryPgkJson.main) {
|
|
386
|
+
return {
|
|
387
|
+
entryPoint: path.join(secondaryPgkPath, secondaryPgkJson.main),
|
|
388
|
+
packageName,
|
|
389
|
+
version,
|
|
390
|
+
esm
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
cand = path.join(secondaryPgkPath, 'index.js');
|
|
394
|
+
if (fs.existsSync(cand)) {
|
|
395
|
+
return {
|
|
396
|
+
entryPoint: cand,
|
|
397
|
+
packageName,
|
|
398
|
+
version,
|
|
399
|
+
esm
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
logger.warn('No entry point found for ' + packageName);
|
|
403
|
+
logger.warn("If you don't need this package, skip it in your federation.config.js or consider moving it into depDependencies in your package.json");
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
function readJson(mainPkgJsonPath) {
|
|
407
|
+
return JSON.parse(fs.readFileSync(mainPkgJsonPath, 'utf-8'));
|
|
408
|
+
}
|
|
409
|
+
function getPkgFolder(packageName) {
|
|
410
|
+
const parts = packageName.split('/');
|
|
411
|
+
let folder = parts[0];
|
|
412
|
+
if (folder.startsWith('@')) {
|
|
413
|
+
folder += '/' + parts[1];
|
|
414
|
+
}
|
|
415
|
+
return folder;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function _extends$1() {
|
|
419
|
+
_extends$1 = Object.assign || function(target) {
|
|
420
|
+
for(var i = 1; i < arguments.length; i++){
|
|
421
|
+
var source = arguments[i];
|
|
422
|
+
for(var key in source){
|
|
423
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
424
|
+
target[key] = source[key];
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return target;
|
|
429
|
+
};
|
|
430
|
+
return _extends$1.apply(this, arguments);
|
|
431
|
+
}
|
|
432
|
+
let _context = {};
|
|
433
|
+
function useWorkspace(workspaceRoot) {
|
|
434
|
+
_context = _extends$1({}, _context, {
|
|
435
|
+
workspaceRoot
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
function usePackageJson(packageJson) {
|
|
439
|
+
_context = _extends$1({}, _context, {
|
|
440
|
+
packageJson
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
function getConfigContext() {
|
|
444
|
+
return _context;
|
|
445
|
+
}
|
|
446
|
+
|
|
56
447
|
let inferVersion = false;
|
|
57
448
|
const DEFAULT_SECONARIES_SKIP_LIST = [
|
|
58
449
|
'@angular/router/upgrade',
|
|
@@ -380,6 +771,289 @@ function normalizeSharedMappings(config, skip) {
|
|
|
380
771
|
return result;
|
|
381
772
|
}
|
|
382
773
|
|
|
774
|
+
async function bundle(options) {
|
|
775
|
+
const adapter = await getBuildAdapter();
|
|
776
|
+
return await adapter(options);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
function copySrcMapIfExists(cachedFile, fullOutputPath) {
|
|
780
|
+
const mapSrc = cachedFile + '.map';
|
|
781
|
+
const mapDest = fullOutputPath + '.map';
|
|
782
|
+
if (fs.existsSync(mapSrc)) {
|
|
783
|
+
fs.copyFileSync(mapSrc, mapDest);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
async function bundleShared(config, fedOptions, externals) {
|
|
788
|
+
const folder = fedOptions.packageJson ? path.dirname(fedOptions.packageJson) : fedOptions.workspaceRoot;
|
|
789
|
+
const cachePath = path.join(fedOptions.workspaceRoot, 'node_modules/.cache/native-federation');
|
|
790
|
+
fs.mkdirSync(cachePath, {
|
|
791
|
+
recursive: true
|
|
792
|
+
});
|
|
793
|
+
const packageInfos = Object.keys(config.shared).map((packageName)=>getPackageInfo(packageName, folder)).filter((pi)=>!!pi);
|
|
794
|
+
const allEntryPoints = packageInfos.map((pi)=>{
|
|
795
|
+
const encName = pi.packageName.replace(/[^A-Za-z0-9]/g, '_');
|
|
796
|
+
const encVersion = pi.version.replace(/[^A-Za-z0-9]/g, '_');
|
|
797
|
+
const outName = fedOptions.dev ? `${encName}-${encVersion}-dev.js` : `${encName}-${encVersion}.js`;
|
|
798
|
+
return {
|
|
799
|
+
fileName: pi.entryPoint,
|
|
800
|
+
outName
|
|
801
|
+
};
|
|
802
|
+
});
|
|
803
|
+
const fullOutputPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath);
|
|
804
|
+
const exptedResults = allEntryPoints.map((ep)=>path.join(fullOutputPath, ep.outName));
|
|
805
|
+
const entryPoints = allEntryPoints.filter((ep)=>!fs.existsSync(path.join(cachePath, ep.outName)));
|
|
806
|
+
if (entryPoints.length > 0) {
|
|
807
|
+
logger.info('Preparing shared npm packages');
|
|
808
|
+
logger.notice('This only needs to be done once, as results are cached');
|
|
809
|
+
logger.notice("Skip packages you don't want to share in your federation config");
|
|
810
|
+
}
|
|
811
|
+
try {
|
|
812
|
+
const build = await bundle({
|
|
813
|
+
entryPoints,
|
|
814
|
+
tsConfigPath: fedOptions.tsConfig,
|
|
815
|
+
external: externals,
|
|
816
|
+
outdir: cachePath,
|
|
817
|
+
mappedPaths: config.sharedMappings,
|
|
818
|
+
dev: fedOptions.dev,
|
|
819
|
+
kind: 'shared-package',
|
|
820
|
+
hash: false,
|
|
821
|
+
packageInfos: packageInfos,
|
|
822
|
+
name: config.name
|
|
823
|
+
});
|
|
824
|
+
for (const fileName of exptedResults){
|
|
825
|
+
const outFileName = path.basename(fileName);
|
|
826
|
+
const cachedFile = path.join(cachePath, outFileName);
|
|
827
|
+
const realoutFileName = 'mf_' + path.basename(fileName);
|
|
828
|
+
const realcachedFile = path.join(cachePath, realoutFileName);
|
|
829
|
+
copyFileIfExists(cachedFile, fileName);
|
|
830
|
+
copySrcMapIfExists(cachedFile, fileName);
|
|
831
|
+
copyFileIfExists(realcachedFile, fileName.replace(outFileName, realoutFileName));
|
|
832
|
+
copySrcMapIfExists(realcachedFile, fileName.replace(outFileName, realoutFileName));
|
|
833
|
+
}
|
|
834
|
+
} catch (e) {
|
|
835
|
+
logger.error('Error bundling shared npm package ');
|
|
836
|
+
if (e instanceof Error) {
|
|
837
|
+
logger.error(e.message);
|
|
838
|
+
}
|
|
839
|
+
logger.error('For more information, run in verbose mode');
|
|
840
|
+
logger.notice('');
|
|
841
|
+
logger.notice('');
|
|
842
|
+
logger.notice('** Important Information: ***');
|
|
843
|
+
logger.notice('The error message above shows an issue with bundling a node_module.');
|
|
844
|
+
logger.notice('In most cases this is because you (indirectly) shared a Node.js package,');
|
|
845
|
+
logger.notice('while Native Federation builds for the browser.');
|
|
846
|
+
logger.notice('You can move such packages into devDependencies or skip them in your federation.config.js.');
|
|
847
|
+
logger.notice('');
|
|
848
|
+
logger.notice('More Details: https://bit.ly/nf-issue');
|
|
849
|
+
logger.notice('');
|
|
850
|
+
logger.notice('');
|
|
851
|
+
logger.verbose(e);
|
|
852
|
+
}
|
|
853
|
+
const outFileNames = [
|
|
854
|
+
...exptedResults
|
|
855
|
+
];
|
|
856
|
+
return packageInfos.map((pi)=>{
|
|
857
|
+
const shared = config.shared[pi.packageName];
|
|
858
|
+
return {
|
|
859
|
+
packageName: pi.packageName,
|
|
860
|
+
outFileName: path.basename(outFileNames.shift() || ''),
|
|
861
|
+
requiredVersion: shared.requiredVersion,
|
|
862
|
+
singleton: shared.singleton,
|
|
863
|
+
strictVersion: shared.strictVersion,
|
|
864
|
+
version: pi.version,
|
|
865
|
+
dev: !fedOptions.dev ? undefined : {
|
|
866
|
+
entryPoint: normalize(pi.entryPoint)
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
function copyFileIfExists(cachedFile, fullOutputPath) {
|
|
872
|
+
fs.mkdirSync(path.dirname(fullOutputPath), {
|
|
873
|
+
recursive: true
|
|
874
|
+
});
|
|
875
|
+
if (fs.existsSync(cachedFile)) {
|
|
876
|
+
fs.copyFileSync(cachedFile, fullOutputPath);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
function createBuildResultMap(buildResult, isHashed) {
|
|
881
|
+
const map = {};
|
|
882
|
+
for (const item of buildResult){
|
|
883
|
+
const resultName = path__default.basename(item.fileName);
|
|
884
|
+
let requestName = resultName;
|
|
885
|
+
if (isHashed) {
|
|
886
|
+
const start = resultName.lastIndexOf('-');
|
|
887
|
+
const end = resultName.lastIndexOf('.');
|
|
888
|
+
const part1 = resultName.substring(0, start);
|
|
889
|
+
const part2 = resultName.substring(end);
|
|
890
|
+
requestName = part1 + part2;
|
|
891
|
+
}
|
|
892
|
+
map[requestName] = resultName;
|
|
893
|
+
}
|
|
894
|
+
return map;
|
|
895
|
+
}
|
|
896
|
+
function lookupInResultMap(map, requestName) {
|
|
897
|
+
const key = path__default.basename(requestName);
|
|
898
|
+
return map[key];
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
async function bundleExposedAndMappings(config, fedOptions, externals) {
|
|
902
|
+
const shared = config.sharedMappings.map((sm)=>{
|
|
903
|
+
const entryPoint = sm.path;
|
|
904
|
+
const tmp = sm.key.replace(/[^A-Za-z0-9]/g, '_');
|
|
905
|
+
const outFilePath = tmp + '.js';
|
|
906
|
+
return {
|
|
907
|
+
fileName: entryPoint,
|
|
908
|
+
outName: outFilePath,
|
|
909
|
+
key: sm.key
|
|
910
|
+
};
|
|
911
|
+
});
|
|
912
|
+
const exposes = Object.keys(config.exposes).map((key)=>{
|
|
913
|
+
const entryPoint = config.exposes[key];
|
|
914
|
+
const outFilePath = key + '.js';
|
|
915
|
+
return {
|
|
916
|
+
fileName: entryPoint,
|
|
917
|
+
outName: outFilePath,
|
|
918
|
+
key
|
|
919
|
+
};
|
|
920
|
+
});
|
|
921
|
+
const entryPoints = [
|
|
922
|
+
...shared,
|
|
923
|
+
...exposes
|
|
924
|
+
];
|
|
925
|
+
const hash = !fedOptions.dev;
|
|
926
|
+
logger.info('Building federation artefacts');
|
|
927
|
+
const result = await bundle({
|
|
928
|
+
entryPoints,
|
|
929
|
+
outdir: fedOptions.outputPath,
|
|
930
|
+
tsConfigPath: fedOptions.tsConfig,
|
|
931
|
+
external: externals,
|
|
932
|
+
dev: !!fedOptions.dev,
|
|
933
|
+
watch: fedOptions.watch,
|
|
934
|
+
mappedPaths: config.sharedMappings,
|
|
935
|
+
kind: 'mapping-or-exposed',
|
|
936
|
+
hash
|
|
937
|
+
});
|
|
938
|
+
const resultMap = createBuildResultMap(result, hash);
|
|
939
|
+
const sharedResult = [];
|
|
940
|
+
for (const item of shared){
|
|
941
|
+
sharedResult.push({
|
|
942
|
+
packageName: item.key,
|
|
943
|
+
outFileName: lookupInResultMap(resultMap, item.outName),
|
|
944
|
+
requiredVersion: '',
|
|
945
|
+
singleton: true,
|
|
946
|
+
strictVersion: false,
|
|
947
|
+
version: '',
|
|
948
|
+
dev: !fedOptions.dev ? undefined : {
|
|
949
|
+
entryPoint: normalize(path__default.normalize(item.fileName))
|
|
950
|
+
}
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
const exposedResult = [];
|
|
954
|
+
for (const item of exposes){
|
|
955
|
+
exposedResult.push({
|
|
956
|
+
key: item.key,
|
|
957
|
+
outFileName: lookupInResultMap(resultMap, item.outName),
|
|
958
|
+
dev: !fedOptions.dev ? undefined : {
|
|
959
|
+
entryPoint: normalize(path__default.join(fedOptions.workspaceRoot, item.fileName))
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
return {
|
|
964
|
+
mappings: sharedResult,
|
|
965
|
+
exposes: exposedResult
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
function describeExposed(config, options) {
|
|
969
|
+
const result = [];
|
|
970
|
+
for(const key in config.exposes){
|
|
971
|
+
const localPath = normalize(path__default.normalize(path__default.join(options.workspaceRoot, config.exposes[key])));
|
|
972
|
+
result.push({
|
|
973
|
+
key,
|
|
974
|
+
outFileName: '',
|
|
975
|
+
dev: !options.dev ? undefined : {
|
|
976
|
+
entryPoint: localPath
|
|
977
|
+
}
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
return result;
|
|
981
|
+
}
|
|
982
|
+
function describeSharedMappings(config, fedOptions) {
|
|
983
|
+
const result = [];
|
|
984
|
+
for (const m of config.sharedMappings){
|
|
985
|
+
result.push({
|
|
986
|
+
packageName: m.key,
|
|
987
|
+
outFileName: '',
|
|
988
|
+
requiredVersion: '',
|
|
989
|
+
singleton: true,
|
|
990
|
+
strictVersion: false,
|
|
991
|
+
version: '',
|
|
992
|
+
dev: !fedOptions.dev ? undefined : {
|
|
993
|
+
entryPoint: normalize(path__default.normalize(m.path))
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
return result;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
const defaultBuildParams = {
|
|
1001
|
+
skipMappingsAndExposed: false
|
|
1002
|
+
};
|
|
1003
|
+
async function buildForFederation(config, fedOptions, externals, buildParams = defaultBuildParams) {
|
|
1004
|
+
let artefactInfo;
|
|
1005
|
+
if (!buildParams.skipMappingsAndExposed) {
|
|
1006
|
+
artefactInfo = await bundleExposedAndMappings(config, fedOptions, externals);
|
|
1007
|
+
}
|
|
1008
|
+
const exposedInfo = !artefactInfo ? describeExposed(config, fedOptions) : artefactInfo.exposes;
|
|
1009
|
+
const sharedPackageInfo = await bundleShared(config, fedOptions, externals);
|
|
1010
|
+
const sharedMappingInfo = !artefactInfo ? describeSharedMappings(config, fedOptions) : artefactInfo.mappings;
|
|
1011
|
+
const sharedInfo = [
|
|
1012
|
+
...sharedPackageInfo,
|
|
1013
|
+
...sharedMappingInfo
|
|
1014
|
+
];
|
|
1015
|
+
const federationInfo = {
|
|
1016
|
+
name: config.name,
|
|
1017
|
+
shared: sharedInfo,
|
|
1018
|
+
exposes: exposedInfo
|
|
1019
|
+
};
|
|
1020
|
+
writeFederationInfo(federationInfo, fedOptions);
|
|
1021
|
+
writeImportMap(sharedInfo, fedOptions);
|
|
1022
|
+
return federationInfo;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
let externals = [];
|
|
1026
|
+
let config;
|
|
1027
|
+
let fedOptions;
|
|
1028
|
+
let fedInfo;
|
|
1029
|
+
async function init(params) {
|
|
1030
|
+
var _getConfigContext;
|
|
1031
|
+
setBuildAdapter(params.adapter);
|
|
1032
|
+
fedOptions = params.options;
|
|
1033
|
+
useWorkspace(params.options.workspaceRoot);
|
|
1034
|
+
usePackageJson(params.options.packageJson);
|
|
1035
|
+
config = await loadFederationConfig(fedOptions);
|
|
1036
|
+
var _getConfigContext_workspaceRoot;
|
|
1037
|
+
params.options.workspaceRoot = (_getConfigContext_workspaceRoot = (_getConfigContext = getConfigContext()) == null ? void 0 : _getConfigContext.workspaceRoot) != null ? _getConfigContext_workspaceRoot : params.options.workspaceRoot;
|
|
1038
|
+
externals = getExternals(config);
|
|
1039
|
+
}
|
|
1040
|
+
async function build(buildParams = defaultBuildParams) {
|
|
1041
|
+
fedInfo = await buildForFederation(config, fedOptions, externals, buildParams);
|
|
1042
|
+
}
|
|
1043
|
+
const federationBuilder = {
|
|
1044
|
+
init,
|
|
1045
|
+
build,
|
|
1046
|
+
get federationInfo () {
|
|
1047
|
+
return fedInfo;
|
|
1048
|
+
},
|
|
1049
|
+
get externals () {
|
|
1050
|
+
return externals;
|
|
1051
|
+
},
|
|
1052
|
+
get config () {
|
|
1053
|
+
return config;
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
|
|
383
1057
|
function hashFile(fileName) {
|
|
384
1058
|
const fileBuffer = fs.readFileSync(fileName);
|
|
385
1059
|
const hashSum = crypto.createHash('md5');
|
|
@@ -387,4 +1061,4 @@ function hashFile(fileName) {
|
|
|
387
1061
|
return hashSum.digest('hex');
|
|
388
1062
|
}
|
|
389
1063
|
|
|
390
|
-
export { DEFAULT_SECONARIES_SKIP_LIST, _findSecondaries, addSecondaries, findPackageJson, findRootTsConfigJson, findSecondaries, getDefaultEntry, getSecondaries, hashFile, inferProjectPath, lookupVersion, lookupVersionInMap, readConfiguredSecondaries, setInferVersion, share, shareAll, withFederation };
|
|
1064
|
+
export { DEFAULT_SECONARIES_SKIP_LIST, _findSecondaries, addSecondaries, buildForFederation, bundleExposedAndMappings, createBuildResultMap, defaultBuildParams, describeExposed, describeSharedMappings, federationBuilder, findPackageJson, findRootTsConfigJson, findSecondaries, getBuildAdapter, getDefaultEntry, getSecondaries, hashFile, inferProjectPath, loadFederationConfig, logger, lookupInResultMap, lookupVersion, lookupVersionInMap, readConfiguredSecondaries, setBuildAdapter, setInferVersion, setLogLevel, share, shareAll, withFederation, writeFederationInfo, writeImportMap };
|