@module-federation/modern-js 0.0.0-next-20250618045528 → 0.0.0-next-20250618090118

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 (94) hide show
  1. package/dist/LICENSE +21 -0
  2. package/dist/cjs/cli/configPlugin.js +381 -0
  3. package/dist/cjs/cli/configPlugin.spec.js +114 -0
  4. package/dist/cjs/cli/index.js +95 -0
  5. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
  6. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
  7. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
  8. package/dist/cjs/cli/server/data-fetch-server-plugin.js +48 -0
  9. package/dist/cjs/cli/ssrPlugin.js +236 -0
  10. package/dist/cjs/cli/utils.js +90 -0
  11. package/dist/cjs/constant.js +31 -0
  12. package/dist/cjs/interfaces/bundler.js +16 -0
  13. package/dist/cjs/logger.js +27 -0
  14. package/dist/cjs/react/index.js +67 -0
  15. package/dist/cjs/runtime/index.js +22 -0
  16. package/dist/cjs/server/fileCache.js +84 -0
  17. package/dist/cjs/server/fileCache.spec.js +28 -0
  18. package/dist/cjs/server/index.js +58 -0
  19. package/dist/cjs/server/staticMiddleware.js +77 -0
  20. package/dist/cjs/server/staticMiddleware.spec.js +185 -0
  21. package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
  22. package/dist/cjs/ssr-runtime/devPlugin.js +71 -0
  23. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +41 -0
  24. package/dist/cjs/types/index.js +16 -0
  25. package/dist/esm/cli/configPlugin.js +388 -0
  26. package/dist/esm/cli/configPlugin.spec.js +110 -0
  27. package/dist/esm/cli/index.js +90 -0
  28. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
  29. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
  30. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
  31. package/dist/esm/cli/server/data-fetch-server-plugin.js +20 -0
  32. package/dist/esm/cli/ssrPlugin.js +219 -0
  33. package/dist/esm/cli/utils.js +53 -0
  34. package/dist/esm/constant.js +6 -0
  35. package/dist/esm/interfaces/bundler.js +0 -0
  36. package/dist/esm/logger.js +7 -0
  37. package/dist/esm/react/index.js +28 -0
  38. package/dist/esm/runtime/index.js +1 -0
  39. package/dist/esm/server/fileCache.js +98 -0
  40. package/dist/esm/server/fileCache.spec.js +50 -0
  41. package/dist/esm/server/index.js +36 -0
  42. package/dist/esm/server/staticMiddleware.js +81 -0
  43. package/dist/esm/server/staticMiddleware.spec.js +328 -0
  44. package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
  45. package/dist/esm/ssr-runtime/devPlugin.js +73 -0
  46. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +35 -0
  47. package/dist/esm/types/index.js +0 -0
  48. package/dist/esm-node/cli/configPlugin.js +340 -0
  49. package/dist/esm-node/cli/configPlugin.spec.js +91 -0
  50. package/dist/esm-node/cli/index.js +70 -0
  51. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
  52. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
  53. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
  54. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +18 -0
  55. package/dist/esm-node/cli/ssrPlugin.js +200 -0
  56. package/dist/esm-node/cli/utils.js +53 -0
  57. package/dist/esm-node/constant.js +6 -0
  58. package/dist/esm-node/interfaces/bundler.js +0 -0
  59. package/dist/esm-node/logger.js +7 -0
  60. package/dist/esm-node/react/index.js +29 -0
  61. package/dist/esm-node/runtime/index.js +1 -0
  62. package/dist/esm-node/server/fileCache.js +49 -0
  63. package/dist/esm-node/server/fileCache.spec.js +27 -0
  64. package/dist/esm-node/server/index.js +34 -0
  65. package/dist/esm-node/server/staticMiddleware.js +43 -0
  66. package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
  67. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
  68. package/dist/esm-node/ssr-runtime/devPlugin.js +37 -0
  69. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +17 -0
  70. package/dist/esm-node/types/index.js +0 -0
  71. package/dist/types/cli/configPlugin.d.ts +21 -0
  72. package/dist/types/cli/configPlugin.spec.d.ts +1 -0
  73. package/dist/types/cli/index.d.ts +6 -0
  74. package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
  75. package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
  76. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
  77. package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
  78. package/dist/types/cli/ssrPlugin.d.ts +6 -0
  79. package/dist/types/cli/utils.d.ts +6 -0
  80. package/dist/types/constant.d.ts +2 -0
  81. package/dist/types/interfaces/bundler.d.ts +18 -0
  82. package/dist/types/logger.d.ts +2 -0
  83. package/dist/types/react/index.d.ts +10 -0
  84. package/dist/types/runtime/index.d.ts +1 -0
  85. package/dist/types/server/fileCache.d.ts +14 -0
  86. package/dist/types/server/fileCache.spec.d.ts +1 -0
  87. package/dist/types/server/index.d.ts +4 -0
  88. package/dist/types/server/staticMiddleware.d.ts +6 -0
  89. package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
  90. package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
  91. package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
  92. package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +4 -0
  93. package/dist/types/types/index.d.ts +24 -0
  94. package/package.json +37 -12
@@ -0,0 +1,328 @@
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, beforeEach } from "vitest";
4
+ import { createStaticMiddleware } from "./staticMiddleware";
5
+ vi.mock("fs-extra", function() {
6
+ return {
7
+ default: {
8
+ pathExists: vi.fn()
9
+ }
10
+ };
11
+ });
12
+ vi.mock("./fileCache", function() {
13
+ return {
14
+ fileCache: {
15
+ getFile: vi.fn()
16
+ }
17
+ };
18
+ });
19
+ import fs from "fs-extra";
20
+ import { fileCache } from "./fileCache";
21
+ describe("staticMiddleware", function() {
22
+ var middleware;
23
+ var mockContext;
24
+ var nextSpy;
25
+ beforeEach(function() {
26
+ vi.clearAllMocks();
27
+ middleware = createStaticMiddleware({
28
+ assetPrefix: "",
29
+ pwd: "/test/path"
30
+ });
31
+ nextSpy = vi.fn();
32
+ mockContext = {
33
+ req: {
34
+ path: ""
35
+ },
36
+ header: vi.fn(),
37
+ body: vi.fn()
38
+ };
39
+ });
40
+ describe("file extension filtering", function() {
41
+ it("should call next() for non-js files", /* @__PURE__ */ _async_to_generator(function() {
42
+ return _ts_generator(this, function(_state) {
43
+ switch (_state.label) {
44
+ case 0:
45
+ mockContext.req.path = "/bundles/test.css";
46
+ return [
47
+ 4,
48
+ middleware(mockContext, nextSpy)
49
+ ];
50
+ case 1:
51
+ _state.sent();
52
+ expect(nextSpy).toHaveBeenCalledOnce();
53
+ expect(mockContext.header).not.toHaveBeenCalled();
54
+ expect(mockContext.body).not.toHaveBeenCalled();
55
+ return [
56
+ 2
57
+ ];
58
+ }
59
+ });
60
+ }));
61
+ it("should call next() for files without extension", /* @__PURE__ */ _async_to_generator(function() {
62
+ return _ts_generator(this, function(_state) {
63
+ switch (_state.label) {
64
+ case 0:
65
+ mockContext.req.path = "/bundles/test";
66
+ return [
67
+ 4,
68
+ middleware(mockContext, nextSpy)
69
+ ];
70
+ case 1:
71
+ _state.sent();
72
+ expect(nextSpy).toHaveBeenCalledOnce();
73
+ expect(mockContext.header).not.toHaveBeenCalled();
74
+ expect(mockContext.body).not.toHaveBeenCalled();
75
+ return [
76
+ 2
77
+ ];
78
+ }
79
+ });
80
+ }));
81
+ it("should process .js files", /* @__PURE__ */ _async_to_generator(function() {
82
+ return _ts_generator(this, function(_state) {
83
+ switch (_state.label) {
84
+ case 0:
85
+ mockContext.req.path = "/bundles/test.js";
86
+ fs.pathExists.mockResolvedValue(false);
87
+ return [
88
+ 4,
89
+ middleware(mockContext, nextSpy)
90
+ ];
91
+ case 1:
92
+ _state.sent();
93
+ expect(fs.pathExists).toHaveBeenCalled();
94
+ return [
95
+ 2
96
+ ];
97
+ }
98
+ });
99
+ }));
100
+ });
101
+ describe("asset prefix filtering", function() {
102
+ it("should call next() for paths not starting with /bundles", /* @__PURE__ */ _async_to_generator(function() {
103
+ return _ts_generator(this, function(_state) {
104
+ switch (_state.label) {
105
+ case 0:
106
+ mockContext.req.path = "/assets/test.js";
107
+ return [
108
+ 4,
109
+ middleware(mockContext, nextSpy)
110
+ ];
111
+ case 1:
112
+ _state.sent();
113
+ expect(nextSpy).toHaveBeenCalledOnce();
114
+ expect(fs.pathExists).not.toHaveBeenCalled();
115
+ expect(mockContext.header).not.toHaveBeenCalled();
116
+ expect(mockContext.body).not.toHaveBeenCalled();
117
+ return [
118
+ 2
119
+ ];
120
+ }
121
+ });
122
+ }));
123
+ it("should call next() for root path", /* @__PURE__ */ _async_to_generator(function() {
124
+ return _ts_generator(this, function(_state) {
125
+ switch (_state.label) {
126
+ case 0:
127
+ mockContext.req.path = "/test.js";
128
+ return [
129
+ 4,
130
+ middleware(mockContext, nextSpy)
131
+ ];
132
+ case 1:
133
+ _state.sent();
134
+ expect(nextSpy).toHaveBeenCalledOnce();
135
+ expect(fs.pathExists).not.toHaveBeenCalled();
136
+ return [
137
+ 2
138
+ ];
139
+ }
140
+ });
141
+ }));
142
+ it("should process paths starting with /bundles", /* @__PURE__ */ _async_to_generator(function() {
143
+ return _ts_generator(this, function(_state) {
144
+ switch (_state.label) {
145
+ case 0:
146
+ mockContext.req.path = "/bundles/test.js";
147
+ fs.pathExists.mockResolvedValue(false);
148
+ return [
149
+ 4,
150
+ middleware(mockContext, nextSpy)
151
+ ];
152
+ case 1:
153
+ _state.sent();
154
+ expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
155
+ return [
156
+ 2
157
+ ];
158
+ }
159
+ });
160
+ }));
161
+ });
162
+ describe("file existence check", function() {
163
+ it("should call next() when file does not exist", /* @__PURE__ */ _async_to_generator(function() {
164
+ return _ts_generator(this, function(_state) {
165
+ switch (_state.label) {
166
+ case 0:
167
+ mockContext.req.path = "/bundles/nonexistent.js";
168
+ fs.pathExists.mockResolvedValue(false);
169
+ return [
170
+ 4,
171
+ middleware(mockContext, nextSpy)
172
+ ];
173
+ case 1:
174
+ _state.sent();
175
+ expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/nonexistent.js");
176
+ expect(nextSpy).toHaveBeenCalledOnce();
177
+ expect(fileCache.getFile).not.toHaveBeenCalled();
178
+ expect(mockContext.header).not.toHaveBeenCalled();
179
+ expect(mockContext.body).not.toHaveBeenCalled();
180
+ return [
181
+ 2
182
+ ];
183
+ }
184
+ });
185
+ }));
186
+ it("should proceed to file cache when file exists", /* @__PURE__ */ _async_to_generator(function() {
187
+ return _ts_generator(this, function(_state) {
188
+ switch (_state.label) {
189
+ case 0:
190
+ mockContext.req.path = "/bundles/existing.js";
191
+ fs.pathExists.mockResolvedValue(true);
192
+ fileCache.getFile.mockResolvedValue(null);
193
+ return [
194
+ 4,
195
+ middleware(mockContext, nextSpy)
196
+ ];
197
+ case 1:
198
+ _state.sent();
199
+ expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/existing.js");
200
+ expect(fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/existing.js");
201
+ return [
202
+ 2
203
+ ];
204
+ }
205
+ });
206
+ }));
207
+ });
208
+ describe("successful file serving", function() {
209
+ it("should serve file content with correct headers", /* @__PURE__ */ _async_to_generator(function() {
210
+ var mockFileContent, mockFileResult, result;
211
+ return _ts_generator(this, function(_state) {
212
+ switch (_state.label) {
213
+ case 0:
214
+ mockFileContent = 'console.log("test");';
215
+ mockFileResult = {
216
+ content: mockFileContent,
217
+ lastModified: Date.now()
218
+ };
219
+ mockContext.req.path = "/bundles/app.js";
220
+ fs.pathExists.mockResolvedValue(true);
221
+ fileCache.getFile.mockResolvedValue(mockFileResult);
222
+ mockContext.body.mockReturnValue("response");
223
+ return [
224
+ 4,
225
+ middleware(mockContext, nextSpy)
226
+ ];
227
+ case 1:
228
+ result = _state.sent();
229
+ expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/app.js");
230
+ expect(fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/app.js");
231
+ expect(nextSpy).not.toHaveBeenCalled();
232
+ expect(mockContext.header).toHaveBeenCalledWith("Content-Type", "application/javascript");
233
+ expect(mockContext.header).toHaveBeenCalledWith("Content-Length", String(mockFileResult.content.length));
234
+ expect(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
235
+ expect(result).toBe("response");
236
+ return [
237
+ 2
238
+ ];
239
+ }
240
+ });
241
+ }));
242
+ it("should handle empty file content", /* @__PURE__ */ _async_to_generator(function() {
243
+ var mockFileResult, result;
244
+ return _ts_generator(this, function(_state) {
245
+ switch (_state.label) {
246
+ case 0:
247
+ mockFileResult = {
248
+ content: "",
249
+ lastModified: Date.now()
250
+ };
251
+ mockContext.req.path = "/bundles/empty.js";
252
+ fs.pathExists.mockResolvedValue(true);
253
+ fileCache.getFile.mockResolvedValue(mockFileResult);
254
+ mockContext.body.mockReturnValue("empty-response");
255
+ return [
256
+ 4,
257
+ middleware(mockContext, nextSpy)
258
+ ];
259
+ case 1:
260
+ result = _state.sent();
261
+ expect(mockContext.header).toHaveBeenCalledWith("Content-Length", "0");
262
+ expect(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
263
+ expect(result).toBe("empty-response");
264
+ expect(nextSpy).not.toHaveBeenCalled();
265
+ return [
266
+ 2
267
+ ];
268
+ }
269
+ });
270
+ }));
271
+ });
272
+ describe("asset prefix handling", function() {
273
+ it("should handle custom asset prefix correctly", /* @__PURE__ */ _async_to_generator(function() {
274
+ var customMiddleware;
275
+ return _ts_generator(this, function(_state) {
276
+ switch (_state.label) {
277
+ case 0:
278
+ customMiddleware = createStaticMiddleware({
279
+ assetPrefix: "/custom-prefix",
280
+ pwd: "/test/path"
281
+ });
282
+ mockContext.req.path = "/bundles/test.js";
283
+ return [
284
+ 4,
285
+ customMiddleware(mockContext, nextSpy)
286
+ ];
287
+ case 1:
288
+ _state.sent();
289
+ expect(nextSpy).toHaveBeenCalledOnce();
290
+ expect(mockContext.header).not.toHaveBeenCalled();
291
+ expect(mockContext.body).not.toHaveBeenCalled();
292
+ return [
293
+ 2
294
+ ];
295
+ }
296
+ });
297
+ }));
298
+ it("should handle asset prefix removal correctly", /* @__PURE__ */ _async_to_generator(function() {
299
+ var customMiddleware, mockFileResult;
300
+ return _ts_generator(this, function(_state) {
301
+ switch (_state.label) {
302
+ case 0:
303
+ customMiddleware = createStaticMiddleware({
304
+ assetPrefix: "/prefix",
305
+ pwd: "/test/path"
306
+ });
307
+ mockFileResult = {
308
+ content: "test content",
309
+ lastModified: Date.now()
310
+ };
311
+ mockContext.req.path = "/prefix/bundles/test.js";
312
+ fs.pathExists.mockResolvedValue(true);
313
+ fileCache.getFile.mockResolvedValue(mockFileResult);
314
+ return [
315
+ 4,
316
+ customMiddleware(mockContext, nextSpy)
317
+ ];
318
+ case 1:
319
+ _state.sent();
320
+ expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
321
+ return [
322
+ 2
323
+ ];
324
+ }
325
+ });
326
+ }));
327
+ });
328
+ });
@@ -0,0 +1,26 @@
1
+ import { _ as _tagged_template_literal } from "@swc/helpers/_/_tagged_template_literal";
2
+ function _templateObject() {
3
+ var data = _tagged_template_literal([
4
+ "\n if(",
5
+ "){\n location.reload();\n }\n "
6
+ ]);
7
+ _templateObject = function _templateObject2() {
8
+ return data;
9
+ };
10
+ return data;
11
+ }
12
+ import { jsx as _jsx } from "react/jsx-runtime";
13
+ function SSRLiveReload() {
14
+ if (process.env.NODE_ENV !== "development") {
15
+ return null;
16
+ }
17
+ return /* @__PURE__ */ _jsx("script", {
18
+ suppressHydrationWarning: true,
19
+ dangerouslySetInnerHTML: {
20
+ __html: String.raw(_templateObject(), globalThis.shouldUpdate)
21
+ }
22
+ });
23
+ }
24
+ export {
25
+ SSRLiveReload
26
+ };
@@ -0,0 +1,73 @@
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 _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
5
+ import { SSRLiveReload } from "./SSRLiveReload";
6
+ import { flushDataFetch } from "@module-federation/bridge-react/lazy-utils";
7
+ var mfSSRDevPlugin = function() {
8
+ return {
9
+ name: "@module-federation/modern-js",
10
+ setup: function(api) {
11
+ api.onBeforeRender(/* @__PURE__ */ _async_to_generator(function() {
12
+ var nodeUtils, shouldUpdate;
13
+ return _ts_generator(this, function(_state) {
14
+ switch (_state.label) {
15
+ case 0:
16
+ if (typeof window !== "undefined") {
17
+ return [
18
+ 2
19
+ ];
20
+ }
21
+ globalThis.shouldUpdate = false;
22
+ return [
23
+ 4,
24
+ import("@module-federation/node/utils")
25
+ ];
26
+ case 1:
27
+ nodeUtils = _state.sent();
28
+ return [
29
+ 4,
30
+ nodeUtils.revalidate()
31
+ ];
32
+ case 2:
33
+ shouldUpdate = _state.sent();
34
+ console.log("shouldUpdate: ", shouldUpdate);
35
+ if (!shouldUpdate)
36
+ return [
37
+ 3,
38
+ 4
39
+ ];
40
+ console.log("should RELOAD", shouldUpdate);
41
+ return [
42
+ 4,
43
+ nodeUtils.flushChunks()
44
+ ];
45
+ case 3:
46
+ _state.sent();
47
+ flushDataFetch();
48
+ globalThis.shouldUpdate = true;
49
+ _state.label = 4;
50
+ case 4:
51
+ return [
52
+ 2
53
+ ];
54
+ }
55
+ });
56
+ }));
57
+ api.wrapRoot(function(App) {
58
+ var AppWrapper = function(props) {
59
+ return /* @__PURE__ */ _jsxs(_Fragment, {
60
+ children: [
61
+ /* @__PURE__ */ _jsx(SSRLiveReload, {}),
62
+ /* @__PURE__ */ _jsx(App, _object_spread({}, props))
63
+ ]
64
+ });
65
+ };
66
+ return AppWrapper;
67
+ });
68
+ }
69
+ };
70
+ };
71
+ export {
72
+ mfSSRDevPlugin
73
+ };
@@ -0,0 +1,35 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { callDataFetch, injectDataFetch } from "@module-federation/bridge-react/data-fetch-utils";
4
+ import { setSSREnv } from "@module-federation/bridge-react/lazy-utils";
5
+ var injectDataFetchFunctionPlugin = function(param) {
6
+ var fetchServerQuery = param.fetchServerQuery;
7
+ return {
8
+ name: "@module-federation/inject-data-fetch-function-plugin",
9
+ setup: function(api) {
10
+ api.onBeforeRender(/* @__PURE__ */ _async_to_generator(function() {
11
+ return _ts_generator(this, function(_state) {
12
+ switch (_state.label) {
13
+ case 0:
14
+ setSSREnv({
15
+ fetchServerQuery
16
+ });
17
+ injectDataFetch();
18
+ return [
19
+ 4,
20
+ callDataFetch()
21
+ ];
22
+ case 1:
23
+ _state.sent();
24
+ return [
25
+ 2
26
+ ];
27
+ }
28
+ });
29
+ }));
30
+ }
31
+ };
32
+ };
33
+ export {
34
+ injectDataFetchFunctionPlugin
35
+ };
File without changes