@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.
- package/dist/LICENSE +21 -0
- package/dist/cjs/cli/configPlugin.js +377 -0
- package/dist/cjs/cli/configPlugin.spec.js +112 -0
- package/dist/cjs/cli/constant.js +28 -0
- package/dist/cjs/cli/index.js +93 -0
- package/dist/cjs/cli/logger.js +27 -0
- package/dist/cjs/cli/manifest.js +75 -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/ssrPlugin.js +217 -0
- package/dist/cjs/cli/utils.js +85 -0
- package/dist/cjs/constant.js +31 -0
- package/dist/cjs/interfaces/bundler.js +16 -0
- package/dist/cjs/runtime/createRemoteSSRComponent.js +201 -0
- package/dist/cjs/runtime/index.js +38 -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/index.js +22 -0
- package/dist/cjs/ssr-runtime/plugin.js +68 -0
- package/dist/cjs/types/index.js +16 -0
- package/dist/esm/cli/configPlugin.js +384 -0
- package/dist/esm/cli/configPlugin.spec.js +108 -0
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/index.js +88 -0
- package/dist/esm/cli/logger.js +7 -0
- package/dist/esm/cli/manifest.js +41 -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/ssrPlugin.js +199 -0
- package/dist/esm/cli/utils.js +49 -0
- package/dist/esm/constant.js +6 -0
- package/dist/esm/interfaces/bundler.js +0 -0
- package/dist/esm/runtime/createRemoteSSRComponent.js +191 -0
- package/dist/esm/runtime/index.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/index.js +1 -0
- package/dist/esm/ssr-runtime/plugin.js +70 -0
- package/dist/esm/types/index.js +0 -0
- package/dist/esm-node/cli/configPlugin.js +336 -0
- package/dist/esm-node/cli/configPlugin.spec.js +89 -0
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/index.js +68 -0
- package/dist/esm-node/cli/logger.js +7 -0
- package/dist/esm-node/cli/manifest.js +41 -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/ssrPlugin.js +181 -0
- package/dist/esm-node/cli/utils.js +49 -0
- package/dist/esm-node/constant.js +6 -0
- package/dist/esm-node/interfaces/bundler.js +0 -0
- package/dist/esm-node/runtime/createRemoteSSRComponent.js +166 -0
- package/dist/esm-node/runtime/index.js +12 -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/index.js +1 -0
- package/dist/esm-node/ssr-runtime/plugin.js +34 -0
- package/dist/esm-node/types/index.js +0 -0
- package/dist/types/cli/configPlugin.d.ts +21 -0
- package/dist/types/cli/configPlugin.spec.d.ts +1 -0
- package/dist/types/cli/constant.d.ts +1 -0
- package/dist/types/cli/index.d.ts +5 -0
- package/dist/types/cli/logger.d.ts +2 -0
- package/dist/types/cli/manifest.d.ts +2 -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/ssrPlugin.d.ts +6 -0
- package/dist/types/cli/utils.d.ts +5 -0
- package/dist/types/constant.d.ts +2 -0
- package/dist/types/interfaces/bundler.d.ts +18 -0
- package/dist/types/runtime/createRemoteSSRComponent.d.ts +18 -0
- package/dist/types/runtime/index.d.ts +5 -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/index.d.ts +1 -0
- package/dist/types/ssr-runtime/plugin.d.ts +2 -0
- package/dist/types/types/index.d.ts +22 -0
- package/package.json +8 -8
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var createRemoteSSRComponent_exports = {};
|
|
30
|
+
__export(createRemoteSSRComponent_exports, {
|
|
31
|
+
collectSSRAssets: () => collectSSRAssets,
|
|
32
|
+
createRemoteSSRComponent: () => createRemoteSSRComponent
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(createRemoteSSRComponent_exports);
|
|
35
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
36
|
+
var import_react = __toESM(require("react"));
|
|
37
|
+
var import_runtime = require("@module-federation/enhanced/runtime");
|
|
38
|
+
var import_react_error_boundary = require("react-error-boundary");
|
|
39
|
+
function getLoadedRemoteInfos(instance, id) {
|
|
40
|
+
const { name, expose } = instance.remoteHandler.idToRemoteMap[id] || {};
|
|
41
|
+
if (!name) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const module2 = instance.moduleCache.get(name);
|
|
45
|
+
if (!module2) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const { remoteSnapshot } = instance.snapshotHandler.getGlobalRemoteInfo(module2.remoteInfo);
|
|
49
|
+
return {
|
|
50
|
+
...module2.remoteInfo,
|
|
51
|
+
snapshot: remoteSnapshot,
|
|
52
|
+
expose
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function getTargetModuleInfo(id) {
|
|
56
|
+
const instance = (0, import_runtime.getInstance)();
|
|
57
|
+
if (!instance) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
|
|
61
|
+
if (!loadedRemoteInfo) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const snapshot = loadedRemoteInfo.snapshot;
|
|
65
|
+
if (!snapshot) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
|
|
69
|
+
if (!publicPath) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const modules = "modules" in snapshot ? snapshot.modules : [];
|
|
73
|
+
const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
|
|
74
|
+
if (!targetModule) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
|
|
78
|
+
if (!remoteEntry) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
module: targetModule,
|
|
83
|
+
publicPath,
|
|
84
|
+
remoteEntry
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function collectSSRAssets(options) {
|
|
88
|
+
const { id, injectLink = true, injectScript = true } = typeof options === "string" ? {
|
|
89
|
+
id: options
|
|
90
|
+
} : options;
|
|
91
|
+
const links = [];
|
|
92
|
+
const scripts = [];
|
|
93
|
+
const instance = (0, import_runtime.getInstance)();
|
|
94
|
+
if (!instance || !injectLink && !injectScript) {
|
|
95
|
+
return [
|
|
96
|
+
...scripts,
|
|
97
|
+
...links
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
const moduleAndPublicPath = getTargetModuleInfo(id);
|
|
101
|
+
if (!moduleAndPublicPath) {
|
|
102
|
+
return [
|
|
103
|
+
...scripts,
|
|
104
|
+
...links
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
|
|
108
|
+
if (injectLink) {
|
|
109
|
+
[
|
|
110
|
+
...targetModule.assets.css.sync,
|
|
111
|
+
...targetModule.assets.css.async
|
|
112
|
+
].forEach((file, index) => {
|
|
113
|
+
links.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("link", {
|
|
114
|
+
href: `${publicPath}${file}`,
|
|
115
|
+
rel: "stylesheet",
|
|
116
|
+
type: "text/css"
|
|
117
|
+
}, `${file.split(".")[0]}_${index}`));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (injectScript) {
|
|
121
|
+
scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
|
|
122
|
+
async: true,
|
|
123
|
+
src: `${publicPath}${remoteEntry}`,
|
|
124
|
+
crossOrigin: "anonymous"
|
|
125
|
+
}, remoteEntry.split(".")[0]));
|
|
126
|
+
[
|
|
127
|
+
...targetModule.assets.js.sync
|
|
128
|
+
].forEach((file, index) => {
|
|
129
|
+
scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
|
|
130
|
+
async: true,
|
|
131
|
+
src: `${publicPath}${file}`,
|
|
132
|
+
crossOrigin: "anonymous"
|
|
133
|
+
}, `${file.split(".")[0]}_${index}`));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return [
|
|
137
|
+
...scripts,
|
|
138
|
+
...links
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
function createRemoteSSRComponent(info) {
|
|
142
|
+
const exportName = (info === null || info === void 0 ? void 0 : info.export) || "default";
|
|
143
|
+
const LazyComponent = /* @__PURE__ */ import_react.default.lazy(async () => {
|
|
144
|
+
try {
|
|
145
|
+
const m = await info.loader();
|
|
146
|
+
if (!m) {
|
|
147
|
+
throw new Error("load remote failed");
|
|
148
|
+
}
|
|
149
|
+
const moduleId = m && m[Symbol.for("mf_module_id")];
|
|
150
|
+
const assets = collectSSRAssets({
|
|
151
|
+
id: moduleId
|
|
152
|
+
});
|
|
153
|
+
const Com = m[exportName];
|
|
154
|
+
if (exportName in m && typeof Com === "function") {
|
|
155
|
+
return {
|
|
156
|
+
default: (props) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
|
|
157
|
+
children: [
|
|
158
|
+
assets,
|
|
159
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Com, {
|
|
160
|
+
...props
|
|
161
|
+
})
|
|
162
|
+
]
|
|
163
|
+
})
|
|
164
|
+
};
|
|
165
|
+
} else {
|
|
166
|
+
throw Error(`Make sure that ${moduleId} has the correct export when export is ${String(exportName)}`);
|
|
167
|
+
}
|
|
168
|
+
} catch (err) {
|
|
169
|
+
if (!info.fallback) {
|
|
170
|
+
throw err;
|
|
171
|
+
}
|
|
172
|
+
const FallbackFunctionComponent = info.fallback;
|
|
173
|
+
const FallbackNode = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FallbackFunctionComponent, {
|
|
174
|
+
error: err,
|
|
175
|
+
resetErrorBoundary: () => {
|
|
176
|
+
console.log('SSR mode not support "resetErrorBoundary" !');
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
return {
|
|
180
|
+
default: () => FallbackNode
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return (props) => {
|
|
185
|
+
const { key, ...args } = props;
|
|
186
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_error_boundary.ErrorBoundary, {
|
|
187
|
+
FallbackComponent: info.fallback,
|
|
188
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react.default.Suspense, {
|
|
189
|
+
fallback: info.loading,
|
|
190
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LazyComponent, {
|
|
191
|
+
...args
|
|
192
|
+
})
|
|
193
|
+
})
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
198
|
+
0 && (module.exports = {
|
|
199
|
+
collectSSRAssets,
|
|
200
|
+
createRemoteSSRComponent
|
|
201
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var runtime_exports = {};
|
|
21
|
+
__export(runtime_exports, {
|
|
22
|
+
kit: () => kit
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(runtime_exports);
|
|
25
|
+
__reExport(runtime_exports, require("@module-federation/enhanced/runtime"), module.exports);
|
|
26
|
+
const kit = {
|
|
27
|
+
get createRemoteSSRComponent() {
|
|
28
|
+
return require("./createRemoteSSRComponent").createRemoteSSRComponent;
|
|
29
|
+
},
|
|
30
|
+
get collectSSRAssets() {
|
|
31
|
+
return require("./createRemoteSSRComponent").collectSSRAssets;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
kit,
|
|
37
|
+
...require("@module-federation/enhanced/runtime")
|
|
38
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var fileCache_exports = {};
|
|
30
|
+
__export(fileCache_exports, {
|
|
31
|
+
FileCache: () => FileCache,
|
|
32
|
+
fileCache: () => fileCache
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(fileCache_exports);
|
|
35
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
36
|
+
var import_lru_cache = require("lru-cache");
|
|
37
|
+
class FileCache {
|
|
38
|
+
/**
|
|
39
|
+
* Check if file exists and return file info
|
|
40
|
+
* @param filepath Path to the file
|
|
41
|
+
* @returns FileResult or null if file doesn't exist
|
|
42
|
+
*/
|
|
43
|
+
async getFile(filepath) {
|
|
44
|
+
if (!await import_fs_extra.default.pathExists(filepath)) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const stat = await import_fs_extra.default.lstat(filepath);
|
|
49
|
+
const currentModified = stat.mtimeMs;
|
|
50
|
+
const cachedEntry = this.cache.get(filepath);
|
|
51
|
+
if (cachedEntry && currentModified <= cachedEntry.lastModified) {
|
|
52
|
+
return {
|
|
53
|
+
content: cachedEntry.content,
|
|
54
|
+
lastModified: cachedEntry.lastModified
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const content = await import_fs_extra.default.readFile(filepath, "utf-8");
|
|
58
|
+
const newEntry = {
|
|
59
|
+
content,
|
|
60
|
+
lastModified: currentModified
|
|
61
|
+
};
|
|
62
|
+
this.cache.set(filepath, newEntry, {
|
|
63
|
+
size: stat.size || content.length
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
content,
|
|
67
|
+
lastModified: currentModified
|
|
68
|
+
};
|
|
69
|
+
} catch (err) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
constructor() {
|
|
74
|
+
this.cache = new import_lru_cache.LRUCache({
|
|
75
|
+
maxSize: 200 * 1024 * 1024
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const fileCache = new FileCache();
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
FileCache,
|
|
83
|
+
fileCache
|
|
84
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var import_vitest = require("vitest");
|
|
3
|
+
var import_fileCache = require("./fileCache");
|
|
4
|
+
(0, import_vitest.beforeAll)(() => {
|
|
5
|
+
import_vitest.vi.mock("fs-extra", () => ({
|
|
6
|
+
default: {
|
|
7
|
+
pathExists: () => {
|
|
8
|
+
return true;
|
|
9
|
+
},
|
|
10
|
+
lstat: () => {
|
|
11
|
+
return {
|
|
12
|
+
mtimeMs: Date.now(),
|
|
13
|
+
size: 4
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
readFile: () => {
|
|
17
|
+
return "test";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}));
|
|
21
|
+
});
|
|
22
|
+
(0, import_vitest.describe)("modern serve static file cache", async () => {
|
|
23
|
+
(0, import_vitest.it)("should cache file", async () => {
|
|
24
|
+
const cache = new import_fileCache.FileCache();
|
|
25
|
+
const result = await cache.getFile("test.txt");
|
|
26
|
+
(0, import_vitest.expect)(result === null || result === void 0 ? void 0 : result.content).toBe("test");
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var server_exports = {};
|
|
20
|
+
__export(server_exports, {
|
|
21
|
+
default: () => server_default,
|
|
22
|
+
staticServePlugin: () => staticServePlugin
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(server_exports);
|
|
25
|
+
var import_staticMiddleware = require("./staticMiddleware");
|
|
26
|
+
const staticServePlugin = () => ({
|
|
27
|
+
name: "@modern-js/module-federation/server",
|
|
28
|
+
setup: (api) => {
|
|
29
|
+
api.onPrepare(() => {
|
|
30
|
+
var _config_output, _config_server;
|
|
31
|
+
console.log(process.env.NODE_ENV);
|
|
32
|
+
if (process.env.NODE_ENV === "development") {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const { middlewares } = api.getServerContext();
|
|
36
|
+
const config = api.getServerConfig();
|
|
37
|
+
const assetPrefix = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.assetPrefix) || "";
|
|
38
|
+
if (!((_config_server = config.server) === null || _config_server === void 0 ? void 0 : _config_server.ssr)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const context = api.getServerContext();
|
|
42
|
+
const pwd = context.distDirectory;
|
|
43
|
+
const serverStaticMiddleware = (0, import_staticMiddleware.createStaticMiddleware)({
|
|
44
|
+
assetPrefix,
|
|
45
|
+
pwd
|
|
46
|
+
});
|
|
47
|
+
middlewares.push({
|
|
48
|
+
name: "module-federation-serve-manifest",
|
|
49
|
+
handler: serverStaticMiddleware
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
var server_default = staticServePlugin;
|
|
55
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
56
|
+
0 && (module.exports = {
|
|
57
|
+
staticServePlugin
|
|
58
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var staticMiddleware_exports = {};
|
|
30
|
+
__export(staticMiddleware_exports, {
|
|
31
|
+
createStaticMiddleware: () => createStaticMiddleware
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(staticMiddleware_exports);
|
|
34
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
35
|
+
var import_node_path = __toESM(require("node:path"));
|
|
36
|
+
var import_fileCache = require("./fileCache");
|
|
37
|
+
const bundlesAssetPrefix = "/bundles";
|
|
38
|
+
const removeHost = (url) => {
|
|
39
|
+
try {
|
|
40
|
+
const hasProtocol = url.includes("://");
|
|
41
|
+
const hasDomain = hasProtocol || url.startsWith("//");
|
|
42
|
+
const pathname = hasDomain ? new URL(hasProtocol ? url : `http:${url}`).pathname : url;
|
|
43
|
+
return pathname;
|
|
44
|
+
} catch (e) {
|
|
45
|
+
return url;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const createStaticMiddleware = (options) => {
|
|
49
|
+
const { assetPrefix, pwd } = options;
|
|
50
|
+
return async (c, next) => {
|
|
51
|
+
const pathname = c.req.path;
|
|
52
|
+
if (import_node_path.default.extname(pathname) !== ".js") {
|
|
53
|
+
return next();
|
|
54
|
+
}
|
|
55
|
+
const prefixWithoutHost = removeHost(assetPrefix);
|
|
56
|
+
const prefixWithBundle = import_node_path.default.join(prefixWithoutHost, bundlesAssetPrefix);
|
|
57
|
+
if (!pathname.startsWith(prefixWithBundle)) {
|
|
58
|
+
return next();
|
|
59
|
+
}
|
|
60
|
+
const pathnameWithoutPrefix = pathname.replace(prefixWithBundle, "");
|
|
61
|
+
const filepath = import_node_path.default.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
|
|
62
|
+
if (!await import_fs_extra.default.pathExists(filepath)) {
|
|
63
|
+
return next();
|
|
64
|
+
}
|
|
65
|
+
const fileResult = await import_fileCache.fileCache.getFile(filepath);
|
|
66
|
+
if (!fileResult) {
|
|
67
|
+
return next();
|
|
68
|
+
}
|
|
69
|
+
c.header("Content-Type", "application/javascript");
|
|
70
|
+
c.header("Content-Length", String(fileResult.content.length));
|
|
71
|
+
return c.body(fileResult.content, 200);
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
75
|
+
0 && (module.exports = {
|
|
76
|
+
createStaticMiddleware
|
|
77
|
+
});
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var import_vitest = require("vitest");
|
|
25
|
+
var import_staticMiddleware = require("./staticMiddleware");
|
|
26
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
27
|
+
var import_fileCache = require("./fileCache");
|
|
28
|
+
import_vitest.vi.mock("fs-extra", () => ({
|
|
29
|
+
default: {
|
|
30
|
+
pathExists: import_vitest.vi.fn()
|
|
31
|
+
}
|
|
32
|
+
}));
|
|
33
|
+
import_vitest.vi.mock("./fileCache", () => ({
|
|
34
|
+
fileCache: {
|
|
35
|
+
getFile: import_vitest.vi.fn()
|
|
36
|
+
}
|
|
37
|
+
}));
|
|
38
|
+
(0, import_vitest.describe)("staticMiddleware", () => {
|
|
39
|
+
let middleware;
|
|
40
|
+
let mockContext;
|
|
41
|
+
let nextSpy;
|
|
42
|
+
(0, import_vitest.beforeEach)(() => {
|
|
43
|
+
import_vitest.vi.clearAllMocks();
|
|
44
|
+
middleware = (0, import_staticMiddleware.createStaticMiddleware)({
|
|
45
|
+
assetPrefix: "",
|
|
46
|
+
pwd: "/test/path"
|
|
47
|
+
});
|
|
48
|
+
nextSpy = import_vitest.vi.fn();
|
|
49
|
+
mockContext = {
|
|
50
|
+
req: {
|
|
51
|
+
path: ""
|
|
52
|
+
},
|
|
53
|
+
header: import_vitest.vi.fn(),
|
|
54
|
+
body: import_vitest.vi.fn()
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
(0, import_vitest.describe)("file extension filtering", () => {
|
|
58
|
+
(0, import_vitest.it)("should call next() for non-js files", async () => {
|
|
59
|
+
mockContext.req.path = "/bundles/test.css";
|
|
60
|
+
await middleware(mockContext, nextSpy);
|
|
61
|
+
(0, import_vitest.expect)(nextSpy).toHaveBeenCalledOnce();
|
|
62
|
+
(0, import_vitest.expect)(mockContext.header).not.toHaveBeenCalled();
|
|
63
|
+
(0, import_vitest.expect)(mockContext.body).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
(0, import_vitest.it)("should call next() for files without extension", async () => {
|
|
66
|
+
mockContext.req.path = "/bundles/test";
|
|
67
|
+
await middleware(mockContext, nextSpy);
|
|
68
|
+
(0, import_vitest.expect)(nextSpy).toHaveBeenCalledOnce();
|
|
69
|
+
(0, import_vitest.expect)(mockContext.header).not.toHaveBeenCalled();
|
|
70
|
+
(0, import_vitest.expect)(mockContext.body).not.toHaveBeenCalled();
|
|
71
|
+
});
|
|
72
|
+
(0, import_vitest.it)("should process .js files", async () => {
|
|
73
|
+
mockContext.req.path = "/bundles/test.js";
|
|
74
|
+
import_fs_extra.default.pathExists.mockResolvedValue(false);
|
|
75
|
+
await middleware(mockContext, nextSpy);
|
|
76
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
(0, import_vitest.describe)("asset prefix filtering", () => {
|
|
80
|
+
(0, import_vitest.it)("should call next() for paths not starting with /bundles", async () => {
|
|
81
|
+
mockContext.req.path = "/assets/test.js";
|
|
82
|
+
await middleware(mockContext, nextSpy);
|
|
83
|
+
(0, import_vitest.expect)(nextSpy).toHaveBeenCalledOnce();
|
|
84
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).not.toHaveBeenCalled();
|
|
85
|
+
(0, import_vitest.expect)(mockContext.header).not.toHaveBeenCalled();
|
|
86
|
+
(0, import_vitest.expect)(mockContext.body).not.toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
(0, import_vitest.it)("should call next() for root path", async () => {
|
|
89
|
+
mockContext.req.path = "/test.js";
|
|
90
|
+
await middleware(mockContext, nextSpy);
|
|
91
|
+
(0, import_vitest.expect)(nextSpy).toHaveBeenCalledOnce();
|
|
92
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).not.toHaveBeenCalled();
|
|
93
|
+
});
|
|
94
|
+
(0, import_vitest.it)("should process paths starting with /bundles", async () => {
|
|
95
|
+
mockContext.req.path = "/bundles/test.js";
|
|
96
|
+
import_fs_extra.default.pathExists.mockResolvedValue(false);
|
|
97
|
+
await middleware(mockContext, nextSpy);
|
|
98
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
(0, import_vitest.describe)("file existence check", () => {
|
|
102
|
+
(0, import_vitest.it)("should call next() when file does not exist", async () => {
|
|
103
|
+
mockContext.req.path = "/bundles/nonexistent.js";
|
|
104
|
+
import_fs_extra.default.pathExists.mockResolvedValue(false);
|
|
105
|
+
await middleware(mockContext, nextSpy);
|
|
106
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).toHaveBeenCalledWith("/test/path/bundles/nonexistent.js");
|
|
107
|
+
(0, import_vitest.expect)(nextSpy).toHaveBeenCalledOnce();
|
|
108
|
+
(0, import_vitest.expect)(import_fileCache.fileCache.getFile).not.toHaveBeenCalled();
|
|
109
|
+
(0, import_vitest.expect)(mockContext.header).not.toHaveBeenCalled();
|
|
110
|
+
(0, import_vitest.expect)(mockContext.body).not.toHaveBeenCalled();
|
|
111
|
+
});
|
|
112
|
+
(0, import_vitest.it)("should proceed to file cache when file exists", async () => {
|
|
113
|
+
mockContext.req.path = "/bundles/existing.js";
|
|
114
|
+
import_fs_extra.default.pathExists.mockResolvedValue(true);
|
|
115
|
+
import_fileCache.fileCache.getFile.mockResolvedValue(null);
|
|
116
|
+
await middleware(mockContext, nextSpy);
|
|
117
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).toHaveBeenCalledWith("/test/path/bundles/existing.js");
|
|
118
|
+
(0, import_vitest.expect)(import_fileCache.fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/existing.js");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
(0, import_vitest.describe)("successful file serving", () => {
|
|
122
|
+
(0, import_vitest.it)("should serve file content with correct headers", async () => {
|
|
123
|
+
const mockFileContent = 'console.log("test");';
|
|
124
|
+
const mockFileResult = {
|
|
125
|
+
content: mockFileContent,
|
|
126
|
+
lastModified: Date.now()
|
|
127
|
+
};
|
|
128
|
+
mockContext.req.path = "/bundles/app.js";
|
|
129
|
+
import_fs_extra.default.pathExists.mockResolvedValue(true);
|
|
130
|
+
import_fileCache.fileCache.getFile.mockResolvedValue(mockFileResult);
|
|
131
|
+
mockContext.body.mockReturnValue("response");
|
|
132
|
+
const result = await middleware(mockContext, nextSpy);
|
|
133
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).toHaveBeenCalledWith("/test/path/bundles/app.js");
|
|
134
|
+
(0, import_vitest.expect)(import_fileCache.fileCache.getFile).toHaveBeenCalledWith("/test/path/bundles/app.js");
|
|
135
|
+
(0, import_vitest.expect)(nextSpy).not.toHaveBeenCalled();
|
|
136
|
+
(0, import_vitest.expect)(mockContext.header).toHaveBeenCalledWith("Content-Type", "application/javascript");
|
|
137
|
+
(0, import_vitest.expect)(mockContext.header).toHaveBeenCalledWith("Content-Length", String(mockFileResult.content.length));
|
|
138
|
+
(0, import_vitest.expect)(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
|
|
139
|
+
(0, import_vitest.expect)(result).toBe("response");
|
|
140
|
+
});
|
|
141
|
+
(0, import_vitest.it)("should handle empty file content", async () => {
|
|
142
|
+
const mockFileResult = {
|
|
143
|
+
content: "",
|
|
144
|
+
lastModified: Date.now()
|
|
145
|
+
};
|
|
146
|
+
mockContext.req.path = "/bundles/empty.js";
|
|
147
|
+
import_fs_extra.default.pathExists.mockResolvedValue(true);
|
|
148
|
+
import_fileCache.fileCache.getFile.mockResolvedValue(mockFileResult);
|
|
149
|
+
mockContext.body.mockReturnValue("empty-response");
|
|
150
|
+
const result = await middleware(mockContext, nextSpy);
|
|
151
|
+
(0, import_vitest.expect)(mockContext.header).toHaveBeenCalledWith("Content-Length", "0");
|
|
152
|
+
(0, import_vitest.expect)(mockContext.body).toHaveBeenCalledWith(mockFileResult.content, 200);
|
|
153
|
+
(0, import_vitest.expect)(result).toBe("empty-response");
|
|
154
|
+
(0, import_vitest.expect)(nextSpy).not.toHaveBeenCalled();
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
(0, import_vitest.describe)("asset prefix handling", () => {
|
|
158
|
+
(0, import_vitest.it)("should handle custom asset prefix correctly", async () => {
|
|
159
|
+
const customMiddleware = (0, import_staticMiddleware.createStaticMiddleware)({
|
|
160
|
+
assetPrefix: "/custom-prefix",
|
|
161
|
+
pwd: "/test/path"
|
|
162
|
+
});
|
|
163
|
+
mockContext.req.path = "/bundles/test.js";
|
|
164
|
+
await customMiddleware(mockContext, nextSpy);
|
|
165
|
+
(0, import_vitest.expect)(nextSpy).toHaveBeenCalledOnce();
|
|
166
|
+
(0, import_vitest.expect)(mockContext.header).not.toHaveBeenCalled();
|
|
167
|
+
(0, import_vitest.expect)(mockContext.body).not.toHaveBeenCalled();
|
|
168
|
+
});
|
|
169
|
+
(0, import_vitest.it)("should handle asset prefix removal correctly", async () => {
|
|
170
|
+
const customMiddleware = (0, import_staticMiddleware.createStaticMiddleware)({
|
|
171
|
+
assetPrefix: "/prefix",
|
|
172
|
+
pwd: "/test/path"
|
|
173
|
+
});
|
|
174
|
+
const mockFileResult = {
|
|
175
|
+
content: "test content",
|
|
176
|
+
lastModified: Date.now()
|
|
177
|
+
};
|
|
178
|
+
mockContext.req.path = "/prefix/bundles/test.js";
|
|
179
|
+
import_fs_extra.default.pathExists.mockResolvedValue(true);
|
|
180
|
+
import_fileCache.fileCache.getFile.mockResolvedValue(mockFileResult);
|
|
181
|
+
await customMiddleware(mockContext, nextSpy);
|
|
182
|
+
(0, import_vitest.expect)(import_fs_extra.default.pathExists).toHaveBeenCalledWith("/test/path/bundles/test.js");
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
});
|