@module-federation/vite 1.0.0-alpha-6cae9ab → 1.0.0-alpha-51eeeb6

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/lib/index.cjs CHANGED
@@ -29,7 +29,8 @@ var path__namespace = /*#__PURE__*/_interopNamespace(path);
29
29
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
30
30
 
31
31
  var addEntry = function addEntry(_ref) {
32
- var entryPath = _ref.entryPath,
32
+ var entryName = _ref.entryName,
33
+ entryPath = _ref.entryPath,
33
34
  fileName = _ref.fileName;
34
35
  var entryFiles = [];
35
36
  var htmlFilePath;
@@ -78,7 +79,9 @@ var addEntry = function addEntry(_ref) {
78
79
  buildStart: function buildStart() {
79
80
  var _this$emitFile;
80
81
  var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
81
- this.emitFile((_this$emitFile = {}, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
82
+ this.emitFile((_this$emitFile = {
83
+ name: entryName
84
+ }, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
82
85
  if (htmlFilePath) {
83
86
  var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
84
87
  var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
@@ -264,6 +267,7 @@ function writeLocalSharedImportMap(pkgList) {
264
267
  return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibPath(pkg) + "\")\n return pkg\n }\n ";
265
268
  }).join(",") + "\n }\n ");
266
269
  }
270
+ var LOAD_SHARE_TAG = "__mf__loadShare_";
267
271
  /**
268
272
  * generate loadShare virtual module
269
273
  */
@@ -271,7 +275,7 @@ var cacheMap1 = {};
271
275
  function getLoadShareModulePath(pkg) {
272
276
  var _getNormalizeModuleFe2 = getNormalizeModuleFederationOptions(),
273
277
  name = _getNormalizeModuleFe2.name;
274
- if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + ("__mf__loadShare_" + npmPackageNameToFileName(pkg) + ".js");
278
+ if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + ("" + LOAD_SHARE_TAG + npmPackageNameToFileName(pkg) + ".js");
275
279
  var filename = cacheMap1[pkg];
276
280
  return path.resolve(emptyNpmDir, filename);
277
281
  }
@@ -413,6 +417,24 @@ function pluginProxyRemotes (options) {
413
417
  };
414
418
  }
415
419
 
420
+ function wrapManualChunks(output, manualChunksCb) {
421
+ if (!output.manualChunks) output.manualChunks = {};
422
+ var wrapManualChunks = function wrapManualChunks(original) {
423
+ return function (id) {
424
+ var args = [].slice.call(arguments, 1);
425
+ var customRes = manualChunksCb == null ? void 0 : manualChunksCb.apply(void 0, [id].concat(args));
426
+ if (customRes) return customRes;
427
+ if (typeof original === 'function') {
428
+ return original.apply(void 0, [id].concat(args));
429
+ }
430
+ if (typeof original === 'object' && original) {
431
+ return original[id];
432
+ }
433
+ };
434
+ };
435
+ output.manualChunks = wrapManualChunks(output.manualChunks);
436
+ }
437
+
416
438
  function proxySharedModule(options) {
417
439
  var _options$shared = options.shared,
418
440
  shared = _options$shared === void 0 ? {} : _options$shared,
@@ -426,29 +448,47 @@ function proxySharedModule(options) {
426
448
  config: function config(_config, _ref) {
427
449
  var _config$resolve$alias, _config$resolve$alias2;
428
450
  var command = _ref.command;
451
+ if (!_config.build) _config.build = {};
452
+ if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
453
+ var rollupOptions = _config.build.rollupOptions;
454
+ if (!rollupOptions.output) rollupOptions.output = {};
429
455
  // config?.optimizeDeps?.include?.push?.("an-empty-js-file");
430
456
  // config.optimizeDeps.needsInterop.push('an-empty-js-file');
457
+ wrapManualChunks(_config.build.rollupOptions.output, function (id) {
458
+ if (id.includes("node_modules/@module-federation/runtime")) {
459
+ return "@module-federation/runtime";
460
+ }
461
+ if (id.includes(LOAD_SHARE_TAG) || id.includes("__mf__prebuildwrap_")) {
462
+ return id.split("/").pop();
463
+ }
464
+ });
431
465
  (_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
432
- var _config$optimizeDeps;
466
+ var _config$optimizeDeps, _config$optimizeDeps2;
433
467
  _config == null || (_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push == null || _config$optimizeDeps.push(getPreBuildLibPath(key));
434
468
  // write proxyFile
435
469
  writeLoadShareModule(key, shared[key], command);
436
470
  var preBuildLibPath = getLoadShareModulePath(key);
471
+ _config == null || (_config$optimizeDeps2 = _config.optimizeDeps) == null || (_config$optimizeDeps2 = _config$optimizeDeps2.needsInterop) == null || _config$optimizeDeps2.push(key);
437
472
  return {
438
473
  // Intercept all dependency requests to the proxy module
439
474
  // Dependency requests issued by localSharedImportMap are allowed without proxying.
440
475
  find: new RegExp("^" + key + "$"),
441
476
  replacement: preBuildLibPath,
442
477
  customResolver: function customResolver(source, importer) {
478
+ var _config$optimizeDeps3;
443
479
  if (importer.includes("node_modules/" + key + "/")) {
444
480
  return this.resolve(key);
445
481
  }
482
+ _config == null || (_config$optimizeDeps3 = _config.optimizeDeps) == null || (_config$optimizeDeps3 = _config$optimizeDeps3.needsInterop) == null || _config$optimizeDeps3.push(preBuildLibPath);
446
483
  return this.resolve(preBuildLibPath);
447
484
  }
448
485
  };
449
486
  }));
450
487
  (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
451
- return {
488
+ return command === "build" ? {
489
+ find: new RegExp("^" + getPreBuildLibPath(key) + "$"),
490
+ replacement: key
491
+ } : {
452
492
  find: new RegExp("^" + getPreBuildLibPath(key) + "$"),
453
493
  customResolver: function customResolver(source, importer) {
454
494
  return this.resolve(key);
@@ -543,39 +583,6 @@ var aliasToArrayPlugin = {
543
583
  }
544
584
  };
545
585
 
546
- var normalizeBuildPlugin = (function (singleChunkModules) {
547
- return {
548
- name: 'normalizeBuild',
549
- config: function config(_config, _ref) {
550
- if (!_config.build) _config.build = {};
551
- if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
552
- var rollupOptions = _config.build.rollupOptions;
553
- if (!rollupOptions.output) rollupOptions.output = {};
554
- normalizeManualChunks(rollupOptions.output, singleChunkModules);
555
- }
556
- };
557
- });
558
- function normalizeManualChunks(output, singleChunkModules) {
559
- var pattern = new RegExp("node_modules/(" + singleChunkModules.join('|') + ")/");
560
- if (!output.manualChunks) output.manualChunks = {};
561
- var wrapManualChunks = function wrapManualChunks(original) {
562
- return function (id) {
563
- var _ref2 = id.match(pattern) || [],
564
- moduleName = _ref2[1];
565
- if (moduleName) {
566
- return moduleName;
567
- }
568
- if (typeof original === 'function') {
569
- return original.apply(void 0, [id].concat([].slice.call(arguments, 1)));
570
- }
571
- if (typeof original === 'object' && original) {
572
- return original[id];
573
- }
574
- };
575
- };
576
- output.manualChunks = wrapManualChunks(output.manualChunks);
577
- }
578
-
579
586
  var normalizeOptimizeDepsPlugin = {
580
587
  name: 'normalizeOptimizeDeps',
581
588
  config: function config(_config, _ref) {
@@ -595,7 +602,7 @@ function federation(mfUserOptions) {
595
602
  shared = options.shared,
596
603
  filename = options.filename;
597
604
  if (!name) throw new Error("name is required");
598
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin, normalizeBuildPlugin([].concat(Object.keys(shared), ["@module-federation/runtime"]))].concat(addEntry({
605
+ return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
599
606
  entryName: 'remoteEntry',
600
607
  entryPath: WRAP_REMOTE_ENTRY_PATH,
601
608
  fileName: filename
package/lib/index.esm.js CHANGED
@@ -7,7 +7,8 @@ import { walk } from 'estree-walker';
7
7
  import MagicString from 'magic-string';
8
8
 
9
9
  var addEntry = function addEntry(_ref) {
10
- var entryPath = _ref.entryPath,
10
+ var entryName = _ref.entryName,
11
+ entryPath = _ref.entryPath,
11
12
  fileName = _ref.fileName;
12
13
  var entryFiles = [];
13
14
  var htmlFilePath;
@@ -56,7 +57,9 @@ var addEntry = function addEntry(_ref) {
56
57
  buildStart: function buildStart() {
57
58
  var _this$emitFile;
58
59
  var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
59
- this.emitFile((_this$emitFile = {}, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
60
+ this.emitFile((_this$emitFile = {
61
+ name: entryName
62
+ }, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
60
63
  if (htmlFilePath) {
61
64
  var htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
62
65
  var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
@@ -242,6 +245,7 @@ function writeLocalSharedImportMap(pkgList) {
242
245
  return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibPath(pkg) + "\")\n return pkg\n }\n ";
243
246
  }).join(",") + "\n }\n ");
244
247
  }
248
+ var LOAD_SHARE_TAG = "__mf__loadShare_";
245
249
  /**
246
250
  * generate loadShare virtual module
247
251
  */
@@ -249,7 +253,7 @@ var cacheMap1 = {};
249
253
  function getLoadShareModulePath(pkg) {
250
254
  var _getNormalizeModuleFe2 = getNormalizeModuleFederationOptions(),
251
255
  name = _getNormalizeModuleFe2.name;
252
- if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + ("__mf__loadShare_" + npmPackageNameToFileName(pkg) + ".js");
256
+ if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + ("" + LOAD_SHARE_TAG + npmPackageNameToFileName(pkg) + ".js");
253
257
  var filename = cacheMap1[pkg];
254
258
  return resolve(emptyNpmDir, filename);
255
259
  }
@@ -391,6 +395,24 @@ function pluginProxyRemotes (options) {
391
395
  };
392
396
  }
393
397
 
398
+ function wrapManualChunks(output, manualChunksCb) {
399
+ if (!output.manualChunks) output.manualChunks = {};
400
+ var wrapManualChunks = function wrapManualChunks(original) {
401
+ return function (id) {
402
+ var args = [].slice.call(arguments, 1);
403
+ var customRes = manualChunksCb == null ? void 0 : manualChunksCb.apply(void 0, [id].concat(args));
404
+ if (customRes) return customRes;
405
+ if (typeof original === 'function') {
406
+ return original.apply(void 0, [id].concat(args));
407
+ }
408
+ if (typeof original === 'object' && original) {
409
+ return original[id];
410
+ }
411
+ };
412
+ };
413
+ output.manualChunks = wrapManualChunks(output.manualChunks);
414
+ }
415
+
394
416
  function proxySharedModule(options) {
395
417
  var _options$shared = options.shared,
396
418
  shared = _options$shared === void 0 ? {} : _options$shared,
@@ -404,29 +426,47 @@ function proxySharedModule(options) {
404
426
  config: function config(_config, _ref) {
405
427
  var _config$resolve$alias, _config$resolve$alias2;
406
428
  var command = _ref.command;
429
+ if (!_config.build) _config.build = {};
430
+ if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
431
+ var rollupOptions = _config.build.rollupOptions;
432
+ if (!rollupOptions.output) rollupOptions.output = {};
407
433
  // config?.optimizeDeps?.include?.push?.("an-empty-js-file");
408
434
  // config.optimizeDeps.needsInterop.push('an-empty-js-file');
435
+ wrapManualChunks(_config.build.rollupOptions.output, function (id) {
436
+ if (id.includes("node_modules/@module-federation/runtime")) {
437
+ return "@module-federation/runtime";
438
+ }
439
+ if (id.includes(LOAD_SHARE_TAG) || id.includes("__mf__prebuildwrap_")) {
440
+ return id.split("/").pop();
441
+ }
442
+ });
409
443
  (_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
410
- var _config$optimizeDeps;
444
+ var _config$optimizeDeps, _config$optimizeDeps2;
411
445
  _config == null || (_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push == null || _config$optimizeDeps.push(getPreBuildLibPath(key));
412
446
  // write proxyFile
413
447
  writeLoadShareModule(key, shared[key], command);
414
448
  var preBuildLibPath = getLoadShareModulePath(key);
449
+ _config == null || (_config$optimizeDeps2 = _config.optimizeDeps) == null || (_config$optimizeDeps2 = _config$optimizeDeps2.needsInterop) == null || _config$optimizeDeps2.push(key);
415
450
  return {
416
451
  // Intercept all dependency requests to the proxy module
417
452
  // Dependency requests issued by localSharedImportMap are allowed without proxying.
418
453
  find: new RegExp("^" + key + "$"),
419
454
  replacement: preBuildLibPath,
420
455
  customResolver: function customResolver(source, importer) {
456
+ var _config$optimizeDeps3;
421
457
  if (importer.includes("node_modules/" + key + "/")) {
422
458
  return this.resolve(key);
423
459
  }
460
+ _config == null || (_config$optimizeDeps3 = _config.optimizeDeps) == null || (_config$optimizeDeps3 = _config$optimizeDeps3.needsInterop) == null || _config$optimizeDeps3.push(preBuildLibPath);
424
461
  return this.resolve(preBuildLibPath);
425
462
  }
426
463
  };
427
464
  }));
428
465
  (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
429
- return {
466
+ return command === "build" ? {
467
+ find: new RegExp("^" + getPreBuildLibPath(key) + "$"),
468
+ replacement: key
469
+ } : {
430
470
  find: new RegExp("^" + getPreBuildLibPath(key) + "$"),
431
471
  customResolver: function customResolver(source, importer) {
432
472
  return this.resolve(key);
@@ -521,39 +561,6 @@ var aliasToArrayPlugin = {
521
561
  }
522
562
  };
523
563
 
524
- var normalizeBuildPlugin = (function (singleChunkModules) {
525
- return {
526
- name: 'normalizeBuild',
527
- config: function config(_config, _ref) {
528
- if (!_config.build) _config.build = {};
529
- if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
530
- var rollupOptions = _config.build.rollupOptions;
531
- if (!rollupOptions.output) rollupOptions.output = {};
532
- normalizeManualChunks(rollupOptions.output, singleChunkModules);
533
- }
534
- };
535
- });
536
- function normalizeManualChunks(output, singleChunkModules) {
537
- var pattern = new RegExp("node_modules/(" + singleChunkModules.join('|') + ")/");
538
- if (!output.manualChunks) output.manualChunks = {};
539
- var wrapManualChunks = function wrapManualChunks(original) {
540
- return function (id) {
541
- var _ref2 = id.match(pattern) || [],
542
- moduleName = _ref2[1];
543
- if (moduleName) {
544
- return moduleName;
545
- }
546
- if (typeof original === 'function') {
547
- return original.apply(void 0, [id].concat([].slice.call(arguments, 1)));
548
- }
549
- if (typeof original === 'object' && original) {
550
- return original[id];
551
- }
552
- };
553
- };
554
- output.manualChunks = wrapManualChunks(output.manualChunks);
555
- }
556
-
557
564
  var normalizeOptimizeDepsPlugin = {
558
565
  name: 'normalizeOptimizeDeps',
559
566
  config: function config(_config, _ref) {
@@ -573,7 +580,7 @@ function federation(mfUserOptions) {
573
580
  shared = options.shared,
574
581
  filename = options.filename;
575
582
  if (!name) throw new Error("name is required");
576
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin, normalizeBuildPlugin([].concat(Object.keys(shared), ["@module-federation/runtime"]))].concat(addEntry({
583
+ return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
577
584
  entryName: 'remoteEntry',
578
585
  entryPath: WRAP_REMOTE_ENTRY_PATH,
579
586
  fileName: filename
@@ -60,6 +60,7 @@ const addEntry = ({
60
60
  buildStart() {
61
61
  const hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
62
62
  this.emitFile({
63
+ name: entryName,
63
64
  [hasHash ? "name" : "fileName"]: fileName,
64
65
  type: 'chunk',
65
66
  id: entryPath,
@@ -257,6 +258,7 @@ function writeLocalSharedImportMap(pkgList) {
257
258
  }
258
259
  `);
259
260
  }
261
+ const LOAD_SHARE_TAG = "__mf__loadShare_";
260
262
  /**
261
263
  * generate loadShare virtual module
262
264
  */
@@ -265,7 +267,7 @@ function getLoadShareModulePath(pkg) {
265
267
  const {
266
268
  name
267
269
  } = getNormalizeModuleFederationOptions();
268
- if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + `__mf__loadShare_${npmPackageNameToFileName(pkg)}.js`;
270
+ if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + `${LOAD_SHARE_TAG}${npmPackageNameToFileName(pkg)}.js`;
269
271
  const filename = cacheMap1[pkg];
270
272
  return resolve(emptyNpmDir, filename);
271
273
  }
@@ -494,6 +496,21 @@ function pluginProxyRemotes (options) {
494
496
  };
495
497
  }
496
498
 
499
+ function wrapManualChunks(output, manualChunksCb) {
500
+ if (!output.manualChunks) output.manualChunks = {};
501
+ const wrapManualChunks = original => (id, ...args) => {
502
+ const customRes = manualChunksCb == null ? void 0 : manualChunksCb(id, ...args);
503
+ if (customRes) return customRes;
504
+ if (typeof original === 'function') {
505
+ return original(id, ...args);
506
+ }
507
+ if (typeof original === 'object' && original) {
508
+ return original[id];
509
+ }
510
+ };
511
+ output.manualChunks = wrapManualChunks(output.manualChunks);
512
+ }
513
+
497
514
  function proxySharedModule(options) {
498
515
  let {
499
516
  shared = {},
@@ -508,29 +525,49 @@ function proxySharedModule(options) {
508
525
  config(config, {
509
526
  command
510
527
  }) {
528
+ if (!config.build) config.build = {};
529
+ if (!config.build.rollupOptions) config.build.rollupOptions = {};
530
+ let {
531
+ rollupOptions
532
+ } = config.build;
533
+ if (!rollupOptions.output) rollupOptions.output = {};
511
534
  // config?.optimizeDeps?.include?.push?.("an-empty-js-file");
512
535
  // config.optimizeDeps.needsInterop.push('an-empty-js-file');
536
+ wrapManualChunks(config.build.rollupOptions.output, id => {
537
+ if (id.includes("node_modules/@module-federation/runtime")) {
538
+ return "@module-federation/runtime";
539
+ }
540
+ if (id.includes(LOAD_SHARE_TAG) || id.includes("__mf__prebuildwrap_")) {
541
+ return id.split("/").pop();
542
+ }
543
+ });
513
544
  config.resolve.alias.push(...Object.keys(shared).map(key => {
514
- var _config$optimizeDeps;
545
+ var _config$optimizeDeps, _config$optimizeDeps2;
515
546
  config == null || (_config$optimizeDeps = config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push == null || _config$optimizeDeps.push(getPreBuildLibPath(key));
516
547
  // write proxyFile
517
548
  writeLoadShareModule(key, shared[key], command);
518
549
  const preBuildLibPath = getLoadShareModulePath(key);
550
+ config == null || (_config$optimizeDeps2 = config.optimizeDeps) == null || (_config$optimizeDeps2 = _config$optimizeDeps2.needsInterop) == null || _config$optimizeDeps2.push(key);
519
551
  return {
520
552
  // Intercept all dependency requests to the proxy module
521
553
  // Dependency requests issued by localSharedImportMap are allowed without proxying.
522
554
  find: new RegExp(`^${key}$`),
523
555
  replacement: preBuildLibPath,
524
556
  customResolver(source, importer) {
557
+ var _config$optimizeDeps3;
525
558
  if (importer.includes(`node_modules/${key}/`)) {
526
559
  return this.resolve(key);
527
560
  }
561
+ config == null || (_config$optimizeDeps3 = config.optimizeDeps) == null || (_config$optimizeDeps3 = _config$optimizeDeps3.needsInterop) == null || _config$optimizeDeps3.push(preBuildLibPath);
528
562
  return this.resolve(preBuildLibPath);
529
563
  }
530
564
  };
531
565
  }));
532
566
  config.resolve.alias.push(...Object.keys(shared).map(key => {
533
- return {
567
+ return command === "build" ? {
568
+ find: new RegExp(`^${getPreBuildLibPath(key)}$`),
569
+ replacement: key
570
+ } : {
534
571
  find: new RegExp(`^${getPreBuildLibPath(key)}$`),
535
572
  customResolver(source, importer) {
536
573
  return this.resolve(key);
@@ -631,38 +668,6 @@ var aliasToArrayPlugin = {
631
668
  }
632
669
  };
633
670
 
634
- var normalizeBuildPlugin = (singleChunkModules => ({
635
- name: 'normalizeBuild',
636
- config: (config, {
637
- command
638
- }) => {
639
- if (!config.build) config.build = {};
640
- if (!config.build.rollupOptions) config.build.rollupOptions = {};
641
- let {
642
- rollupOptions
643
- } = config.build;
644
- if (!rollupOptions.output) rollupOptions.output = {};
645
- normalizeManualChunks(rollupOptions.output, singleChunkModules);
646
- }
647
- }));
648
- function normalizeManualChunks(output, singleChunkModules) {
649
- const pattern = new RegExp(`node_modules/(${singleChunkModules.join('|')})/`);
650
- if (!output.manualChunks) output.manualChunks = {};
651
- const wrapManualChunks = original => (id, ...args) => {
652
- const [_, moduleName] = id.match(pattern) || [];
653
- if (moduleName) {
654
- return moduleName;
655
- }
656
- if (typeof original === 'function') {
657
- return original(id, ...args);
658
- }
659
- if (typeof original === 'object' && original) {
660
- return original[id];
661
- }
662
- };
663
- output.manualChunks = wrapManualChunks(output.manualChunks);
664
- }
665
-
666
671
  var normalizeOptimizeDepsPlugin = {
667
672
  name: 'normalizeOptimizeDeps',
668
673
  config: (config, {
@@ -689,7 +694,7 @@ function federation(mfUserOptions) {
689
694
  filename
690
695
  } = options;
691
696
  if (!name) throw new Error("name is required");
692
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin, normalizeBuildPlugin([...Object.keys(shared), "@module-federation/runtime"]), ...addEntry({
697
+ return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin, ...addEntry({
693
698
  entryName: 'remoteEntry',
694
699
  entryPath: WRAP_REMOTE_ENTRY_PATH,
695
700
  fileName: filename
package/lib/index.umd.js CHANGED
@@ -28,7 +28,8 @@
28
28
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
29
29
 
30
30
  var addEntry = function addEntry(_ref) {
31
- var entryPath = _ref.entryPath,
31
+ var entryName = _ref.entryName,
32
+ entryPath = _ref.entryPath,
32
33
  fileName = _ref.fileName;
33
34
  var entryFiles = [];
34
35
  var htmlFilePath;
@@ -77,7 +78,9 @@
77
78
  buildStart: function buildStart() {
78
79
  var _this$emitFile;
79
80
  var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
80
- this.emitFile((_this$emitFile = {}, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
81
+ this.emitFile((_this$emitFile = {
82
+ name: entryName
83
+ }, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
81
84
  if (htmlFilePath) {
82
85
  var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
83
86
  var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
@@ -263,6 +266,7 @@
263
266
  return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibPath(pkg) + "\")\n return pkg\n }\n ";
264
267
  }).join(",") + "\n }\n ");
265
268
  }
269
+ var LOAD_SHARE_TAG = "__mf__loadShare_";
266
270
  /**
267
271
  * generate loadShare virtual module
268
272
  */
@@ -270,7 +274,7 @@
270
274
  function getLoadShareModulePath(pkg) {
271
275
  var _getNormalizeModuleFe2 = getNormalizeModuleFederationOptions(),
272
276
  name = _getNormalizeModuleFe2.name;
273
- if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + ("__mf__loadShare_" + npmPackageNameToFileName(pkg) + ".js");
277
+ if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + ("" + LOAD_SHARE_TAG + npmPackageNameToFileName(pkg) + ".js");
274
278
  var filename = cacheMap1[pkg];
275
279
  return path.resolve(emptyNpmDir, filename);
276
280
  }
@@ -412,6 +416,24 @@
412
416
  };
413
417
  }
414
418
 
419
+ function wrapManualChunks(output, manualChunksCb) {
420
+ if (!output.manualChunks) output.manualChunks = {};
421
+ var wrapManualChunks = function wrapManualChunks(original) {
422
+ return function (id) {
423
+ var args = [].slice.call(arguments, 1);
424
+ var customRes = manualChunksCb == null ? void 0 : manualChunksCb.apply(void 0, [id].concat(args));
425
+ if (customRes) return customRes;
426
+ if (typeof original === 'function') {
427
+ return original.apply(void 0, [id].concat(args));
428
+ }
429
+ if (typeof original === 'object' && original) {
430
+ return original[id];
431
+ }
432
+ };
433
+ };
434
+ output.manualChunks = wrapManualChunks(output.manualChunks);
435
+ }
436
+
415
437
  function proxySharedModule(options) {
416
438
  var _options$shared = options.shared,
417
439
  shared = _options$shared === void 0 ? {} : _options$shared,
@@ -425,29 +447,47 @@
425
447
  config: function config(_config, _ref) {
426
448
  var _config$resolve$alias, _config$resolve$alias2;
427
449
  var command = _ref.command;
450
+ if (!_config.build) _config.build = {};
451
+ if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
452
+ var rollupOptions = _config.build.rollupOptions;
453
+ if (!rollupOptions.output) rollupOptions.output = {};
428
454
  // config?.optimizeDeps?.include?.push?.("an-empty-js-file");
429
455
  // config.optimizeDeps.needsInterop.push('an-empty-js-file');
456
+ wrapManualChunks(_config.build.rollupOptions.output, function (id) {
457
+ if (id.includes("node_modules/@module-federation/runtime")) {
458
+ return "@module-federation/runtime";
459
+ }
460
+ if (id.includes(LOAD_SHARE_TAG) || id.includes("__mf__prebuildwrap_")) {
461
+ return id.split("/").pop();
462
+ }
463
+ });
430
464
  (_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
431
- var _config$optimizeDeps;
465
+ var _config$optimizeDeps, _config$optimizeDeps2;
432
466
  _config == null || (_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push == null || _config$optimizeDeps.push(getPreBuildLibPath(key));
433
467
  // write proxyFile
434
468
  writeLoadShareModule(key, shared[key], command);
435
469
  var preBuildLibPath = getLoadShareModulePath(key);
470
+ _config == null || (_config$optimizeDeps2 = _config.optimizeDeps) == null || (_config$optimizeDeps2 = _config$optimizeDeps2.needsInterop) == null || _config$optimizeDeps2.push(key);
436
471
  return {
437
472
  // Intercept all dependency requests to the proxy module
438
473
  // Dependency requests issued by localSharedImportMap are allowed without proxying.
439
474
  find: new RegExp("^" + key + "$"),
440
475
  replacement: preBuildLibPath,
441
476
  customResolver: function customResolver(source, importer) {
477
+ var _config$optimizeDeps3;
442
478
  if (importer.includes("node_modules/" + key + "/")) {
443
479
  return this.resolve(key);
444
480
  }
481
+ _config == null || (_config$optimizeDeps3 = _config.optimizeDeps) == null || (_config$optimizeDeps3 = _config$optimizeDeps3.needsInterop) == null || _config$optimizeDeps3.push(preBuildLibPath);
445
482
  return this.resolve(preBuildLibPath);
446
483
  }
447
484
  };
448
485
  }));
449
486
  (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
450
- return {
487
+ return command === "build" ? {
488
+ find: new RegExp("^" + getPreBuildLibPath(key) + "$"),
489
+ replacement: key
490
+ } : {
451
491
  find: new RegExp("^" + getPreBuildLibPath(key) + "$"),
452
492
  customResolver: function customResolver(source, importer) {
453
493
  return this.resolve(key);
@@ -542,39 +582,6 @@
542
582
  }
543
583
  };
544
584
 
545
- var normalizeBuildPlugin = (function (singleChunkModules) {
546
- return {
547
- name: 'normalizeBuild',
548
- config: function config(_config, _ref) {
549
- if (!_config.build) _config.build = {};
550
- if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
551
- var rollupOptions = _config.build.rollupOptions;
552
- if (!rollupOptions.output) rollupOptions.output = {};
553
- normalizeManualChunks(rollupOptions.output, singleChunkModules);
554
- }
555
- };
556
- });
557
- function normalizeManualChunks(output, singleChunkModules) {
558
- var pattern = new RegExp("node_modules/(" + singleChunkModules.join('|') + ")/");
559
- if (!output.manualChunks) output.manualChunks = {};
560
- var wrapManualChunks = function wrapManualChunks(original) {
561
- return function (id) {
562
- var _ref2 = id.match(pattern) || [],
563
- moduleName = _ref2[1];
564
- if (moduleName) {
565
- return moduleName;
566
- }
567
- if (typeof original === 'function') {
568
- return original.apply(void 0, [id].concat([].slice.call(arguments, 1)));
569
- }
570
- if (typeof original === 'object' && original) {
571
- return original[id];
572
- }
573
- };
574
- };
575
- output.manualChunks = wrapManualChunks(output.manualChunks);
576
- }
577
-
578
585
  var normalizeOptimizeDepsPlugin = {
579
586
  name: 'normalizeOptimizeDeps',
580
587
  config: function config(_config, _ref) {
@@ -594,7 +601,7 @@
594
601
  shared = options.shared,
595
602
  filename = options.filename;
596
603
  if (!name) throw new Error("name is required");
597
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin, normalizeBuildPlugin([].concat(Object.keys(shared), ["@module-federation/runtime"]))].concat(addEntry({
604
+ return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
598
605
  entryName: 'remoteEntry',
599
606
  entryPath: WRAP_REMOTE_ENTRY_PATH,
600
607
  fileName: filename
@@ -0,0 +1 @@
1
+ export declare function wrapManualChunks(output: any, manualChunksCb: Function): void;
@@ -8,5 +8,6 @@ export declare function getPreBuildLibPath(pkg: string): string;
8
8
  export declare function getLocalSharedImportMapId(): string;
9
9
  export declare function getLocalSharedImportMapPath(): string;
10
10
  export declare function writeLocalSharedImportMap(pkgList: string[]): void;
11
+ export declare const LOAD_SHARE_TAG = "__mf__loadShare_";
11
12
  export declare function getLoadShareModulePath(pkg: string): string;
12
13
  export declare function writeLoadShareModule(pkg: string, shareItem: ShareItem, command: string): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.0.0-alpha-6cae9ab",
3
+ "version": "1.0.0-alpha-51eeeb6",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "source": "src/index.ts",
@@ -38,7 +38,7 @@
38
38
  "homepage": "https://github.com/module-federation/vite#readme",
39
39
  "packageManager": "pnpm@9.1.3",
40
40
  "dependencies": {
41
- "@module-federation/runtime": "^0.3.5",
41
+ "@module-federation/runtime": "^0.5.0",
42
42
  "@rollup/pluginutils": "^5.1.0",
43
43
  "an-empty-js-file": "1.0.0",
44
44
  "estree-walker": "^2",
@@ -51,6 +51,6 @@
51
51
  "mime-types": "^2.1.35",
52
52
  "pretty-quick": "^4.0.0",
53
53
  "rimraf": "^6.0.1",
54
- "vite": "^5.4.0"
54
+ "vite": "^5.4.1"
55
55
  }
56
56
  }
@@ -1,8 +0,0 @@
1
- import { UserConfig } from 'vite';
2
- declare const _default: (singleChunkModules: string[]) => {
3
- name: string;
4
- config: (config: UserConfig, { command }: {
5
- command: string;
6
- }) => void;
7
- };
8
- export default _default;