@module-federation/modern-js 0.0.0-next-20250526074234 → 0.0.0-next-20250527094526
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/cjs/cli/configPlugin.js +6 -8
- package/dist/cjs/cli/configPlugin.spec.js +1 -3
- package/dist/cjs/{runtime/wrapNoSSR.js → cli/constant.js} +6 -13
- package/dist/cjs/cli/index.js +9 -3
- package/dist/cjs/{logger.js → cli/logger.js} +1 -1
- package/dist/cjs/cli/manifest.js +3 -3
- package/dist/cjs/cli/ssrPlugin.js +15 -34
- package/dist/cjs/cli/utils.js +0 -5
- package/dist/cjs/constant.js +2 -49
- package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
- package/dist/cjs/runtime/index.js +3 -15
- 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/{interfaces/global.js → ssr-runtime/index.js} +8 -2
- package/dist/cjs/ssr-runtime/{devPlugin.js → plugin.js} +6 -9
- package/dist/esm/cli/configPlugin.js +7 -9
- package/dist/esm/cli/configPlugin.spec.js +1 -3
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/index.js +10 -3
- package/dist/esm/{logger.js → cli/logger.js} +1 -1
- package/dist/esm/cli/manifest.js +1 -1
- package/dist/esm/cli/ssrPlugin.js +8 -28
- package/dist/esm/cli/utils.js +0 -4
- package/dist/esm/constant.js +1 -37
- package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
- package/dist/esm/runtime/index.js +3 -13
- 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/index.js +1 -0
- package/dist/esm/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
- package/dist/esm-node/cli/configPlugin.js +7 -9
- package/dist/esm-node/cli/configPlugin.spec.js +1 -3
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/index.js +9 -3
- package/dist/esm-node/{logger.js → cli/logger.js} +1 -1
- package/dist/esm-node/cli/manifest.js +1 -1
- package/dist/esm-node/cli/ssrPlugin.js +8 -27
- package/dist/esm-node/cli/utils.js +0 -4
- package/dist/esm-node/constant.js +1 -37
- package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
- package/dist/esm-node/runtime/index.js +3 -13
- 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/index.js +1 -0
- package/dist/esm-node/ssr-runtime/{devPlugin.js → plugin.js} +2 -5
- package/dist/types/cli/constant.d.ts +1 -0
- package/dist/types/cli/index.d.ts +0 -1
- package/dist/types/cli/utils.d.ts +0 -1
- package/dist/types/constant.d.ts +0 -23
- package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
- package/dist/types/runtime/index.d.ts +2 -7
- 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/index.d.ts +1 -0
- package/dist/types/ssr-runtime/plugin.d.ts +2 -0
- package/dist/types/types/index.d.ts +0 -2
- package/package.json +23 -39
- package/dist/cjs/cli/mfRuntimePlugins/auto-fetch-data.js +0 -98
- package/dist/cjs/cli/server/data-fetch-server-plugin.js +0 -192
- package/dist/cjs/runtime/AwaitDataFetch.js +0 -144
- package/dist/cjs/runtime/createRemoteComponent.js +0 -327
- package/dist/cjs/ssr-runtime/downgrade.js +0 -114
- package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -136
- package/dist/cjs/utils/dataFetch.js +0 -211
- package/dist/cjs/utils/index.js +0 -54
- package/dist/esm/cli/mfRuntimePlugins/auto-fetch-data.js +0 -74
- package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -240
- package/dist/esm/interfaces/global.js +0 -0
- package/dist/esm/runtime/AwaitDataFetch.js +0 -131
- package/dist/esm/runtime/createRemoteComponent.js +0 -417
- package/dist/esm/runtime/wrapNoSSR.js +0 -12
- package/dist/esm/ssr-runtime/downgrade.js +0 -150
- package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -177
- package/dist/esm/utils/dataFetch.js +0 -237
- package/dist/esm/utils/index.js +0 -28
- package/dist/esm-node/cli/mfRuntimePlugins/auto-fetch-data.js +0 -68
- package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -162
- package/dist/esm-node/interfaces/global.js +0 -0
- package/dist/esm-node/runtime/AwaitDataFetch.js +0 -109
- package/dist/esm-node/runtime/createRemoteComponent.js +0 -291
- package/dist/esm-node/runtime/wrapNoSSR.js +0 -11
- package/dist/esm-node/ssr-runtime/downgrade.js +0 -88
- package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -101
- package/dist/esm-node/utils/dataFetch.js +0 -166
- package/dist/esm-node/utils/index.js +0 -27
- package/dist/types/cli/mfRuntimePlugins/auto-fetch-data.d.ts +0 -3
- package/dist/types/cli/server/data-fetch-server-plugin.d.ts +0 -3
- package/dist/types/interfaces/global.d.ts +0 -27
- package/dist/types/runtime/AwaitDataFetch.d.ts +0 -19
- package/dist/types/runtime/createRemoteComponent.d.ts +0 -26
- package/dist/types/runtime/wrapNoSSR.d.ts +0 -9
- package/dist/types/ssr-runtime/devPlugin.d.ts +0 -2
- package/dist/types/ssr-runtime/downgrade.d.ts +0 -4
- package/dist/types/ssr-runtime/injectDataFetchFunctionPlugin.d.ts +0 -7
- package/dist/types/utils/dataFetch.d.ts +0 -26
- package/dist/types/utils/index.d.ts +0 -15
- /package/dist/types/{logger.d.ts → cli/logger.d.ts} +0 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { getInstance } from "@module-federation/enhanced/runtime";
|
|
4
|
+
import { ErrorBoundary } from "react-error-boundary";
|
|
5
|
+
function getLoadedRemoteInfos(instance, id) {
|
|
6
|
+
const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
|
|
7
|
+
if (!name) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const module = instance.moduleCache.get(name);
|
|
11
|
+
if (!module) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(module.remoteInfo);
|
|
15
|
+
return {
|
|
16
|
+
...module.remoteInfo,
|
|
17
|
+
snapshot: remoteSnapshot,
|
|
18
|
+
expose
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function getTargetModuleInfo(id) {
|
|
22
|
+
const instance = getInstance();
|
|
23
|
+
if (!instance) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
|
|
27
|
+
if (!loadedRemoteInfo) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const snapshot = loadedRemoteInfo.snapshot;
|
|
31
|
+
if (!snapshot) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
|
|
35
|
+
if (!publicPath) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const modules = "modules" in snapshot ? snapshot.modules : [];
|
|
39
|
+
const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
|
|
40
|
+
if (!targetModule) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
|
|
44
|
+
if (!remoteEntry) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
module: targetModule,
|
|
49
|
+
publicPath,
|
|
50
|
+
remoteEntry
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function collectSSRAssets(options) {
|
|
54
|
+
const { id, injectLink = true, injectScript = true } = typeof options === "string" ? {
|
|
55
|
+
id: options
|
|
56
|
+
} : options;
|
|
57
|
+
const links = [];
|
|
58
|
+
const scripts = [];
|
|
59
|
+
const instance = getInstance();
|
|
60
|
+
if (!instance || !injectLink && !injectScript) {
|
|
61
|
+
return [
|
|
62
|
+
...scripts,
|
|
63
|
+
...links
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
const moduleAndPublicPath = getTargetModuleInfo(id);
|
|
67
|
+
if (!moduleAndPublicPath) {
|
|
68
|
+
return [
|
|
69
|
+
...scripts,
|
|
70
|
+
...links
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
|
|
74
|
+
if (injectLink) {
|
|
75
|
+
[
|
|
76
|
+
...targetModule.assets.css.sync,
|
|
77
|
+
...targetModule.assets.css.async
|
|
78
|
+
].forEach((file, index) => {
|
|
79
|
+
links.push(/* @__PURE__ */ _jsx("link", {
|
|
80
|
+
href: `${publicPath}${file}`,
|
|
81
|
+
rel: "stylesheet",
|
|
82
|
+
type: "text/css"
|
|
83
|
+
}, `${file.split(".")[0]}_${index}`));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (injectScript) {
|
|
87
|
+
scripts.push(/* @__PURE__ */ _jsx("script", {
|
|
88
|
+
async: true,
|
|
89
|
+
src: `${publicPath}${remoteEntry}`,
|
|
90
|
+
crossOrigin: "anonymous"
|
|
91
|
+
}, remoteEntry.split(".")[0]));
|
|
92
|
+
[
|
|
93
|
+
...targetModule.assets.js.sync
|
|
94
|
+
].forEach((file, index) => {
|
|
95
|
+
scripts.push(/* @__PURE__ */ _jsx("script", {
|
|
96
|
+
async: true,
|
|
97
|
+
src: `${publicPath}${file}`,
|
|
98
|
+
crossOrigin: "anonymous"
|
|
99
|
+
}, `${file.split(".")[0]}_${index}`));
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return [
|
|
103
|
+
...scripts,
|
|
104
|
+
...links
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
function createRemoteSSRComponent(info) {
|
|
108
|
+
const exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
|
|
109
|
+
const LazyComponent = /* @__PURE__ */ React.lazy(async () => {
|
|
110
|
+
try {
|
|
111
|
+
const m = await info.loader();
|
|
112
|
+
if (!m) {
|
|
113
|
+
throw new Error("load remote failed");
|
|
114
|
+
}
|
|
115
|
+
const moduleId = m && m[Symbol.for("mf_module_id")];
|
|
116
|
+
const assets = collectSSRAssets({
|
|
117
|
+
id: moduleId
|
|
118
|
+
});
|
|
119
|
+
const Com = m[exportName];
|
|
120
|
+
if (exportName in m && typeof Com === "function") {
|
|
121
|
+
return {
|
|
122
|
+
default: (props) => /* @__PURE__ */ _jsxs(_Fragment, {
|
|
123
|
+
children: [
|
|
124
|
+
assets,
|
|
125
|
+
/* @__PURE__ */ _jsx(Com, {
|
|
126
|
+
...props
|
|
127
|
+
})
|
|
128
|
+
]
|
|
129
|
+
})
|
|
130
|
+
};
|
|
131
|
+
} else {
|
|
132
|
+
throw Error(`Make sure that ${moduleId} has the correct export when export is ${String(exportName)}`);
|
|
133
|
+
}
|
|
134
|
+
} catch (err) {
|
|
135
|
+
if (!info.fallback) {
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
const FallbackFunctionComponent = info.fallback;
|
|
139
|
+
const FallbackNode = /* @__PURE__ */ _jsx(FallbackFunctionComponent, {
|
|
140
|
+
error: err,
|
|
141
|
+
resetErrorBoundary: () => {
|
|
142
|
+
console.log('SSR mode not support "resetErrorBoundary" !');
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
default: () => FallbackNode
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return (props) => {
|
|
151
|
+
const { key, ...args } = props;
|
|
152
|
+
return /* @__PURE__ */ _jsx(ErrorBoundary, {
|
|
153
|
+
FallbackComponent: info.fallback,
|
|
154
|
+
children: /* @__PURE__ */ _jsx(React.Suspense, {
|
|
155
|
+
fallback: info.loading,
|
|
156
|
+
children: /* @__PURE__ */ _jsx(LazyComponent, {
|
|
157
|
+
...args
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
export {
|
|
164
|
+
collectSSRAssets,
|
|
165
|
+
createRemoteSSRComponent
|
|
166
|
+
};
|
|
@@ -1,22 +1,12 @@
|
|
|
1
1
|
export * from "@module-federation/enhanced/runtime";
|
|
2
|
-
import { ERROR_TYPE } from "../constant";
|
|
3
2
|
const kit = {
|
|
4
3
|
get createRemoteSSRComponent() {
|
|
5
|
-
return require("./
|
|
6
|
-
},
|
|
7
|
-
get createRemoteComponent() {
|
|
8
|
-
return require("./createRemoteComponent").createRemoteComponent;
|
|
4
|
+
return require("./createRemoteSSRComponent").createRemoteSSRComponent;
|
|
9
5
|
},
|
|
10
6
|
get collectSSRAssets() {
|
|
11
|
-
return require("./
|
|
12
|
-
},
|
|
13
|
-
get wrapNoSSR() {
|
|
14
|
-
return require("./wrapNoSSR").wrapNoSSR;
|
|
7
|
+
return require("./createRemoteSSRComponent").collectSSRAssets;
|
|
15
8
|
}
|
|
16
9
|
};
|
|
17
|
-
import { setSSREnv } from "../ssr-runtime/injectDataFetchFunctionPlugin";
|
|
18
10
|
export {
|
|
19
|
-
|
|
20
|
-
kit,
|
|
21
|
-
setSSREnv
|
|
11
|
+
kit
|
|
22
12
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import { LRUCache } from "lru-cache";
|
|
3
|
+
class FileCache {
|
|
4
|
+
/**
|
|
5
|
+
* Check if file exists and return file info
|
|
6
|
+
* @param filepath Path to the file
|
|
7
|
+
* @returns FileResult or null if file doesn't exist
|
|
8
|
+
*/
|
|
9
|
+
async getFile(filepath) {
|
|
10
|
+
if (!await fs.pathExists(filepath)) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const stat = await fs.lstat(filepath);
|
|
15
|
+
const currentModified = stat.mtimeMs;
|
|
16
|
+
const cachedEntry = this.cache.get(filepath);
|
|
17
|
+
if (cachedEntry && currentModified <= cachedEntry.lastModified) {
|
|
18
|
+
return {
|
|
19
|
+
content: cachedEntry.content,
|
|
20
|
+
lastModified: cachedEntry.lastModified
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const content = await fs.readFile(filepath, "utf-8");
|
|
24
|
+
const newEntry = {
|
|
25
|
+
content,
|
|
26
|
+
lastModified: currentModified
|
|
27
|
+
};
|
|
28
|
+
this.cache.set(filepath, newEntry, {
|
|
29
|
+
size: stat.size || content.length
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
content,
|
|
33
|
+
lastModified: currentModified
|
|
34
|
+
};
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
constructor() {
|
|
40
|
+
this.cache = new LRUCache({
|
|
41
|
+
maxSize: 200 * 1024 * 1024
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const fileCache = new FileCache();
|
|
46
|
+
export {
|
|
47
|
+
FileCache,
|
|
48
|
+
fileCache
|
|
49
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { it, expect, describe, vi, beforeAll } from "vitest";
|
|
2
|
+
import { FileCache } from "./fileCache";
|
|
3
|
+
beforeAll(() => {
|
|
4
|
+
vi.mock("fs-extra", () => ({
|
|
5
|
+
default: {
|
|
6
|
+
pathExists: () => {
|
|
7
|
+
return true;
|
|
8
|
+
},
|
|
9
|
+
lstat: () => {
|
|
10
|
+
return {
|
|
11
|
+
mtimeMs: Date.now(),
|
|
12
|
+
size: 4
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
readFile: () => {
|
|
16
|
+
return "test";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}));
|
|
20
|
+
});
|
|
21
|
+
describe("modern serve static file cache", async () => {
|
|
22
|
+
it("should cache file", async () => {
|
|
23
|
+
const cache = new FileCache();
|
|
24
|
+
const result = await cache.getFile("test.txt");
|
|
25
|
+
expect(result === null || result === void 0 ? void 0 : result.content).toBe("test");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createStaticMiddleware } from "./staticMiddleware";
|
|
2
|
+
const staticServePlugin = () => ({
|
|
3
|
+
name: "@modern-js/module-federation/server",
|
|
4
|
+
setup: (api) => {
|
|
5
|
+
api.onPrepare(() => {
|
|
6
|
+
var _config_output, _config_server;
|
|
7
|
+
console.log(process.env.NODE_ENV);
|
|
8
|
+
if (process.env.NODE_ENV === "development") {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const { middlewares } = api.getServerContext();
|
|
12
|
+
const config = api.getServerConfig();
|
|
13
|
+
const assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
|
|
14
|
+
if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const context = api.getServerContext();
|
|
18
|
+
const pwd = context.distDirectory;
|
|
19
|
+
const serverStaticMiddleware = createStaticMiddleware({
|
|
20
|
+
assetPrefix,
|
|
21
|
+
pwd
|
|
22
|
+
});
|
|
23
|
+
middlewares.push({
|
|
24
|
+
name: "module-federation-serve-manifest",
|
|
25
|
+
handler: serverStaticMiddleware
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
var server_default = staticServePlugin;
|
|
31
|
+
export {
|
|
32
|
+
server_default as default,
|
|
33
|
+
staticServePlugin
|
|
34
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileCache } from "./fileCache";
|
|
4
|
+
const bundlesAssetPrefix = "/bundles";
|
|
5
|
+
const removeHost = (url) => {
|
|
6
|
+
try {
|
|
7
|
+
const hasProtocol = url.includes("://");
|
|
8
|
+
const hasDomain = hasProtocol || url.startsWith("//");
|
|
9
|
+
const pathname = hasDomain ? new URL(hasProtocol ? url : `http:${url}`).pathname : url;
|
|
10
|
+
return pathname;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
return url;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const createStaticMiddleware = (options) => {
|
|
16
|
+
const { assetPrefix, pwd } = options;
|
|
17
|
+
return async (c, next) => {
|
|
18
|
+
const pathname = c.req.path;
|
|
19
|
+
if (path.extname(pathname) !== ".js") {
|
|
20
|
+
return next();
|
|
21
|
+
}
|
|
22
|
+
const prefixWithoutHost = removeHost(assetPrefix);
|
|
23
|
+
const prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
|
|
24
|
+
if (!pathname.startsWith(prefixWithBundle)) {
|
|
25
|
+
return next();
|
|
26
|
+
}
|
|
27
|
+
const pathnameWithoutPrefix = pathname.replace(prefixWithBundle, "");
|
|
28
|
+
const filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
|
|
29
|
+
if (!await fs.pathExists(filepath)) {
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
const fileResult = await fileCache.getFile(filepath);
|
|
33
|
+
if (!fileResult) {
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
c.header("Content-Type", "application/javascript");
|
|
37
|
+
c.header("Content-Length", String(fileResult.content.length));
|
|
38
|
+
return c.body(fileResult.content, 200);
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
createStaticMiddleware
|
|
43
|
+
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { it, expect, describe, vi, beforeEach } from "vitest";
|
|
2
|
+
import { createStaticMiddleware } from "./staticMiddleware";
|
|
3
|
+
vi.mock("fs-extra", () => ({
|
|
4
|
+
default: {
|
|
5
|
+
pathExists: vi.fn()
|
|
6
|
+
}
|
|
7
|
+
}));
|
|
8
|
+
vi.mock("./fileCache", () => ({
|
|
9
|
+
fileCache: {
|
|
10
|
+
getFile: vi.fn()
|
|
11
|
+
}
|
|
12
|
+
}));
|
|
13
|
+
import fs from "fs-extra";
|
|
14
|
+
import { fileCache } from "./fileCache";
|
|
15
|
+
describe("staticMiddleware", () => {
|
|
16
|
+
let middleware;
|
|
17
|
+
let mockContext;
|
|
18
|
+
let nextSpy;
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
vi.clearAllMocks();
|
|
21
|
+
middleware = createStaticMiddleware({
|
|
22
|
+
assetPrefix: "",
|
|
23
|
+
pwd: "/test/path"
|
|
24
|
+
});
|
|
25
|
+
nextSpy = vi.fn();
|
|
26
|
+
mockContext = {
|
|
27
|
+
req: {
|
|
28
|
+
path: ""
|
|
29
|
+
},
|
|
30
|
+
header: vi.fn(),
|
|
31
|
+
body: vi.fn()
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
describe("file extension filtering", () => {
|
|
35
|
+
it("should call next() for non-js files", async () => {
|
|
36
|
+
mockContext.req.path = "/bundles/test.css";
|
|
37
|
+
await middleware(mockContext, nextSpy);
|
|
38
|
+
expect(nextSpy).toHaveBeenCalledOnce();
|
|
39
|
+
expect(mockContext.header).not.toHaveBeenCalled();
|
|
40
|
+
expect(mockContext.body).not.toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
it("should call next() for files without extension", async () => {
|
|
43
|
+
mockContext.req.path = "/bundles/test";
|
|
44
|
+
await middleware(mockContext, nextSpy);
|
|
45
|
+
expect(nextSpy).toHaveBeenCalledOnce();
|
|
46
|
+
expect(mockContext.header).not.toHaveBeenCalled();
|
|
47
|
+
expect(mockContext.body).not.toHaveBeenCalled();
|
|
48
|
+
});
|
|
49
|
+
it("should process .js files", async () => {
|
|
50
|
+
mockContext.req.path = "/bundles/test.js";
|
|
51
|
+
fs.pathExists.mockResolvedValue(false);
|
|
52
|
+
await middleware(mockContext, nextSpy);
|
|
53
|
+
expect(fs.pathExists).toHaveBeenCalled();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe("asset prefix filtering", () => {
|
|
57
|
+
it("should call next() for paths not starting with /bundles", async () => {
|
|
58
|
+
mockContext.req.path = "/assets/test.js";
|
|
59
|
+
await middleware(mockContext, nextSpy);
|
|
60
|
+
expect(nextSpy).toHaveBeenCalledOnce();
|
|
61
|
+
expect(fs.pathExists).not.toHaveBeenCalled();
|
|
62
|
+
expect(mockContext.header).not.toHaveBeenCalled();
|
|
63
|
+
expect(mockContext.body).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
it("should call next() for root path", async () => {
|
|
66
|
+
mockContext.req.path = "/test.js";
|
|
67
|
+
await middleware(mockContext, nextSpy);
|
|
68
|
+
expect(nextSpy).toHaveBeenCalledOnce();
|
|
69
|
+
expect(fs.pathExists).not.toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
it("should process paths starting with /bundles", async () => {
|
|
72
|
+
mockContext.req.path = "/bundles/test.js";
|
|
73
|
+
fs.pathExists.mockResolvedValue(false);
|
|
74
|
+
await middleware(mockContext, nextSpy);
|
|
75
|
+
expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
describe("file existence check", () => {
|
|
79
|
+
it("should call next() when file does not exist", async () => {
|
|
80
|
+
mockContext.req.path = "/bundles/nonexistent.js";
|
|
81
|
+
fs.pathExists.mockResolvedValue(false);
|
|
82
|
+
await middleware(mockContext, nextSpy);
|
|
83
|
+
expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/nonexistent.js");
|
|
84
|
+
expect(nextSpy).toHaveBeenCalledOnce();
|
|
85
|
+
expect(fileCache.getFile).not.toHaveBeenCalled();
|
|
86
|
+
expect(mockContext.header).not.toHaveBeenCalled();
|
|
87
|
+
expect(mockContext.body).not.toHaveBeenCalled();
|
|
88
|
+
});
|
|
89
|
+
it("should proceed to file cache when file exists", async () => {
|
|
90
|
+
mockContext.req.path = "/bundles/existing.js";
|
|
91
|
+
fs.pathExists.mockResolvedValue(true);
|
|
92
|
+
fileCache.getFile.mockResolvedValue(null);
|
|
93
|
+
await middleware(mockContext, nextSpy);
|
|
94
|
+
expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/existing.js");
|
|
95
|
+
expect(fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/existing.js");
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
describe("successful file serving", () => {
|
|
99
|
+
it("should serve file content with correct headers", async () => {
|
|
100
|
+
const mockFileContent = 'console.log("test");';
|
|
101
|
+
const mockFileResult = {
|
|
102
|
+
content: mockFileContent,
|
|
103
|
+
lastModified: Date.now()
|
|
104
|
+
};
|
|
105
|
+
mockContext.req.path = "/bundles/app.js";
|
|
106
|
+
fs.pathExists.mockResolvedValue(true);
|
|
107
|
+
fileCache.getFile.mockResolvedValue(mockFileResult);
|
|
108
|
+
mockContext.body.mockReturnValue("response");
|
|
109
|
+
const result = await middleware(mockContext, nextSpy);
|
|
110
|
+
expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/app.js");
|
|
111
|
+
expect(fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/app.js");
|
|
112
|
+
expect(nextSpy).not.toHaveBeenCalled();
|
|
113
|
+
expect(mockContext.header).toHaveBeenCalledWith("Content-Type", "application/javascript");
|
|
114
|
+
expect(mockContext.header).toHaveBeenCalledWith("Content-Length", String(mockFileResult.content.length));
|
|
115
|
+
expect(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
|
|
116
|
+
expect(result).toBe("response");
|
|
117
|
+
});
|
|
118
|
+
it("should handle empty file content", async () => {
|
|
119
|
+
const mockFileResult = {
|
|
120
|
+
content: "",
|
|
121
|
+
lastModified: Date.now()
|
|
122
|
+
};
|
|
123
|
+
mockContext.req.path = "/bundles/empty.js";
|
|
124
|
+
fs.pathExists.mockResolvedValue(true);
|
|
125
|
+
fileCache.getFile.mockResolvedValue(mockFileResult);
|
|
126
|
+
mockContext.body.mockReturnValue("empty-response");
|
|
127
|
+
const result = await middleware(mockContext, nextSpy);
|
|
128
|
+
expect(mockContext.header).toHaveBeenCalledWith("Content-Length", "0");
|
|
129
|
+
expect(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
|
|
130
|
+
expect(result).toBe("empty-response");
|
|
131
|
+
expect(nextSpy).not.toHaveBeenCalled();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
describe("asset prefix handling", () => {
|
|
135
|
+
it("should handle custom asset prefix correctly", async () => {
|
|
136
|
+
const customMiddleware = createStaticMiddleware({
|
|
137
|
+
assetPrefix: "/custom-prefix",
|
|
138
|
+
pwd: "/test/path"
|
|
139
|
+
});
|
|
140
|
+
mockContext.req.path = "/bundles/test.js";
|
|
141
|
+
await customMiddleware(mockContext, nextSpy);
|
|
142
|
+
expect(nextSpy).toHaveBeenCalledOnce();
|
|
143
|
+
expect(mockContext.header).not.toHaveBeenCalled();
|
|
144
|
+
expect(mockContext.body).not.toHaveBeenCalled();
|
|
145
|
+
});
|
|
146
|
+
it("should handle asset prefix removal correctly", async () => {
|
|
147
|
+
const customMiddleware = createStaticMiddleware({
|
|
148
|
+
assetPrefix: "/prefix",
|
|
149
|
+
pwd: "/test/path"
|
|
150
|
+
});
|
|
151
|
+
const mockFileResult = {
|
|
152
|
+
content: "test content",
|
|
153
|
+
lastModified: Date.now()
|
|
154
|
+
};
|
|
155
|
+
mockContext.req.path = "/prefix/bundles/test.js";
|
|
156
|
+
fs.pathExists.mockResolvedValue(true);
|
|
157
|
+
fileCache.getFile.mockResolvedValue(mockFileResult);
|
|
158
|
+
await customMiddleware(mockContext, nextSpy);
|
|
159
|
+
expect(fs.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./plugin";
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { SSRLiveReload } from "./SSRLiveReload";
|
|
3
|
-
|
|
4
|
-
const mfSSRDevPlugin = () => ({
|
|
3
|
+
const mfSSRPlugin = () => ({
|
|
5
4
|
name: "@module-federation/modern-js",
|
|
6
5
|
setup: (api) => {
|
|
7
6
|
api.onBeforeRender(async () => {
|
|
@@ -11,11 +10,9 @@ const mfSSRDevPlugin = () => ({
|
|
|
11
10
|
globalThis.shouldUpdate = false;
|
|
12
11
|
const nodeUtils = await import("@module-federation/node/utils");
|
|
13
12
|
const shouldUpdate = await nodeUtils.revalidate();
|
|
14
|
-
console.log("shouldUpdate: ", shouldUpdate);
|
|
15
13
|
if (shouldUpdate) {
|
|
16
14
|
console.log("should RELOAD", shouldUpdate);
|
|
17
15
|
await nodeUtils.flushChunks();
|
|
18
|
-
flushDataFetch();
|
|
19
16
|
globalThis.shouldUpdate = true;
|
|
20
17
|
}
|
|
21
18
|
});
|
|
@@ -33,5 +30,5 @@ const mfSSRDevPlugin = () => ({
|
|
|
33
30
|
}
|
|
34
31
|
});
|
|
35
32
|
export {
|
|
36
|
-
|
|
33
|
+
mfSSRPlugin
|
|
37
34
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isDev: boolean;
|
|
@@ -3,4 +3,3 @@ import type { PluginOptions } from '../types';
|
|
|
3
3
|
export declare const moduleFederationPlugin: (userConfig?: PluginOptions) => CliPluginFuture<AppTools>;
|
|
4
4
|
export default moduleFederationPlugin;
|
|
5
5
|
export { createModuleFederationConfig } from '@module-federation/enhanced';
|
|
6
|
-
export type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
|
|
@@ -3,4 +3,3 @@ export type ConfigType<T> = T extends 'webpack' ? webpack.Configuration : T exte
|
|
|
3
3
|
export declare const getIPV4: () => string;
|
|
4
4
|
export declare const isWebTarget: (target: string[] | string) => boolean;
|
|
5
5
|
export declare const skipByTarget: (target: string[] | string) => boolean;
|
|
6
|
-
export declare function isDev(): boolean;
|
package/dist/types/constant.d.ts
CHANGED
|
@@ -1,25 +1,2 @@
|
|
|
1
1
|
export declare const LOCALHOST = "localhost";
|
|
2
2
|
export declare const PLUGIN_IDENTIFIER = "[ Modern.js Module Federation ]";
|
|
3
|
-
export declare const DATA_FETCH_QUERY = "x-mf-data-fetch";
|
|
4
|
-
export declare const DATA_FETCH_ERROR_PREFIX = "caught the following error during dataFetch: ";
|
|
5
|
-
export declare const LOAD_REMOTE_ERROR_PREFIX = "caught the following error during loadRemote: ";
|
|
6
|
-
export declare const DOWNGRADE_KEY = "_mfSSRDowngrade";
|
|
7
|
-
export declare const DATA_FETCH_MAP_KEY = "__MF_DATA_FETCH_MAP__";
|
|
8
|
-
export declare const DATA_FETCH_FUNCTION = "_mfDataFetch";
|
|
9
|
-
export declare const FS_HREF = "_mfFSHref";
|
|
10
|
-
export declare const ERROR_TYPE: {
|
|
11
|
-
DATA_FETCH: number;
|
|
12
|
-
LOAD_REMOTE: number;
|
|
13
|
-
UNKNOWN: number;
|
|
14
|
-
};
|
|
15
|
-
export declare const WRAP_DATA_FETCH_ID_IDENTIFIER = "wrap_dfip_identifier";
|
|
16
|
-
export declare const enum MF_DATA_FETCH_TYPE {
|
|
17
|
-
FETCH_SERVER = 1,
|
|
18
|
-
FETCH_CLIENT = 2
|
|
19
|
-
}
|
|
20
|
-
export declare const enum MF_DATA_FETCH_STATUS {
|
|
21
|
-
LOADED = 1,
|
|
22
|
-
LOADING = 2,
|
|
23
|
-
AWAIT = 0,
|
|
24
|
-
ERROR = 3
|
|
25
|
-
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ErrorBoundaryPropsWithComponent } from 'react-error-boundary';
|
|
3
|
+
type IProps = {
|
|
4
|
+
id: string;
|
|
5
|
+
injectScript?: boolean;
|
|
6
|
+
injectLink?: boolean;
|
|
7
|
+
};
|
|
8
|
+
type ReactKey = {
|
|
9
|
+
key?: React.Key | null;
|
|
10
|
+
};
|
|
11
|
+
export declare function collectSSRAssets(options: IProps): React.ReactNode[];
|
|
12
|
+
export declare function createRemoteSSRComponent<T, E extends keyof T>(info: {
|
|
13
|
+
loader: () => Promise<T>;
|
|
14
|
+
loading: React.ReactNode;
|
|
15
|
+
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
16
|
+
export?: E;
|
|
17
|
+
}): (props: T[E] extends (...args: any) => any ? Parameters<T[E]>[0] extends undefined ? ReactKey : Parameters<T[E]>[0] & ReactKey : ReactKey) => React.JSX.Element;
|
|
18
|
+
export {};
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
export * from '@module-federation/enhanced/runtime';
|
|
2
|
-
export type { DataFetchParams } from '../interfaces/global';
|
|
3
|
-
export { ERROR_TYPE } from '../constant';
|
|
4
2
|
export declare const kit: {
|
|
5
|
-
readonly createRemoteSSRComponent:
|
|
6
|
-
readonly
|
|
7
|
-
readonly collectSSRAssets: typeof import("./createRemoteComponent").collectSSRAssets;
|
|
8
|
-
readonly wrapNoSSR: typeof import("./wrapNoSSR").wrapNoSSR;
|
|
3
|
+
readonly createRemoteSSRComponent: any;
|
|
4
|
+
readonly collectSSRAssets: any;
|
|
9
5
|
};
|
|
10
|
-
export { setSSREnv } from '../ssr-runtime/injectDataFetchFunctionPlugin';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface FileResult {
|
|
2
|
+
content: string;
|
|
3
|
+
lastModified: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class FileCache {
|
|
6
|
+
private cache;
|
|
7
|
+
/**
|
|
8
|
+
* Check if file exists and return file info
|
|
9
|
+
* @param filepath Path to the file
|
|
10
|
+
* @returns FileResult or null if file doesn't exist
|
|
11
|
+
*/
|
|
12
|
+
getFile(filepath: string): Promise<FileResult | null>;
|
|
13
|
+
}
|
|
14
|
+
export declare const fileCache: FileCache;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './plugin';
|
|
@@ -8,7 +8,6 @@ export interface PluginOptions {
|
|
|
8
8
|
distOutputDir?: string;
|
|
9
9
|
} | boolean;
|
|
10
10
|
remoteIpStrategy?: 'ipv4' | 'inherit';
|
|
11
|
-
fetchServerQuery?: Record<string, unknown>;
|
|
12
11
|
}
|
|
13
12
|
export interface InternalModernPluginOptions {
|
|
14
13
|
csrConfig?: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
@@ -19,6 +18,5 @@ export interface InternalModernPluginOptions {
|
|
|
19
18
|
nodePlugin?: BundlerPlugin;
|
|
20
19
|
remoteIpStrategy?: 'ipv4' | 'inherit';
|
|
21
20
|
userConfig?: PluginOptions;
|
|
22
|
-
fetchServerQuery?: Record<string, unknown>;
|
|
23
21
|
}
|
|
24
22
|
export type BundlerPlugin = WebpackModuleFederationPlugin | RspackModuleFederationPlugin;
|