@module-federation/esbuild 0.0.0-next-20240528203231 → 0.0.0-next-20240529015908

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/dist/build.esm.js CHANGED
@@ -1,94 +1,11 @@
1
- import npmlog from 'npmlog';
1
+ export { g as getExternals } from './get-externals.esm.js';
2
2
  import * as path from 'path';
3
3
  import path__default from 'path';
4
4
  import * as fs from 'fs';
5
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';
8
6
  import process from 'process';
9
- import * as JSON5 from 'json5';
10
- import * as crypto from 'crypto';
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
- }
7
+ import npmlog from 'npmlog';
8
+ import 'json5';
92
9
 
93
10
  async function loadFederationConfig(fedOptions) {
94
11
  const fullConfigPath = path.join(fedOptions.workspaceRoot, fedOptions.federationConfig);
@@ -127,22 +44,66 @@ function prepareSkipList(skipList) {
127
44
  return {
128
45
  strings: new Set(skipList.filter((e)=>typeof e === 'string')),
129
46
  functions: skipList.filter((e)=>typeof e === 'function'),
130
- regexps: skipList.filter((e)=>typeof e === 'object')
47
+ regexps: skipList.filter((e)=>e instanceof RegExp)
131
48
  };
132
49
  }
133
50
  function isInSkipList(entry, skipList) {
134
51
  if (skipList.strings.has(entry)) {
135
52
  return true;
136
53
  }
137
- if (skipList.functions.find((f)=>f(entry))) {
54
+ if (skipList.functions.some((f)=>f(entry))) {
138
55
  return true;
139
56
  }
140
- if (skipList.regexps.find((r)=>r.test(entry))) {
57
+ if (skipList.regexps.some((r)=>r.test(entry))) {
141
58
  return true;
142
59
  }
143
60
  return false;
144
61
  }
145
62
 
63
+ let _context = {};
64
+ function getConfigContext() {
65
+ return _context;
66
+ }
67
+
68
+ /* eslint-disable @typescript-eslint/no-explicit-any */ //@ts-ignore
69
+ const levels = npmlog.levels;
70
+ npmlog.addLevel('error', levels.error, {
71
+ fg: 'brightWhite',
72
+ bg: 'red'
73
+ }, ' ERR! ');
74
+ npmlog.addLevel('warn', levels.info, {
75
+ fg: 'brightWhite',
76
+ bg: 'yellow'
77
+ }, ' WARN ');
78
+ npmlog.addLevel('info', levels.warn, {
79
+ fg: 'brightWhite',
80
+ bg: 'green'
81
+ }, ' INFO ');
82
+ npmlog.addLevel('notice', levels.notice, {
83
+ fg: 'black',
84
+ bg: 'brightYellow'
85
+ }, ' NOTE ');
86
+ npmlog.addLevel('verbose', levels.verbose, {
87
+ fg: 'brightWhite',
88
+ bg: 'brightBlue'
89
+ }, ' VRB! ');
90
+ npmlog.addLevel('silly', levels.silly, {
91
+ fg: 'black',
92
+ bg: 'white'
93
+ }, ' DBG! ');
94
+ const logger = {
95
+ error: (msg)=>npmlog.error('', msg),
96
+ warn: (msg)=>npmlog.warn('', msg),
97
+ notice: (msg)=>npmlog.notice('', msg),
98
+ info: (msg)=>npmlog.info('', msg),
99
+ verbose: (msg)=>npmlog.verbose('', msg),
100
+ debug: (msg)=>npmlog.silly('', msg)
101
+ };
102
+ const setLogLevel = (level)=>{
103
+ npmlog.level = level === 'debug' ? 'silly' : level;
104
+ };
105
+ setLogLevel('info');
106
+
146
107
  function normalize(path, trailingSlash) {
147
108
  let cand = path.replace(/\\/g, '/');
148
109
  if (typeof trailingSlash === 'undefined') {
@@ -157,8 +118,8 @@ function normalize(path, trailingSlash) {
157
118
  return cand;
158
119
  }
159
120
 
160
- function _extends$2() {
161
- _extends$2 = Object.assign || function(target) {
121
+ function _extends$1() {
122
+ _extends$1 = Object.assign || function(target) {
162
123
  for(var i = 1; i < arguments.length; i++){
163
124
  var source = arguments[i];
164
125
  for(var key in source){
@@ -169,7 +130,7 @@ function _extends$2() {
169
130
  }
170
131
  return target;
171
132
  };
172
- return _extends$2.apply(this, arguments);
133
+ return _extends$1.apply(this, arguments);
173
134
  }
174
135
  const packageCache = {};
175
136
  function findPackageJsonFiles(project, workspace) {
@@ -180,7 +141,7 @@ function expandFolders(child, parent) {
180
141
  parent = normalize(parent, true);
181
142
  child = normalize(child, true);
182
143
  if (!child.startsWith(parent)) {
183
- throw new Error(`Workspace folder ${path} needs to be a parent of the project folder ${child}`);
144
+ throw new Error(`Workspace folder ${parent} needs to be a parent of the project folder ${child}`);
184
145
  }
185
146
  let current = child;
186
147
  while(current !== parent){
@@ -194,23 +155,11 @@ function expandFolders(child, parent) {
194
155
  result.push(parent);
195
156
  return result;
196
157
  }
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
158
  function getVersionMapCacheKey(project, workspace) {
210
159
  return `${project}**${workspace}`;
211
160
  }
212
161
  function getVersionMaps(project, workspace) {
213
- return getPackageJsonFiles(project, workspace).map((json)=>_extends$2({}, json.content['dependencies']));
162
+ return getPackageJsonFiles(project, workspace).map((json)=>_extends$1({}, json.content['dependencies']));
214
163
  }
215
164
  function getPackageJsonFiles(project, workspace) {
216
165
  const cacheKey = getVersionMapCacheKey(project, workspace);
@@ -249,163 +198,6 @@ function findDepPackageJson(packageName, projectRoot) {
249
198
  }
250
199
  return mainPkgJsonPath;
251
200
  }
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
201
  function getPkgFolder(packageName) {
410
202
  const parts = packageName.split('/');
411
203
  let folder = parts[0];
@@ -415,35 +207,6 @@ function getPkgFolder(packageName) {
415
207
  return folder;
416
208
  }
417
209
 
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
-
447
210
  let inferVersion = false;
448
211
  const DEFAULT_SECONARIES_SKIP_LIST = [
449
212
  '@angular/router/upgrade',
@@ -575,25 +338,20 @@ function readConfiguredSecondaries(parent, libPath, exclude, shareObject) {
575
338
  return result;
576
339
  }
577
340
  function getDefaultEntry(exports, key) {
578
- var _a;
579
- let entry = '';
341
+ let entry;
580
342
  if (typeof exports[key] === 'string') {
581
343
  entry = exports[key];
582
344
  } else {
583
- entry = (_a = exports[key]) === null || _a === void 0 ? void 0 : _a['default'];
345
+ var _exports_key;
346
+ entry = (_exports_key = exports[key]) == null ? void 0 : _exports_key['default'];
584
347
  if (typeof entry === 'object') {
585
348
  entry = entry['default'];
586
349
  }
587
350
  }
588
351
  return entry;
589
352
  }
590
- function shareAll(config = {}, skip = DEFAULT_SKIP_LIST, projectPath = '') {
591
- // let workspacePath: string | undefined = undefined;
353
+ function shareAll(config, skip = DEFAULT_SKIP_LIST, projectPath = '') {
592
354
  projectPath = inferProjectPath(projectPath);
593
- // workspacePath = getConfigContext().workspaceRoot ?? '';
594
- // if (!workspacePath) {
595
- // workspacePath = projectPath;
596
- // }
597
355
  const versionMaps = getVersionMaps(projectPath, projectPath);
598
356
  const share = {};
599
357
  for (const versions of versionMaps){
@@ -614,7 +372,7 @@ function shareAll(config = {}, skip = DEFAULT_SKIP_LIST, projectPath = '') {
614
372
  return module.exports.share(share, projectPath);
615
373
  }
616
374
  function inferProjectPath(projectPath) {
617
- if (!projectPath && getConfigContext().packageJson) {
375
+ if (!projectPath) {
618
376
  projectPath = path__default.dirname(getConfigContext().packageJson || '');
619
377
  }
620
378
  if (!projectPath && getConfigContext().workspaceRoot) {
@@ -671,38 +429,6 @@ function addSecondaries(secondaries, result) {
671
429
  }
672
430
  }
673
431
 
674
- function getMappedPaths({ rootTsConfigPath, sharedMappings, rootPath }) {
675
- var _tsConfig_compilerOptions;
676
- const result = [];
677
- if (!path.isAbsolute(rootTsConfigPath)) {
678
- throw new Error('SharedMappings.register: tsConfigPath needs to be an absolute path!');
679
- }
680
- if (!rootPath) {
681
- rootPath = path.normalize(path.dirname(rootTsConfigPath));
682
- }
683
- const shareAll = !sharedMappings;
684
- if (!sharedMappings) {
685
- sharedMappings = [];
686
- }
687
- const tsConfig = JSON5.parse(fs.readFileSync(rootTsConfigPath, {
688
- encoding: 'utf-8'
689
- }));
690
- const mappings = tsConfig == null ? void 0 : (_tsConfig_compilerOptions = tsConfig.compilerOptions) == null ? void 0 : _tsConfig_compilerOptions.paths;
691
- if (!mappings) {
692
- return result;
693
- }
694
- for(const key in mappings){
695
- const libPath = path.normalize(path.join(rootPath, mappings[key][0]));
696
- if (sharedMappings.includes(key) || shareAll) {
697
- result.push({
698
- key,
699
- path: libPath
700
- });
701
- }
702
- }
703
- return result;
704
- }
705
-
706
432
  function _extends() {
707
433
  _extends = Object.assign || function(target) {
708
434
  for(var i = 1; i < arguments.length; i++){
@@ -726,8 +452,7 @@ function withFederation(config) {
726
452
  filename: (_config_filename = config.filename) != null ? _config_filename : 'remoteEntry',
727
453
  exposes: (_config_exposes = config.exposes) != null ? _config_exposes : {},
728
454
  remotes: (_config_remotes = config.remotes) != null ? _config_remotes : {},
729
- shared: normalizeShared(config, skip),
730
- sharedMappings: normalizeSharedMappings(config, skip)
455
+ shared: normalizeShared(config, skip)
731
456
  };
732
457
  }
733
458
  function normalizeShared(config, skip) {
@@ -758,307 +483,5 @@ function normalizeShared(config, skip) {
758
483
  }), {});
759
484
  return result;
760
485
  }
761
- function normalizeSharedMappings(config, skip) {
762
- const rootTsConfigPath = findRootTsConfigJson();
763
- const paths = getMappedPaths({
764
- rootTsConfigPath,
765
- sharedMappings: config.sharedMappings
766
- });
767
- const result = paths.filter((p)=>!isInSkipList(p.key, skip) && !p.key.includes('*'));
768
- if (paths.find((p)=>p.key.includes('*'))) {
769
- logger.warn('Sharing mapped paths with wildcards (*) not supported');
770
- }
771
- return result;
772
- }
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
-
1057
- function hashFile(fileName) {
1058
- const fileBuffer = fs.readFileSync(fileName);
1059
- const hashSum = crypto.createHash('md5');
1060
- hashSum.update(fileBuffer);
1061
- return hashSum.digest('hex');
1062
- }
1063
486
 
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 };
487
+ export { DEFAULT_SECONARIES_SKIP_LIST, _findSecondaries, addSecondaries, findPackageJson, findRootTsConfigJson, findSecondaries, getDefaultEntry, getSecondaries, loadFederationConfig, logger, lookupVersion, lookupVersionInMap, readConfiguredSecondaries, setInferVersion, setLogLevel, share, shareAll, withFederation };