@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,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 @@
1
+ export * from "./plugin";
@@ -0,0 +1,70 @@
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
+ var mfSSRPlugin = function() {
7
+ return {
8
+ name: "@module-federation/modern-js",
9
+ setup: function(api) {
10
+ api.onBeforeRender(/* @__PURE__ */ _async_to_generator(function() {
11
+ var nodeUtils, shouldUpdate;
12
+ return _ts_generator(this, function(_state) {
13
+ switch (_state.label) {
14
+ case 0:
15
+ if (typeof window !== "undefined") {
16
+ return [
17
+ 2
18
+ ];
19
+ }
20
+ globalThis.shouldUpdate = false;
21
+ return [
22
+ 4,
23
+ import("@module-federation/node/utils")
24
+ ];
25
+ case 1:
26
+ nodeUtils = _state.sent();
27
+ return [
28
+ 4,
29
+ nodeUtils.revalidate()
30
+ ];
31
+ case 2:
32
+ shouldUpdate = _state.sent();
33
+ if (!shouldUpdate)
34
+ return [
35
+ 3,
36
+ 4
37
+ ];
38
+ console.log("should RELOAD", shouldUpdate);
39
+ return [
40
+ 4,
41
+ nodeUtils.flushChunks()
42
+ ];
43
+ case 3:
44
+ _state.sent();
45
+ globalThis.shouldUpdate = true;
46
+ _state.label = 4;
47
+ case 4:
48
+ return [
49
+ 2
50
+ ];
51
+ }
52
+ });
53
+ }));
54
+ api.wrapRoot(function(App) {
55
+ var AppWrapper = function(props) {
56
+ return /* @__PURE__ */ _jsxs(_Fragment, {
57
+ children: [
58
+ /* @__PURE__ */ _jsx(SSRLiveReload, {}),
59
+ /* @__PURE__ */ _jsx(App, _object_spread({}, props))
60
+ ]
61
+ });
62
+ };
63
+ return AppWrapper;
64
+ });
65
+ }
66
+ };
67
+ };
68
+ export {
69
+ mfSSRPlugin
70
+ };
File without changes