@module-federation/vite 1.1.7 → 1.1.9

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 CHANGED
@@ -17,6 +17,8 @@ This plugin makes Module Federation work together with [Vite](https://vitejs.dev
17
17
 
18
18
  ### [Svelte](https://github.com/gioboa/svelte-microfrontend-demo)<br>
19
19
 
20
+ ### [SvelteKit](https://github.com/gioboa/sveltekit-microfrontend-demo)<br>
21
+
20
22
  ### [SolidJS](https://github.com/gioboa/solid-microfrontend-demo)<br>
21
23
 
22
24
  ### [More examples here](https://github.com/module-federation/vite/tree/main/examples)<br>
package/lib/index.cjs CHANGED
@@ -30,6 +30,11 @@ var path__namespace = /*#__PURE__*/_interopNamespace(path);
30
30
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
31
31
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
32
32
 
33
+ function getFirstHtmlEntryFile(entryFiles) {
34
+ return entryFiles.find(function (file) {
35
+ return file.endsWith('.html');
36
+ });
37
+ }
33
38
  var addEntry = function addEntry(_ref) {
34
39
  var entryName = _ref.entryName,
35
40
  entryPath = _ref.entryPath,
@@ -42,6 +47,12 @@ var addEntry = function addEntry(_ref) {
42
47
  var _command;
43
48
  var emitFileId;
44
49
  var viteConfig;
50
+ function injectHtml() {
51
+ return inject === 'html' && htmlFilePath;
52
+ }
53
+ function injectEntry() {
54
+ return inject === 'entry' || !htmlFilePath;
55
+ }
45
56
  return [{
46
57
  name: 'add-entry',
47
58
  apply: 'serve',
@@ -56,8 +67,11 @@ var addEntry = function addEntry(_ref) {
56
67
  configureServer: function configureServer(server) {
57
68
  var _server$httpServer;
58
69
  (_server$httpServer = server.httpServer) == null || _server$httpServer.once == null || _server$httpServer.once('listening', function () {
59
- var port = server.config.server.port;
60
- fetch("http://localhost:" + port + devEntryPath)["catch"](function (e) {});
70
+ var _server$config$server = server.config.server,
71
+ port = _server$config$server.port,
72
+ hostConfig = _server$config$server.host;
73
+ var host = typeof hostConfig === 'string' && hostConfig !== '0.0.0.0' ? hostConfig : 'localhost';
74
+ fetch("http://" + host + ":" + port + devEntryPath)["catch"](function (e) {});
61
75
  });
62
76
  server.middlewares.use(function (req, res, next) {
63
77
  if (!fileName) {
@@ -71,8 +85,17 @@ var addEntry = function addEntry(_ref) {
71
85
  });
72
86
  },
73
87
  transformIndexHtml: function transformIndexHtml(c) {
74
- if (inject !== 'html') return;
88
+ if (!injectHtml()) return;
75
89
  return c.replace('<head>', "<head><script type=\"module\" src=" + JSON.stringify(devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/')) + "></script>");
90
+ },
91
+ transform: function transform(code, id) {
92
+ if (id.includes('node_modules') || inject !== 'html' || htmlFilePath) {
93
+ return;
94
+ }
95
+ if (id.includes('.svelte-kit') && id.includes('internal.js')) {
96
+ var src = devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/');
97
+ return code.replace(/<head>/g, '<head><script type=\\"module\\" src=\\"' + src + '\\"></script>');
98
+ }
76
99
  }
77
100
  }, {
78
101
  name: 'add-entry',
@@ -84,13 +107,13 @@ var addEntry = function addEntry(_ref) {
84
107
  htmlFilePath = path__namespace.resolve(config.root, 'index.html');
85
108
  } else if (typeof inputOptions === 'string') {
86
109
  entryFiles = [inputOptions];
87
- htmlFilePath = path__namespace.resolve(config.root, inputOptions);
88
110
  } else if (Array.isArray(inputOptions)) {
89
111
  entryFiles = inputOptions;
90
- htmlFilePath = path__namespace.resolve(config.root, inputOptions[0]);
91
112
  } else if (typeof inputOptions === 'object') {
92
113
  entryFiles = Object.values(inputOptions);
93
- htmlFilePath = path__namespace.resolve(config.root, Object.values(inputOptions)[0]);
114
+ }
115
+ if (entryFiles && entryFiles.length > 0) {
116
+ htmlFilePath = getFirstHtmlEntryFile(entryFiles);
94
117
  }
95
118
  },
96
119
  buildStart: function buildStart() {
@@ -116,7 +139,7 @@ var addEntry = function addEntry(_ref) {
116
139
  }
117
140
  },
118
141
  generateBundle: function generateBundle(options, bundle) {
119
- if (inject !== 'html') return;
142
+ if (!injectHtml()) return;
120
143
  var file = this.getFileName(emitFileId);
121
144
  var scriptContent = "\n <script type=\"module\" src=\"" + (viteConfig.base + file) + "\"></script>\n ";
122
145
  for (var _fileName in bundle) {
@@ -130,7 +153,7 @@ var addEntry = function addEntry(_ref) {
130
153
  }
131
154
  },
132
155
  transform: function transform(code, id) {
133
- if (inject === 'entry' && entryFiles.some(function (file) {
156
+ if (injectEntry() && entryFiles.some(function (file) {
134
157
  return id.endsWith(file);
135
158
  })) {
136
159
  var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
@@ -630,10 +653,16 @@ function generateLocalSharedImportMap() {
630
653
  return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\")\n return pkg\n }\n ";
631
654
  }).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).map(function (key) {
632
655
  var shareItem = getNormalizeShareItem(key);
656
+ if (!shareItem) return null;
633
657
  return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap \n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }\n }\n ";
658
+ }).filter(function (x) {
659
+ return x !== null;
634
660
  }).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
635
661
  var remote = options.remotes[key];
662
+ if (!remote) return null;
636
663
  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 ";
664
+ }).filter(function (x) {
665
+ return x !== null;
637
666
  }).join(',') + "\n ]\n export {\n usedShared,\n usedRemotes\n }\n ";
638
667
  }
639
668
  var REMOTE_ENTRY_ID = 'virtual:mf-REMOTE_ENTRY_ID';
@@ -1075,8 +1104,9 @@ function pluginProxyRemoteEntry () {
1075
1104
  if (id.includes(getHostAutoInitPath())) {
1076
1105
  var options = getNormalizeModuleFederationOptions();
1077
1106
  if (_command === 'serve') {
1078
- var _viteConfig$server;
1079
- return Promise.resolve("\n const {init} = await import(\"//localhost:" + ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.port) + (viteConfig.base + options.filename) + "\")\n init()\n ");
1107
+ var _viteConfig$server, _viteConfig$server2;
1108
+ var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
1109
+ return Promise.resolve("\n const {init} = await import(\"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + (viteConfig.base + options.filename) + "\")\n init()\n ");
1080
1110
  }
1081
1111
  return Promise.resolve(code);
1082
1112
  }
package/lib/index.esm.js CHANGED
@@ -7,6 +7,11 @@ import { walk } from 'estree-walker';
7
7
  import MagicString from 'magic-string';
8
8
  import { defu } from 'defu';
9
9
 
10
+ function getFirstHtmlEntryFile(entryFiles) {
11
+ return entryFiles.find(function (file) {
12
+ return file.endsWith('.html');
13
+ });
14
+ }
10
15
  var addEntry = function addEntry(_ref) {
11
16
  var entryName = _ref.entryName,
12
17
  entryPath = _ref.entryPath,
@@ -19,6 +24,12 @@ var addEntry = function addEntry(_ref) {
19
24
  var _command;
20
25
  var emitFileId;
21
26
  var viteConfig;
27
+ function injectHtml() {
28
+ return inject === 'html' && htmlFilePath;
29
+ }
30
+ function injectEntry() {
31
+ return inject === 'entry' || !htmlFilePath;
32
+ }
22
33
  return [{
23
34
  name: 'add-entry',
24
35
  apply: 'serve',
@@ -33,8 +44,11 @@ var addEntry = function addEntry(_ref) {
33
44
  configureServer: function configureServer(server) {
34
45
  var _server$httpServer;
35
46
  (_server$httpServer = server.httpServer) == null || _server$httpServer.once == null || _server$httpServer.once('listening', function () {
36
- var port = server.config.server.port;
37
- fetch("http://localhost:" + port + devEntryPath)["catch"](function (e) {});
47
+ var _server$config$server = server.config.server,
48
+ port = _server$config$server.port,
49
+ hostConfig = _server$config$server.host;
50
+ var host = typeof hostConfig === 'string' && hostConfig !== '0.0.0.0' ? hostConfig : 'localhost';
51
+ fetch("http://" + host + ":" + port + devEntryPath)["catch"](function (e) {});
38
52
  });
39
53
  server.middlewares.use(function (req, res, next) {
40
54
  if (!fileName) {
@@ -48,8 +62,17 @@ var addEntry = function addEntry(_ref) {
48
62
  });
49
63
  },
50
64
  transformIndexHtml: function transformIndexHtml(c) {
51
- if (inject !== 'html') return;
65
+ if (!injectHtml()) return;
52
66
  return c.replace('<head>', "<head><script type=\"module\" src=" + JSON.stringify(devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/')) + "></script>");
67
+ },
68
+ transform: function transform(code, id) {
69
+ if (id.includes('node_modules') || inject !== 'html' || htmlFilePath) {
70
+ return;
71
+ }
72
+ if (id.includes('.svelte-kit') && id.includes('internal.js')) {
73
+ var src = devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/');
74
+ return code.replace(/<head>/g, '<head><script type=\\"module\\" src=\\"' + src + '\\"></script>');
75
+ }
53
76
  }
54
77
  }, {
55
78
  name: 'add-entry',
@@ -61,13 +84,13 @@ var addEntry = function addEntry(_ref) {
61
84
  htmlFilePath = path.resolve(config.root, 'index.html');
62
85
  } else if (typeof inputOptions === 'string') {
63
86
  entryFiles = [inputOptions];
64
- htmlFilePath = path.resolve(config.root, inputOptions);
65
87
  } else if (Array.isArray(inputOptions)) {
66
88
  entryFiles = inputOptions;
67
- htmlFilePath = path.resolve(config.root, inputOptions[0]);
68
89
  } else if (typeof inputOptions === 'object') {
69
90
  entryFiles = Object.values(inputOptions);
70
- htmlFilePath = path.resolve(config.root, Object.values(inputOptions)[0]);
91
+ }
92
+ if (entryFiles && entryFiles.length > 0) {
93
+ htmlFilePath = getFirstHtmlEntryFile(entryFiles);
71
94
  }
72
95
  },
73
96
  buildStart: function buildStart() {
@@ -93,7 +116,7 @@ var addEntry = function addEntry(_ref) {
93
116
  }
94
117
  },
95
118
  generateBundle: function generateBundle(options, bundle) {
96
- if (inject !== 'html') return;
119
+ if (!injectHtml()) return;
97
120
  var file = this.getFileName(emitFileId);
98
121
  var scriptContent = "\n <script type=\"module\" src=\"" + (viteConfig.base + file) + "\"></script>\n ";
99
122
  for (var _fileName in bundle) {
@@ -107,7 +130,7 @@ var addEntry = function addEntry(_ref) {
107
130
  }
108
131
  },
109
132
  transform: function transform(code, id) {
110
- if (inject === 'entry' && entryFiles.some(function (file) {
133
+ if (injectEntry() && entryFiles.some(function (file) {
111
134
  return id.endsWith(file);
112
135
  })) {
113
136
  var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
@@ -607,10 +630,16 @@ function generateLocalSharedImportMap() {
607
630
  return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\")\n return pkg\n }\n ";
608
631
  }).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).map(function (key) {
609
632
  var shareItem = getNormalizeShareItem(key);
633
+ if (!shareItem) return null;
610
634
  return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap \n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }\n }\n ";
635
+ }).filter(function (x) {
636
+ return x !== null;
611
637
  }).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
612
638
  var remote = options.remotes[key];
639
+ if (!remote) return null;
613
640
  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 ";
641
+ }).filter(function (x) {
642
+ return x !== null;
614
643
  }).join(',') + "\n ]\n export {\n usedShared,\n usedRemotes\n }\n ";
615
644
  }
616
645
  var REMOTE_ENTRY_ID = 'virtual:mf-REMOTE_ENTRY_ID';
@@ -1052,8 +1081,9 @@ function pluginProxyRemoteEntry () {
1052
1081
  if (id.includes(getHostAutoInitPath())) {
1053
1082
  var options = getNormalizeModuleFederationOptions();
1054
1083
  if (_command === 'serve') {
1055
- var _viteConfig$server;
1056
- return Promise.resolve("\n const {init} = await import(\"//localhost:" + ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.port) + (viteConfig.base + options.filename) + "\")\n init()\n ");
1084
+ var _viteConfig$server, _viteConfig$server2;
1085
+ var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
1086
+ return Promise.resolve("\n const {init} = await import(\"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + (viteConfig.base + options.filename) + "\")\n init()\n ");
1057
1087
  }
1058
1088
  return Promise.resolve(code);
1059
1089
  }
@@ -7,6 +7,9 @@ import { walk } from 'estree-walker';
7
7
  import MagicString from 'magic-string';
8
8
  import { defu } from 'defu';
9
9
 
10
+ function getFirstHtmlEntryFile(entryFiles) {
11
+ return entryFiles.find(file => file.endsWith('.html'));
12
+ }
10
13
  const addEntry = ({
11
14
  entryName,
12
15
  entryPath,
@@ -19,6 +22,12 @@ const addEntry = ({
19
22
  let _command;
20
23
  let emitFileId;
21
24
  let viteConfig;
25
+ function injectHtml() {
26
+ return _inject === 'html' && htmlFilePath;
27
+ }
28
+ function injectEntry() {
29
+ return _inject === 'entry' || !htmlFilePath;
30
+ }
22
31
  return [{
23
32
  name: 'add-entry',
24
33
  apply: 'serve',
@@ -35,9 +44,11 @@ const addEntry = ({
35
44
  var _server$httpServer;
36
45
  (_server$httpServer = server.httpServer) == null || _server$httpServer.once == null || _server$httpServer.once('listening', () => {
37
46
  const {
38
- port
47
+ port,
48
+ host: hostConfig
39
49
  } = server.config.server;
40
- fetch(`http://localhost:${port}${devEntryPath}`).catch(e => {});
50
+ const host = typeof hostConfig === 'string' && hostConfig !== '0.0.0.0' ? hostConfig : 'localhost';
51
+ fetch(`http://${host}:${port}${devEntryPath}`).catch(e => {});
41
52
  });
42
53
  server.middlewares.use((req, res, next) => {
43
54
  if (!fileName) {
@@ -51,8 +62,17 @@ const addEntry = ({
51
62
  });
52
63
  },
53
64
  transformIndexHtml(c) {
54
- if (_inject !== 'html') return;
65
+ if (!injectHtml()) return;
55
66
  return c.replace('<head>', `<head><script type="module" src=${JSON.stringify(devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/'))}></script>`);
67
+ },
68
+ transform(code, id) {
69
+ if (id.includes('node_modules') || _inject !== 'html' || htmlFilePath) {
70
+ return;
71
+ }
72
+ if (id.includes('.svelte-kit') && id.includes('internal.js')) {
73
+ const src = devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/');
74
+ return code.replace(/<head>/g, '<head><script type=\\"module\\" src=\\"' + src + '\\"></script>');
75
+ }
56
76
  }
57
77
  }, {
58
78
  name: 'add-entry',
@@ -64,13 +84,13 @@ const addEntry = ({
64
84
  htmlFilePath = path.resolve(config.root, 'index.html');
65
85
  } else if (typeof inputOptions === 'string') {
66
86
  entryFiles = [inputOptions];
67
- htmlFilePath = path.resolve(config.root, inputOptions);
68
87
  } else if (Array.isArray(inputOptions)) {
69
88
  entryFiles = inputOptions;
70
- htmlFilePath = path.resolve(config.root, inputOptions[0]);
71
89
  } else if (typeof inputOptions === 'object') {
72
90
  entryFiles = Object.values(inputOptions);
73
- htmlFilePath = path.resolve(config.root, Object.values(inputOptions)[0]);
91
+ }
92
+ if (entryFiles && entryFiles.length > 0) {
93
+ htmlFilePath = getFirstHtmlEntryFile(entryFiles);
74
94
  }
75
95
  },
76
96
  buildStart() {
@@ -96,7 +116,7 @@ const addEntry = ({
96
116
  }
97
117
  },
98
118
  generateBundle(options, bundle) {
99
- if (_inject !== 'html') return;
119
+ if (!injectHtml()) return;
100
120
  const file = this.getFileName(emitFileId);
101
121
  const scriptContent = `
102
122
  <script type="module" src="${viteConfig.base + file}"></script>
@@ -112,7 +132,7 @@ const addEntry = ({
112
132
  }
113
133
  },
114
134
  transform(code, id) {
115
- if (_inject === 'entry' && entryFiles.some(file => id.endsWith(file))) {
135
+ if (injectEntry() && entryFiles.some(file => id.endsWith(file))) {
116
136
  const injection = `
117
137
  import ${JSON.stringify(entryPath)};
118
138
  `;
@@ -624,6 +644,7 @@ function generateLocalSharedImportMap() {
624
644
  const usedShared = {
625
645
  ${Array.from(getUsedShares()).map(key => {
626
646
  const shareItem = getNormalizeShareItem(key);
647
+ if (!shareItem) return null;
627
648
  return `
628
649
  ${JSON.stringify(key)}: {
629
650
  name: ${JSON.stringify(key)},
@@ -651,10 +672,11 @@ function generateLocalSharedImportMap() {
651
672
  }
652
673
  }
653
674
  `;
654
- }).join(',')}
675
+ }).filter(x => x !== null).join(',')}
655
676
  }
656
677
  const usedRemotes = [${Object.keys(getUsedRemotesMap()).map(key => {
657
678
  const remote = options.remotes[key];
679
+ if (!remote) return null;
658
680
  return `
659
681
  {
660
682
  entryGlobalName: ${JSON.stringify(remote.entryGlobalName)},
@@ -663,7 +685,7 @@ function generateLocalSharedImportMap() {
663
685
  entry: ${JSON.stringify(remote.entry)},
664
686
  }
665
687
  `;
666
- }).join(',')}
688
+ }).filter(x => x !== null).join(',')}
667
689
  ]
668
690
  export {
669
691
  usedShared,
@@ -1125,9 +1147,10 @@ function pluginProxyRemoteEntry () {
1125
1147
  if (id.includes(getHostAutoInitPath())) {
1126
1148
  const options = getNormalizeModuleFederationOptions();
1127
1149
  if (_command === 'serve') {
1128
- var _viteConfig$server;
1150
+ var _viteConfig$server, _viteConfig$server2;
1151
+ 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';
1129
1152
  return `
1130
- const {init} = await import("//localhost:${(_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.port}${viteConfig.base + options.filename}")
1153
+ const {init} = await import("//${host}:${(_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port}${viteConfig.base + options.filename}")
1131
1154
  init()
1132
1155
  `;
1133
1156
  }
package/lib/index.umd.js CHANGED
@@ -28,6 +28,11 @@
28
28
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
29
29
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
30
30
 
31
+ function getFirstHtmlEntryFile(entryFiles) {
32
+ return entryFiles.find(function (file) {
33
+ return file.endsWith('.html');
34
+ });
35
+ }
31
36
  var addEntry = function addEntry(_ref) {
32
37
  var entryName = _ref.entryName,
33
38
  entryPath = _ref.entryPath,
@@ -40,6 +45,12 @@
40
45
  var _command;
41
46
  var emitFileId;
42
47
  var viteConfig;
48
+ function injectHtml() {
49
+ return inject === 'html' && htmlFilePath;
50
+ }
51
+ function injectEntry() {
52
+ return inject === 'entry' || !htmlFilePath;
53
+ }
43
54
  return [{
44
55
  name: 'add-entry',
45
56
  apply: 'serve',
@@ -54,8 +65,11 @@
54
65
  configureServer: function configureServer(server) {
55
66
  var _server$httpServer;
56
67
  (_server$httpServer = server.httpServer) == null || _server$httpServer.once == null || _server$httpServer.once('listening', function () {
57
- var port = server.config.server.port;
58
- fetch("http://localhost:" + port + devEntryPath)["catch"](function (e) {});
68
+ var _server$config$server = server.config.server,
69
+ port = _server$config$server.port,
70
+ hostConfig = _server$config$server.host;
71
+ var host = typeof hostConfig === 'string' && hostConfig !== '0.0.0.0' ? hostConfig : 'localhost';
72
+ fetch("http://" + host + ":" + port + devEntryPath)["catch"](function (e) {});
59
73
  });
60
74
  server.middlewares.use(function (req, res, next) {
61
75
  if (!fileName) {
@@ -69,8 +83,17 @@
69
83
  });
70
84
  },
71
85
  transformIndexHtml: function transformIndexHtml(c) {
72
- if (inject !== 'html') return;
86
+ if (!injectHtml()) return;
73
87
  return c.replace('<head>', "<head><script type=\"module\" src=" + JSON.stringify(devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/')) + "></script>");
88
+ },
89
+ transform: function transform(code, id) {
90
+ if (id.includes('node_modules') || inject !== 'html' || htmlFilePath) {
91
+ return;
92
+ }
93
+ if (id.includes('.svelte-kit') && id.includes('internal.js')) {
94
+ var src = devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/');
95
+ return code.replace(/<head>/g, '<head><script type=\\"module\\" src=\\"' + src + '\\"></script>');
96
+ }
74
97
  }
75
98
  }, {
76
99
  name: 'add-entry',
@@ -82,13 +105,13 @@
82
105
  htmlFilePath = path__namespace.resolve(config.root, 'index.html');
83
106
  } else if (typeof inputOptions === 'string') {
84
107
  entryFiles = [inputOptions];
85
- htmlFilePath = path__namespace.resolve(config.root, inputOptions);
86
108
  } else if (Array.isArray(inputOptions)) {
87
109
  entryFiles = inputOptions;
88
- htmlFilePath = path__namespace.resolve(config.root, inputOptions[0]);
89
110
  } else if (typeof inputOptions === 'object') {
90
111
  entryFiles = Object.values(inputOptions);
91
- htmlFilePath = path__namespace.resolve(config.root, Object.values(inputOptions)[0]);
112
+ }
113
+ if (entryFiles && entryFiles.length > 0) {
114
+ htmlFilePath = getFirstHtmlEntryFile(entryFiles);
92
115
  }
93
116
  },
94
117
  buildStart: function buildStart() {
@@ -114,7 +137,7 @@
114
137
  }
115
138
  },
116
139
  generateBundle: function generateBundle(options, bundle) {
117
- if (inject !== 'html') return;
140
+ if (!injectHtml()) return;
118
141
  var file = this.getFileName(emitFileId);
119
142
  var scriptContent = "\n <script type=\"module\" src=\"" + (viteConfig.base + file) + "\"></script>\n ";
120
143
  for (var _fileName in bundle) {
@@ -128,7 +151,7 @@
128
151
  }
129
152
  },
130
153
  transform: function transform(code, id) {
131
- if (inject === 'entry' && entryFiles.some(function (file) {
154
+ if (injectEntry() && entryFiles.some(function (file) {
132
155
  return id.endsWith(file);
133
156
  })) {
134
157
  var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
@@ -628,10 +651,16 @@
628
651
  return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\")\n return pkg\n }\n ";
629
652
  }).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).map(function (key) {
630
653
  var shareItem = getNormalizeShareItem(key);
654
+ if (!shareItem) return null;
631
655
  return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap \n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }\n }\n ";
656
+ }).filter(function (x) {
657
+ return x !== null;
632
658
  }).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
633
659
  var remote = options.remotes[key];
660
+ if (!remote) return null;
634
661
  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 ";
662
+ }).filter(function (x) {
663
+ return x !== null;
635
664
  }).join(',') + "\n ]\n export {\n usedShared,\n usedRemotes\n }\n ";
636
665
  }
637
666
  var REMOTE_ENTRY_ID = 'virtual:mf-REMOTE_ENTRY_ID';
@@ -1073,8 +1102,9 @@
1073
1102
  if (id.includes(getHostAutoInitPath())) {
1074
1103
  var options = getNormalizeModuleFederationOptions();
1075
1104
  if (_command === 'serve') {
1076
- var _viteConfig$server;
1077
- return Promise.resolve("\n const {init} = await import(\"//localhost:" + ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.port) + (viteConfig.base + options.filename) + "\")\n init()\n ");
1105
+ var _viteConfig$server, _viteConfig$server2;
1106
+ var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
1107
+ return Promise.resolve("\n const {init} = await import(\"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + (viteConfig.base + options.filename) + "\")\n init()\n ");
1078
1108
  }
1079
1109
  return Promise.resolve(code);
1080
1110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "source": "src/index.ts",
@@ -46,13 +46,12 @@
46
46
  "homepage": "https://github.com/module-federation/vite#readme",
47
47
  "packageManager": "pnpm@9.1.3",
48
48
  "dependencies": {
49
- "@module-federation/runtime": "^0.7.1",
49
+ "@module-federation/runtime": "^0.8.0",
50
50
  "@rollup/pluginutils": "^5.1.0",
51
51
  "defu": "^6.1.4",
52
52
  "estree-walker": "^2",
53
53
  "magic-string": "^0.30.11",
54
- "pathe": "^1.1.2",
55
- "vitest": "^2.1.1"
54
+ "pathe": "^1.1.2"
56
55
  },
57
56
  "devDependencies": {
58
57
  "@playwright/test": "^1.47.2",
@@ -64,6 +63,7 @@
64
63
  "pretty-quick": "^4.0.0",
65
64
  "rimraf": "^6.0.1",
66
65
  "vite": "^5.4.3",
66
+ "vitest": "^2.1.1",
67
67
  "wait-on": "^8.0.1"
68
68
  }
69
69
  }