@softarc/native-federation 3.5.5 → 4.0.0-RC10

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.
Files changed (152) hide show
  1. package/package.json +33 -5
  2. package/src/config.d.ts +4 -1
  3. package/src/config.js +4 -6
  4. package/src/domain.d.ts +2 -0
  5. package/src/domain.js +2 -0
  6. package/src/index.d.ts +10 -1
  7. package/src/index.js +10 -5
  8. package/src/internal.d.ts +13 -0
  9. package/src/internal.js +10 -0
  10. package/src/lib/config/configuration-context.js +3 -9
  11. package/src/lib/config/default-skip-list.d.ts +4 -0
  12. package/src/lib/config/default-skip-list.js +31 -0
  13. package/src/lib/config/remove-unused-deps.d.ts +3 -0
  14. package/src/lib/config/remove-unused-deps.js +10 -0
  15. package/src/lib/config/share-utils.d.ts +6 -18
  16. package/src/lib/config/share-utils.js +49 -128
  17. package/src/lib/config/with-native-federation.d.ts +1 -1
  18. package/src/lib/config/with-native-federation.js +48 -75
  19. package/src/lib/core/build-adapter.d.ts +3 -28
  20. package/src/lib/core/build-adapter.js +8 -13
  21. package/src/lib/core/build-for-federation.d.ts +4 -10
  22. package/src/lib/core/build-for-federation.js +84 -79
  23. package/src/lib/core/bundle-exposed-and-mappings.d.ts +6 -10
  24. package/src/lib/core/bundle-exposed-and-mappings.js +78 -60
  25. package/src/lib/core/bundle-shared.d.ts +11 -6
  26. package/src/lib/core/bundle-shared.js +88 -72
  27. package/src/lib/core/default-external-list.js +1 -8
  28. package/src/lib/core/federation-builder.d.ts +11 -6
  29. package/src/lib/core/federation-builder.js +27 -21
  30. package/src/lib/core/federation-cache.d.ts +8 -0
  31. package/src/lib/core/federation-cache.js +11 -0
  32. package/src/lib/core/get-externals.d.ts +1 -1
  33. package/src/lib/core/get-externals.js +2 -6
  34. package/src/lib/core/normalize-options.d.ts +12 -0
  35. package/src/lib/core/normalize-options.js +57 -0
  36. package/src/lib/core/rebuild-for-federation.d.ts +4 -0
  37. package/src/lib/core/rebuild-for-federation.js +37 -0
  38. package/src/lib/core/write-federation-info.d.ts +2 -2
  39. package/src/lib/core/write-federation-info.js +3 -8
  40. package/src/lib/core/write-import-map.d.ts +6 -3
  41. package/src/lib/core/write-import-map.js +13 -9
  42. package/src/lib/domain/config/external-config.contract.d.ts +43 -0
  43. package/src/lib/domain/config/external-config.contract.js +1 -0
  44. package/src/lib/domain/config/federation-config.contract.d.ts +35 -0
  45. package/src/lib/domain/config/federation-config.contract.js +1 -0
  46. package/src/lib/domain/config/index.d.ts +3 -0
  47. package/src/lib/domain/config/index.js +1 -0
  48. package/src/lib/{core/default-skip-list.d.ts → domain/config/skip-list.contract.d.ts} +0 -3
  49. package/src/lib/domain/config/skip-list.contract.js +1 -0
  50. package/src/lib/domain/config/with-native-federation.contract.d.ts +2 -0
  51. package/src/lib/domain/config/with-native-federation.contract.js +1 -0
  52. package/src/lib/domain/core/build-adapter.contract.d.ts +40 -0
  53. package/src/lib/domain/core/build-adapter.contract.js +1 -0
  54. package/src/lib/domain/core/build-notification-options.contract.d.ts +9 -0
  55. package/src/lib/domain/core/build-notification-options.contract.js +6 -0
  56. package/src/lib/domain/core/chunk.d.ts +2 -0
  57. package/src/lib/domain/core/chunk.js +8 -0
  58. package/src/lib/domain/core/federation-cache.contract.d.ts +7 -0
  59. package/src/lib/domain/core/federation-cache.contract.js +1 -0
  60. package/src/lib/domain/core/federation-info.contract.d.ts +33 -0
  61. package/src/lib/domain/core/federation-info.contract.js +1 -0
  62. package/src/lib/domain/core/federation-options.contract.d.ts +21 -0
  63. package/src/lib/domain/core/federation-options.contract.js +1 -0
  64. package/src/lib/domain/core/index.d.ts +6 -0
  65. package/src/lib/domain/core/index.js +2 -0
  66. package/src/lib/domain/utils/index.d.ts +2 -0
  67. package/src/lib/domain/utils/index.js +1 -0
  68. package/src/lib/domain/utils/keyvaluepair.contract.d.ts +4 -0
  69. package/src/lib/domain/utils/keyvaluepair.contract.js +1 -0
  70. package/src/lib/domain/utils/mapped-path.contract.d.ts +1 -0
  71. package/src/lib/domain/utils/mapped-path.contract.js +5 -0
  72. package/src/lib/domain/utils/used-dependencies.contract.d.ts +5 -0
  73. package/src/lib/domain/utils/used-dependencies.contract.js +1 -0
  74. package/src/lib/utils/build-result-map.d.ts +3 -2
  75. package/src/lib/utils/build-result-map.js +14 -13
  76. package/src/lib/utils/{bundle-caching.d.ts → cache-persistence.d.ts} +6 -4
  77. package/src/lib/utils/cache-persistence.js +66 -0
  78. package/src/lib/utils/errors.js +1 -6
  79. package/src/lib/utils/get-external-imports.js +5 -10
  80. package/src/lib/utils/get-used-dependencies.d.ts +7 -0
  81. package/src/lib/utils/get-used-dependencies.js +123 -0
  82. package/src/lib/utils/hash-file.js +3 -8
  83. package/src/lib/utils/logger.js +10 -16
  84. package/src/lib/utils/mapped-paths.d.ts +7 -10
  85. package/src/lib/utils/mapped-paths.js +18 -37
  86. package/src/lib/utils/normalize.js +2 -7
  87. package/src/lib/utils/package-info.js +36 -47
  88. package/src/lib/utils/rebuild-queue.d.ts +14 -1
  89. package/src/lib/utils/rebuild-queue.js +36 -21
  90. package/src/lib/utils/resolve-glob.js +7 -12
  91. package/src/lib/utils/resolve-wildcard-keys.js +9 -15
  92. package/src/lib/utils/rewrite-chunk-imports.d.ts +0 -2
  93. package/src/lib/utils/rewrite-chunk-imports.js +13 -29
  94. package/LICENSE +0 -8
  95. package/README.md +0 -509
  96. package/build.d.ts +0 -1
  97. package/build.js +0 -5
  98. package/build.js.map +0 -1
  99. package/src/build.d.ts +0 -19
  100. package/src/build.js +0 -38
  101. package/src/build.js.map +0 -1
  102. package/src/config.js.map +0 -1
  103. package/src/index.js.map +0 -1
  104. package/src/lib/config/configuration-context.js.map +0 -1
  105. package/src/lib/config/federation-config.d.ts +0 -55
  106. package/src/lib/config/federation-config.js +0 -3
  107. package/src/lib/config/federation-config.js.map +0 -1
  108. package/src/lib/config/share-utils.js.map +0 -1
  109. package/src/lib/config/with-native-federation.js.map +0 -1
  110. package/src/lib/core/build-adapter.js.map +0 -1
  111. package/src/lib/core/build-for-federation.js.map +0 -1
  112. package/src/lib/core/bundle-exposed-and-mappings.js.map +0 -1
  113. package/src/lib/core/bundle-shared.js.map +0 -1
  114. package/src/lib/core/default-external-list.js.map +0 -1
  115. package/src/lib/core/default-server-deps-list.d.ts +0 -2
  116. package/src/lib/core/default-server-deps-list.js +0 -10
  117. package/src/lib/core/default-server-deps-list.js.map +0 -1
  118. package/src/lib/core/default-skip-list.js +0 -49
  119. package/src/lib/core/default-skip-list.js.map +0 -1
  120. package/src/lib/core/federation-builder.js.map +0 -1
  121. package/src/lib/core/federation-options.d.ts +0 -14
  122. package/src/lib/core/federation-options.js +0 -3
  123. package/src/lib/core/federation-options.js.map +0 -1
  124. package/src/lib/core/get-externals.js.map +0 -1
  125. package/src/lib/core/load-federation-config.d.ts +0 -3
  126. package/src/lib/core/load-federation-config.js +0 -24
  127. package/src/lib/core/load-federation-config.js.map +0 -1
  128. package/src/lib/core/remove-unused-deps.d.ts +0 -2
  129. package/src/lib/core/remove-unused-deps.js +0 -98
  130. package/src/lib/core/remove-unused-deps.js.map +0 -1
  131. package/src/lib/core/write-federation-info.js.map +0 -1
  132. package/src/lib/core/write-import-map.js.map +0 -1
  133. package/src/lib/utils/build-result-map.js.map +0 -1
  134. package/src/lib/utils/build-utils.d.ts +0 -2
  135. package/src/lib/utils/build-utils.js +0 -9
  136. package/src/lib/utils/build-utils.js.map +0 -1
  137. package/src/lib/utils/bundle-caching.js +0 -78
  138. package/src/lib/utils/bundle-caching.js.map +0 -1
  139. package/src/lib/utils/config-utils.d.ts +0 -2
  140. package/src/lib/utils/config-utils.js +0 -13
  141. package/src/lib/utils/config-utils.js.map +0 -1
  142. package/src/lib/utils/errors.js.map +0 -1
  143. package/src/lib/utils/get-external-imports.js.map +0 -1
  144. package/src/lib/utils/hash-file.js.map +0 -1
  145. package/src/lib/utils/logger.js.map +0 -1
  146. package/src/lib/utils/mapped-paths.js.map +0 -1
  147. package/src/lib/utils/normalize.js.map +0 -1
  148. package/src/lib/utils/package-info.js.map +0 -1
  149. package/src/lib/utils/rebuild-queue.js.map +0 -1
  150. package/src/lib/utils/resolve-glob.js.map +0 -1
  151. package/src/lib/utils/resolve-wildcard-keys.js.map +0 -1
  152. package/src/lib/utils/rewrite-chunk-imports.js.map +0 -1
@@ -1,96 +1,115 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.bundleExposedAndMappings = bundleExposedAndMappings;
4
- exports.describeExposed = describeExposed;
5
- exports.describeSharedMappings = describeSharedMappings;
6
- const tslib_1 = require("tslib");
7
- const fs_1 = tslib_1.__importDefault(require("fs"));
8
- const path_1 = tslib_1.__importDefault(require("path"));
9
- const build_result_map_1 = require("../utils/build-result-map");
10
- const build_utils_1 = require("../utils/build-utils");
11
- const logger_1 = require("../utils/logger");
12
- const normalize_1 = require("../utils/normalize");
13
- const errors_1 = require("../utils/errors");
14
- async function bundleExposedAndMappings(config, fedOptions, externals, signal) {
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { createBuildResultMap, popFromResultMap } from '../utils/build-result-map.js';
4
+ import { logger } from '../utils/logger.js';
5
+ import { normalize } from '../utils/normalize.js';
6
+ import { AbortedError } from '../utils/errors.js';
7
+ import { rewriteChunkImports } from '../utils/rewrite-chunk-imports.js';
8
+ import { getBuildAdapter } from './build-adapter.js';
9
+ export async function bundleExposedAndMappings(config, fedOptions, externals, modifiedFiles, signal) {
15
10
  if (signal?.aborted) {
16
- throw new errors_1.AbortedError('[bundle-exposed-and-mappings] Aborted before bundling');
11
+ throw new AbortedError('[bundle-exposed-and-mappings] Aborted before bundling');
17
12
  }
18
- const shared = config.sharedMappings.map((sm) => {
19
- const entryPoint = sm.path;
20
- const tmp = sm.key.replace(/[^A-Za-z0-9]/g, '_');
21
- const outFilePath = tmp + '.js';
22
- return { fileName: entryPoint, outName: outFilePath, key: sm.key };
13
+ const shared = Object.entries(config.sharedMappings).map(([entryPoint, mappedImport]) => {
14
+ return {
15
+ fileName: entryPoint,
16
+ outName: mappedImport.replace(/[^A-Za-z0-9]/g, '_') + '.js',
17
+ key: mappedImport,
18
+ };
23
19
  });
24
- const exposes = Object.keys(config.exposes).map((key) => {
25
- const entryPoint = config.exposes[key];
20
+ const exposes = Object.entries(config.exposes).map(([key, entry]) => {
26
21
  const outFilePath = key + '.js';
27
- return { fileName: entryPoint, outName: outFilePath, key };
22
+ return { fileName: entry, outName: outFilePath, key };
28
23
  });
29
24
  const entryPoints = [...shared, ...exposes];
30
25
  const hash = !fedOptions.dev;
31
- logger_1.logger.info('Building federation artefacts');
32
26
  let result;
33
27
  try {
34
- result = await (0, build_utils_1.bundle)({
35
- entryPoints,
36
- outdir: fedOptions.outputPath,
37
- tsConfigPath: fedOptions.tsConfig,
38
- external: externals,
39
- dev: !!fedOptions.dev,
40
- watch: fedOptions.watch,
41
- mappedPaths: config.sharedMappings,
42
- kind: 'mapping-or-exposed',
43
- hash,
44
- optimizedMappings: config.features.ignoreUnusedDeps,
28
+ if (!modifiedFiles) {
29
+ await getBuildAdapter().setup('mapping-or-exposed', {
30
+ entryPoints,
31
+ outdir: fedOptions.outputPath,
32
+ tsConfigPath: fedOptions.tsConfig,
33
+ external: externals,
34
+ dev: !!fedOptions.dev,
35
+ watch: fedOptions.watch,
36
+ mappedPaths: config.sharedMappings,
37
+ chunks: config.chunks,
38
+ hash,
39
+ optimizedMappings: config.features.ignoreUnusedDeps,
40
+ isMappingOrExposed: true,
41
+ cache: fedOptions.federationCache,
42
+ });
43
+ }
44
+ result = await getBuildAdapter().build('mapping-or-exposed', {
45
45
  signal,
46
+ modifiedFiles,
46
47
  });
47
48
  if (signal?.aborted) {
48
- throw new errors_1.AbortedError('[bundle-exposed-and-mappings] Aborted after bundle');
49
+ throw new AbortedError('[bundle-exposed-and-mappings] Aborted after bundle');
49
50
  }
50
51
  }
51
52
  catch (error) {
52
- if (!(error instanceof errors_1.AbortedError)) {
53
- logger_1.logger.error('Error building federation artefacts');
53
+ if (!(error instanceof AbortedError)) {
54
+ logger.error('Error building federation artifacts');
54
55
  }
55
56
  throw error;
56
57
  }
57
- const resultMap = (0, build_result_map_1.createBuildResultMap)(result, hash);
58
+ const resultMap = createBuildResultMap(result, hash);
58
59
  const sharedResult = [];
60
+ const entryFiles = [];
61
+ // Pick shared-mappings
59
62
  for (const item of shared) {
63
+ const distEntryFile = popFromResultMap(resultMap, item.outName);
60
64
  sharedResult.push({
61
65
  packageName: item.key,
62
- outFileName: (0, build_result_map_1.lookupInResultMap)(resultMap, item.outName),
66
+ outFileName: path.basename(distEntryFile),
63
67
  requiredVersion: '',
64
68
  singleton: true,
65
69
  strictVersion: false,
66
- version: config.features.mappingVersion
67
- ? getMappingVersion(item.fileName)
68
- : '',
70
+ version: config.features.mappingVersion ? getMappingVersion(item.fileName) : '',
69
71
  dev: !fedOptions.dev
70
72
  ? undefined
71
73
  : {
72
- entryPoint: (0, normalize_1.normalize)(path_1.default.normalize(item.fileName)),
74
+ entryPoint: normalize(path.normalize(item.fileName)),
73
75
  },
74
76
  });
77
+ entryFiles.push(distEntryFile);
75
78
  }
76
79
  const exposedResult = [];
80
+ // Pick exposed-modules
77
81
  for (const item of exposes) {
82
+ const distEntryFile = popFromResultMap(resultMap, item.outName);
78
83
  exposedResult.push({
79
84
  key: item.key,
80
- outFileName: (0, build_result_map_1.lookupInResultMap)(resultMap, item.outName),
85
+ outFileName: path.basename(distEntryFile),
81
86
  dev: !fedOptions.dev
82
87
  ? undefined
83
88
  : {
84
- entryPoint: (0, normalize_1.normalize)(path_1.default.join(fedOptions.workspaceRoot, item.fileName)),
89
+ entryPoint: normalize(path.join(fedOptions.workspaceRoot, item.fileName)),
85
90
  },
86
91
  });
92
+ entryFiles.push(distEntryFile);
93
+ }
94
+ // Remove .map files
95
+ Object.keys(resultMap)
96
+ .filter(f => f.endsWith('.map'))
97
+ .forEach(f => popFromResultMap(resultMap, f));
98
+ // Process remaining chunks and lazy loaded internal modules
99
+ let exportedChunks = undefined;
100
+ if (config.chunks && config.features.denseChunking) {
101
+ for (const entryFile of entryFiles)
102
+ rewriteChunkImports(entryFile);
103
+ exportedChunks = {
104
+ ['mapping-or-exposed']: Object.values(resultMap).map(chunk => path.basename(chunk)),
105
+ };
87
106
  }
88
- return { mappings: sharedResult, exposes: exposedResult };
107
+ return { mappings: sharedResult, exposes: exposedResult, chunks: exportedChunks };
89
108
  }
90
- function describeExposed(config, options) {
109
+ export function describeExposed(config, options) {
91
110
  const result = [];
92
111
  for (const key in config.exposes) {
93
- const localPath = (0, normalize_1.normalize)(path_1.default.normalize(path_1.default.join(options.workspaceRoot, config.exposes[key])));
112
+ const localPath = normalize(path.normalize(path.join(options.workspaceRoot, config.exposes[key])));
94
113
  result.push({
95
114
  key,
96
115
  outFileName: '',
@@ -103,34 +122,33 @@ function describeExposed(config, options) {
103
122
  }
104
123
  return result;
105
124
  }
106
- function describeSharedMappings(config, fedOptions) {
125
+ export function describeSharedMappings(config, fedOptions) {
107
126
  const result = [];
108
- for (const m of config.sharedMappings) {
127
+ for (const [mappedPath, mappedImport] of Object.entries(config.sharedMappings)) {
109
128
  result.push({
110
- packageName: m.key,
129
+ packageName: mappedImport,
111
130
  outFileName: '',
112
131
  requiredVersion: '',
113
132
  singleton: true,
114
133
  strictVersion: false,
115
- version: config.features.mappingVersion ? getMappingVersion(m.path) : '',
134
+ version: config.features.mappingVersion ? getMappingVersion(mappedPath) : '',
116
135
  dev: !fedOptions.dev
117
136
  ? undefined
118
137
  : {
119
- entryPoint: (0, normalize_1.normalize)(path_1.default.normalize(m.path)),
138
+ entryPoint: normalize(path.normalize(mappedPath)),
120
139
  },
121
140
  });
122
141
  }
123
142
  return result;
124
143
  }
125
144
  function getMappingVersion(fileName) {
126
- const entryFileDir = path_1.default.dirname(fileName);
127
- const cand1 = path_1.default.join(entryFileDir, 'package.json');
128
- const cand2 = path_1.default.join(path_1.default.dirname(entryFileDir), 'package.json');
129
- const packageJsonPath = [cand1, cand2].find((cand) => fs_1.default.existsSync(cand));
145
+ const entryFileDir = path.dirname(fileName);
146
+ const cand1 = path.join(entryFileDir, 'package.json');
147
+ const cand2 = path.join(path.dirname(entryFileDir), 'package.json');
148
+ const packageJsonPath = [cand1, cand2].find(cand => fs.existsSync(cand));
130
149
  if (packageJsonPath) {
131
- const json = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf-8'));
150
+ const json = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
132
151
  return json.version ?? '';
133
152
  }
134
153
  return '';
135
154
  }
136
- //# sourceMappingURL=bundle-exposed-and-mappings.js.map
@@ -1,7 +1,12 @@
1
- import { NormalizedFederationConfig, NormalizedSharedConfig } from '../config/federation-config';
2
- import { SharedInfo } from '@softarc/native-federation-runtime';
3
- import { FederationOptions } from './federation-options';
4
- export declare function bundleShared(sharedBundles: Record<string, NormalizedSharedConfig>, config: NormalizedFederationConfig, fedOptions: FederationOptions, externals: string[], platform: "browser" | "node" | undefined, cacheOptions: {
5
- pathToCache: string;
1
+ import type { NormalizedFederationConfig } from '../domain/config/federation-config.contract.js';
2
+ import type { SharedInfo } from '../domain/core/federation-info.contract.js';
3
+ import { type NormalizedFederationOptions } from '../domain/core/federation-options.contract.js';
4
+ import type { NormalizedExternalConfig } from '../domain/config/external-config.contract.js';
5
+ export declare function bundleShared(sharedBundles: Record<string, NormalizedExternalConfig>, config: NormalizedFederationConfig, fedOptions: NormalizedFederationOptions, externals: string[], buildOptions: {
6
+ platform: 'browser' | 'node';
6
7
  bundleName: string;
7
- }): Promise<Array<SharedInfo>>;
8
+ chunks: boolean;
9
+ }): Promise<{
10
+ externals: SharedInfo[];
11
+ chunks?: Record<string, string[]>;
12
+ }>;
@@ -1,135 +1,149 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.bundleShared = bundleShared;
4
- const tslib_1 = require("tslib");
5
- const path = tslib_1.__importStar(require("path"));
6
- const fs = tslib_1.__importStar(require("fs"));
7
- const build_utils_1 = require("../utils/build-utils");
8
- const package_info_1 = require("../utils/package-info");
9
- const logger_1 = require("../utils/logger");
10
- const crypto_1 = tslib_1.__importDefault(require("crypto"));
11
- const default_external_list_1 = require("./default-external-list");
12
- const rewrite_chunk_imports_1 = require("../utils/rewrite-chunk-imports");
13
- const bundle_caching_1 = require("./../utils/bundle-caching");
14
- async function bundleShared(sharedBundles, config, fedOptions, externals, platform = 'browser', cacheOptions) {
15
- const checksum = (0, bundle_caching_1.getChecksum)(sharedBundles, fedOptions.dev ? '1' : '0');
1
+ import * as path from 'path';
2
+ import * as fs from 'fs';
3
+ import { getPackageInfo } from '../utils/package-info.js';
4
+ import { logger } from '../utils/logger.js';
5
+ import crypto from 'crypto';
6
+ import { DEFAULT_EXTERNAL_LIST } from './default-external-list.js';
7
+ import { isSourceFile, rewriteChunkImports } from '../utils/rewrite-chunk-imports.js';
8
+ import { toChunkImport } from '../domain/core/chunk.js';
9
+ import { cacheEntry, getChecksum, getFilename } from '../utils/cache-persistence.js';
10
+ import { fileURLToPath } from 'url';
11
+ import { getBuildAdapter } from './build-adapter.js';
12
+ export async function bundleShared(sharedBundles, config, fedOptions, externals, buildOptions) {
13
+ const checksum = getChecksum(sharedBundles, fedOptions.dev ? '1' : '0');
16
14
  const folder = fedOptions.packageJson
17
15
  ? path.dirname(fedOptions.packageJson)
18
16
  : fedOptions.workspaceRoot;
19
- const bundleCache = (0, bundle_caching_1.cacheEntry)(cacheOptions.pathToCache, (0, bundle_caching_1.getFilename)(cacheOptions.bundleName, fedOptions.dev));
17
+ const bundleCache = cacheEntry(fedOptions.federationCache.cachePath, getFilename(buildOptions.bundleName, fedOptions.dev));
20
18
  if (fedOptions?.cacheExternalArtifacts) {
21
19
  const cacheMetadata = bundleCache.getMetadata(checksum);
22
20
  if (cacheMetadata) {
23
- logger_1.logger.debug(`Checksum of ${cacheOptions.bundleName} matched, Skipped artifact bundling`);
21
+ logger.debug(`Checksum of ${buildOptions.bundleName} matched, Skipped artifact bundling`);
24
22
  bundleCache.copyFiles(path.join(fedOptions.workspaceRoot, fedOptions.outputPath));
25
- return cacheMetadata.externals;
23
+ return { externals: cacheMetadata.externals, chunks: cacheMetadata.chunks };
26
24
  }
27
25
  }
28
26
  bundleCache.clear();
29
27
  const inferredPackageInfos = Object.keys(sharedBundles)
30
- .filter((packageName) => !sharedBundles[packageName].packageInfo)
31
- .map((packageName) => (0, package_info_1.getPackageInfo)(packageName, folder))
32
- .filter((pi) => !!pi);
28
+ .filter(packageName => !sharedBundles[packageName]?.packageInfo)
29
+ .map(packageName => getPackageInfo(packageName, folder))
30
+ .filter(pi => !!pi);
33
31
  const configuredPackageInfos = Object.keys(sharedBundles)
34
- .filter((packageName) => !!sharedBundles[packageName].packageInfo)
35
- .map((packageName) => ({
32
+ .filter(packageName => !!sharedBundles[packageName]?.packageInfo)
33
+ .map(packageName => ({
36
34
  packageName,
37
- ...sharedBundles[packageName].packageInfo,
35
+ ...sharedBundles[packageName]?.packageInfo,
38
36
  }));
39
37
  const packageInfos = [...inferredPackageInfos, ...configuredPackageInfos];
38
+ const __filename = fileURLToPath(import.meta.url);
40
39
  const configState = 'BUNDLER_CHUNKS' + // TODO: Replace this with lib version
41
- fs.readFileSync(path.join(__dirname, '../../../package.json')) +
40
+ fs.readFileSync(path.join(path.dirname(__filename), '../../../package.json')) +
42
41
  JSON.stringify(config);
43
- const allEntryPoints = packageInfos.map((pi) => {
42
+ const allEntryPoints = packageInfos.map(pi => {
44
43
  const encName = pi.packageName.replace(/[^A-Za-z0-9]/g, '_');
45
44
  const outName = createOutName(pi, configState, fedOptions, encName);
46
45
  return { fileName: pi.entryPoint, outName };
47
46
  });
48
47
  const fullOutputPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath);
49
- const expectedResults = allEntryPoints.map((ep) => path.join(fullOutputPath, ep.outName));
50
- const entryPoints = allEntryPoints.filter((ep) => !fs.existsSync(path.join(cacheOptions.pathToCache, ep.outName)));
48
+ const expectedResults = allEntryPoints.map(ep => path.join(fullOutputPath, ep.outName));
49
+ const entryPoints = allEntryPoints.filter(ep => !fs.existsSync(path.join(fedOptions.federationCache.cachePath, ep.outName)));
51
50
  // If we build for the browser and don't remote unused deps from the shared config,
52
51
  // we need to exclude typical node libs to avoid compilation issues
53
- const useDefaultExternalList = platform === 'browser' && !config.features.ignoreUnusedDeps;
54
- const additionalExternals = useDefaultExternalList
55
- ? default_external_list_1.DEFAULT_EXTERNAL_LIST
56
- : [];
52
+ const useDefaultExternalList = buildOptions.platform === 'browser' && !config.features.ignoreUnusedDeps;
53
+ const additionalExternals = useDefaultExternalList ? DEFAULT_EXTERNAL_LIST : [];
57
54
  let bundleResult = null;
58
55
  try {
59
- bundleResult = await (0, build_utils_1.bundle)({
56
+ await getBuildAdapter().setup(buildOptions.bundleName, {
60
57
  entryPoints,
61
58
  tsConfigPath: fedOptions.tsConfig,
62
59
  external: [...additionalExternals, ...externals],
63
- outdir: cacheOptions.pathToCache,
60
+ outdir: fedOptions.federationCache.cachePath,
64
61
  mappedPaths: config.sharedMappings,
65
62
  dev: fedOptions.dev,
66
- kind: 'shared-package',
63
+ isMappingOrExposed: false,
67
64
  hash: false,
68
- platform,
65
+ chunks: buildOptions.chunks,
66
+ platform: buildOptions.platform,
69
67
  optimizedMappings: config.features.ignoreUnusedDeps,
68
+ cache: fedOptions.federationCache,
70
69
  });
71
- const cachedFiles = bundleResult.map((br) => path.basename(br.fileName));
72
- rewriteImports(cachedFiles, cacheOptions.pathToCache);
70
+ bundleResult = await getBuildAdapter().build(buildOptions.bundleName);
71
+ await getBuildAdapter().dispose(buildOptions.bundleName);
72
+ const cachedFiles = bundleResult.map(br => path.basename(br.fileName));
73
+ rewriteImports(cachedFiles, fedOptions.federationCache.cachePath);
73
74
  }
74
75
  catch (e) {
75
- logger_1.logger.error('Error bundling shared npm package ');
76
+ logger.error('Error bundling shared npm package ');
76
77
  if (e instanceof Error) {
77
- logger_1.logger.error(e.message);
78
+ logger.error(e.message);
78
79
  }
79
- logger_1.logger.error('For more information, run in verbose mode');
80
- logger_1.logger.notice('');
81
- logger_1.logger.notice('');
82
- logger_1.logger.notice('** Important Information: ***');
83
- logger_1.logger.notice('The error message above shows an issue with bundling a node_module.');
84
- logger_1.logger.notice('In most cases this is because you (indirectly) shared a Node.js package,');
85
- logger_1.logger.notice('while Native Federation builds for the browser.');
86
- logger_1.logger.notice('You can move such packages into devDependencies or skip them in your federation.config.js.');
87
- logger_1.logger.notice('');
88
- logger_1.logger.notice('More Details: https://bit.ly/nf-issue');
89
- logger_1.logger.notice('');
90
- logger_1.logger.notice('');
91
- logger_1.logger.verbose(e);
80
+ logger.error('For more information, run in verbose mode');
81
+ logger.notice('');
82
+ logger.notice('');
83
+ logger.notice('** Important Information: ***');
84
+ logger.notice('The error message above shows an issue with bundling a node_module.');
85
+ logger.notice('In most cases this is because you (indirectly) shared a Node.js package,');
86
+ logger.notice('while Native Federation builds for the browser.');
87
+ logger.notice('You can move such packages into devDependencies or skip them in your federation.config.js.');
88
+ logger.notice('');
89
+ logger.notice('More Details: https://bit.ly/nf-issue');
90
+ logger.notice('');
91
+ logger.notice('');
92
+ logger.verbose(e);
92
93
  throw e;
93
94
  }
94
95
  const outFileNames = [...expectedResults];
95
96
  const result = buildResult(packageInfos, sharedBundles, outFileNames);
96
- // TODO: Decide whether/when to add .map files
97
- const chunks = bundleResult.filter((br) => !br.fileName.endsWith('.map') &&
98
- !result.find((r) => r.outFileName === path.basename(br.fileName)));
99
- addChunksToResult(chunks, result, fedOptions.dev);
97
+ const chunks = bundleResult.filter(br => !br.fileName.endsWith('.map') &&
98
+ !result.find(r => r.outFileName === path.basename(br.fileName)));
99
+ /**
100
+ * Chunking
101
+ */
102
+ let exportedChunks = undefined;
103
+ if (buildOptions.chunks && config.features.denseChunking) {
104
+ result.forEach(external => {
105
+ external.bundle = buildOptions.bundleName;
106
+ });
107
+ if (chunks.length > 0) {
108
+ exportedChunks = { [buildOptions.bundleName]: getChunkFileNames(chunks) };
109
+ }
110
+ }
111
+ else {
112
+ addChunksToResult(chunks, result);
113
+ }
100
114
  bundleCache.persist({
101
115
  checksum,
102
116
  externals: result,
103
- files: bundleResult.map((r) => r.fileName.split(path.sep).pop() ?? r.fileName),
117
+ files: bundleResult.map(r => r.fileName.split(path.sep).pop() ?? r.fileName),
118
+ chunks: exportedChunks,
104
119
  });
105
120
  bundleCache.copyFiles(path.join(fedOptions.workspaceRoot, fedOptions.outputPath));
106
- return result;
121
+ return { externals: result, chunks: exportedChunks };
107
122
  }
108
123
  function rewriteImports(cachedFiles, cachePath) {
109
- const newSourceFiles = cachedFiles.filter((cf) => (0, rewrite_chunk_imports_1.isSourceFile)(cf));
124
+ const newSourceFiles = cachedFiles.filter(cf => isSourceFile(cf));
110
125
  for (const sourceFile of newSourceFiles) {
111
126
  const sourceFilePath = path.join(cachePath, sourceFile);
112
- (0, rewrite_chunk_imports_1.rewriteChunkImports)(sourceFilePath);
127
+ rewriteChunkImports(sourceFilePath);
113
128
  }
114
129
  }
115
130
  function createOutName(pi, configState, fedOptions, encName) {
116
131
  const hashBase = pi.version + '_' + pi.entryPoint + '_' + configState;
117
132
  const hash = calcHash(hashBase);
118
- const outName = fedOptions.dev
119
- ? `${encName}.${hash}-dev.js`
120
- : `${encName}.${hash}.js`;
133
+ const outName = fedOptions.dev ? `${encName}.${hash}-dev.js` : `${encName}.${hash}.js`;
121
134
  return outName;
122
135
  }
123
136
  function buildResult(packageInfos, sharedBundles, outFileNames) {
124
- return packageInfos.map((pi) => {
137
+ return packageInfos.map(pi => {
125
138
  const shared = sharedBundles[pi.packageName];
126
139
  return {
127
140
  packageName: pi.packageName,
128
141
  outFileName: path.basename(outFileNames.shift() || ''),
129
- requiredVersion: shared.requiredVersion,
130
- singleton: shared.singleton,
131
- strictVersion: shared.strictVersion,
142
+ requiredVersion: shared?.requiredVersion,
143
+ singleton: shared?.singleton,
144
+ strictVersion: shared?.strictVersion,
132
145
  version: pi.version,
146
+ ...(shared?.shareScope && { shareScope: shared.shareScope }),
133
147
  // TODO: Decide whether/when we need debug infos
134
148
  // dev: !fedOptions.dev
135
149
  // ? undefined
@@ -139,7 +153,10 @@ function buildResult(packageInfos, sharedBundles, outFileNames) {
139
153
  };
140
154
  });
141
155
  }
142
- function addChunksToResult(chunks, result, dev) {
156
+ function getChunkFileNames(chunks) {
157
+ return chunks.map(chunk => path.basename(chunk.fileName));
158
+ }
159
+ function addChunksToResult(chunks, result) {
143
160
  for (const item of chunks) {
144
161
  const fileName = path.basename(item.fileName);
145
162
  result.push({
@@ -155,7 +172,7 @@ function addChunksToResult(chunks, result, dev) {
155
172
  // take care of singleton and strictVersion.
156
173
  version: '0.0.0',
157
174
  requiredVersion: '0.0.0',
158
- packageName: (0, rewrite_chunk_imports_1.deriveInternalName)(fileName),
175
+ packageName: toChunkImport(fileName),
159
176
  outFileName: fileName,
160
177
  // dev: dev
161
178
  // ? undefined
@@ -166,7 +183,7 @@ function addChunksToResult(chunks, result, dev) {
166
183
  }
167
184
  }
168
185
  function calcHash(hashBase) {
169
- const hash = crypto_1.default
186
+ const hash = crypto
170
187
  .createHash('sha256')
171
188
  .update(hashBase)
172
189
  .digest('base64')
@@ -176,4 +193,3 @@ function calcHash(hashBase) {
176
193
  .substring(0, 10);
177
194
  return hash;
178
195
  }
179
- //# sourceMappingURL=bundle-shared.js.map
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_EXTERNAL_LIST = void 0;
4
1
  const NODE_PACKAGES = [
5
2
  'assert',
6
3
  'buffer',
@@ -29,8 +26,4 @@ const NODE_PACKAGES = [
29
26
  'vm',
30
27
  'zlib',
31
28
  ];
32
- exports.DEFAULT_EXTERNAL_LIST = NODE_PACKAGES.flatMap((p) => [
33
- p,
34
- 'node:' + p,
35
- ]);
36
- //# sourceMappingURL=default-external-list.js.map
29
+ export const DEFAULT_EXTERNAL_LIST = NODE_PACKAGES.flatMap(p => [p, 'node:' + p]);
@@ -1,16 +1,21 @@
1
- import { FederationInfo } from '@softarc/native-federation-runtime';
2
- import { NormalizedFederationConfig } from '../config/federation-config';
3
- import { BuildAdapter } from './build-adapter';
4
- import { FederationOptions } from './federation-options';
1
+ import type { FederationInfo } from '../domain/core/federation-info.contract.js';
2
+ import type { NormalizedFederationConfig } from '../domain/config/federation-config.contract.js';
3
+ import { type FederationOptions } from '../domain/core/federation-options.contract.js';
4
+ import type { NFBuildAdapter } from '../domain/core/build-adapter.contract.js';
5
5
  export interface BuildHelperParams {
6
6
  options: FederationOptions;
7
- adapter: BuildAdapter;
7
+ adapter: NFBuildAdapter;
8
8
  }
9
9
  declare function init(params: BuildHelperParams): Promise<void>;
10
- declare function build(buildParams?: import("./build-for-federation").BuildParams): Promise<void>;
10
+ declare function build(opts?: {
11
+ modifiedFiles?: string[];
12
+ signal?: AbortSignal;
13
+ }): Promise<void>;
14
+ declare function close(): Promise<void>;
11
15
  export declare const federationBuilder: {
12
16
  init: typeof init;
13
17
  build: typeof build;
18
+ close: typeof close;
14
19
  readonly federationInfo: FederationInfo;
15
20
  readonly externals: string[];
16
21
  readonly config: NormalizedFederationConfig;
@@ -1,31 +1,38 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.federationBuilder = void 0;
4
- const configuration_context_1 = require("../config/configuration-context");
5
- const build_adapter_1 = require("./build-adapter");
6
- const build_for_federation_1 = require("./build-for-federation");
7
- const get_externals_1 = require("./get-externals");
8
- const load_federation_config_1 = require("./load-federation-config");
1
+ import { getConfigContext, usePackageJson, useWorkspace } from '../config/configuration-context.js';
2
+ import { getBuildAdapter, setBuildAdapter } from './build-adapter.js';
3
+ import { buildForFederation } from './build-for-federation.js';
4
+ import { getExternals } from './get-externals.js';
5
+ import { normalizeFederationOptions } from './normalize-options.js';
6
+ import { rebuildForFederation } from './rebuild-for-federation.js';
9
7
  let externals = [];
10
8
  let config;
11
- let fedOptions;
9
+ let options;
12
10
  let fedInfo;
13
11
  async function init(params) {
14
- (0, build_adapter_1.setBuildAdapter)(params.adapter);
15
- fedOptions = params.options;
16
- (0, configuration_context_1.useWorkspace)(params.options.workspaceRoot);
17
- (0, configuration_context_1.usePackageJson)(params.options.packageJson);
18
- config = await (0, load_federation_config_1.loadFederationConfig)(fedOptions);
19
- params.options.workspaceRoot =
20
- (0, configuration_context_1.getConfigContext)().workspaceRoot ?? params.options.workspaceRoot;
21
- externals = (0, get_externals_1.getExternals)(config);
12
+ setBuildAdapter(params.adapter);
13
+ useWorkspace(params.options.workspaceRoot);
14
+ usePackageJson(params.options.packageJson);
15
+ params.options.workspaceRoot = getConfigContext().workspaceRoot ?? params.options.workspaceRoot;
16
+ const normalized = await normalizeFederationOptions(params.options);
17
+ options = normalized.options;
18
+ config = normalized.config;
19
+ externals = getExternals(config);
22
20
  }
23
- async function build(buildParams = build_for_federation_1.defaultBuildParams) {
24
- fedInfo = await (0, build_for_federation_1.buildForFederation)(config, fedOptions, externals, buildParams);
21
+ async function build(opts = {}) {
22
+ if (!fedInfo) {
23
+ fedInfo = await buildForFederation(config, options, externals, opts.signal);
24
+ }
25
+ else {
26
+ fedInfo = await rebuildForFederation(config, options, externals, opts.modifiedFiles ?? [], opts.signal);
27
+ }
25
28
  }
26
- exports.federationBuilder = {
29
+ async function close() {
30
+ return getBuildAdapter().dispose();
31
+ }
32
+ export const federationBuilder = {
27
33
  init,
28
34
  build,
35
+ close,
29
36
  get federationInfo() {
30
37
  return fedInfo;
31
38
  },
@@ -36,4 +43,3 @@ exports.federationBuilder = {
36
43
  return config;
37
44
  },
38
45
  };
39
- //# sourceMappingURL=federation-builder.js.map
@@ -0,0 +1,8 @@
1
+ import type { FederationCache } from '../domain/core/federation-cache.contract.js';
2
+ import type { ChunkInfo, SharedInfo } from '../domain/core/federation-info.contract.js';
3
+ export declare function createFederationCache(cachePath: string): FederationCache<undefined>;
4
+ export declare function createFederationCache<TBundlerCache>(cachePath: string, bundlerCache: TBundlerCache): FederationCache<TBundlerCache>;
5
+ export declare function addExternalsToCache(cache: FederationCache, { externals, chunks }: {
6
+ externals: SharedInfo[];
7
+ chunks?: ChunkInfo;
8
+ }): void;
@@ -0,0 +1,11 @@
1
+ export function createFederationCache(cachePath, bundlerCache) {
2
+ return { externals: [], cachePath, bundlerCache };
3
+ }
4
+ export function addExternalsToCache(cache, { externals, chunks }) {
5
+ cache.externals.push(...externals);
6
+ if (chunks) {
7
+ if (!cache.chunks)
8
+ cache.chunks = {};
9
+ cache.chunks = { ...cache.chunks, ...chunks };
10
+ }
11
+ }
@@ -1,2 +1,2 @@
1
- import { NormalizedFederationConfig } from '../config/federation-config';
1
+ import type { NormalizedFederationConfig } from '../domain/config/federation-config.contract.js';
2
2
  export declare function getExternals(config: NormalizedFederationConfig): string[];
@@ -1,10 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getExternals = getExternals;
4
- function getExternals(config) {
1
+ export function getExternals(config) {
5
2
  const shared = Object.keys(config.shared);
6
- const sharedMappings = config.sharedMappings.map((m) => m.key);
3
+ const sharedMappings = Object.values(config.sharedMappings);
7
4
  const externals = [...shared, ...sharedMappings, ...config.externals];
8
5
  return externals;
9
6
  }
10
- //# sourceMappingURL=get-externals.js.map