@bleedingdev/mf-manifest 2.9.1

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.
@@ -0,0 +1,441 @@
1
+ import { composeKeyWithSeparator } from "@module-federation/sdk";
2
+ import path from "path";
3
+ import { ContainerManager, RemoteManager, SharedManager } from "@module-federation/managers";
4
+ import { getFileNameWithOutExt } from "./utils.mjs";
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+ const REMOTE_REFERENCE_PREFIX = /^(?:webpack|rspack)\/container\/reference\//;
15
+ const isNonEmptyString = (value)=>{
16
+ return typeof value === 'string' && value.trim().length > 0;
17
+ };
18
+ const normalizeExposeValue = (exposeValue)=>{
19
+ if (!exposeValue) {
20
+ return undefined;
21
+ }
22
+ const toImportArray = (value)=>{
23
+ if (isNonEmptyString(value)) {
24
+ return [
25
+ value
26
+ ];
27
+ }
28
+ if (Array.isArray(value)) {
29
+ const normalized = value.filter(isNonEmptyString);
30
+ return normalized.length ? normalized : undefined;
31
+ }
32
+ return undefined;
33
+ };
34
+ if (typeof exposeValue === 'object') {
35
+ if ('import' in exposeValue) {
36
+ const { import: rawImport, name } = exposeValue;
37
+ const normalizedImport = toImportArray(rawImport);
38
+ if (!normalizedImport?.length) {
39
+ return undefined;
40
+ }
41
+ return {
42
+ import: normalizedImport,
43
+ ...isNonEmptyString(name) ? {
44
+ name
45
+ } : {}
46
+ };
47
+ }
48
+ return undefined;
49
+ }
50
+ const normalizedImport = toImportArray(exposeValue);
51
+ if (!normalizedImport?.length) {
52
+ return undefined;
53
+ }
54
+ return {
55
+ import: normalizedImport
56
+ };
57
+ };
58
+ const parseContainerExposeEntries = (identifier)=>{
59
+ const startIndex = identifier.indexOf('[');
60
+ if (startIndex < 0) {
61
+ return undefined;
62
+ }
63
+ let depth = 0;
64
+ let inString = false;
65
+ let isEscaped = false;
66
+ for(let cursor = startIndex; cursor < identifier.length; cursor++){
67
+ const char = identifier[cursor];
68
+ if (isEscaped) {
69
+ isEscaped = false;
70
+ continue;
71
+ }
72
+ if (char === '\\') {
73
+ isEscaped = true;
74
+ continue;
75
+ }
76
+ if (char === '"') {
77
+ inString = !inString;
78
+ continue;
79
+ }
80
+ if (inString) {
81
+ continue;
82
+ }
83
+ if (char === '[') {
84
+ depth++;
85
+ } else if (char === ']') {
86
+ depth--;
87
+ if (depth === 0) {
88
+ const serialized = identifier.slice(startIndex, cursor + 1);
89
+ try {
90
+ return JSON.parse(serialized);
91
+ } catch {
92
+ return undefined;
93
+ }
94
+ }
95
+ }
96
+ }
97
+ return undefined;
98
+ };
99
+ const getExposeName = (exposeKey)=>{
100
+ return exposeKey.replace('./', '');
101
+ };
102
+ function getExposeItem({ exposeKey, name, file }) {
103
+ const exposeModuleName = getExposeName(exposeKey);
104
+ return {
105
+ path: exposeKey,
106
+ id: composeKeyWithSeparator(name, exposeModuleName),
107
+ name: exposeModuleName,
108
+ // @ts-ignore to deduplicate
109
+ requires: [],
110
+ file: path.relative(process.cwd(), file.import[0]),
111
+ assets: {
112
+ js: {
113
+ async: [],
114
+ sync: []
115
+ },
116
+ css: {
117
+ async: [],
118
+ sync: []
119
+ }
120
+ }
121
+ };
122
+ }
123
+ const getShareItem = ({ pkgName, normalizedShareOptions, pkgVersion, hostName })=>{
124
+ return {
125
+ ...normalizedShareOptions,
126
+ id: `${hostName}:${pkgName}`,
127
+ requiredVersion: normalizedShareOptions?.requiredVersion || `^${pkgVersion}`,
128
+ name: pkgName,
129
+ version: pkgVersion,
130
+ assets: {
131
+ js: {
132
+ async: [],
133
+ sync: []
134
+ },
135
+ css: {
136
+ async: [],
137
+ sync: []
138
+ }
139
+ },
140
+ // @ts-ignore to deduplicate
141
+ usedIn: new Set(),
142
+ usedExports: [],
143
+ fallback: ''
144
+ };
145
+ };
146
+ class ModuleHandler {
147
+ get isRspack() {
148
+ return this._bundler === 'rspack';
149
+ }
150
+ _handleSharedModule(mod, sharedMap, exposesMap) {
151
+ const { identifier, moduleType } = mod;
152
+ if (!identifier) {
153
+ return;
154
+ }
155
+ const sharedManagerNormalizedOptions = this._sharedManager.normalizedOptions;
156
+ const initShared = (pkgName, pkgVersion)=>{
157
+ if (sharedMap[pkgName]) {
158
+ return;
159
+ }
160
+ sharedMap[pkgName] = getShareItem({
161
+ pkgName,
162
+ pkgVersion,
163
+ normalizedShareOptions: sharedManagerNormalizedOptions[pkgName],
164
+ hostName: this._options.name
165
+ });
166
+ };
167
+ const collectRelationshipMap = (mod, pkgName)=>{
168
+ const { issuerName, reasons } = mod;
169
+ if (issuerName) {
170
+ if (exposesMap[getFileNameWithOutExt(issuerName)]) {
171
+ const expose = exposesMap[getFileNameWithOutExt(issuerName)];
172
+ // @ts-ignore use Set to deduplicate
173
+ expose.requires.push(pkgName);
174
+ // @ts-ignore use Set to deduplicate
175
+ sharedMap[pkgName].usedIn.add(expose.path);
176
+ }
177
+ }
178
+ if (reasons) {
179
+ reasons.forEach(({ resolvedModule, moduleName })=>{
180
+ let exposeModName = this.isRspack ? moduleName : resolvedModule;
181
+ // filters out entrypoints
182
+ if (exposeModName) {
183
+ if (exposesMap[getFileNameWithOutExt(exposeModName)]) {
184
+ const expose = exposesMap[getFileNameWithOutExt(exposeModName)];
185
+ // @ts-ignore to deduplicate
186
+ expose.requires.push(pkgName);
187
+ // @ts-ignore to deduplicate
188
+ sharedMap[pkgName].usedIn.add(expose.path);
189
+ }
190
+ }
191
+ });
192
+ }
193
+ };
194
+ const parseResolvedIdentifier = (nameAndVersion)=>{
195
+ let name = '';
196
+ let version = '';
197
+ if (nameAndVersion.startsWith('@')) {
198
+ const splitInfo = nameAndVersion.split('@');
199
+ splitInfo[0] = '@';
200
+ name = splitInfo[0] + splitInfo[1];
201
+ version = splitInfo[2];
202
+ } else if (nameAndVersion.includes('@')) {
203
+ [name, version] = nameAndVersion.split('@');
204
+ version = version.replace(/[\^~>|>=]/g, '');
205
+ }
206
+ return {
207
+ name,
208
+ version
209
+ };
210
+ };
211
+ if (moduleType === 'provide-module') {
212
+ // identifier(rspack) = provide shared module (default) react@18.2.0 = /temp/node_modules/.pnpm/react@18.2.0/node_modules/react/index.js
213
+ // identifier(webpack) = provide module (default) react@18.2.0 = /temp/node_modules/.pnpm/react@18.2.0/node_modules/react/index.js
214
+ const data = identifier.split(' ');
215
+ const nameAndVersion = this.isRspack ? data[4] : data[3];
216
+ const { name, version } = parseResolvedIdentifier(nameAndVersion);
217
+ if (name && version) {
218
+ initShared(name, version);
219
+ collectRelationshipMap(mod, name);
220
+ }
221
+ }
222
+ if (moduleType === 'consume-shared-module') {
223
+ // identifier(rspack) = consume shared module (default) lodash/get@^4.17.21 (strict) (fallback: /temp/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/get.js)
224
+ // identifier(webpack) = consume-shared-module|default|react-dom|!=1.8...2...0|false|/temp/node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/index.js|true|false
225
+ const SEPARATOR = this.isRspack ? ' ' : '|';
226
+ const data = identifier.split(SEPARATOR);
227
+ let pkgName = '';
228
+ let pkgVersion = '';
229
+ if (this.isRspack) {
230
+ const nameAndVersion = data[4];
231
+ const res = parseResolvedIdentifier(nameAndVersion);
232
+ pkgName = res.name;
233
+ pkgVersion = res.version;
234
+ } else {
235
+ pkgName = data[2];
236
+ const pkgVersionRange = data[3];
237
+ pkgVersion = '';
238
+ if (pkgVersionRange.startsWith('=')) {
239
+ pkgVersion = data[3].replace('=', '');
240
+ } else {
241
+ if (sharedManagerNormalizedOptions[pkgName]) {
242
+ pkgVersion = sharedManagerNormalizedOptions[pkgName].version;
243
+ } else {
244
+ const fullPkgName = pkgName.split('/').slice(0, -1).join('/');
245
+ // pkgName: react-dom/
246
+ if (sharedManagerNormalizedOptions[`${fullPkgName}/`]) {
247
+ if (sharedManagerNormalizedOptions[fullPkgName]) {
248
+ pkgVersion = sharedManagerNormalizedOptions[fullPkgName].version;
249
+ } else {
250
+ pkgVersion = sharedManagerNormalizedOptions[`${fullPkgName}/`].version;
251
+ }
252
+ }
253
+ }
254
+ }
255
+ }
256
+ if (pkgName && pkgVersion) {
257
+ initShared(pkgName, pkgVersion);
258
+ collectRelationshipMap(mod, pkgName);
259
+ }
260
+ }
261
+ }
262
+ _handleRemoteModule(mod, remotes, remotesConsumerMap) {
263
+ const { identifier, reasons, nameForCondition } = mod;
264
+ if (!identifier) {
265
+ return;
266
+ }
267
+ const remoteManagerNormalizedOptions = this._remoteManager.normalizedOptions;
268
+ // identifier = remote (default) [webpack|rspack]/container/reference/app2 ./Button
269
+ const data = identifier.split(' ');
270
+ if (data.length === 4) {
271
+ const moduleName = data[3].replace('./', '');
272
+ const remoteAlias = data[2].replace(REMOTE_REFERENCE_PREFIX, '');
273
+ const normalizedRemote = remoteManagerNormalizedOptions[remoteAlias];
274
+ const basicRemote = {
275
+ alias: normalizedRemote.alias,
276
+ consumingFederationContainerName: this._options.name || '',
277
+ federationContainerName: remoteManagerNormalizedOptions[remoteAlias].name,
278
+ moduleName,
279
+ // @ts-ignore to deduplicate
280
+ usedIn: new Set()
281
+ };
282
+ if (!nameForCondition) {
283
+ return;
284
+ }
285
+ let remote;
286
+ if ('version' in normalizedRemote) {
287
+ remote = {
288
+ ...basicRemote,
289
+ version: normalizedRemote.version
290
+ };
291
+ } else {
292
+ remote = {
293
+ ...basicRemote,
294
+ entry: normalizedRemote.entry
295
+ };
296
+ }
297
+ remotes.push(remote);
298
+ remotesConsumerMap[nameForCondition] = remote;
299
+ }
300
+ if (reasons) {
301
+ reasons.forEach(({ userRequest, resolvedModule, moduleName })=>{
302
+ let exposeModName = this.isRspack ? moduleName : resolvedModule;
303
+ if (userRequest && exposeModName && remotesConsumerMap[userRequest]) {
304
+ // @ts-ignore to deduplicate
305
+ remotesConsumerMap[userRequest].usedIn.add(exposeModName.replace('./', ''));
306
+ }
307
+ });
308
+ }
309
+ }
310
+ _handleContainerModule(mod, exposesMap) {
311
+ const { identifier } = mod;
312
+ if (!identifier) {
313
+ return;
314
+ }
315
+ // identifier: container entry (default) [[".",{"import":["./src/routes/page.tsx"],"name":"__federation_expose_default_export"}]]'
316
+ const entries = parseContainerExposeEntries(identifier) ?? this._getContainerExposeEntriesFromOptions();
317
+ if (!entries) {
318
+ return;
319
+ }
320
+ entries.forEach(([prefixedName, file])=>{
321
+ // TODO: support multiple import
322
+ exposesMap[getFileNameWithOutExt(file.import[0])] = getExposeItem({
323
+ exposeKey: prefixedName,
324
+ name: this._options.name,
325
+ file
326
+ });
327
+ });
328
+ }
329
+ _getContainerExposeEntriesFromOptions() {
330
+ const exposes = this._containerManager.containerPluginExposesOptions;
331
+ const normalizedEntries = Object.entries(exposes).reduce((acc, [exposeKey, exposeOptions])=>{
332
+ const normalizedExpose = normalizeExposeValue(exposeOptions);
333
+ if (!normalizedExpose?.import.length) {
334
+ return acc;
335
+ }
336
+ acc.push([
337
+ exposeKey,
338
+ normalizedExpose
339
+ ]);
340
+ return acc;
341
+ }, []);
342
+ if (normalizedEntries.length) {
343
+ return normalizedEntries;
344
+ }
345
+ const rawExposes = this._options.exposes;
346
+ if (!rawExposes || Array.isArray(rawExposes)) {
347
+ return undefined;
348
+ }
349
+ const normalizedFromOptions = Object.entries(rawExposes).reduce((acc, [exposeKey, exposeOptions])=>{
350
+ const normalizedExpose = normalizeExposeValue(exposeOptions);
351
+ if (!normalizedExpose?.import.length) {
352
+ return acc;
353
+ }
354
+ acc.push([
355
+ exposeKey,
356
+ normalizedExpose
357
+ ]);
358
+ return acc;
359
+ }, []);
360
+ return normalizedFromOptions.length ? normalizedFromOptions : undefined;
361
+ }
362
+ _initializeExposesFromOptions(exposesMap) {
363
+ if (!this._options.name || !this._containerManager.enable) {
364
+ return;
365
+ }
366
+ const exposes = this._containerManager.containerPluginExposesOptions;
367
+ Object.entries(exposes).forEach(([exposeKey, exposeOptions])=>{
368
+ if (!exposeOptions.import?.length) {
369
+ return;
370
+ }
371
+ const [exposeImport] = exposeOptions.import;
372
+ if (!exposeImport) {
373
+ return;
374
+ }
375
+ const exposeMapKey = getFileNameWithOutExt(exposeImport);
376
+ if (!exposesMap[exposeMapKey]) {
377
+ exposesMap[exposeMapKey] = getExposeItem({
378
+ exposeKey,
379
+ name: this._options.name,
380
+ file: exposeOptions
381
+ });
382
+ }
383
+ });
384
+ }
385
+ collect() {
386
+ const remotes = [];
387
+ const remotesConsumerMap = {};
388
+ const exposesMap = {};
389
+ const sharedMap = {};
390
+ this._initializeExposesFromOptions(exposesMap);
391
+ const isSharedModule = (moduleType)=>{
392
+ return Boolean(moduleType && [
393
+ 'provide-module',
394
+ 'consume-shared-module'
395
+ ].includes(moduleType));
396
+ };
397
+ const isContainerModule = (identifier)=>{
398
+ return identifier.startsWith('container entry');
399
+ };
400
+ const isRemoteModule = (identifier)=>{
401
+ return identifier.startsWith('remote ');
402
+ };
403
+ // handle remote/expose
404
+ this._modules.forEach((mod)=>{
405
+ const { identifier, reasons, nameForCondition, moduleType } = mod;
406
+ if (!identifier) {
407
+ return;
408
+ }
409
+ if (isSharedModule(moduleType)) {
410
+ this._handleSharedModule(mod, sharedMap, exposesMap);
411
+ }
412
+ if (isRemoteModule(identifier)) {
413
+ this._handleRemoteModule(mod, remotes, remotesConsumerMap);
414
+ } else if (isContainerModule(identifier)) {
415
+ this._handleContainerModule(mod, exposesMap);
416
+ }
417
+ });
418
+ return {
419
+ remotes,
420
+ exposesMap,
421
+ sharedMap
422
+ };
423
+ }
424
+ constructor(options, modules, { bundler }){
425
+ this._bundler = 'webpack';
426
+ this._remoteManager = new RemoteManager();
427
+ this._sharedManager = new SharedManager();
428
+ this._options = options;
429
+ this._modules = modules;
430
+ this._bundler = bundler;
431
+ this._containerManager = new ContainerManager();
432
+ this._containerManager.init(options);
433
+ this._remoteManager = new RemoteManager();
434
+ this._remoteManager.init(options);
435
+ this._sharedManager = new SharedManager();
436
+ this._sharedManager.init(options);
437
+ }
438
+ }
439
+
440
+
441
+ export { ModuleHandler, getExposeItem, getExposeName, getShareItem };
@@ -0,0 +1,29 @@
1
+ import { BasicStatsMetaData, StatsMetaData, Stats, moduleFederationPlugin } from '@module-federation/sdk';
2
+ import { Compilation, Compiler } from 'webpack';
3
+ declare class StatsManager {
4
+ private _options;
5
+ private _publicPath?;
6
+ private _pluginVersion?;
7
+ private _bundler;
8
+ private _containerManager;
9
+ private _remoteManager;
10
+ private _sharedManager;
11
+ private _pkgJsonManager;
12
+ private getBuildInfo;
13
+ get fileName(): string;
14
+ setMetaDataPublicPath(metaData: BasicStatsMetaData, compiler: Compiler): StatsMetaData;
15
+ private _getMetaData;
16
+ private _getFilteredModules;
17
+ private _getModuleAssets;
18
+ private _getProvideSharedAssets;
19
+ private _generateStats;
20
+ getPublicPath(compiler: Compiler): string;
21
+ init(options: moduleFederationPlugin.ModuleFederationPluginOptions, { pluginVersion, bundler, }: {
22
+ pluginVersion: string;
23
+ bundler: 'webpack' | 'rspack';
24
+ }): void;
25
+ updateStats(stats: Stats, compiler: Compiler): Stats;
26
+ generateStats(compiler: Compiler, compilation: Compilation): Promise<Stats>;
27
+ validate(compiler: Compiler): boolean;
28
+ }
29
+ export { StatsManager };