@module-federation/modern-js 0.14.1 → 0.14.3

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.
Files changed (98) hide show
  1. package/dist/LICENSE +21 -0
  2. package/dist/cjs/cli/configPlugin.js +377 -0
  3. package/dist/cjs/cli/configPlugin.spec.js +112 -0
  4. package/dist/cjs/cli/constant.js +28 -0
  5. package/dist/cjs/cli/index.js +93 -0
  6. package/dist/cjs/cli/logger.js +27 -0
  7. package/dist/cjs/cli/manifest.js +75 -0
  8. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
  9. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
  10. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
  11. package/dist/cjs/cli/ssrPlugin.js +217 -0
  12. package/dist/cjs/cli/utils.js +85 -0
  13. package/dist/cjs/constant.js +31 -0
  14. package/dist/cjs/interfaces/bundler.js +16 -0
  15. package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
  16. package/dist/cjs/runtime/index.js +38 -0
  17. package/dist/cjs/server/fileCache.js +84 -0
  18. package/dist/cjs/server/fileCache.spec.js +28 -0
  19. package/dist/cjs/server/index.js +58 -0
  20. package/dist/cjs/server/staticMiddleware.js +77 -0
  21. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  22. package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
  23. package/dist/cjs/ssr-runtime/index.js +22 -0
  24. package/dist/cjs/ssr-runtime/plugin.js +68 -0
  25. package/dist/cjs/types/index.js +16 -0
  26. package/dist/esm/cli/configPlugin.js +384 -0
  27. package/dist/esm/cli/configPlugin.spec.js +108 -0
  28. package/dist/esm/cli/constant.js +4 -0
  29. package/dist/esm/cli/index.js +88 -0
  30. package/dist/esm/cli/logger.js +7 -0
  31. package/dist/esm/cli/manifest.js +41 -0
  32. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
  33. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
  34. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  35. package/dist/esm/cli/ssrPlugin.js +199 -0
  36. package/dist/esm/cli/utils.js +49 -0
  37. package/dist/esm/constant.js +6 -0
  38. package/dist/esm/interfaces/bundler.js +0 -0
  39. package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
  40. package/dist/esm/runtime/index.js +12 -0
  41. package/dist/esm/server/fileCache.js +98 -0
  42. package/dist/esm/server/fileCache.spec.js +50 -0
  43. package/dist/esm/server/index.js +36 -0
  44. package/dist/esm/server/staticMiddleware.js +81 -0
  45. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  46. package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
  47. package/dist/esm/ssr-runtime/index.js +1 -0
  48. package/dist/esm/ssr-runtime/plugin.js +70 -0
  49. package/dist/esm/types/index.js +0 -0
  50. package/dist/esm-node/cli/configPlugin.js +336 -0
  51. package/dist/esm-node/cli/configPlugin.spec.js +89 -0
  52. package/dist/esm-node/cli/constant.js +4 -0
  53. package/dist/esm-node/cli/index.js +68 -0
  54. package/dist/esm-node/cli/logger.js +7 -0
  55. package/dist/esm-node/cli/manifest.js +41 -0
  56. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
  57. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
  58. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  59. package/dist/esm-node/cli/ssrPlugin.js +181 -0
  60. package/dist/esm-node/cli/utils.js +49 -0
  61. package/dist/esm-node/constant.js +6 -0
  62. package/dist/esm-node/interfaces/bundler.js +0 -0
  63. package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
  64. package/dist/esm-node/runtime/index.js +12 -0
  65. package/dist/esm-node/server/fileCache.js +49 -0
  66. package/dist/esm-node/server/fileCache.spec.js +27 -0
  67. package/dist/esm-node/server/index.js +34 -0
  68. package/dist/esm-node/server/staticMiddleware.js +43 -0
  69. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  70. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
  71. package/dist/esm-node/ssr-runtime/index.js +1 -0
  72. package/dist/esm-node/ssr-runtime/plugin.js +34 -0
  73. package/dist/esm-node/types/index.js +0 -0
  74. package/dist/types/cli/configPlugin.d.ts +21 -0
  75. package/dist/types/cli/configPlugin.spec.d.ts +1 -0
  76. package/dist/types/cli/constant.d.ts +1 -0
  77. package/dist/types/cli/index.d.ts +5 -0
  78. package/dist/types/cli/logger.d.ts +2 -0
  79. package/dist/types/cli/manifest.d.ts +2 -0
  80. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
  81. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
  82. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  83. package/dist/types/cli/ssrPlugin.d.ts +6 -0
  84. package/dist/types/cli/utils.d.ts +5 -0
  85. package/dist/types/constant.d.ts +2 -0
  86. package/dist/types/interfaces/bundler.d.ts +18 -0
  87. package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
  88. package/dist/types/runtime/index.d.ts +5 -0
  89. package/dist/types/server/fileCache.d.ts +14 -0
  90. package/dist/types/server/fileCache.spec.d.ts +1 -0
  91. package/dist/types/server/index.d.ts +4 -0
  92. package/dist/types/server/staticMiddleware.d.ts +6 -0
  93. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  94. package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
  95. package/dist/types/ssr-runtime/index.d.ts +1 -0
  96. package/dist/types/ssr-runtime/plugin.d.ts +2 -0
  97. package/dist/types/types/index.d.ts +22 -0
  98. package/package.json +8 -8
@@ -0,0 +1,191 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
+ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
5
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
8
+ import React from "react";
9
+ import { getInstance } from "@module-federation/enhanced/runtime";
10
+ import { ErrorBoundary } from "react-error-boundary";
11
+ function getLoadedRemoteInfos(instance, id) {
12
+ var _ref = instance.remoteHandler.idToRemoteMap[id] || {}, name = _ref.name, expose = _ref.expose;
13
+ if (!name) {
14
+ return;
15
+ }
16
+ var module = instance.moduleCache.get(name);
17
+ if (!module) {
18
+ return;
19
+ }
20
+ var remoteSnapshot = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo).remoteSnapshot;
21
+ return _object_spread_props(_object_spread({}, module.remoteInfo), {
22
+ snapshot: remoteSnapshot,
23
+ expose
24
+ });
25
+ }
26
+ function getTargetModuleInfo(id) {
27
+ var instance = getInstance();
28
+ if (!instance) {
29
+ return;
30
+ }
31
+ var loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
32
+ if (!loadedRemoteInfo) {
33
+ return;
34
+ }
35
+ var snapshot = loadedRemoteInfo.snapshot;
36
+ if (!snapshot) {
37
+ return;
38
+ }
39
+ var publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
40
+ if (!publicPath) {
41
+ return;
42
+ }
43
+ var modules = "modules" in snapshot ? snapshot.modules : [];
44
+ var targetModule = modules.find(function(m) {
45
+ return m.modulePath === loadedRemoteInfo.expose;
46
+ });
47
+ if (!targetModule) {
48
+ return;
49
+ }
50
+ var remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
51
+ if (!remoteEntry) {
52
+ return;
53
+ }
54
+ return {
55
+ module: targetModule,
56
+ publicPath,
57
+ remoteEntry
58
+ };
59
+ }
60
+ function collectSSRAssets(options) {
61
+ var _ref = typeof options === "string" ? {
62
+ id: options
63
+ } : options, id = _ref.id, _ref_injectLink = _ref.injectLink, injectLink = _ref_injectLink === void 0 ? true : _ref_injectLink, _ref_injectScript = _ref.injectScript, injectScript = _ref_injectScript === void 0 ? true : _ref_injectScript;
64
+ var links = [];
65
+ var scripts = [];
66
+ var instance = getInstance();
67
+ if (!instance || !injectLink && !injectScript) {
68
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
69
+ }
70
+ var moduleAndPublicPath = getTargetModuleInfo(id);
71
+ if (!moduleAndPublicPath) {
72
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
73
+ }
74
+ var targetModule = moduleAndPublicPath.module, publicPath = moduleAndPublicPath.publicPath, remoteEntry = moduleAndPublicPath.remoteEntry;
75
+ if (injectLink) {
76
+ _to_consumable_array(targetModule.assets.css.sync).concat(_to_consumable_array(targetModule.assets.css.async)).forEach(function(file, index) {
77
+ links.push(/* @__PURE__ */ _jsx("link", {
78
+ href: "".concat(publicPath).concat(file),
79
+ rel: "stylesheet",
80
+ type: "text/css"
81
+ }, "".concat(file.split(".")[0], "_").concat(index)));
82
+ });
83
+ }
84
+ if (injectScript) {
85
+ scripts.push(/* @__PURE__ */ _jsx("script", {
86
+ async: true,
87
+ src: "".concat(publicPath).concat(remoteEntry),
88
+ crossOrigin: "anonymous"
89
+ }, remoteEntry.split(".")[0]));
90
+ _to_consumable_array(targetModule.assets.js.sync).forEach(function(file, index) {
91
+ scripts.push(/* @__PURE__ */ _jsx("script", {
92
+ async: true,
93
+ src: "".concat(publicPath).concat(file),
94
+ crossOrigin: "anonymous"
95
+ }, "".concat(file.split(".")[0], "_").concat(index)));
96
+ });
97
+ }
98
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
99
+ }
100
+ function createRemoteSSRComponent(info) {
101
+ var exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
102
+ var LazyComponent = /* @__PURE__ */ React.lazy(/* @__PURE__ */ _async_to_generator(function() {
103
+ var m, moduleId, assets, Com, err, FallbackFunctionComponent, FallbackNode;
104
+ return _ts_generator(this, function(_state) {
105
+ switch (_state.label) {
106
+ case 0:
107
+ _state.trys.push([
108
+ 0,
109
+ 2,
110
+ ,
111
+ 3
112
+ ]);
113
+ return [
114
+ 4,
115
+ info.loader()
116
+ ];
117
+ case 1:
118
+ m = _state.sent();
119
+ if (!m) {
120
+ throw new Error("load remote failed");
121
+ }
122
+ moduleId = m && m[Symbol.for("mf_module_id")];
123
+ assets = collectSSRAssets({
124
+ id: moduleId
125
+ });
126
+ Com = m[exportName];
127
+ if (exportName in m && typeof Com === "function") {
128
+ return [
129
+ 2,
130
+ {
131
+ default: function(props) {
132
+ return /* @__PURE__ */ _jsxs(_Fragment, {
133
+ children: [
134
+ assets,
135
+ /* @__PURE__ */ _jsx(Com, _object_spread({}, props))
136
+ ]
137
+ });
138
+ }
139
+ }
140
+ ];
141
+ } else {
142
+ throw Error("Make sure that ".concat(moduleId, " has the correct export when export is ").concat(String(exportName)));
143
+ }
144
+ return [
145
+ 3,
146
+ 3
147
+ ];
148
+ case 2:
149
+ err = _state.sent();
150
+ if (!info.fallback) {
151
+ throw err;
152
+ }
153
+ FallbackFunctionComponent = info.fallback;
154
+ FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
155
+ error: err,
156
+ resetErrorBoundary: function() {
157
+ console.log('SSR mode not support "resetErrorBoundary" !');
158
+ }
159
+ });
160
+ return [
161
+ 2,
162
+ {
163
+ default: function() {
164
+ return FallbackNode;
165
+ }
166
+ }
167
+ ];
168
+ case 3:
169
+ return [
170
+ 2
171
+ ];
172
+ }
173
+ });
174
+ }));
175
+ return function(props) {
176
+ var key = props.key, args = _object_without_properties(props, [
177
+ "key"
178
+ ]);
179
+ return /* @__PURE__ */ _jsx(ErrorBoundary, {
180
+ FallbackComponent: info.fallback,
181
+ children: /* @__PURE__ */ _jsx(React.Suspense, {
182
+ fallback: info.loading,
183
+ children: /* @__PURE__ */ _jsx(LazyComponent, _object_spread({}, args))
184
+ })
185
+ });
186
+ };
187
+ }
188
+ export {
189
+ collectSSRAssets,
190
+ createRemoteSSRComponent
191
+ };
@@ -0,0 +1,12 @@
1
+ export * from "@module-federation/enhanced/runtime";
2
+ var kit = {
3
+ get createRemoteSSRComponent() {
4
+ return require("./createRemoteSSRComponent").createRemoteSSRComponent;
5
+ },
6
+ get collectSSRAssets() {
7
+ return require("./createRemoteSSRComponent").collectSSRAssets;
8
+ }
9
+ };
10
+ export {
11
+ kit
12
+ };
@@ -0,0 +1,98 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import fs from "fs-extra";
5
+ import { LRUCache } from "lru-cache";
6
+ var FileCache = /* @__PURE__ */ function() {
7
+ "use strict";
8
+ function FileCache2() {
9
+ _class_call_check(this, FileCache2);
10
+ this.cache = new LRUCache({
11
+ maxSize: 200 * 1024 * 1024
12
+ });
13
+ }
14
+ var _proto = FileCache2.prototype;
15
+ _proto.getFile = function getFile(filepath) {
16
+ var _this = this;
17
+ return _async_to_generator(function() {
18
+ var stat, currentModified, cachedEntry, content, newEntry, err;
19
+ return _ts_generator(this, function(_state) {
20
+ switch (_state.label) {
21
+ case 0:
22
+ return [
23
+ 4,
24
+ fs.pathExists(filepath)
25
+ ];
26
+ case 1:
27
+ if (!_state.sent()) {
28
+ return [
29
+ 2,
30
+ null
31
+ ];
32
+ }
33
+ _state.label = 2;
34
+ case 2:
35
+ _state.trys.push([
36
+ 2,
37
+ 5,
38
+ ,
39
+ 6
40
+ ]);
41
+ return [
42
+ 4,
43
+ fs.lstat(filepath)
44
+ ];
45
+ case 3:
46
+ stat = _state.sent();
47
+ currentModified = stat.mtimeMs;
48
+ cachedEntry = _this.cache.get(filepath);
49
+ if (cachedEntry && currentModified <= cachedEntry.lastModified) {
50
+ return [
51
+ 2,
52
+ {
53
+ content: cachedEntry.content,
54
+ lastModified: cachedEntry.lastModified
55
+ }
56
+ ];
57
+ }
58
+ return [
59
+ 4,
60
+ fs.readFile(filepath, "utf-8")
61
+ ];
62
+ case 4:
63
+ content = _state.sent();
64
+ newEntry = {
65
+ content,
66
+ lastModified: currentModified
67
+ };
68
+ _this.cache.set(filepath, newEntry, {
69
+ size: stat.size || content.length
70
+ });
71
+ return [
72
+ 2,
73
+ {
74
+ content,
75
+ lastModified: currentModified
76
+ }
77
+ ];
78
+ case 5:
79
+ err = _state.sent();
80
+ return [
81
+ 2,
82
+ null
83
+ ];
84
+ case 6:
85
+ return [
86
+ 2
87
+ ];
88
+ }
89
+ });
90
+ })();
91
+ };
92
+ return FileCache2;
93
+ }();
94
+ var fileCache = new FileCache();
95
+ export {
96
+ FileCache,
97
+ fileCache
98
+ };
@@ -0,0 +1,50 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { it, expect, describe, vi, beforeAll } from "vitest";
4
+ import { FileCache } from "./fileCache";
5
+ beforeAll(function() {
6
+ vi.mock("fs-extra", function() {
7
+ return {
8
+ default: {
9
+ pathExists: function() {
10
+ return true;
11
+ },
12
+ lstat: function() {
13
+ return {
14
+ mtimeMs: Date.now(),
15
+ size: 4
16
+ };
17
+ },
18
+ readFile: function() {
19
+ return "test";
20
+ }
21
+ }
22
+ };
23
+ });
24
+ });
25
+ describe("modern serve static file cache", /* @__PURE__ */ _async_to_generator(function() {
26
+ return _ts_generator(this, function(_state) {
27
+ it("should cache file", /* @__PURE__ */ _async_to_generator(function() {
28
+ var cache, result;
29
+ return _ts_generator(this, function(_state2) {
30
+ switch (_state2.label) {
31
+ case 0:
32
+ cache = new FileCache();
33
+ return [
34
+ 4,
35
+ cache.getFile("test.txt")
36
+ ];
37
+ case 1:
38
+ result = _state2.sent();
39
+ expect(result === null || result === void 0 ? void 0 : result.content).toBe("test");
40
+ return [
41
+ 2
42
+ ];
43
+ }
44
+ });
45
+ }));
46
+ return [
47
+ 2
48
+ ];
49
+ });
50
+ }));
@@ -0,0 +1,36 @@
1
+ import { createStaticMiddleware } from "./staticMiddleware";
2
+ var staticServePlugin = function() {
3
+ return {
4
+ name: "@modern-js/module-federation/server",
5
+ setup: function(api) {
6
+ api.onPrepare(function() {
7
+ var _config_output, _config_server;
8
+ console.log(process.env.NODE_ENV);
9
+ if (process.env.NODE_ENV === "development") {
10
+ return;
11
+ }
12
+ var middlewares = api.getServerContext().middlewares;
13
+ var config = api.getServerConfig();
14
+ var assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
15
+ if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
16
+ return;
17
+ }
18
+ var context = api.getServerContext();
19
+ var pwd = context.distDirectory;
20
+ var serverStaticMiddleware = createStaticMiddleware({
21
+ assetPrefix,
22
+ pwd
23
+ });
24
+ middlewares.push({
25
+ name: "module-federation-serve-manifest",
26
+ handler: serverStaticMiddleware
27
+ });
28
+ });
29
+ }
30
+ };
31
+ };
32
+ var server_default = staticServePlugin;
33
+ export {
34
+ server_default as default,
35
+ staticServePlugin
36
+ };
@@ -0,0 +1,81 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import fs from "fs-extra";
4
+ import path from "node:path";
5
+ import { fileCache } from "./fileCache";
6
+ var bundlesAssetPrefix = "/bundles";
7
+ var removeHost = function(url) {
8
+ try {
9
+ var hasProtocol = url.includes("://");
10
+ var hasDomain = hasProtocol || url.startsWith("//");
11
+ var pathname = hasDomain ? new URL(hasProtocol ? url : "http:".concat(url)).pathname : url;
12
+ return pathname;
13
+ } catch (e) {
14
+ return url;
15
+ }
16
+ };
17
+ var createStaticMiddleware = function(options) {
18
+ var assetPrefix = options.assetPrefix, pwd = options.pwd;
19
+ return function() {
20
+ var _ref = _async_to_generator(function(c, next) {
21
+ var pathname, prefixWithoutHost, prefixWithBundle, pathnameWithoutPrefix, filepath, fileResult;
22
+ return _ts_generator(this, function(_state) {
23
+ switch (_state.label) {
24
+ case 0:
25
+ pathname = c.req.path;
26
+ if (path.extname(pathname) !== ".js") {
27
+ return [
28
+ 2,
29
+ next()
30
+ ];
31
+ }
32
+ prefixWithoutHost = removeHost(assetPrefix);
33
+ prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
34
+ if (!pathname.startsWith(prefixWithBundle)) {
35
+ return [
36
+ 2,
37
+ next()
38
+ ];
39
+ }
40
+ pathnameWithoutPrefix = pathname.replace(prefixWithBundle, "");
41
+ filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
42
+ return [
43
+ 4,
44
+ fs.pathExists(filepath)
45
+ ];
46
+ case 1:
47
+ if (!_state.sent()) {
48
+ return [
49
+ 2,
50
+ next()
51
+ ];
52
+ }
53
+ return [
54
+ 4,
55
+ fileCache.getFile(filepath)
56
+ ];
57
+ case 2:
58
+ fileResult = _state.sent();
59
+ if (!fileResult) {
60
+ return [
61
+ 2,
62
+ next()
63
+ ];
64
+ }
65
+ c.header("Content-Type", "application/javascript");
66
+ c.header("Content-Length", String(fileResult.content.length));
67
+ return [
68
+ 2,
69
+ c.body(fileResult.content, 200)
70
+ ];
71
+ }
72
+ });
73
+ });
74
+ return function(c, next) {
75
+ return _ref.apply(this, arguments);
76
+ };
77
+ }();
78
+ };
79
+ export {
80
+ createStaticMiddleware
81
+ };