@module-federation/vite 1.9.7 → 1.10.0
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 +4 -0
- package/lib/index.cjs +443 -427
- package/lib/index.esm.js +443 -427
- package/lib/index.modern.js +424 -408
- package/lib/index.umd.js +443 -427
- package/lib/plugins/pluginModuleParseEnd.d.ts +3 -1
- package/lib/utils/normalizeModuleFederationOptions.d.ts +6 -0
- package/package.json +2 -2
package/lib/index.modern.js
CHANGED
|
@@ -300,6 +300,306 @@ function _extends() {
|
|
|
300
300
|
}, _extends.apply(null, arguments);
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
+
const DEFAULT_DEV_OPTIONS = {
|
|
304
|
+
disableLiveReload: true,
|
|
305
|
+
disableHotTypesReload: false,
|
|
306
|
+
disableDynamicRemoteTypeHints: false
|
|
307
|
+
};
|
|
308
|
+
const DYNAMIC_HINTS_PLUGIN = '@module-federation/dts-plugin/dynamic-remote-type-hints-plugin';
|
|
309
|
+
const getIPv4 = () => process.env['FEDERATION_IPV4'] || '127.0.0.1';
|
|
310
|
+
const forkDevWorkerPath = (() => {
|
|
311
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
312
|
+
return require.resolve('@module-federation/dts-plugin/dist/fork-dev-worker.js');
|
|
313
|
+
})();
|
|
314
|
+
class DevWorker {
|
|
315
|
+
constructor(options) {
|
|
316
|
+
this.worker = rpc.createRpcWorker(forkDevWorkerPath, {}, undefined, false);
|
|
317
|
+
this.worker.connect(options);
|
|
318
|
+
}
|
|
319
|
+
update() {
|
|
320
|
+
var _this$worker$process;
|
|
321
|
+
(_this$worker$process = this.worker.process) == null || _this$worker$process.send == null || _this$worker$process.send({
|
|
322
|
+
type: rpc.RpcGMCallTypes.CALL,
|
|
323
|
+
id: this.worker.id,
|
|
324
|
+
args: [undefined, 'update']
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
exit() {
|
|
328
|
+
this.worker.terminate();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const normalizeDevOptions = dev => {
|
|
332
|
+
if (dev === false) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
if (dev === true || typeof dev === 'undefined') {
|
|
336
|
+
return _extends({}, DEFAULT_DEV_OPTIONS);
|
|
337
|
+
}
|
|
338
|
+
return _extends({}, DEFAULT_DEV_OPTIONS, dev);
|
|
339
|
+
};
|
|
340
|
+
const buildDtsModuleFederationConfig = options => {
|
|
341
|
+
const exposes = {};
|
|
342
|
+
Object.entries(options.exposes).forEach(([key, value]) => {
|
|
343
|
+
if (typeof value === 'string') {
|
|
344
|
+
exposes[key] = value;
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const importValue = Array.isArray(value.import) ? value.import[0] : value.import;
|
|
348
|
+
if (importValue) {
|
|
349
|
+
exposes[key] = importValue;
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
const remotes = {};
|
|
353
|
+
Object.entries(options.remotes).forEach(([key, remote]) => {
|
|
354
|
+
var _remote$entryGlobalNa, _remote$entryGlobalNa2;
|
|
355
|
+
if (typeof remote === 'string') {
|
|
356
|
+
remotes[key] = remote;
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (!remote.entry) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const entryLooksLikeUrl = ((_remote$entryGlobalNa = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa.startsWith('http')) || ((_remote$entryGlobalNa2 = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa2.includes('.json'));
|
|
363
|
+
const entryGlobalName = entryLooksLikeUrl ? remote.name || key : remote.entryGlobalName || remote.name || key;
|
|
364
|
+
remotes[key] = `${entryGlobalName}@${remote.entry}`;
|
|
365
|
+
});
|
|
366
|
+
return _extends({}, options, {
|
|
367
|
+
exposes,
|
|
368
|
+
remotes
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
const resolveOutputDir = config => {
|
|
372
|
+
const {
|
|
373
|
+
outDir
|
|
374
|
+
} = config.build;
|
|
375
|
+
if (path.isAbsolute(outDir)) {
|
|
376
|
+
return path.relative(config.root, outDir);
|
|
377
|
+
}
|
|
378
|
+
return outDir;
|
|
379
|
+
};
|
|
380
|
+
const ensureRuntimePlugin = (options, pluginId) => {
|
|
381
|
+
const hasPlugin = options.runtimePlugins.some(plugin => {
|
|
382
|
+
if (typeof plugin === 'string') {
|
|
383
|
+
return plugin === pluginId;
|
|
384
|
+
}
|
|
385
|
+
return plugin[0] === pluginId;
|
|
386
|
+
});
|
|
387
|
+
if (!hasPlugin) {
|
|
388
|
+
options.runtimePlugins.push(pluginId);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
const normalizeDevDtsOptions = (dts, context) => {
|
|
392
|
+
const defaultGenerateTypes = {
|
|
393
|
+
compileInChildProcess: true
|
|
394
|
+
};
|
|
395
|
+
const defaultConsumeTypes = {
|
|
396
|
+
consumeAPITypes: true
|
|
397
|
+
};
|
|
398
|
+
return normalizeOptions(isTSProject(dts, context), {
|
|
399
|
+
generateTypes: defaultGenerateTypes,
|
|
400
|
+
consumeTypes: defaultConsumeTypes,
|
|
401
|
+
extraOptions: {},
|
|
402
|
+
displayErrorInTerminal: typeof dts === 'object' && dts ? dts.displayErrorInTerminal : undefined
|
|
403
|
+
}, 'mfOptions.dts')(dts);
|
|
404
|
+
};
|
|
405
|
+
const logDtsError = (error, dtsOptions) => {
|
|
406
|
+
if (dtsOptions === false) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (typeof dtsOptions === 'object' && dtsOptions && dtsOptions.displayErrorInTerminal === false) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
console.error(error);
|
|
413
|
+
};
|
|
414
|
+
function pluginDts(options) {
|
|
415
|
+
if (options.dts === false) {
|
|
416
|
+
return [];
|
|
417
|
+
}
|
|
418
|
+
const dtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
|
|
419
|
+
let resolvedConfig;
|
|
420
|
+
let devWorker;
|
|
421
|
+
let normalizedDevOptions;
|
|
422
|
+
let hasGeneratedBundle = false;
|
|
423
|
+
const devPlugin = {
|
|
424
|
+
name: 'module-federation-dts-dev',
|
|
425
|
+
apply: 'serve',
|
|
426
|
+
config(config) {
|
|
427
|
+
normalizedDevOptions = normalizeDevOptions(options.dev);
|
|
428
|
+
if (!normalizedDevOptions) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (normalizedDevOptions.disableDynamicRemoteTypeHints) {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
ensureRuntimePlugin(options, DYNAMIC_HINTS_PLUGIN);
|
|
435
|
+
const define = config.define ? _extends({}, config.define) : {};
|
|
436
|
+
if (!('FEDERATION_IPV4' in define)) {
|
|
437
|
+
define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
|
|
438
|
+
}
|
|
439
|
+
config.define = define;
|
|
440
|
+
},
|
|
441
|
+
configResolved(config) {
|
|
442
|
+
resolvedConfig = config;
|
|
443
|
+
},
|
|
444
|
+
configureServer(server) {
|
|
445
|
+
if (!normalizedDevOptions || !resolvedConfig) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const devOptions = normalizedDevOptions;
|
|
449
|
+
if (devOptions.disableDynamicRemoteTypeHints && devOptions.disableHotTypesReload && devOptions.disableLiveReload) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (!options.name) {
|
|
453
|
+
throw new Error('name is required if you want to enable dev server!');
|
|
454
|
+
}
|
|
455
|
+
const outputDir = resolveOutputDir(resolvedConfig);
|
|
456
|
+
const normalizedDtsOptions = normalizeDevDtsOptions(options.dts, resolvedConfig.root);
|
|
457
|
+
if (typeof normalizedDtsOptions !== 'object') {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const normalizedGenerateTypes = normalizeOptions(Boolean(normalizedDtsOptions), {
|
|
461
|
+
compileInChildProcess: true
|
|
462
|
+
}, 'mfOptions.dts.generateTypes')(normalizedDtsOptions.generateTypes);
|
|
463
|
+
const remote = normalizedGenerateTypes === false ? undefined : _extends({
|
|
464
|
+
implementation: normalizedDtsOptions.implementation,
|
|
465
|
+
context: resolvedConfig.root,
|
|
466
|
+
outputDir,
|
|
467
|
+
moduleFederationConfig: _extends({}, dtsModuleFederationConfig),
|
|
468
|
+
hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || '@mf-types'
|
|
469
|
+
}, normalizedGenerateTypes, {
|
|
470
|
+
typesFolder: '.dev-server'
|
|
471
|
+
});
|
|
472
|
+
if (remote && !remote.tsConfigPath && typeof normalizedDtsOptions === 'object' && normalizedDtsOptions.tsConfigPath) {
|
|
473
|
+
remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
|
|
474
|
+
}
|
|
475
|
+
const normalizedConsumeTypes = normalizeOptions(Boolean(normalizedDtsOptions), {
|
|
476
|
+
consumeAPITypes: true
|
|
477
|
+
}, 'mfOptions.dts.consumeTypes')(normalizedDtsOptions.consumeTypes);
|
|
478
|
+
const host = normalizedConsumeTypes === false ? undefined : _extends({
|
|
479
|
+
implementation: normalizedDtsOptions.implementation,
|
|
480
|
+
context: resolvedConfig.root,
|
|
481
|
+
moduleFederationConfig: dtsModuleFederationConfig,
|
|
482
|
+
typesFolder: normalizedConsumeTypes.typesFolder || '@mf-types',
|
|
483
|
+
abortOnError: false
|
|
484
|
+
}, normalizedConsumeTypes);
|
|
485
|
+
const extraOptions = normalizedDtsOptions.extraOptions || {};
|
|
486
|
+
if (!remote && !host && devOptions.disableLiveReload) {
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const startDevWorker = async () => {
|
|
490
|
+
var _server$httpServer;
|
|
491
|
+
let remoteTypeUrls;
|
|
492
|
+
if (host) {
|
|
493
|
+
remoteTypeUrls = await new Promise(resolve => {
|
|
494
|
+
consumeTypesAPI({
|
|
495
|
+
host,
|
|
496
|
+
extraOptions,
|
|
497
|
+
displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
|
|
498
|
+
}, resolve);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
devWorker = new DevWorker({
|
|
502
|
+
name: options.name,
|
|
503
|
+
remote,
|
|
504
|
+
host: host ? _extends({}, host, {
|
|
505
|
+
remoteTypeUrls
|
|
506
|
+
}) : undefined,
|
|
507
|
+
extraOptions,
|
|
508
|
+
disableLiveReload: devOptions.disableLiveReload,
|
|
509
|
+
disableHotTypesReload: devOptions.disableHotTypesReload
|
|
510
|
+
});
|
|
511
|
+
const update = () => {
|
|
512
|
+
var _devWorker;
|
|
513
|
+
return (_devWorker = devWorker) == null ? void 0 : _devWorker.update();
|
|
514
|
+
};
|
|
515
|
+
server.watcher.on('change', update);
|
|
516
|
+
server.watcher.on('add', update);
|
|
517
|
+
server.watcher.on('unlink', update);
|
|
518
|
+
(_server$httpServer = server.httpServer) == null || _server$httpServer.once('close', () => {
|
|
519
|
+
var _devWorker2;
|
|
520
|
+
(_devWorker2 = devWorker) == null || _devWorker2.exit();
|
|
521
|
+
server.watcher.off('change', update);
|
|
522
|
+
server.watcher.off('add', update);
|
|
523
|
+
server.watcher.off('unlink', update);
|
|
524
|
+
});
|
|
525
|
+
};
|
|
526
|
+
startDevWorker().catch(error => {
|
|
527
|
+
logDtsError(error, normalizedDtsOptions);
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
const buildPlugin = {
|
|
532
|
+
name: 'module-federation-dts-build',
|
|
533
|
+
apply: 'build',
|
|
534
|
+
configResolved(config) {
|
|
535
|
+
resolvedConfig = config;
|
|
536
|
+
},
|
|
537
|
+
async generateBundle() {
|
|
538
|
+
var _consumeOptions;
|
|
539
|
+
if (hasGeneratedBundle) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
hasGeneratedBundle = true;
|
|
543
|
+
if (!resolvedConfig) {
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
let normalizedDtsOptions;
|
|
547
|
+
try {
|
|
548
|
+
normalizedDtsOptions = normalizeDtsOptions(dtsModuleFederationConfig, resolvedConfig.root);
|
|
549
|
+
} catch (error) {
|
|
550
|
+
logDtsError(error, options.dts);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (typeof normalizedDtsOptions !== 'object') {
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
const context = resolvedConfig.root;
|
|
557
|
+
const outputDir = resolveOutputDir(resolvedConfig);
|
|
558
|
+
let consumeOptions;
|
|
559
|
+
try {
|
|
560
|
+
consumeOptions = normalizeConsumeTypesOptions({
|
|
561
|
+
context,
|
|
562
|
+
dtsOptions: normalizedDtsOptions,
|
|
563
|
+
pluginOptions: dtsModuleFederationConfig
|
|
564
|
+
});
|
|
565
|
+
} catch (error) {
|
|
566
|
+
logDtsError(error, normalizedDtsOptions);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
if ((_consumeOptions = consumeOptions) != null && (_consumeOptions = _consumeOptions.host) != null && _consumeOptions.typesOnBuild) {
|
|
570
|
+
try {
|
|
571
|
+
await consumeTypesAPI(consumeOptions);
|
|
572
|
+
} catch (error) {
|
|
573
|
+
logDtsError(error, normalizedDtsOptions);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
let generateOptions;
|
|
577
|
+
try {
|
|
578
|
+
generateOptions = normalizeGenerateTypesOptions({
|
|
579
|
+
context,
|
|
580
|
+
outputDir,
|
|
581
|
+
dtsOptions: normalizedDtsOptions,
|
|
582
|
+
pluginOptions: dtsModuleFederationConfig
|
|
583
|
+
});
|
|
584
|
+
} catch (error) {
|
|
585
|
+
logDtsError(error, normalizedDtsOptions);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (!generateOptions) {
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
try {
|
|
592
|
+
await generateTypesAPI({
|
|
593
|
+
dtsManagerOptions: generateOptions
|
|
594
|
+
});
|
|
595
|
+
} catch (error) {
|
|
596
|
+
logDtsError(error, normalizedDtsOptions);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
return [devPlugin, buildPlugin];
|
|
601
|
+
}
|
|
602
|
+
|
|
303
603
|
function normalizeExposesItem(key, item) {
|
|
304
604
|
let importPath = '';
|
|
305
605
|
if (typeof item === 'string') {
|
|
@@ -488,7 +788,8 @@ function normalizeModuleFederationOptions(options) {
|
|
|
488
788
|
ignoreOrigin: options.ignoreOrigin || false,
|
|
489
789
|
virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
|
|
490
790
|
hostInitInjectLocation: options.hostInitInjectLocation || 'html',
|
|
491
|
-
bundleAllCSS: options.bundleAllCSS || false
|
|
791
|
+
bundleAllCSS: options.bundleAllCSS || false,
|
|
792
|
+
moduleParseTimeout: options.moduleParseTimeout || 10
|
|
492
793
|
};
|
|
493
794
|
}
|
|
494
795
|
|
|
@@ -1448,15 +1749,28 @@ const Manifest = () => {
|
|
|
1448
1749
|
}
|
|
1449
1750
|
};
|
|
1450
1751
|
|
|
1451
|
-
let _resolve,
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1752
|
+
let _resolve, _parseTimeout;
|
|
1753
|
+
const promise = new Promise((resolve, reject) => {
|
|
1754
|
+
_resolve = v => {
|
|
1755
|
+
clearTimeout(_parseTimeout);
|
|
1756
|
+
_parseTimeout = null;
|
|
1757
|
+
resolve(v);
|
|
1758
|
+
};
|
|
1759
|
+
});
|
|
1760
|
+
function setParseTimeout(timeout) {
|
|
1761
|
+
if (!_parseTimeout) {
|
|
1762
|
+
_parseTimeout = setTimeout(() => {
|
|
1763
|
+
console.warn(`Parse timeout (${timeout}s) - forcing resolve`);
|
|
1764
|
+
_resolve(1);
|
|
1765
|
+
}, timeout * 1000);
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1455
1768
|
let parsePromise = promise;
|
|
1456
1769
|
let exposesParseEnd = false;
|
|
1457
1770
|
const parseStartSet = new Set();
|
|
1458
1771
|
const parseEndSet = new Set();
|
|
1459
|
-
function pluginModuleParseEnd (excludeFn) {
|
|
1772
|
+
function pluginModuleParseEnd (excludeFn, options) {
|
|
1773
|
+
setParseTimeout(options.moduleParseTimeout);
|
|
1460
1774
|
return [{
|
|
1461
1775
|
name: '_',
|
|
1462
1776
|
apply: 'serve',
|
|
@@ -1468,427 +1782,127 @@ function pluginModuleParseEnd (excludeFn) {
|
|
|
1468
1782
|
enforce: 'pre',
|
|
1469
1783
|
name: 'parseStart',
|
|
1470
1784
|
apply: 'build',
|
|
1471
|
-
load(id) {
|
|
1472
|
-
if (excludeFn(id)) {
|
|
1473
|
-
return;
|
|
1474
|
-
}
|
|
1475
|
-
parseStartSet.add(id);
|
|
1476
|
-
}
|
|
1477
|
-
}, {
|
|
1478
|
-
enforce: 'post',
|
|
1479
|
-
name: 'parseEnd',
|
|
1480
|
-
apply: 'build',
|
|
1481
|
-
moduleParsed(module) {
|
|
1482
|
-
const id = module.id;
|
|
1483
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1484
|
-
// When the entry JS file is empty and only contains exposes export code, it’s necessary to wait for the exposes modules to be resolved in order to collect the dependencies being used.
|
|
1485
|
-
exposesParseEnd = true;
|
|
1486
|
-
}
|
|
1487
|
-
if (excludeFn(id)) {
|
|
1488
|
-
return;
|
|
1489
|
-
}
|
|
1490
|
-
parseEndSet.add(id);
|
|
1491
|
-
if (exposesParseEnd && parseStartSet.size === parseEndSet.size) {
|
|
1492
|
-
_resolve(1);
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
}];
|
|
1496
|
-
}
|
|
1497
|
-
|
|
1498
|
-
const filter = createFilter();
|
|
1499
|
-
function pluginProxyRemoteEntry () {
|
|
1500
|
-
let viteConfig, _command;
|
|
1501
|
-
return {
|
|
1502
|
-
name: 'proxyRemoteEntry',
|
|
1503
|
-
enforce: 'post',
|
|
1504
|
-
configResolved(config) {
|
|
1505
|
-
viteConfig = config;
|
|
1506
|
-
},
|
|
1507
|
-
config(config, {
|
|
1508
|
-
command
|
|
1509
|
-
}) {
|
|
1510
|
-
_command = command;
|
|
1511
|
-
},
|
|
1512
|
-
resolveId(id) {
|
|
1513
|
-
if (id === REMOTE_ENTRY_ID) {
|
|
1514
|
-
return REMOTE_ENTRY_ID;
|
|
1515
|
-
}
|
|
1516
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1517
|
-
return VIRTUAL_EXPOSES;
|
|
1518
|
-
}
|
|
1519
|
-
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
|
|
1520
|
-
return id;
|
|
1521
|
-
}
|
|
1522
|
-
},
|
|
1523
|
-
load(id) {
|
|
1524
|
-
if (id === REMOTE_ENTRY_ID) {
|
|
1525
|
-
return parsePromise.then(_ => generateRemoteEntry(getNormalizeModuleFederationOptions()));
|
|
1526
|
-
}
|
|
1527
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1528
|
-
return generateExposes();
|
|
1529
|
-
}
|
|
1530
|
-
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
|
|
1531
|
-
return id;
|
|
1532
|
-
}
|
|
1533
|
-
},
|
|
1534
|
-
transform(code, id) {
|
|
1535
|
-
const transformedCode = (() => {
|
|
1536
|
-
if (!filter(id)) return;
|
|
1537
|
-
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
1538
|
-
return parsePromise.then(_ => generateRemoteEntry(getNormalizeModuleFederationOptions()));
|
|
1539
|
-
}
|
|
1540
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1541
|
-
return generateExposes();
|
|
1542
|
-
}
|
|
1543
|
-
if (id.includes(getHostAutoInitPath())) {
|
|
1544
|
-
const options = getNormalizeModuleFederationOptions();
|
|
1545
|
-
if (_command === 'serve') {
|
|
1546
|
-
var _viteConfig$server, _viteConfig$server2;
|
|
1547
|
-
const host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
|
|
1548
|
-
const publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
|
|
1549
|
-
return `
|
|
1550
|
-
const origin = (window && ${!options.ignoreOrigin}) ? window.origin : "//${host}:${(_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port}"
|
|
1551
|
-
const remoteEntryPromise = await import(origin + ${publicPath})
|
|
1552
|
-
// __tla only serves as a hack for vite-plugin-top-level-await.
|
|
1553
|
-
Promise.resolve(remoteEntryPromise)
|
|
1554
|
-
.then(remoteEntry => {
|
|
1555
|
-
return Promise.resolve(remoteEntry.__tla)
|
|
1556
|
-
.then(remoteEntry.init).catch(remoteEntry.init)
|
|
1557
|
-
})
|
|
1558
|
-
`;
|
|
1559
|
-
}
|
|
1560
|
-
return code;
|
|
1561
|
-
}
|
|
1562
|
-
})();
|
|
1563
|
-
return mapCodeToCodeWithSourcemap(transformedCode);
|
|
1564
|
-
}
|
|
1565
|
-
};
|
|
1566
|
-
}
|
|
1567
|
-
|
|
1568
|
-
createFilter();
|
|
1569
|
-
function pluginProxyRemotes (options) {
|
|
1570
|
-
const {
|
|
1571
|
-
remotes
|
|
1572
|
-
} = options;
|
|
1573
|
-
return {
|
|
1574
|
-
name: 'proxyRemotes',
|
|
1575
|
-
config(config, {
|
|
1576
|
-
command: _command
|
|
1577
|
-
}) {
|
|
1578
|
-
Object.keys(remotes).forEach(key => {
|
|
1579
|
-
const remote = remotes[key];
|
|
1580
|
-
config.resolve.alias.push({
|
|
1581
|
-
find: new RegExp(`^(${remote.name}(\/.*|$))`),
|
|
1582
|
-
replacement: '$1',
|
|
1583
|
-
customResolver(source) {
|
|
1584
|
-
const remoteModule = getRemoteVirtualModule(source, _command);
|
|
1585
|
-
addUsedRemote(remote.name, source);
|
|
1586
|
-
return remoteModule.getPath();
|
|
1587
|
-
}
|
|
1588
|
-
});
|
|
1589
|
-
});
|
|
1590
|
-
}
|
|
1591
|
-
};
|
|
1592
|
-
}
|
|
1593
|
-
|
|
1594
|
-
const DEFAULT_DEV_OPTIONS = {
|
|
1595
|
-
disableLiveReload: true,
|
|
1596
|
-
disableHotTypesReload: false,
|
|
1597
|
-
disableDynamicRemoteTypeHints: false
|
|
1598
|
-
};
|
|
1599
|
-
const DYNAMIC_HINTS_PLUGIN = '@module-federation/dts-plugin/dynamic-remote-type-hints-plugin';
|
|
1600
|
-
const getIPv4 = () => process.env['FEDERATION_IPV4'] || '127.0.0.1';
|
|
1601
|
-
const forkDevWorkerPath = (() => {
|
|
1602
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
1603
|
-
return require.resolve('@module-federation/dts-plugin/dist/fork-dev-worker.js');
|
|
1604
|
-
})();
|
|
1605
|
-
class DevWorker {
|
|
1606
|
-
constructor(options) {
|
|
1607
|
-
this.worker = rpc.createRpcWorker(forkDevWorkerPath, {}, undefined, false);
|
|
1608
|
-
this.worker.connect(options);
|
|
1609
|
-
}
|
|
1610
|
-
update() {
|
|
1611
|
-
var _this$worker$process;
|
|
1612
|
-
(_this$worker$process = this.worker.process) == null || _this$worker$process.send == null || _this$worker$process.send({
|
|
1613
|
-
type: rpc.RpcGMCallTypes.CALL,
|
|
1614
|
-
id: this.worker.id,
|
|
1615
|
-
args: [undefined, 'update']
|
|
1616
|
-
});
|
|
1617
|
-
}
|
|
1618
|
-
exit() {
|
|
1619
|
-
this.worker.terminate();
|
|
1620
|
-
}
|
|
1621
|
-
}
|
|
1622
|
-
const normalizeDevOptions = dev => {
|
|
1623
|
-
if (dev === false) {
|
|
1624
|
-
return false;
|
|
1625
|
-
}
|
|
1626
|
-
if (dev === true || typeof dev === 'undefined') {
|
|
1627
|
-
return _extends({}, DEFAULT_DEV_OPTIONS);
|
|
1628
|
-
}
|
|
1629
|
-
return _extends({}, DEFAULT_DEV_OPTIONS, dev);
|
|
1630
|
-
};
|
|
1631
|
-
const buildDtsModuleFederationConfig = options => {
|
|
1632
|
-
const exposes = {};
|
|
1633
|
-
Object.entries(options.exposes).forEach(([key, value]) => {
|
|
1634
|
-
if (typeof value === 'string') {
|
|
1635
|
-
exposes[key] = value;
|
|
1636
|
-
return;
|
|
1637
|
-
}
|
|
1638
|
-
const importValue = Array.isArray(value.import) ? value.import[0] : value.import;
|
|
1639
|
-
if (importValue) {
|
|
1640
|
-
exposes[key] = importValue;
|
|
1641
|
-
}
|
|
1642
|
-
});
|
|
1643
|
-
const remotes = {};
|
|
1644
|
-
Object.entries(options.remotes).forEach(([key, remote]) => {
|
|
1645
|
-
var _remote$entryGlobalNa, _remote$entryGlobalNa2;
|
|
1646
|
-
if (typeof remote === 'string') {
|
|
1647
|
-
remotes[key] = remote;
|
|
1648
|
-
return;
|
|
1649
|
-
}
|
|
1650
|
-
if (!remote.entry) {
|
|
1651
|
-
return;
|
|
1652
|
-
}
|
|
1653
|
-
const entryLooksLikeUrl = ((_remote$entryGlobalNa = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa.startsWith('http')) || ((_remote$entryGlobalNa2 = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa2.includes('.json'));
|
|
1654
|
-
const entryGlobalName = entryLooksLikeUrl ? remote.name || key : remote.entryGlobalName || remote.name || key;
|
|
1655
|
-
remotes[key] = `${entryGlobalName}@${remote.entry}`;
|
|
1656
|
-
});
|
|
1657
|
-
return _extends({}, options, {
|
|
1658
|
-
exposes,
|
|
1659
|
-
remotes
|
|
1660
|
-
});
|
|
1661
|
-
};
|
|
1662
|
-
const resolveOutputDir = config => {
|
|
1663
|
-
const {
|
|
1664
|
-
outDir
|
|
1665
|
-
} = config.build;
|
|
1666
|
-
if (path.isAbsolute(outDir)) {
|
|
1667
|
-
return path.relative(config.root, outDir);
|
|
1668
|
-
}
|
|
1669
|
-
return outDir;
|
|
1670
|
-
};
|
|
1671
|
-
const ensureRuntimePlugin = (options, pluginId) => {
|
|
1672
|
-
const hasPlugin = options.runtimePlugins.some(plugin => {
|
|
1673
|
-
if (typeof plugin === 'string') {
|
|
1674
|
-
return plugin === pluginId;
|
|
1675
|
-
}
|
|
1676
|
-
return plugin[0] === pluginId;
|
|
1677
|
-
});
|
|
1678
|
-
if (!hasPlugin) {
|
|
1679
|
-
options.runtimePlugins.push(pluginId);
|
|
1680
|
-
}
|
|
1681
|
-
};
|
|
1682
|
-
const normalizeDevDtsOptions = (dts, context) => {
|
|
1683
|
-
const defaultGenerateTypes = {
|
|
1684
|
-
compileInChildProcess: true
|
|
1685
|
-
};
|
|
1686
|
-
const defaultConsumeTypes = {
|
|
1687
|
-
consumeAPITypes: true
|
|
1688
|
-
};
|
|
1689
|
-
return normalizeOptions(isTSProject(dts, context), {
|
|
1690
|
-
generateTypes: defaultGenerateTypes,
|
|
1691
|
-
consumeTypes: defaultConsumeTypes,
|
|
1692
|
-
extraOptions: {},
|
|
1693
|
-
displayErrorInTerminal: typeof dts === 'object' && dts ? dts.displayErrorInTerminal : undefined
|
|
1694
|
-
}, 'mfOptions.dts')(dts);
|
|
1695
|
-
};
|
|
1696
|
-
const logDtsError = (error, dtsOptions) => {
|
|
1697
|
-
if (dtsOptions === false) {
|
|
1698
|
-
return;
|
|
1699
|
-
}
|
|
1700
|
-
if (typeof dtsOptions === 'object' && dtsOptions && dtsOptions.displayErrorInTerminal === false) {
|
|
1701
|
-
return;
|
|
1702
|
-
}
|
|
1703
|
-
console.error(error);
|
|
1704
|
-
};
|
|
1705
|
-
function pluginDts(options) {
|
|
1706
|
-
if (options.dts === false) {
|
|
1707
|
-
return [];
|
|
1708
|
-
}
|
|
1709
|
-
const dtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
|
|
1710
|
-
let resolvedConfig;
|
|
1711
|
-
let devWorker;
|
|
1712
|
-
let normalizedDevOptions;
|
|
1713
|
-
let hasGeneratedBundle = false;
|
|
1714
|
-
const devPlugin = {
|
|
1715
|
-
name: 'module-federation-dts-dev',
|
|
1716
|
-
apply: 'serve',
|
|
1717
|
-
config(config) {
|
|
1718
|
-
normalizedDevOptions = normalizeDevOptions(options.dev);
|
|
1719
|
-
if (!normalizedDevOptions) {
|
|
1785
|
+
load(id) {
|
|
1786
|
+
if (excludeFn(id)) {
|
|
1720
1787
|
return;
|
|
1721
1788
|
}
|
|
1722
|
-
|
|
1789
|
+
parseStartSet.add(id);
|
|
1790
|
+
}
|
|
1791
|
+
}, {
|
|
1792
|
+
enforce: 'post',
|
|
1793
|
+
name: 'parseEnd',
|
|
1794
|
+
apply: 'build',
|
|
1795
|
+
moduleParsed(module) {
|
|
1796
|
+
const id = module.id;
|
|
1797
|
+
if (id === VIRTUAL_EXPOSES) {
|
|
1798
|
+
// When the entry JS file is empty and only contains exposes export code, it’s necessary to wait for the exposes modules to be resolved in order to collect the dependencies being used.
|
|
1799
|
+
exposesParseEnd = true;
|
|
1800
|
+
}
|
|
1801
|
+
if (excludeFn(id)) {
|
|
1723
1802
|
return;
|
|
1724
1803
|
}
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
|
|
1804
|
+
parseEndSet.add(id);
|
|
1805
|
+
if (exposesParseEnd && parseStartSet.size === parseEndSet.size) {
|
|
1806
|
+
_resolve(1);
|
|
1729
1807
|
}
|
|
1730
|
-
|
|
1731
|
-
|
|
1808
|
+
}
|
|
1809
|
+
}];
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
const filter = createFilter();
|
|
1813
|
+
function pluginProxyRemoteEntry () {
|
|
1814
|
+
let viteConfig, _command;
|
|
1815
|
+
return {
|
|
1816
|
+
name: 'proxyRemoteEntry',
|
|
1817
|
+
enforce: 'post',
|
|
1732
1818
|
configResolved(config) {
|
|
1733
|
-
|
|
1819
|
+
viteConfig = config;
|
|
1734
1820
|
},
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1821
|
+
config(config, {
|
|
1822
|
+
command
|
|
1823
|
+
}) {
|
|
1824
|
+
_command = command;
|
|
1825
|
+
},
|
|
1826
|
+
resolveId(id) {
|
|
1827
|
+
if (id === REMOTE_ENTRY_ID) {
|
|
1828
|
+
return REMOTE_ENTRY_ID;
|
|
1738
1829
|
}
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
return;
|
|
1830
|
+
if (id === VIRTUAL_EXPOSES) {
|
|
1831
|
+
return VIRTUAL_EXPOSES;
|
|
1742
1832
|
}
|
|
1743
|
-
if (
|
|
1744
|
-
|
|
1833
|
+
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
|
|
1834
|
+
return id;
|
|
1745
1835
|
}
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
if (
|
|
1749
|
-
return;
|
|
1836
|
+
},
|
|
1837
|
+
load(id) {
|
|
1838
|
+
if (id === REMOTE_ENTRY_ID) {
|
|
1839
|
+
return parsePromise.then(_ => generateRemoteEntry(getNormalizeModuleFederationOptions()));
|
|
1750
1840
|
}
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
}, 'mfOptions.dts.generateTypes')(normalizedDtsOptions.generateTypes);
|
|
1754
|
-
const remote = normalizedGenerateTypes === false ? undefined : _extends({
|
|
1755
|
-
implementation: normalizedDtsOptions.implementation,
|
|
1756
|
-
context: resolvedConfig.root,
|
|
1757
|
-
outputDir,
|
|
1758
|
-
moduleFederationConfig: _extends({}, dtsModuleFederationConfig),
|
|
1759
|
-
hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || '@mf-types'
|
|
1760
|
-
}, normalizedGenerateTypes, {
|
|
1761
|
-
typesFolder: '.dev-server'
|
|
1762
|
-
});
|
|
1763
|
-
if (remote && !remote.tsConfigPath && typeof normalizedDtsOptions === 'object' && normalizedDtsOptions.tsConfigPath) {
|
|
1764
|
-
remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
|
|
1841
|
+
if (id === VIRTUAL_EXPOSES) {
|
|
1842
|
+
return generateExposes();
|
|
1765
1843
|
}
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
}, 'mfOptions.dts.consumeTypes')(normalizedDtsOptions.consumeTypes);
|
|
1769
|
-
const host = normalizedConsumeTypes === false ? undefined : _extends({
|
|
1770
|
-
implementation: normalizedDtsOptions.implementation,
|
|
1771
|
-
context: resolvedConfig.root,
|
|
1772
|
-
moduleFederationConfig: dtsModuleFederationConfig,
|
|
1773
|
-
typesFolder: normalizedConsumeTypes.typesFolder || '@mf-types',
|
|
1774
|
-
abortOnError: false
|
|
1775
|
-
}, normalizedConsumeTypes);
|
|
1776
|
-
const extraOptions = normalizedDtsOptions.extraOptions || {};
|
|
1777
|
-
if (!remote && !host && devOptions.disableLiveReload) {
|
|
1778
|
-
return;
|
|
1844
|
+
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
|
|
1845
|
+
return id;
|
|
1779
1846
|
}
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
if (
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
host,
|
|
1787
|
-
extraOptions,
|
|
1788
|
-
displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
|
|
1789
|
-
}, resolve);
|
|
1790
|
-
});
|
|
1847
|
+
},
|
|
1848
|
+
transform(code, id) {
|
|
1849
|
+
const transformedCode = (() => {
|
|
1850
|
+
if (!filter(id)) return;
|
|
1851
|
+
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
1852
|
+
return parsePromise.then(_ => generateRemoteEntry(getNormalizeModuleFederationOptions()));
|
|
1791
1853
|
}
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
};
|
|
1817
|
-
startDevWorker().catch(error => {
|
|
1818
|
-
logDtsError(error, normalizedDtsOptions);
|
|
1819
|
-
});
|
|
1854
|
+
if (id === VIRTUAL_EXPOSES) {
|
|
1855
|
+
return generateExposes();
|
|
1856
|
+
}
|
|
1857
|
+
if (id.includes(getHostAutoInitPath())) {
|
|
1858
|
+
const options = getNormalizeModuleFederationOptions();
|
|
1859
|
+
if (_command === 'serve') {
|
|
1860
|
+
var _viteConfig$server, _viteConfig$server2;
|
|
1861
|
+
const host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
|
|
1862
|
+
const publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
|
|
1863
|
+
return `
|
|
1864
|
+
const origin = (window && ${!options.ignoreOrigin}) ? window.origin : "//${host}:${(_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port}"
|
|
1865
|
+
const remoteEntryPromise = await import(origin + ${publicPath})
|
|
1866
|
+
// __tla only serves as a hack for vite-plugin-top-level-await.
|
|
1867
|
+
Promise.resolve(remoteEntryPromise)
|
|
1868
|
+
.then(remoteEntry => {
|
|
1869
|
+
return Promise.resolve(remoteEntry.__tla)
|
|
1870
|
+
.then(remoteEntry.init).catch(remoteEntry.init)
|
|
1871
|
+
})
|
|
1872
|
+
`;
|
|
1873
|
+
}
|
|
1874
|
+
return code;
|
|
1875
|
+
}
|
|
1876
|
+
})();
|
|
1877
|
+
return mapCodeToCodeWithSourcemap(transformedCode);
|
|
1820
1878
|
}
|
|
1821
1879
|
};
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
if (typeof normalizedDtsOptions !== 'object') {
|
|
1845
|
-
return;
|
|
1846
|
-
}
|
|
1847
|
-
const context = resolvedConfig.root;
|
|
1848
|
-
const outputDir = resolveOutputDir(resolvedConfig);
|
|
1849
|
-
let consumeOptions;
|
|
1850
|
-
try {
|
|
1851
|
-
consumeOptions = normalizeConsumeTypesOptions({
|
|
1852
|
-
context,
|
|
1853
|
-
dtsOptions: normalizedDtsOptions,
|
|
1854
|
-
pluginOptions: dtsModuleFederationConfig
|
|
1855
|
-
});
|
|
1856
|
-
} catch (error) {
|
|
1857
|
-
logDtsError(error, normalizedDtsOptions);
|
|
1858
|
-
return;
|
|
1859
|
-
}
|
|
1860
|
-
if ((_consumeOptions = consumeOptions) != null && (_consumeOptions = _consumeOptions.host) != null && _consumeOptions.typesOnBuild) {
|
|
1861
|
-
try {
|
|
1862
|
-
await consumeTypesAPI(consumeOptions);
|
|
1863
|
-
} catch (error) {
|
|
1864
|
-
logDtsError(error, normalizedDtsOptions);
|
|
1865
|
-
}
|
|
1866
|
-
}
|
|
1867
|
-
let generateOptions;
|
|
1868
|
-
try {
|
|
1869
|
-
generateOptions = normalizeGenerateTypesOptions({
|
|
1870
|
-
context,
|
|
1871
|
-
outputDir,
|
|
1872
|
-
dtsOptions: normalizedDtsOptions,
|
|
1873
|
-
pluginOptions: dtsModuleFederationConfig
|
|
1874
|
-
});
|
|
1875
|
-
} catch (error) {
|
|
1876
|
-
logDtsError(error, normalizedDtsOptions);
|
|
1877
|
-
return;
|
|
1878
|
-
}
|
|
1879
|
-
if (!generateOptions) {
|
|
1880
|
-
return;
|
|
1881
|
-
}
|
|
1882
|
-
try {
|
|
1883
|
-
await generateTypesAPI({
|
|
1884
|
-
dtsManagerOptions: generateOptions
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
createFilter();
|
|
1883
|
+
function pluginProxyRemotes (options) {
|
|
1884
|
+
const {
|
|
1885
|
+
remotes
|
|
1886
|
+
} = options;
|
|
1887
|
+
return {
|
|
1888
|
+
name: 'proxyRemotes',
|
|
1889
|
+
config(config, {
|
|
1890
|
+
command: _command
|
|
1891
|
+
}) {
|
|
1892
|
+
Object.keys(remotes).forEach(key => {
|
|
1893
|
+
const remote = remotes[key];
|
|
1894
|
+
config.resolve.alias.push({
|
|
1895
|
+
find: new RegExp(`^(${remote.name}(\/.*|$))`),
|
|
1896
|
+
replacement: '$1',
|
|
1897
|
+
customResolver(source) {
|
|
1898
|
+
const remoteModule = getRemoteVirtualModule(source, _command);
|
|
1899
|
+
addUsedRemote(remote.name, source);
|
|
1900
|
+
return remoteModule.getPath();
|
|
1901
|
+
}
|
|
1885
1902
|
});
|
|
1886
|
-
}
|
|
1887
|
-
logDtsError(error, normalizedDtsOptions);
|
|
1888
|
-
}
|
|
1903
|
+
});
|
|
1889
1904
|
}
|
|
1890
1905
|
};
|
|
1891
|
-
return [devPlugin, buildPlugin];
|
|
1892
1906
|
}
|
|
1893
1907
|
|
|
1894
1908
|
/**
|
|
@@ -2073,6 +2087,8 @@ function federation(mfUserOptions) {
|
|
|
2073
2087
|
entryPath: VIRTUAL_EXPOSES
|
|
2074
2088
|
}), pluginProxyRemoteEntry(), pluginProxyRemotes(options), ...pluginModuleParseEnd(id => {
|
|
2075
2089
|
return id.includes(getHostAutoInitImportId()) || id.includes(REMOTE_ENTRY_ID) || id.includes(VIRTUAL_EXPOSES) || id.includes(getLocalSharedImportMapPath());
|
|
2090
|
+
}, {
|
|
2091
|
+
moduleParseTimeout: options.moduleParseTimeout
|
|
2076
2092
|
}), ...proxySharedModule({
|
|
2077
2093
|
shared
|
|
2078
2094
|
}), PluginDevProxyModuleTopLevelAwait(), {
|