@module-federation/vite 1.0.0-alpha-f87d7d3 → 1.0.0-alpha-1aa30c0

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
@@ -27,6 +27,7 @@ function _interopNamespace(e) {
27
27
 
28
28
  var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
29
29
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
30
+ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
30
31
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
31
32
 
32
33
  var addEntry = function addEntry(_ref) {
@@ -309,11 +310,24 @@ function removePathFromNpmPackage(packageString) {
309
310
  return match ? match[0] : packageString;
310
311
  }
311
312
 
312
- var nodeModulesDir = path.resolve("./node_modules");
313
+ var nodeModulesDir = function findNodeModulesDir(startDir) {
314
+ if (startDir === void 0) {
315
+ startDir = process.cwd();
316
+ }
317
+ var currentDir = startDir;
318
+ while (currentDir !== path.parse(currentDir).root) {
319
+ var nodeModulesPath = path.join(currentDir, 'node_modules');
320
+ if (fs.existsSync(nodeModulesPath)) {
321
+ return nodeModulesPath;
322
+ }
323
+ currentDir = path.dirname(currentDir);
324
+ }
325
+ return "";
326
+ }();
313
327
  var virtualPackageName = "__mf__virtual";
314
- try {
328
+ if (!fs.existsSync(path.resolve(nodeModulesDir, virtualPackageName))) {
315
329
  fs.mkdirSync(path.resolve(nodeModulesDir, virtualPackageName));
316
- } catch (e) {}
330
+ }
317
331
  fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, "empty.js"), "");
318
332
  fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.stringify({
319
333
  name: virtualPackageName,
@@ -325,6 +339,7 @@ fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, "package.json"
325
339
  var VirtualModule = /*#__PURE__*/function () {
326
340
  function VirtualModule(name) {
327
341
  this.originName = void 0;
342
+ this.inited = false;
328
343
  this.originName = name;
329
344
  }
330
345
  var _proto = VirtualModule.prototype;
@@ -334,7 +349,11 @@ var VirtualModule = /*#__PURE__*/function () {
334
349
  _proto.getImportId = function getImportId() {
335
350
  return virtualPackageName + "/" + packageNameEncode(this.originName);
336
351
  };
337
- _proto.writeSync = function writeSync(code) {
352
+ _proto.writeSync = function writeSync(code, force) {
353
+ if (!force && this.inited) return;
354
+ if (!this.inited) {
355
+ this.inited = true;
356
+ }
338
357
  fs.writeFileSync(this.getPath() + ".js", code);
339
358
  };
340
359
  _proto.write = function write(code) {
@@ -343,6 +362,26 @@ var VirtualModule = /*#__PURE__*/function () {
343
362
  return VirtualModule;
344
363
  }();
345
364
 
365
+ /**
366
+ * https://github.com/module-federation/vite/issues/68
367
+ */
368
+ function getLocalSharedImportMapPath_windows() {
369
+ var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
370
+ name = _getNormalizeModuleFe.name;
371
+ return path__default["default"].resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
372
+ }
373
+ function writeLocalSharedImportMap_windows(content) {
374
+ var localSharedImportMapId = getLocalSharedImportMapPath_windows();
375
+ createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
376
+ }
377
+ function createFile(filePath, content) {
378
+ var dir = path__default["default"].dirname(filePath);
379
+ fs.mkdirSync(dir, {
380
+ recursive: true
381
+ });
382
+ fs.writeFileSync(filePath, content);
383
+ }
384
+
346
385
  /**
347
386
  * Even the resolveId hook cannot interfere with vite pre-build,
348
387
  * and adding query parameter virtual modules will also fail.
@@ -364,14 +403,28 @@ var generateLocalSharedImportMap = function generateLocalSharedImportMap() {
364
403
  // *** __loadShare__
365
404
  var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
366
405
  try {
406
+ var _exit;
367
407
  var sharedCount = shareds.size;
368
408
  return Promise.resolve(function () {
369
409
  if (prevSharedCount !== sharedCount) {
410
+ var _temp2 = function _temp2(_result) {
411
+ if (_exit) return _result;
412
+ var _writeSync = localSharedImportMapModule.writeSync;
413
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI2) {
414
+ return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI2, true);
415
+ });
416
+ };
370
417
  prevSharedCount = sharedCount;
371
- var _writeSync = localSharedImportMapModule.writeSync;
372
- return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
373
- return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI);
374
- });
418
+ var _temp = function () {
419
+ if (process.platform === "win32") {
420
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
421
+ var _writeLocalSharedImpo = writeLocalSharedImportMap_windows(_generateLocalSharedI);
422
+ _exit = 1;
423
+ return _writeLocalSharedImpo;
424
+ });
425
+ }
426
+ }();
427
+ return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
375
428
  }
376
429
  }());
377
430
  } catch (e) {
@@ -396,7 +449,10 @@ function addShare(pkg) {
396
449
  // *** Expose locally provided shared modules here
397
450
  var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
398
451
  localSharedImportMapModule.writeSync("");
399
- function getLocalSharedImportMapId() {
452
+ function getLocalSharedImportMapPath() {
453
+ if (process.platform === "win32") {
454
+ return getLocalSharedImportMapPath_windows();
455
+ }
400
456
  return localSharedImportMapModule.getPath();
401
457
  }
402
458
  var prevSharedCount = 0;
@@ -420,7 +476,7 @@ function generateRemoteEntry(options) {
420
476
  return item[1];
421
477
  }).join('\n') + "\n\n const exposesMap = {\n " + Object.keys(options.exposes).map(function (key) {
422
478
  return "\n " + JSON.stringify(key) + ": async () => {\n const importModule = await import(" + JSON.stringify(options.exposes[key]["import"]) + ")\n const exportModule = {}\n Object.assign(exportModule, importModule)\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return exportModule\n }\n ";
423
- }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapId() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
479
+ }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapPath() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
424
480
  var remote = options.remotes[key];
425
481
  return "\n {\n entryGlobalName: " + JSON.stringify(remote.entryGlobalName) + ",\n name: " + JSON.stringify(remote.name) + ",\n type: " + JSON.stringify(remote.type) + ",\n entry: " + JSON.stringify(remote.entry) + ",\n }\n ";
426
482
  }).join(',') + "\n ],\n shared: localSharedImportMap,\n plugins: [" + pluginImportNames.map(function (item) {
@@ -593,15 +649,15 @@ function proxySharedModule(options) {
593
649
  name: "generateLocalSharedImportMap",
594
650
  enforce: "post",
595
651
  resolveId: function resolveId(id) {
596
- if (id.includes(getLocalSharedImportMapId())) return id;
652
+ if (id.includes(getLocalSharedImportMapPath())) return id;
597
653
  },
598
654
  load: function load(id) {
599
- if (id.includes(getLocalSharedImportMapId())) {
655
+ if (id.includes(getLocalSharedImportMapPath())) {
600
656
  return generateLocalSharedImportMap();
601
657
  }
602
658
  },
603
659
  transform: function transform(code, id) {
604
- if (id.includes(getLocalSharedImportMapId())) {
660
+ if (id.includes(getLocalSharedImportMapPath())) {
605
661
  return generateLocalSharedImportMap();
606
662
  }
607
663
  }
@@ -677,6 +733,9 @@ function proxySharedModule(options) {
677
733
  name: "watchLocalSharedImportMap",
678
734
  apply: "serve",
679
735
  config: function config(_config2) {
736
+ _config2.optimizeDeps = defu.defu(_config2.optimizeDeps, {
737
+ exclude: [getLocalSharedImportMapPath()]
738
+ });
680
739
  _config2.server = defu.defu(_config2.server, {
681
740
  watch: {
682
741
  ignored: []
@@ -684,7 +743,7 @@ function proxySharedModule(options) {
684
743
  });
685
744
  var watch = _config2.server.watch;
686
745
  watch.ignored = [].concat(watch.ignored);
687
- watch.ignored.push("!**/node_modules/" + localSharedImportMapModule.getImportId() + ".js");
746
+ watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
688
747
  }
689
748
  }, {
690
749
  name: "prebuild-top-level-await",
@@ -798,7 +857,7 @@ function federation(mfUserOptions) {
798
857
  entryName: 'hostInit',
799
858
  entryPath: HOST_AUTO_INIT_PATH
800
859
  }), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], pluginModuleParseEnd(function (id) {
801
- return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapId());
860
+ return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath());
802
861
  }), proxySharedModule({
803
862
  shared: shared
804
863
  }), [{
package/lib/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as fs from 'fs';
2
- import { mkdirSync, writeFileSync, writeFile } from 'fs';
2
+ import { existsSync, mkdirSync, writeFileSync, writeFile } from 'fs';
3
3
  import * as path from 'pathe';
4
- import { resolve } from 'pathe';
4
+ import path__default, { parse, join, dirname, resolve } from 'pathe';
5
5
  import { createFilter } from '@rollup/pluginutils';
6
6
  import { defu } from 'defu';
7
7
  import { walk } from 'estree-walker';
@@ -287,11 +287,24 @@ function removePathFromNpmPackage(packageString) {
287
287
  return match ? match[0] : packageString;
288
288
  }
289
289
 
290
- var nodeModulesDir = resolve("./node_modules");
290
+ var nodeModulesDir = function findNodeModulesDir(startDir) {
291
+ if (startDir === void 0) {
292
+ startDir = process.cwd();
293
+ }
294
+ var currentDir = startDir;
295
+ while (currentDir !== parse(currentDir).root) {
296
+ var nodeModulesPath = join(currentDir, 'node_modules');
297
+ if (existsSync(nodeModulesPath)) {
298
+ return nodeModulesPath;
299
+ }
300
+ currentDir = dirname(currentDir);
301
+ }
302
+ return "";
303
+ }();
291
304
  var virtualPackageName = "__mf__virtual";
292
- try {
305
+ if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) {
293
306
  mkdirSync(resolve(nodeModulesDir, virtualPackageName));
294
- } catch (e) {}
307
+ }
295
308
  writeFileSync(resolve(nodeModulesDir, virtualPackageName, "empty.js"), "");
296
309
  writeFileSync(resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.stringify({
297
310
  name: virtualPackageName,
@@ -303,6 +316,7 @@ writeFileSync(resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.
303
316
  var VirtualModule = /*#__PURE__*/function () {
304
317
  function VirtualModule(name) {
305
318
  this.originName = void 0;
319
+ this.inited = false;
306
320
  this.originName = name;
307
321
  }
308
322
  var _proto = VirtualModule.prototype;
@@ -312,7 +326,11 @@ var VirtualModule = /*#__PURE__*/function () {
312
326
  _proto.getImportId = function getImportId() {
313
327
  return virtualPackageName + "/" + packageNameEncode(this.originName);
314
328
  };
315
- _proto.writeSync = function writeSync(code) {
329
+ _proto.writeSync = function writeSync(code, force) {
330
+ if (!force && this.inited) return;
331
+ if (!this.inited) {
332
+ this.inited = true;
333
+ }
316
334
  writeFileSync(this.getPath() + ".js", code);
317
335
  };
318
336
  _proto.write = function write(code) {
@@ -321,6 +339,26 @@ var VirtualModule = /*#__PURE__*/function () {
321
339
  return VirtualModule;
322
340
  }();
323
341
 
342
+ /**
343
+ * https://github.com/module-federation/vite/issues/68
344
+ */
345
+ function getLocalSharedImportMapPath_windows() {
346
+ var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
347
+ name = _getNormalizeModuleFe.name;
348
+ return path__default.resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
349
+ }
350
+ function writeLocalSharedImportMap_windows(content) {
351
+ var localSharedImportMapId = getLocalSharedImportMapPath_windows();
352
+ createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
353
+ }
354
+ function createFile(filePath, content) {
355
+ var dir = path__default.dirname(filePath);
356
+ mkdirSync(dir, {
357
+ recursive: true
358
+ });
359
+ writeFileSync(filePath, content);
360
+ }
361
+
324
362
  /**
325
363
  * Even the resolveId hook cannot interfere with vite pre-build,
326
364
  * and adding query parameter virtual modules will also fail.
@@ -342,14 +380,28 @@ var generateLocalSharedImportMap = function generateLocalSharedImportMap() {
342
380
  // *** __loadShare__
343
381
  var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
344
382
  try {
383
+ var _exit;
345
384
  var sharedCount = shareds.size;
346
385
  return Promise.resolve(function () {
347
386
  if (prevSharedCount !== sharedCount) {
387
+ var _temp2 = function _temp2(_result) {
388
+ if (_exit) return _result;
389
+ var _writeSync = localSharedImportMapModule.writeSync;
390
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI2) {
391
+ return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI2, true);
392
+ });
393
+ };
348
394
  prevSharedCount = sharedCount;
349
- var _writeSync = localSharedImportMapModule.writeSync;
350
- return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
351
- return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI);
352
- });
395
+ var _temp = function () {
396
+ if (process.platform === "win32") {
397
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
398
+ var _writeLocalSharedImpo = writeLocalSharedImportMap_windows(_generateLocalSharedI);
399
+ _exit = 1;
400
+ return _writeLocalSharedImpo;
401
+ });
402
+ }
403
+ }();
404
+ return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
353
405
  }
354
406
  }());
355
407
  } catch (e) {
@@ -374,7 +426,10 @@ function addShare(pkg) {
374
426
  // *** Expose locally provided shared modules here
375
427
  var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
376
428
  localSharedImportMapModule.writeSync("");
377
- function getLocalSharedImportMapId() {
429
+ function getLocalSharedImportMapPath() {
430
+ if (process.platform === "win32") {
431
+ return getLocalSharedImportMapPath_windows();
432
+ }
378
433
  return localSharedImportMapModule.getPath();
379
434
  }
380
435
  var prevSharedCount = 0;
@@ -398,7 +453,7 @@ function generateRemoteEntry(options) {
398
453
  return item[1];
399
454
  }).join('\n') + "\n\n const exposesMap = {\n " + Object.keys(options.exposes).map(function (key) {
400
455
  return "\n " + JSON.stringify(key) + ": async () => {\n const importModule = await import(" + JSON.stringify(options.exposes[key]["import"]) + ")\n const exportModule = {}\n Object.assign(exportModule, importModule)\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return exportModule\n }\n ";
401
- }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapId() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
456
+ }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapPath() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
402
457
  var remote = options.remotes[key];
403
458
  return "\n {\n entryGlobalName: " + JSON.stringify(remote.entryGlobalName) + ",\n name: " + JSON.stringify(remote.name) + ",\n type: " + JSON.stringify(remote.type) + ",\n entry: " + JSON.stringify(remote.entry) + ",\n }\n ";
404
459
  }).join(',') + "\n ],\n shared: localSharedImportMap,\n plugins: [" + pluginImportNames.map(function (item) {
@@ -571,15 +626,15 @@ function proxySharedModule(options) {
571
626
  name: "generateLocalSharedImportMap",
572
627
  enforce: "post",
573
628
  resolveId: function resolveId(id) {
574
- if (id.includes(getLocalSharedImportMapId())) return id;
629
+ if (id.includes(getLocalSharedImportMapPath())) return id;
575
630
  },
576
631
  load: function load(id) {
577
- if (id.includes(getLocalSharedImportMapId())) {
632
+ if (id.includes(getLocalSharedImportMapPath())) {
578
633
  return generateLocalSharedImportMap();
579
634
  }
580
635
  },
581
636
  transform: function transform(code, id) {
582
- if (id.includes(getLocalSharedImportMapId())) {
637
+ if (id.includes(getLocalSharedImportMapPath())) {
583
638
  return generateLocalSharedImportMap();
584
639
  }
585
640
  }
@@ -655,6 +710,9 @@ function proxySharedModule(options) {
655
710
  name: "watchLocalSharedImportMap",
656
711
  apply: "serve",
657
712
  config: function config(_config2) {
713
+ _config2.optimizeDeps = defu(_config2.optimizeDeps, {
714
+ exclude: [getLocalSharedImportMapPath()]
715
+ });
658
716
  _config2.server = defu(_config2.server, {
659
717
  watch: {
660
718
  ignored: []
@@ -662,7 +720,7 @@ function proxySharedModule(options) {
662
720
  });
663
721
  var watch = _config2.server.watch;
664
722
  watch.ignored = [].concat(watch.ignored);
665
- watch.ignored.push("!**/node_modules/" + localSharedImportMapModule.getImportId() + ".js");
723
+ watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
666
724
  }
667
725
  }, {
668
726
  name: "prebuild-top-level-await",
@@ -776,7 +834,7 @@ function federation(mfUserOptions) {
776
834
  entryName: 'hostInit',
777
835
  entryPath: HOST_AUTO_INIT_PATH
778
836
  }), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], pluginModuleParseEnd(function (id) {
779
- return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapId());
837
+ return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath());
780
838
  }), proxySharedModule({
781
839
  shared: shared
782
840
  }), [{
@@ -1,7 +1,7 @@
1
1
  import * as fs from 'fs';
2
- import { mkdirSync, writeFileSync, writeFile } from 'fs';
2
+ import { existsSync, mkdirSync, writeFileSync, writeFile } from 'fs';
3
3
  import * as path from 'pathe';
4
- import { resolve } from 'pathe';
4
+ import path__default, { parse, join, dirname, resolve } from 'pathe';
5
5
  import { createFilter } from '@rollup/pluginutils';
6
6
  import { defu } from 'defu';
7
7
  import { walk } from 'estree-walker';
@@ -292,11 +292,21 @@ function removePathFromNpmPackage(packageString) {
292
292
  return match ? match[0] : packageString;
293
293
  }
294
294
 
295
- const nodeModulesDir = resolve("./node_modules");
295
+ const nodeModulesDir = function findNodeModulesDir(startDir = process.cwd()) {
296
+ let currentDir = startDir;
297
+ while (currentDir !== parse(currentDir).root) {
298
+ const nodeModulesPath = join(currentDir, 'node_modules');
299
+ if (existsSync(nodeModulesPath)) {
300
+ return nodeModulesPath;
301
+ }
302
+ currentDir = dirname(currentDir);
303
+ }
304
+ return "";
305
+ }();
296
306
  const virtualPackageName = "__mf__virtual";
297
- try {
307
+ if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) {
298
308
  mkdirSync(resolve(nodeModulesDir, virtualPackageName));
299
- } catch (e) {}
309
+ }
300
310
  writeFileSync(resolve(nodeModulesDir, virtualPackageName, "empty.js"), "");
301
311
  writeFileSync(resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.stringify({
302
312
  name: virtualPackageName,
@@ -308,6 +318,7 @@ writeFileSync(resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.
308
318
  class VirtualModule {
309
319
  constructor(name) {
310
320
  this.originName = void 0;
321
+ this.inited = false;
311
322
  this.originName = name;
312
323
  }
313
324
  getPath() {
@@ -316,7 +327,11 @@ class VirtualModule {
316
327
  getImportId() {
317
328
  return `${virtualPackageName}/${packageNameEncode(this.originName)}`;
318
329
  }
319
- writeSync(code) {
330
+ writeSync(code, force) {
331
+ if (!force && this.inited) return;
332
+ if (!this.inited) {
333
+ this.inited = true;
334
+ }
320
335
  writeFileSync(this.getPath() + ".js", code);
321
336
  }
322
337
  write(code) {
@@ -324,6 +339,27 @@ class VirtualModule {
324
339
  }
325
340
  }
326
341
 
342
+ /**
343
+ * https://github.com/module-federation/vite/issues/68
344
+ */
345
+ function getLocalSharedImportMapPath_windows() {
346
+ const {
347
+ name
348
+ } = getNormalizeModuleFederationOptions();
349
+ return path__default.resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
350
+ }
351
+ function writeLocalSharedImportMap_windows(content) {
352
+ const localSharedImportMapId = getLocalSharedImportMapPath_windows();
353
+ createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
354
+ }
355
+ function createFile(filePath, content) {
356
+ const dir = path__default.dirname(filePath);
357
+ mkdirSync(dir, {
358
+ recursive: true
359
+ });
360
+ writeFileSync(filePath, content);
361
+ }
362
+
327
363
  /**
328
364
  * Even the resolveId hook cannot interfere with vite pre-build,
329
365
  * and adding query parameter virtual modules will also fail.
@@ -348,7 +384,10 @@ function addShare(pkg) {
348
384
  // *** Expose locally provided shared modules here
349
385
  const localSharedImportMapModule = new VirtualModule("localSharedImportMap");
350
386
  localSharedImportMapModule.writeSync("");
351
- function getLocalSharedImportMapId() {
387
+ function getLocalSharedImportMapPath() {
388
+ if (process.platform === "win32") {
389
+ return getLocalSharedImportMapPath_windows();
390
+ }
352
391
  return localSharedImportMapModule.getPath();
353
392
  }
354
393
  let prevSharedCount = 0;
@@ -356,7 +395,10 @@ async function writeLocalSharedImportMap() {
356
395
  const sharedCount = shareds.size;
357
396
  if (prevSharedCount !== sharedCount) {
358
397
  prevSharedCount = sharedCount;
359
- return localSharedImportMapModule.writeSync(await generateLocalSharedImportMap());
398
+ if (process.platform === "win32") {
399
+ return writeLocalSharedImportMap_windows(await generateLocalSharedImportMap());
400
+ }
401
+ return localSharedImportMapModule.writeSync(await generateLocalSharedImportMap(), true);
360
402
  }
361
403
  }
362
404
  async function generateLocalSharedImportMap() {
@@ -455,7 +497,7 @@ function generateRemoteEntry(options) {
455
497
  `;
456
498
  }).join(',')}
457
499
  }
458
- import localSharedImportMap from "${getLocalSharedImportMapId()}"
500
+ import localSharedImportMap from "${getLocalSharedImportMapPath()}"
459
501
  async function init(shared = {}) {
460
502
  const initRes = runtimeInit({
461
503
  name: ${JSON.stringify(options.name)},
@@ -654,15 +696,15 @@ function proxySharedModule(options) {
654
696
  name: "generateLocalSharedImportMap",
655
697
  enforce: "post",
656
698
  resolveId(id) {
657
- if (id.includes(getLocalSharedImportMapId())) return id;
699
+ if (id.includes(getLocalSharedImportMapPath())) return id;
658
700
  },
659
701
  load(id) {
660
- if (id.includes(getLocalSharedImportMapId())) {
702
+ if (id.includes(getLocalSharedImportMapPath())) {
661
703
  return generateLocalSharedImportMap();
662
704
  }
663
705
  },
664
706
  transform(code, id) {
665
- if (id.includes(getLocalSharedImportMapId())) {
707
+ if (id.includes(getLocalSharedImportMapPath())) {
666
708
  return generateLocalSharedImportMap();
667
709
  }
668
710
  }
@@ -730,6 +772,9 @@ function proxySharedModule(options) {
730
772
  name: "watchLocalSharedImportMap",
731
773
  apply: "serve",
732
774
  config(config) {
775
+ config.optimizeDeps = defu(config.optimizeDeps, {
776
+ exclude: [getLocalSharedImportMapPath()]
777
+ });
733
778
  config.server = defu(config.server, {
734
779
  watch: {
735
780
  ignored: []
@@ -737,7 +782,7 @@ function proxySharedModule(options) {
737
782
  });
738
783
  const watch = config.server.watch;
739
784
  watch.ignored = [].concat(watch.ignored);
740
- watch.ignored.push(`!**/node_modules/${localSharedImportMapModule.getImportId()}.js`);
785
+ watch.ignored.push(`!**${getLocalSharedImportMapPath()}**`);
741
786
  }
742
787
  }, {
743
788
  name: "prebuild-top-level-await",
@@ -864,7 +909,7 @@ function federation(mfUserOptions) {
864
909
  entryName: 'hostInit',
865
910
  entryPath: HOST_AUTO_INIT_PATH
866
911
  }), pluginProxyRemoteEntry(), pluginProxyRemotes(options), ...pluginModuleParseEnd(id => {
867
- return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapId());
912
+ return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath());
868
913
  }), ...proxySharedModule({
869
914
  shared
870
915
  }), {
package/lib/index.umd.js CHANGED
@@ -25,6 +25,7 @@
25
25
 
26
26
  var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
27
27
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
28
+ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
28
29
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
29
30
 
30
31
  var addEntry = function addEntry(_ref) {
@@ -307,11 +308,24 @@
307
308
  return match ? match[0] : packageString;
308
309
  }
309
310
 
310
- var nodeModulesDir = path.resolve("./node_modules");
311
+ var nodeModulesDir = function findNodeModulesDir(startDir) {
312
+ if (startDir === void 0) {
313
+ startDir = process.cwd();
314
+ }
315
+ var currentDir = startDir;
316
+ while (currentDir !== path.parse(currentDir).root) {
317
+ var nodeModulesPath = path.join(currentDir, 'node_modules');
318
+ if (fs.existsSync(nodeModulesPath)) {
319
+ return nodeModulesPath;
320
+ }
321
+ currentDir = path.dirname(currentDir);
322
+ }
323
+ return "";
324
+ }();
311
325
  var virtualPackageName = "__mf__virtual";
312
- try {
326
+ if (!fs.existsSync(path.resolve(nodeModulesDir, virtualPackageName))) {
313
327
  fs.mkdirSync(path.resolve(nodeModulesDir, virtualPackageName));
314
- } catch (e) {}
328
+ }
315
329
  fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, "empty.js"), "");
316
330
  fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.stringify({
317
331
  name: virtualPackageName,
@@ -323,6 +337,7 @@
323
337
  var VirtualModule = /*#__PURE__*/function () {
324
338
  function VirtualModule(name) {
325
339
  this.originName = void 0;
340
+ this.inited = false;
326
341
  this.originName = name;
327
342
  }
328
343
  var _proto = VirtualModule.prototype;
@@ -332,7 +347,11 @@
332
347
  _proto.getImportId = function getImportId() {
333
348
  return virtualPackageName + "/" + packageNameEncode(this.originName);
334
349
  };
335
- _proto.writeSync = function writeSync(code) {
350
+ _proto.writeSync = function writeSync(code, force) {
351
+ if (!force && this.inited) return;
352
+ if (!this.inited) {
353
+ this.inited = true;
354
+ }
336
355
  fs.writeFileSync(this.getPath() + ".js", code);
337
356
  };
338
357
  _proto.write = function write(code) {
@@ -341,6 +360,26 @@
341
360
  return VirtualModule;
342
361
  }();
343
362
 
363
+ /**
364
+ * https://github.com/module-federation/vite/issues/68
365
+ */
366
+ function getLocalSharedImportMapPath_windows() {
367
+ var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
368
+ name = _getNormalizeModuleFe.name;
369
+ return path__default["default"].resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
370
+ }
371
+ function writeLocalSharedImportMap_windows(content) {
372
+ var localSharedImportMapId = getLocalSharedImportMapPath_windows();
373
+ createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
374
+ }
375
+ function createFile(filePath, content) {
376
+ var dir = path__default["default"].dirname(filePath);
377
+ fs.mkdirSync(dir, {
378
+ recursive: true
379
+ });
380
+ fs.writeFileSync(filePath, content);
381
+ }
382
+
344
383
  /**
345
384
  * Even the resolveId hook cannot interfere with vite pre-build,
346
385
  * and adding query parameter virtual modules will also fail.
@@ -362,14 +401,28 @@
362
401
  // *** __loadShare__
363
402
  var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
364
403
  try {
404
+ var _exit;
365
405
  var sharedCount = shareds.size;
366
406
  return Promise.resolve(function () {
367
407
  if (prevSharedCount !== sharedCount) {
408
+ var _temp2 = function _temp2(_result) {
409
+ if (_exit) return _result;
410
+ var _writeSync = localSharedImportMapModule.writeSync;
411
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI2) {
412
+ return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI2, true);
413
+ });
414
+ };
368
415
  prevSharedCount = sharedCount;
369
- var _writeSync = localSharedImportMapModule.writeSync;
370
- return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
371
- return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI);
372
- });
416
+ var _temp = function () {
417
+ if (process.platform === "win32") {
418
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
419
+ var _writeLocalSharedImpo = writeLocalSharedImportMap_windows(_generateLocalSharedI);
420
+ _exit = 1;
421
+ return _writeLocalSharedImpo;
422
+ });
423
+ }
424
+ }();
425
+ return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
373
426
  }
374
427
  }());
375
428
  } catch (e) {
@@ -394,7 +447,10 @@
394
447
  // *** Expose locally provided shared modules here
395
448
  var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
396
449
  localSharedImportMapModule.writeSync("");
397
- function getLocalSharedImportMapId() {
450
+ function getLocalSharedImportMapPath() {
451
+ if (process.platform === "win32") {
452
+ return getLocalSharedImportMapPath_windows();
453
+ }
398
454
  return localSharedImportMapModule.getPath();
399
455
  }
400
456
  var prevSharedCount = 0;
@@ -418,7 +474,7 @@
418
474
  return item[1];
419
475
  }).join('\n') + "\n\n const exposesMap = {\n " + Object.keys(options.exposes).map(function (key) {
420
476
  return "\n " + JSON.stringify(key) + ": async () => {\n const importModule = await import(" + JSON.stringify(options.exposes[key]["import"]) + ")\n const exportModule = {}\n Object.assign(exportModule, importModule)\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return exportModule\n }\n ";
421
- }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapId() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
477
+ }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapPath() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
422
478
  var remote = options.remotes[key];
423
479
  return "\n {\n entryGlobalName: " + JSON.stringify(remote.entryGlobalName) + ",\n name: " + JSON.stringify(remote.name) + ",\n type: " + JSON.stringify(remote.type) + ",\n entry: " + JSON.stringify(remote.entry) + ",\n }\n ";
424
480
  }).join(',') + "\n ],\n shared: localSharedImportMap,\n plugins: [" + pluginImportNames.map(function (item) {
@@ -591,15 +647,15 @@
591
647
  name: "generateLocalSharedImportMap",
592
648
  enforce: "post",
593
649
  resolveId: function resolveId(id) {
594
- if (id.includes(getLocalSharedImportMapId())) return id;
650
+ if (id.includes(getLocalSharedImportMapPath())) return id;
595
651
  },
596
652
  load: function load(id) {
597
- if (id.includes(getLocalSharedImportMapId())) {
653
+ if (id.includes(getLocalSharedImportMapPath())) {
598
654
  return generateLocalSharedImportMap();
599
655
  }
600
656
  },
601
657
  transform: function transform(code, id) {
602
- if (id.includes(getLocalSharedImportMapId())) {
658
+ if (id.includes(getLocalSharedImportMapPath())) {
603
659
  return generateLocalSharedImportMap();
604
660
  }
605
661
  }
@@ -675,6 +731,9 @@
675
731
  name: "watchLocalSharedImportMap",
676
732
  apply: "serve",
677
733
  config: function config(_config2) {
734
+ _config2.optimizeDeps = defu.defu(_config2.optimizeDeps, {
735
+ exclude: [getLocalSharedImportMapPath()]
736
+ });
678
737
  _config2.server = defu.defu(_config2.server, {
679
738
  watch: {
680
739
  ignored: []
@@ -682,7 +741,7 @@
682
741
  });
683
742
  var watch = _config2.server.watch;
684
743
  watch.ignored = [].concat(watch.ignored);
685
- watch.ignored.push("!**/node_modules/" + localSharedImportMapModule.getImportId() + ".js");
744
+ watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
686
745
  }
687
746
  }, {
688
747
  name: "prebuild-top-level-await",
@@ -796,7 +855,7 @@
796
855
  entryName: 'hostInit',
797
856
  entryPath: HOST_AUTO_INIT_PATH
798
857
  }), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], pluginModuleParseEnd(function (id) {
799
- return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapId());
858
+ return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath());
800
859
  }), proxySharedModule({
801
860
  shared: shared
802
861
  }), [{
@@ -4,9 +4,10 @@ export declare const virtualPackageName = "__mf__virtual";
4
4
  */
5
5
  export default class VirtualModule {
6
6
  originName: string;
7
+ inited: boolean;
7
8
  constructor(name: string);
8
9
  getPath(): string;
9
10
  getImportId(): string;
10
- writeSync(code: string): void;
11
+ writeSync(code: string, force?: boolean): void;
11
12
  write(code: string): void;
12
13
  }
@@ -0,0 +1,2 @@
1
+ export declare function getLocalSharedImportMapPath_windows(): string;
2
+ export declare function writeLocalSharedImportMap_windows(content: string): void;
@@ -15,7 +15,7 @@ export declare function writePreBuildLibPath(pkg: string): void;
15
15
  export declare function getPreBuildLibImportId(pkg: string): string;
16
16
  export declare function addShare(pkg: string): void;
17
17
  export declare const localSharedImportMapModule: VirtualModule;
18
- export declare function getLocalSharedImportMapId(): string;
18
+ export declare function getLocalSharedImportMapPath(): string;
19
19
  export declare function writeLocalSharedImportMap(): Promise<void>;
20
20
  export declare function generateLocalSharedImportMap(): Promise<string>;
21
21
  export declare const LOAD_SHARE_TAG = "__loadShare__";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.0.0-alpha-f87d7d3",
3
+ "version": "1.0.0-alpha-1aa30c0",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "source": "src/index.ts",
@@ -14,9 +14,9 @@
14
14
  "format": "pretty-quick",
15
15
  "dev": "microbundle watch",
16
16
  "build": "rimraf lib && microbundle --no-sourcemap --compress=false",
17
- "dev-rust-vite": "pnpm -filter 'examples-rust-vite*' run dev",
18
- "dev-vite-vite": "pnpm -filter 'examples-vite-vite*' run dev",
19
- "preview-vite-vite": "pnpm -filter 'examples-vite-vite*' run preview"
17
+ "dev-rv": "pnpm -filter 'examples-rust-vite*' run dev",
18
+ "dev-vv": "pnpm -filter 'examples-vite-vite*' run dev",
19
+ "preview-vv": "pnpm -filter 'examples-vite-vite*' run preview"
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",