@module-federation/modern-js 0.0.0-next-20240816090839 → 0.0.0-next-20240816102736
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/ast/constant.js +49 -0
- package/dist/cjs/cli/ast/generateRoutes.js +140 -0
- package/dist/cjs/cli/ast/generateSerializableRoutes.js +91 -0
- package/dist/cjs/cli/ast/generateSlimRoutes.js +106 -0
- package/dist/cjs/cli/ast/index.js +34 -0
- package/dist/cjs/cli/bundler-plugins/SerializableRoutesPlugin.js +65 -0
- package/dist/cjs/cli/constant.js +6 -0
- package/dist/cjs/cli/dataLoaderPlugin.js +281 -0
- package/dist/cjs/cli/index.js +6 -1
- package/dist/cjs/cli/server/dataLoaderPlugin.js +83 -0
- package/dist/cjs/cli/ssrPlugin.js +4 -4
- package/dist/cjs/cli/utils.js +4 -2
- package/dist/cjs/interfaces/route.js +16 -0
- package/dist/cjs/runtime/constant.js +34 -0
- package/dist/cjs/runtime/dataLoader.js +87 -0
- package/dist/cjs/runtime/index.js +4 -1
- package/dist/cjs/runtime/utils.js +43 -0
- package/dist/cjs/runtime/withMFRouteId.js +31 -0
- package/dist/cjs/ssr-runtime/plugin.js +8 -7
- package/dist/esm/cli/ast/constant.js +18 -0
- package/dist/esm/cli/ast/generateRoutes.js +107 -0
- package/dist/esm/cli/ast/generateSerializableRoutes.js +58 -0
- package/dist/esm/cli/ast/generateSlimRoutes.js +75 -0
- package/dist/esm/cli/ast/index.js +8 -0
- package/dist/esm/cli/bundler-plugins/SerializableRoutesPlugin.js +45 -0
- package/dist/esm/cli/constant.js +4 -0
- package/dist/esm/cli/dataLoaderPlugin.js +326 -0
- package/dist/esm/cli/index.js +6 -1
- package/dist/esm/cli/server/dataLoaderPlugin.js +146 -0
- package/dist/esm/cli/ssrPlugin.js +2 -2
- package/dist/esm/cli/utils.js +2 -1
- package/dist/esm/interfaces/route.js +0 -0
- package/dist/esm/runtime/constant.js +8 -0
- package/dist/esm/runtime/dataLoader.js +125 -0
- package/dist/esm/runtime/index.js +3 -1
- package/dist/esm/runtime/utils.js +16 -0
- package/dist/esm/runtime/withMFRouteId.js +7 -0
- package/dist/esm/ssr-runtime/plugin.js +8 -10
- package/dist/esm-node/cli/ast/constant.js +18 -0
- package/dist/esm-node/cli/ast/generateRoutes.js +106 -0
- package/dist/esm-node/cli/ast/generateSerializableRoutes.js +57 -0
- package/dist/esm-node/cli/ast/generateSlimRoutes.js +72 -0
- package/dist/esm-node/cli/ast/index.js +8 -0
- package/dist/esm-node/cli/bundler-plugins/SerializableRoutesPlugin.js +30 -0
- package/dist/esm-node/cli/constant.js +4 -0
- package/dist/esm-node/cli/dataLoaderPlugin.js +245 -0
- package/dist/esm-node/cli/index.js +6 -1
- package/dist/esm-node/cli/server/dataLoaderPlugin.js +63 -0
- package/dist/esm-node/cli/ssrPlugin.js +2 -2
- package/dist/esm-node/cli/utils.js +2 -1
- package/dist/esm-node/interfaces/route.js +0 -0
- package/dist/esm-node/runtime/constant.js +8 -0
- package/dist/esm-node/runtime/dataLoader.js +63 -0
- package/dist/esm-node/runtime/index.js +3 -1
- package/dist/esm-node/runtime/utils.js +19 -0
- package/dist/esm-node/runtime/withMFRouteId.js +7 -0
- package/dist/esm-node/ssr-runtime/plugin.js +8 -7
- package/dist/types/cli/ast/constant.d.ts +8 -0
- package/dist/types/cli/ast/generateRoutes.d.ts +7 -0
- package/dist/types/cli/ast/generateSerializableRoutes.d.ts +5 -0
- package/dist/types/cli/ast/generateSlimRoutes.d.ts +7 -0
- package/dist/types/cli/ast/index.d.ts +3 -0
- package/dist/types/cli/bundler-plugins/SerializableRoutesPlugin.d.ts +12 -0
- package/dist/types/cli/constant.d.ts +2 -0
- package/dist/types/cli/dataLoaderPlugin.d.ts +6 -0
- package/dist/types/cli/server/dataLoaderPlugin.d.ts +5 -0
- package/dist/types/cli/utils.d.ts +1 -0
- package/dist/types/interfaces/route.d.ts +13 -0
- package/dist/types/runtime/constant.d.ts +3 -0
- package/dist/types/runtime/dataLoader.d.ts +7 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/utils.d.ts +1 -0
- package/dist/types/runtime/withMFRouteId.d.ts +1 -0
- package/dist/types/types/index.d.ts +18 -0
- package/package.json +37 -11
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { generateSerializableRoutes } from "../ast";
|
|
3
|
+
const SERIALIZABLE_ROUTES = "serializable-routes.json";
|
|
4
|
+
class SerializableRoutesPlugin {
|
|
5
|
+
apply(compiler) {
|
|
6
|
+
compiler.hooks.thisCompilation.tap("generateStats", (compilation) => {
|
|
7
|
+
compilation.hooks.processAssets.tapPromise({
|
|
8
|
+
name: "generateSerializableRoutes",
|
|
9
|
+
// @ts-ignore use runtime variable in case peer dep not installed
|
|
10
|
+
stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
|
|
11
|
+
}, async () => {
|
|
12
|
+
const sourceCode = fs.readFileSync(this.routesFilePath, "utf-8");
|
|
13
|
+
const routesCode = generateSerializableRoutes({
|
|
14
|
+
sourceCode,
|
|
15
|
+
prefix: this.prefix
|
|
16
|
+
});
|
|
17
|
+
compilation.emitAsset(SERIALIZABLE_ROUTES, new compiler.webpack.sources.RawSource(JSON.stringify(routesCode, null, 2)));
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
constructor(options) {
|
|
22
|
+
const { routesFilePath, prefix } = options;
|
|
23
|
+
this.routesFilePath = routesFilePath;
|
|
24
|
+
this.prefix = prefix;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
SERIALIZABLE_ROUTES,
|
|
29
|
+
SerializableRoutesPlugin
|
|
30
|
+
};
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { transformName2Prefix } from "../runtime/utils";
|
|
4
|
+
import { PLUGIN_IDENTIFIER } from "../constant";
|
|
5
|
+
import { MF_FULL_ROUTES, MF_SLIM_ROUTES, MF_ROUTES_META } from "../runtime/constant";
|
|
6
|
+
import { generateRoutes, generateSlimRoutes } from "./ast";
|
|
7
|
+
import { MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME, META_NAME } from "./constant";
|
|
8
|
+
import { getIPV4, replaceRemoteUrl } from "./utils";
|
|
9
|
+
import { SerializableRoutesPlugin, SERIALIZABLE_ROUTES } from "./bundler-plugins/SerializableRoutesPlugin";
|
|
10
|
+
function generateExtraExposeFiles(options) {
|
|
11
|
+
const { routesFilePath, mfConfig, isServer, baseName } = options;
|
|
12
|
+
const outputDir = path.resolve(process.cwd(), "node_modules/.federation");
|
|
13
|
+
fs.ensureDirSync(outputDir);
|
|
14
|
+
const addSuffix = (fileName) => {
|
|
15
|
+
if (!isServer) {
|
|
16
|
+
return `${fileName}.jsx`;
|
|
17
|
+
}
|
|
18
|
+
return `${fileName}.server.jsx`;
|
|
19
|
+
};
|
|
20
|
+
const routesFileContent = fs.readFileSync(routesFilePath, "utf-8");
|
|
21
|
+
const outputSlimRoutesPath = path.resolve(outputDir, addSuffix(MF_SLIM_ROUTES));
|
|
22
|
+
const outputFullRoutesPath = path.resolve(outputDir, addSuffix(MF_FULL_ROUTES));
|
|
23
|
+
const outputRoutesMetaPath = path.resolve(outputDir, `${MF_ROUTES_META}.js`);
|
|
24
|
+
generateSlimRoutes({
|
|
25
|
+
sourceCode: routesFileContent,
|
|
26
|
+
filePath: outputSlimRoutesPath,
|
|
27
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
28
|
+
baseName
|
|
29
|
+
});
|
|
30
|
+
generateRoutes({
|
|
31
|
+
sourceCode: routesFileContent,
|
|
32
|
+
filePath: outputFullRoutesPath,
|
|
33
|
+
prefix: transformName2Prefix(mfConfig.name),
|
|
34
|
+
baseName
|
|
35
|
+
});
|
|
36
|
+
fs.writeFileSync(outputRoutesMetaPath, `export const baseName = '${baseName}';`);
|
|
37
|
+
return {
|
|
38
|
+
outputSlimRoutesPath,
|
|
39
|
+
outputFullRoutesPath,
|
|
40
|
+
outputRoutesMetaPath
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function addExpose(options) {
|
|
44
|
+
const { mfConfig } = options;
|
|
45
|
+
const { outputSlimRoutesPath, outputFullRoutesPath, outputRoutesMetaPath } = generateExtraExposeFiles(options);
|
|
46
|
+
const fullRoutesKey = `./${MF_FULL_ROUTES}`;
|
|
47
|
+
const slimRoutesKey = `./${MF_SLIM_ROUTES}`;
|
|
48
|
+
const routeMetaKey = `./${MF_ROUTES_META}`;
|
|
49
|
+
if (!mfConfig.exposes) {
|
|
50
|
+
mfConfig.exposes = {
|
|
51
|
+
[fullRoutesKey]: outputFullRoutesPath,
|
|
52
|
+
[slimRoutesKey]: outputSlimRoutesPath,
|
|
53
|
+
[routeMetaKey]: outputRoutesMetaPath
|
|
54
|
+
};
|
|
55
|
+
} else {
|
|
56
|
+
if (!Array.isArray(mfConfig.exposes)) {
|
|
57
|
+
if (!mfConfig.exposes[fullRoutesKey]) {
|
|
58
|
+
mfConfig.exposes[fullRoutesKey] = outputFullRoutesPath;
|
|
59
|
+
}
|
|
60
|
+
if (!mfConfig.exposes[slimRoutesKey]) {
|
|
61
|
+
mfConfig.exposes[slimRoutesKey] = outputSlimRoutesPath;
|
|
62
|
+
}
|
|
63
|
+
if (!mfConfig.exposes[routeMetaKey]) {
|
|
64
|
+
mfConfig.exposes[routeMetaKey] = outputRoutesMetaPath;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function addShared(options) {
|
|
70
|
+
const { metaName, mfConfig } = options;
|
|
71
|
+
const alias = `@${metaName}/runtime/router`;
|
|
72
|
+
if (!mfConfig.shared) {
|
|
73
|
+
mfConfig.shared = {
|
|
74
|
+
[alias]: {
|
|
75
|
+
singleton: true
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
} else {
|
|
79
|
+
if (!Array.isArray(mfConfig.shared)) {
|
|
80
|
+
mfConfig.shared[alias] = {
|
|
81
|
+
singleton: true
|
|
82
|
+
};
|
|
83
|
+
} else {
|
|
84
|
+
mfConfig.shared.push(alias);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function _pathMfConfig(options) {
|
|
89
|
+
addShared(options);
|
|
90
|
+
addExpose(options);
|
|
91
|
+
}
|
|
92
|
+
async function _fetchSSRByRouteIds(partialSSRRemotes, mfConfig, transformRuntimeFn) {
|
|
93
|
+
const partialMfConfig = {
|
|
94
|
+
name: mfConfig.name,
|
|
95
|
+
remotes: {
|
|
96
|
+
...mfConfig.remotes
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
replaceRemoteUrl(partialMfConfig, "ipv4");
|
|
100
|
+
const runtimeInitOptions = transformRuntimeFn(partialMfConfig);
|
|
101
|
+
if (!runtimeInitOptions.remotes.length || !partialSSRRemotes.length) {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
const remoteProviderRouteIds = /* @__PURE__ */ new Set();
|
|
105
|
+
const collectIds = (route) => {
|
|
106
|
+
remoteProviderRouteIds.add(route.id);
|
|
107
|
+
if (route.children) {
|
|
108
|
+
route.children.forEach((r) => {
|
|
109
|
+
collectIds(r);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
await Promise.all(runtimeInitOptions.remotes.map(async (remote) => {
|
|
114
|
+
const entry = "entry" in remote ? remote.entry : "";
|
|
115
|
+
if (!entry) {
|
|
116
|
+
return void 0;
|
|
117
|
+
}
|
|
118
|
+
const ipv4 = getIPV4();
|
|
119
|
+
const url = new URL(entry);
|
|
120
|
+
const serializableRoutesUrl = url.href.replace(url.pathname, `/${SERIALIZABLE_ROUTES}`).replace("localhost", ipv4);
|
|
121
|
+
const serializableRoutesRet = await fetch(serializableRoutesUrl);
|
|
122
|
+
const serializableRoutes = await serializableRoutesRet.json();
|
|
123
|
+
serializableRoutes.forEach((serializableRoute) => {
|
|
124
|
+
collectIds(serializableRoute);
|
|
125
|
+
});
|
|
126
|
+
}));
|
|
127
|
+
return [
|
|
128
|
+
...remoteProviderRouteIds
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
function _transformRuntimeOptions(buildOptions) {
|
|
132
|
+
const remotes = buildOptions.remotes || {};
|
|
133
|
+
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
134
|
+
const [_alias, nameAndEntry] = remote;
|
|
135
|
+
const [name, entry] = nameAndEntry.split("@");
|
|
136
|
+
return {
|
|
137
|
+
name,
|
|
138
|
+
entry
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
name: buildOptions.name,
|
|
143
|
+
remotes: runtimeRemotes
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
const moduleFederationDataLoaderPlugin = (enable, internalOptions, userConfig) => ({
|
|
147
|
+
name: "@modern-js/plugin-module-federation-data-loader",
|
|
148
|
+
pre: [
|
|
149
|
+
"@modern-js/plugin-module-federation-config"
|
|
150
|
+
],
|
|
151
|
+
post: [
|
|
152
|
+
"@modern-js/plugin-router",
|
|
153
|
+
"@modern-js/plugin-module-federation"
|
|
154
|
+
],
|
|
155
|
+
setup: async ({ useConfigContext, useAppContext }) => {
|
|
156
|
+
var _modernjsConfig_server, _internalOptions_csrConfig;
|
|
157
|
+
if (!enable) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const { baseName, partialSSRRemotes = [], fetchSSRByRouteIds, patchMFConfig, metaName = META_NAME, serverPlugin = "@module-federation/modern-js/data-loader-server", transformRuntimeOptions } = userConfig;
|
|
161
|
+
if (!baseName) {
|
|
162
|
+
throw new Error(`${PLUGIN_IDENTIFIER} 'baseName' is required if you enable 'dataLoader'!`);
|
|
163
|
+
}
|
|
164
|
+
const modernjsConfig = useConfigContext();
|
|
165
|
+
const appContext = useAppContext();
|
|
166
|
+
const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
167
|
+
const name = (_internalOptions_csrConfig = internalOptions.csrConfig) === null || _internalOptions_csrConfig === void 0 ? void 0 : _internalOptions_csrConfig.name;
|
|
168
|
+
const routesFilePath = path.resolve(appContext.internalDirectory.replace(META_NAME, metaName || META_NAME), `./main/${MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME}`);
|
|
169
|
+
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
170
|
+
return {
|
|
171
|
+
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
172
|
+
plugins.push({
|
|
173
|
+
name: "ssrDataLoader",
|
|
174
|
+
path: "@module-federation/modern-js/data-loader",
|
|
175
|
+
config: {}
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
entrypoint,
|
|
179
|
+
plugins
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
_internalServerPlugins({ plugins }) {
|
|
183
|
+
plugins.push({
|
|
184
|
+
name: serverPlugin,
|
|
185
|
+
options: transformRuntimeFn(internalOptions.csrConfig)
|
|
186
|
+
});
|
|
187
|
+
return {
|
|
188
|
+
plugins
|
|
189
|
+
};
|
|
190
|
+
},
|
|
191
|
+
config: async () => {
|
|
192
|
+
const fetchFn = fetchSSRByRouteIds || _fetchSSRByRouteIds;
|
|
193
|
+
const ssrByRouteIds = await fetchFn(partialSSRRemotes, internalOptions.csrConfig, transformRuntimeFn);
|
|
194
|
+
const patchMFConfigFn = patchMFConfig || _pathMfConfig;
|
|
195
|
+
return {
|
|
196
|
+
server: {
|
|
197
|
+
ssrByRouteIds
|
|
198
|
+
},
|
|
199
|
+
tools: {
|
|
200
|
+
rspack(_config, { isServer }) {
|
|
201
|
+
patchMFConfigFn({
|
|
202
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
203
|
+
baseName,
|
|
204
|
+
metaName,
|
|
205
|
+
isServer,
|
|
206
|
+
routesFilePath
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
webpack(_config, { isServer }) {
|
|
210
|
+
patchMFConfigFn({
|
|
211
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
212
|
+
baseName,
|
|
213
|
+
metaName,
|
|
214
|
+
isServer,
|
|
215
|
+
routesFilePath
|
|
216
|
+
});
|
|
217
|
+
},
|
|
218
|
+
bundlerChain(chain, { isServer }) {
|
|
219
|
+
if (!isServer && internalOptions.csrConfig && Object.keys(internalOptions.csrConfig.exposes).length) {
|
|
220
|
+
chain.plugin("SerializableRoutesPlugin").use(SerializableRoutesPlugin, [
|
|
221
|
+
{
|
|
222
|
+
routesFilePath,
|
|
223
|
+
prefix: transformName2Prefix(internalOptions.csrConfig.name)
|
|
224
|
+
}
|
|
225
|
+
]);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
source: {
|
|
230
|
+
define: {
|
|
231
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
var dataLoaderPlugin_default = moduleFederationDataLoaderPlugin;
|
|
240
|
+
export {
|
|
241
|
+
dataLoaderPlugin_default as default,
|
|
242
|
+
generateRoutes,
|
|
243
|
+
generateSlimRoutes,
|
|
244
|
+
moduleFederationDataLoaderPlugin
|
|
245
|
+
};
|
|
@@ -2,6 +2,7 @@ import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryP
|
|
|
2
2
|
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
3
3
|
import { moduleFederationConfigPlugin } from "./configPlugin";
|
|
4
4
|
import { moduleFederationSSRPlugin } from "./ssrPlugin";
|
|
5
|
+
import { moduleFederationDataLoaderPlugin } from "./dataLoaderPlugin";
|
|
5
6
|
const moduleFederationPlugin = (userConfig = {}) => {
|
|
6
7
|
const internalModernPluginOptions = {
|
|
7
8
|
csrConfig: void 0,
|
|
@@ -53,7 +54,11 @@ const moduleFederationPlugin = (userConfig = {}) => {
|
|
|
53
54
|
},
|
|
54
55
|
usePlugins: [
|
|
55
56
|
moduleFederationConfigPlugin(internalModernPluginOptions),
|
|
56
|
-
moduleFederationSSRPlugin(internalModernPluginOptions)
|
|
57
|
+
moduleFederationSSRPlugin(internalModernPluginOptions),
|
|
58
|
+
moduleFederationDataLoaderPlugin(Boolean(userConfig.dataLoader), internalModernPluginOptions, {
|
|
59
|
+
baseName: "",
|
|
60
|
+
...userConfig.dataLoader
|
|
61
|
+
})
|
|
57
62
|
]
|
|
58
63
|
};
|
|
59
64
|
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { getInstance, init } from "@module-federation/enhanced/runtime";
|
|
2
|
+
import { MF_SLIM_ROUTES } from "../../runtime/constant";
|
|
3
|
+
async function getSlimRemotes(mfInstance) {
|
|
4
|
+
return await Promise.all(mfInstance.options.remotes.map(async (remote) => {
|
|
5
|
+
const { routes, baseName } = await mfInstance.loadRemote(`${remote.name}/${MF_SLIM_ROUTES}`);
|
|
6
|
+
return {
|
|
7
|
+
routes,
|
|
8
|
+
baseName
|
|
9
|
+
};
|
|
10
|
+
}));
|
|
11
|
+
}
|
|
12
|
+
var dataLoaderPlugin_default = (mfRuntimeOptions) => ({
|
|
13
|
+
name: "MFDataLoaderServerPlugin",
|
|
14
|
+
pre: [
|
|
15
|
+
"@modern-js/plugin-inject-resource"
|
|
16
|
+
],
|
|
17
|
+
setup(api) {
|
|
18
|
+
const { remotes, name } = mfRuntimeOptions;
|
|
19
|
+
if (!remotes.length) {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
let isHandled = false;
|
|
23
|
+
return {
|
|
24
|
+
prepare() {
|
|
25
|
+
const { middlewares } = api.useAppContext();
|
|
26
|
+
middlewares.push({
|
|
27
|
+
name: "MFDataLoaderServerPlugin",
|
|
28
|
+
handler: async (c, next) => {
|
|
29
|
+
console.log("isHandled : ", isHandled);
|
|
30
|
+
const serverManifest = c.get("serverManifest");
|
|
31
|
+
const { loaderBundles, nestedRoutesJson } = serverManifest;
|
|
32
|
+
console.log("nestedRoutesJson: ", nestedRoutesJson);
|
|
33
|
+
console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
|
|
34
|
+
if (isHandled) {
|
|
35
|
+
await next();
|
|
36
|
+
} else {
|
|
37
|
+
const instance = getInstance() || init({
|
|
38
|
+
name,
|
|
39
|
+
remotes
|
|
40
|
+
});
|
|
41
|
+
const slimRoutes = await getSlimRemotes(instance);
|
|
42
|
+
slimRoutes.forEach((slimRoute) => {
|
|
43
|
+
const { routes, baseName } = slimRoute;
|
|
44
|
+
routes[0].path = `/${baseName}`;
|
|
45
|
+
loaderBundles.main.routes.push(...routes);
|
|
46
|
+
nestedRoutesJson.default.main.push(...routes);
|
|
47
|
+
});
|
|
48
|
+
console.log("push loaderBundles.main.routes done ");
|
|
49
|
+
isHandled = true;
|
|
50
|
+
await next();
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
before: [
|
|
54
|
+
"render"
|
|
55
|
+
]
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
export {
|
|
62
|
+
dataLoaderPlugin_default as default
|
|
63
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { fs } from "@modern-js/utils";
|
|
3
|
-
import { ModuleFederationPlugin } from "@module-federation/enhanced
|
|
3
|
+
import { ModuleFederationPlugin } from "@module-federation/enhanced";
|
|
4
4
|
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
|
|
5
|
-
import UniverseEntryChunkTrackerPlugin from "@module-federation/node
|
|
5
|
+
import { UniverseEntryChunkTrackerPlugin } from "@module-federation/node";
|
|
6
6
|
import { updateStatsAndManifest } from "./manifest";
|
|
7
7
|
import { isDev } from "./constant";
|
|
8
8
|
function setEnv() {
|
|
File without changes
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { getInstance, loadRemote } from "@module-federation/enhanced/runtime";
|
|
3
|
+
import { MF_FULL_ROUTES } from "./constant";
|
|
4
|
+
globalThis.mfRemoteRoutes = globalThis.mfRemoteRoutes || [];
|
|
5
|
+
var _globalThis_mfHasLoadedRemoteRoutes;
|
|
6
|
+
globalThis.mfHasLoadedRemoteRoutes = (_globalThis_mfHasLoadedRemoteRoutes = globalThis.mfHasLoadedRemoteRoutes) !== null && _globalThis_mfHasLoadedRemoteRoutes !== void 0 ? _globalThis_mfHasLoadedRemoteRoutes : false;
|
|
7
|
+
async function loadRoutes() {
|
|
8
|
+
var _getInstance;
|
|
9
|
+
if (globalThis.mfHasLoadedRemoteRoutes) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const remotes = (_getInstance = getInstance()) === null || _getInstance === void 0 ? void 0 : _getInstance.options.remotes;
|
|
13
|
+
if (!remotes) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const remoteModuleIds = remotes.map((remote) => {
|
|
17
|
+
return `${remote.name}/${MF_FULL_ROUTES}`;
|
|
18
|
+
});
|
|
19
|
+
const traverse = (cRoutes, prefix) => {
|
|
20
|
+
cRoutes.forEach((i) => {
|
|
21
|
+
i.id = prefix + i.id;
|
|
22
|
+
const Comp = i.component;
|
|
23
|
+
i.element = /* @__PURE__ */ _jsx(Comp, {});
|
|
24
|
+
if (i.children) {
|
|
25
|
+
traverse(i.children, prefix);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
await Promise.all(remoteModuleIds.map(async (remoteModuleId) => {
|
|
30
|
+
const remoteName = remoteModuleId.split(`/${MF_FULL_ROUTES}`)[0];
|
|
31
|
+
const { routes, baseName } = await loadRemote(remoteModuleId);
|
|
32
|
+
routes[0].path = `/${baseName}`;
|
|
33
|
+
routes[0].isRoot = false;
|
|
34
|
+
globalThis.mfRemoteRoutes.push(...routes);
|
|
35
|
+
}));
|
|
36
|
+
globalThis.mfHasLoadedRemoteRoutes = true;
|
|
37
|
+
return globalThis.mfRemoteRoutes;
|
|
38
|
+
}
|
|
39
|
+
const ssrDataLoaderPlugin = () => {
|
|
40
|
+
return {
|
|
41
|
+
name: "@modern-js/plugin-mf-data-loader",
|
|
42
|
+
post: [
|
|
43
|
+
"@modern-js/plugin-router"
|
|
44
|
+
],
|
|
45
|
+
setup: () => {
|
|
46
|
+
return {
|
|
47
|
+
async beforeRender({ context }) {
|
|
48
|
+
console.log("init");
|
|
49
|
+
await loadRoutes();
|
|
50
|
+
},
|
|
51
|
+
modifyRoutes: (routes) => {
|
|
52
|
+
console.log("modifyRoutes");
|
|
53
|
+
routes.push(...globalThis.mfRemoteRoutes);
|
|
54
|
+
console.log("routes: ", routes);
|
|
55
|
+
return routes;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
ssrDataLoaderPlugin
|
|
63
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function transformName2Prefix(name) {
|
|
2
|
+
const NameTransformSymbol = {
|
|
3
|
+
AT: "@",
|
|
4
|
+
HYPHEN: "-",
|
|
5
|
+
SLASH: "/",
|
|
6
|
+
UNDERLINE: "_"
|
|
7
|
+
};
|
|
8
|
+
const NameTransformMap = {
|
|
9
|
+
[NameTransformSymbol.AT]: "SCOPE",
|
|
10
|
+
[NameTransformSymbol.HYPHEN]: "HYPHEN",
|
|
11
|
+
[NameTransformSymbol.SLASH]: "SLASH",
|
|
12
|
+
[NameTransformSymbol.UNDERLINE]: "UNDERLINE"
|
|
13
|
+
};
|
|
14
|
+
const SPLIT_SYMBOL = "@";
|
|
15
|
+
return `${name.replace(new RegExp(`${NameTransformSymbol.AT}`, "g"), NameTransformMap[NameTransformSymbol.AT]).replace(new RegExp(`${NameTransformSymbol.HYPHEN}`, "g"), NameTransformMap[NameTransformSymbol.HYPHEN]).replace(new RegExp(`${NameTransformSymbol.SLASH}`, "g"), NameTransformMap[NameTransformSymbol.SLASH]).replace(new RegExp(`${NameTransformSymbol.UNDERLINE}`, "g"), NameTransformMap[NameTransformSymbol.UNDERLINE])}${SPLIT_SYMBOL}`;
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
transformName2Prefix
|
|
19
|
+
};
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import hoistNonReactStatics from "hoist-non-react-statics";
|
|
3
3
|
import { SSRLiveReload } from "./SSRLiveReload";
|
|
4
|
+
console.log("mfSSRPlugin trigger");
|
|
4
5
|
const mfSSRPlugin = () => ({
|
|
5
6
|
name: "@module-federation/modern-js",
|
|
7
|
+
pre: [
|
|
8
|
+
"@modern-js/plugin-router"
|
|
9
|
+
],
|
|
6
10
|
setup: () => {
|
|
7
11
|
return {
|
|
8
|
-
async
|
|
12
|
+
async beforeRender() {
|
|
13
|
+
console.log(111, "beforeRender");
|
|
9
14
|
if (typeof window !== "undefined") {
|
|
10
|
-
return
|
|
11
|
-
context
|
|
12
|
-
});
|
|
15
|
+
return;
|
|
13
16
|
}
|
|
14
17
|
globalThis.shouldUpdate = false;
|
|
15
18
|
const nodeUtils = await import("@module-federation/node/utils");
|
|
@@ -19,9 +22,7 @@ const mfSSRPlugin = () => ({
|
|
|
19
22
|
await nodeUtils.flushChunks();
|
|
20
23
|
globalThis.shouldUpdate = true;
|
|
21
24
|
}
|
|
22
|
-
return
|
|
23
|
-
context
|
|
24
|
-
});
|
|
25
|
+
return;
|
|
25
26
|
},
|
|
26
27
|
wrapRoot(App) {
|
|
27
28
|
const AppWrapper = (props) => /* @__PURE__ */ _jsxs(_Fragment, {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const IS_ROOT = "isRoot";
|
|
2
|
+
export declare const ID = "id";
|
|
3
|
+
export declare const COMPONENT = "component";
|
|
4
|
+
export declare const LAZY_COMPONENT = "lazyImport";
|
|
5
|
+
export declare const SHOULD_REVALIDATE = "shouldRevalidate";
|
|
6
|
+
export declare const PRIVATE_COMPONENT = "_component";
|
|
7
|
+
export declare const ELEMENT = "element";
|
|
8
|
+
export declare const LOADER = "loader";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { webpack } from '@modern-js/app-tools';
|
|
2
|
+
export declare const SERIALIZABLE_ROUTES = "serializable-routes.json";
|
|
3
|
+
declare class SerializableRoutesPlugin implements webpack.WebpackPluginInstance {
|
|
4
|
+
routesFilePath: string;
|
|
5
|
+
prefix: string;
|
|
6
|
+
constructor(options: {
|
|
7
|
+
routesFilePath: string;
|
|
8
|
+
prefix: string;
|
|
9
|
+
});
|
|
10
|
+
apply(compiler: webpack.Compiler): void;
|
|
11
|
+
}
|
|
12
|
+
export { SerializableRoutesPlugin };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { DataLoaderOptions, InternalModernPluginOptions } from '../types';
|
|
3
|
+
import { generateRoutes, generateSlimRoutes } from './ast';
|
|
4
|
+
export declare const moduleFederationDataLoaderPlugin: (enable: boolean, internalOptions: InternalModernPluginOptions, userConfig: DataLoaderOptions) => CliPlugin<AppTools>;
|
|
5
|
+
export default moduleFederationDataLoaderPlugin;
|
|
6
|
+
export { generateRoutes, generateSlimRoutes };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
|
+
import { init } from '@module-federation/enhanced/runtime';
|
|
3
|
+
type MFRuntimeOptions = Parameters<typeof init>[0];
|
|
4
|
+
declare const _default: (mfRuntimeOptions: MFRuntimeOptions) => ServerPlugin;
|
|
5
|
+
export default _default;
|
|
@@ -4,6 +4,7 @@ import { PluginOptions } from '../types';
|
|
|
4
4
|
import { BundlerConfig } from '../interfaces/bundler';
|
|
5
5
|
export type ConfigType<T> = T extends 'webpack' ? webpack.Configuration : T extends 'rspack' ? Rspack.Configuration : never;
|
|
6
6
|
export declare const getMFConfig: (userConfig: PluginOptions) => Promise<moduleFederationPlugin.ModuleFederationPluginOptions>;
|
|
7
|
+
export declare const replaceRemoteUrl: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, remoteIpStrategy?: "ipv4" | "inherit") => void;
|
|
7
8
|
export declare const patchMFConfig: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, isServer: boolean, remoteIpStrategy?: "ipv4" | "inherit") => moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
8
9
|
export declare function patchIgnoreWarning<T extends Bundler>(bundlerConfig: BundlerConfig<T>): void;
|
|
9
10
|
export declare function patchBundlerConfig<T extends Bundler>(options: {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type Route = {
|
|
2
|
+
id: string;
|
|
3
|
+
type: string;
|
|
4
|
+
loader?: boolean;
|
|
5
|
+
index?: boolean;
|
|
6
|
+
isRoot?: boolean;
|
|
7
|
+
children?: Route[];
|
|
8
|
+
};
|
|
9
|
+
export type MFModernRouteJson = {
|
|
10
|
+
baseName: string;
|
|
11
|
+
routes: Record<string, Route[]>;
|
|
12
|
+
prefix: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Plugin } from '@modern-js/runtime';
|
|
2
|
+
import { type RouteObject } from '@modern-js/runtime/router';
|
|
3
|
+
declare global {
|
|
4
|
+
var mfRemoteRoutes: RouteObject[];
|
|
5
|
+
var mfHasLoadedRemoteRoutes: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare const ssrDataLoaderPlugin: () => Plugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function transformName2Prefix(name: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function withMFRouteId(id: string): string;
|