@module-federation/modern-js 0.14.2 → 0.15.0
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/dist/LICENSE +21 -0
- package/dist/cjs/cli/configPlugin.js +381 -0
- package/dist/cjs/cli/configPlugin.spec.js +114 -0
- package/dist/cjs/cli/index.js +95 -0
- package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +98 -0
- package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +44 -0
- package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +82 -0
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +43 -0
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +189 -0
- package/dist/cjs/cli/ssrPlugin.js +236 -0
- package/dist/cjs/cli/utils.js +90 -0
- package/dist/cjs/constant.js +78 -0
- package/dist/cjs/interfaces/bundler.js +16 -0
- package/dist/cjs/interfaces/global.js +16 -0
- package/dist/cjs/logger.js +27 -0
- package/dist/cjs/runtime/AwaitDataFetch.js +144 -0
- package/dist/cjs/runtime/createRemoteComponent.js +327 -0
- package/dist/cjs/runtime/index.js +50 -0
- package/dist/cjs/runtime/wrapNoSSR.js +35 -0
- package/dist/cjs/server/fileCache.js +84 -0
- package/dist/cjs/server/fileCache.spec.js +28 -0
- package/dist/cjs/server/index.js +58 -0
- package/dist/cjs/server/staticMiddleware.js +77 -0
- package/dist/cjs/server/staticMiddleware.spec.js +185 -0
- package/dist/cjs/ssr-runtime/SSRLiveReload.js +43 -0
- package/dist/cjs/ssr-runtime/devPlugin.js +71 -0
- package/dist/cjs/ssr-runtime/downgrade.js +114 -0
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +136 -0
- package/dist/cjs/types/index.js +16 -0
- package/dist/cjs/utils/dataFetch.js +211 -0
- package/dist/cjs/utils/index.js +54 -0
- package/dist/esm/cli/configPlugin.js +388 -0
- package/dist/esm/cli/configPlugin.spec.js +110 -0
- package/dist/esm/cli/index.js +90 -0
- package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +74 -0
- package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +16 -0
- package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +74 -0
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +25 -0
- package/dist/esm/cli/server/data-fetch-server-plugin.js +237 -0
- package/dist/esm/cli/ssrPlugin.js +219 -0
- package/dist/esm/cli/utils.js +53 -0
- package/dist/esm/constant.js +42 -0
- package/dist/esm/interfaces/bundler.js +0 -0
- package/dist/esm/interfaces/global.js +0 -0
- package/dist/esm/logger.js +7 -0
- package/dist/esm/runtime/AwaitDataFetch.js +131 -0
- package/dist/esm/runtime/createRemoteComponent.js +417 -0
- package/dist/esm/runtime/index.js +22 -0
- package/dist/esm/runtime/wrapNoSSR.js +12 -0
- package/dist/esm/server/fileCache.js +98 -0
- package/dist/esm/server/fileCache.spec.js +50 -0
- package/dist/esm/server/index.js +36 -0
- package/dist/esm/server/staticMiddleware.js +81 -0
- package/dist/esm/server/staticMiddleware.spec.js +328 -0
- package/dist/esm/ssr-runtime/SSRLiveReload.js +26 -0
- package/dist/esm/ssr-runtime/devPlugin.js +73 -0
- package/dist/esm/ssr-runtime/downgrade.js +150 -0
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +177 -0
- package/dist/esm/types/index.js +0 -0
- package/dist/esm/utils/dataFetch.js +237 -0
- package/dist/esm/utils/index.js +28 -0
- package/dist/esm-node/cli/configPlugin.js +340 -0
- package/dist/esm-node/cli/configPlugin.spec.js +91 -0
- package/dist/esm-node/cli/index.js +70 -0
- package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +68 -0
- package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +14 -0
- package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +62 -0
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +23 -0
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +159 -0
- package/dist/esm-node/cli/ssrPlugin.js +200 -0
- package/dist/esm-node/cli/utils.js +53 -0
- package/dist/esm-node/constant.js +42 -0
- package/dist/esm-node/interfaces/bundler.js +0 -0
- package/dist/esm-node/interfaces/global.js +0 -0
- package/dist/esm-node/logger.js +7 -0
- package/dist/esm-node/runtime/AwaitDataFetch.js +109 -0
- package/dist/esm-node/runtime/createRemoteComponent.js +291 -0
- package/dist/esm-node/runtime/index.js +22 -0
- package/dist/esm-node/runtime/wrapNoSSR.js +11 -0
- package/dist/esm-node/server/fileCache.js +49 -0
- package/dist/esm-node/server/fileCache.spec.js +27 -0
- package/dist/esm-node/server/index.js +34 -0
- package/dist/esm-node/server/staticMiddleware.js +43 -0
- package/dist/esm-node/server/staticMiddleware.spec.js +162 -0
- package/dist/esm-node/ssr-runtime/SSRLiveReload.js +19 -0
- package/dist/esm-node/ssr-runtime/devPlugin.js +37 -0
- package/dist/esm-node/ssr-runtime/downgrade.js +88 -0
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +101 -0
- package/dist/esm-node/types/index.js +0 -0
- package/dist/esm-node/utils/dataFetch.js +166 -0
- package/dist/esm-node/utils/index.js +27 -0
- package/dist/types/cli/configPlugin.d.ts +21 -0
- package/dist/types/cli/configPlugin.spec.d.ts +1 -0
- package/dist/types/cli/index.d.ts +6 -0
- package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts +3 -0
- package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +3 -0
- package/dist/types/cli/server/data-fetch-server-plugin.d.ts +3 -0
- package/dist/types/cli/ssrPlugin.d.ts +6 -0
- package/dist/types/cli/utils.d.ts +6 -0
- package/dist/types/constant.d.ts +25 -0
- package/dist/types/interfaces/bundler.d.ts +18 -0
- package/dist/types/interfaces/global.d.ts +27 -0
- package/dist/types/logger.d.ts +2 -0
- package/dist/types/runtime/AwaitDataFetch.d.ts +19 -0
- package/dist/types/runtime/createRemoteComponent.d.ts +26 -0
- package/dist/types/runtime/index.d.ts +10 -0
- package/dist/types/runtime/wrapNoSSR.d.ts +9 -0
- package/dist/types/server/fileCache.d.ts +14 -0
- package/dist/types/server/fileCache.spec.d.ts +1 -0
- package/dist/types/server/index.d.ts +4 -0
- package/dist/types/server/staticMiddleware.d.ts +6 -0
- package/dist/types/server/staticMiddleware.spec.d.ts +1 -0
- package/dist/types/ssr-runtime/SSRLiveReload.d.ts +1 -0
- package/dist/types/ssr-runtime/devPlugin.d.ts +2 -0
- package/dist/types/ssr-runtime/downgrade.d.ts +4 -0
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +7 -0
- package/dist/types/types/index.d.ts +24 -0
- package/dist/types/utils/dataFetch.d.ts +26 -0
- package/dist/types/utils/index.d.ts +15 -0
- package/package.json +37 -12
|
@@ -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
|
+
};
|
|
@@ -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 "../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,150 @@
|
|
|
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 _type_of } from "@swc/helpers/_/_type_of";
|
|
5
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
6
|
+
import { DATA_FETCH_QUERY, DOWNGRADE_KEY } from "../constant";
|
|
7
|
+
import { MF_DATA_FETCH_STATUS, MF_DATA_FETCH_TYPE } from "../constant";
|
|
8
|
+
import { getDataFetchMap } from "../utils";
|
|
9
|
+
function getDowngradeTag() {
|
|
10
|
+
return globalThis[DOWNGRADE_KEY];
|
|
11
|
+
}
|
|
12
|
+
function callAllDowngrade() {
|
|
13
|
+
var dataFetchMap = getDataFetchMap();
|
|
14
|
+
if (!dataFetchMap) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
Object.keys(dataFetchMap).forEach(function(key) {
|
|
18
|
+
callDowngrade(key);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function callDowngrade(id, params, remoteInfo) {
|
|
22
|
+
return _callDowngrade.apply(this, arguments);
|
|
23
|
+
}
|
|
24
|
+
function _callDowngrade() {
|
|
25
|
+
_callDowngrade = _async_to_generator(function(id, params, remoteInfo) {
|
|
26
|
+
var dataFetchMap, mfDataFetch, promise, res, rej, dataFetchType, currentUrl, fetchServerQuery, fetchUrl, data, e;
|
|
27
|
+
return _ts_generator(this, function(_state) {
|
|
28
|
+
switch (_state.label) {
|
|
29
|
+
case 0:
|
|
30
|
+
dataFetchMap = getDataFetchMap();
|
|
31
|
+
if (!dataFetchMap) {
|
|
32
|
+
return [
|
|
33
|
+
2
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
mfDataFetch = dataFetchMap[id];
|
|
37
|
+
if (!((mfDataFetch === null || mfDataFetch === void 0 ? void 0 : mfDataFetch[2]) === MF_DATA_FETCH_STATUS.AWAIT))
|
|
38
|
+
return [
|
|
39
|
+
3,
|
|
40
|
+
6
|
|
41
|
+
];
|
|
42
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADING;
|
|
43
|
+
if (mfDataFetch[1]) {
|
|
44
|
+
promise = mfDataFetch[1][0];
|
|
45
|
+
res = mfDataFetch[1][1];
|
|
46
|
+
rej = mfDataFetch[1][2];
|
|
47
|
+
} else {
|
|
48
|
+
promise = new Promise(function(resolve, reject) {
|
|
49
|
+
res = resolve;
|
|
50
|
+
rej = reject;
|
|
51
|
+
});
|
|
52
|
+
mfDataFetch[1] = [
|
|
53
|
+
promise,
|
|
54
|
+
res,
|
|
55
|
+
rej
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
dataFetchType = mfDataFetch[0][1];
|
|
59
|
+
if (!(dataFetchType === MF_DATA_FETCH_TYPE.FETCH_CLIENT))
|
|
60
|
+
return [
|
|
61
|
+
3,
|
|
62
|
+
1
|
|
63
|
+
];
|
|
64
|
+
try {
|
|
65
|
+
mfDataFetch[0][0]().then(function(getDataFetchFn) {
|
|
66
|
+
return getDataFetchFn(_object_spread_props(_object_spread({}, params), {
|
|
67
|
+
isDowngrade: true
|
|
68
|
+
})).then(function(data2) {
|
|
69
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
70
|
+
res(data2);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
} catch (e2) {
|
|
74
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
75
|
+
rej(e2);
|
|
76
|
+
}
|
|
77
|
+
return [
|
|
78
|
+
3,
|
|
79
|
+
5
|
|
80
|
+
];
|
|
81
|
+
case 1:
|
|
82
|
+
if (!(dataFetchType === MF_DATA_FETCH_TYPE.FETCH_SERVER))
|
|
83
|
+
return [
|
|
84
|
+
3,
|
|
85
|
+
5
|
|
86
|
+
];
|
|
87
|
+
_state.label = 2;
|
|
88
|
+
case 2:
|
|
89
|
+
_state.trys.push([
|
|
90
|
+
2,
|
|
91
|
+
4,
|
|
92
|
+
,
|
|
93
|
+
5
|
|
94
|
+
]);
|
|
95
|
+
currentUrl = new URL(window.location.href);
|
|
96
|
+
currentUrl.searchParams.set(DATA_FETCH_QUERY, encodeURIComponent(id));
|
|
97
|
+
if (params) {
|
|
98
|
+
currentUrl.searchParams.set("params", encodeURIComponent(JSON.stringify(params)));
|
|
99
|
+
}
|
|
100
|
+
if (remoteInfo) {
|
|
101
|
+
currentUrl.searchParams.set("remoteInfo", encodeURIComponent(JSON.stringify(remoteInfo)));
|
|
102
|
+
}
|
|
103
|
+
fetchServerQuery = globalThis.FEDERATION_SERVER_QUERY;
|
|
104
|
+
if (fetchServerQuery && (typeof fetchServerQuery === "undefined" ? "undefined" : _type_of(fetchServerQuery)) === "object") {
|
|
105
|
+
Object.keys(fetchServerQuery).forEach(function(key) {
|
|
106
|
+
currentUrl.searchParams.set(key, JSON.stringify(fetchServerQuery[key]));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
fetchUrl = currentUrl.toString();
|
|
110
|
+
return [
|
|
111
|
+
4,
|
|
112
|
+
fetch(fetchUrl).then(function(res2) {
|
|
113
|
+
return res2.json();
|
|
114
|
+
})
|
|
115
|
+
];
|
|
116
|
+
case 3:
|
|
117
|
+
data = _state.sent();
|
|
118
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.LOADED;
|
|
119
|
+
res(data);
|
|
120
|
+
return [
|
|
121
|
+
3,
|
|
122
|
+
5
|
|
123
|
+
];
|
|
124
|
+
case 4:
|
|
125
|
+
e = _state.sent();
|
|
126
|
+
mfDataFetch[2] = MF_DATA_FETCH_STATUS.ERROR;
|
|
127
|
+
rej(e);
|
|
128
|
+
return [
|
|
129
|
+
3,
|
|
130
|
+
5
|
|
131
|
+
];
|
|
132
|
+
case 5:
|
|
133
|
+
return [
|
|
134
|
+
2,
|
|
135
|
+
promise
|
|
136
|
+
];
|
|
137
|
+
case 6:
|
|
138
|
+
return [
|
|
139
|
+
2
|
|
140
|
+
];
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
return _callDowngrade.apply(this, arguments);
|
|
145
|
+
}
|
|
146
|
+
export {
|
|
147
|
+
callAllDowngrade,
|
|
148
|
+
callDowngrade,
|
|
149
|
+
getDowngradeTag
|
|
150
|
+
};
|