@module-federation/modern-js 0.0.0-next-20241016083243 → 0.0.0-next-20241017090853
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/index.js +5 -4
- package/dist/cjs/cli/routes/exportRoutePlugin.js +77 -0
- package/dist/cjs/cli/routes/importRoutesPlugin.js +120 -0
- package/dist/cjs/cli/routes/plugin.js +155 -0
- package/dist/cjs/cli/{dataLoader/generateRouteFile.js → routes/traverseRouteFiles.js} +12 -12
- package/dist/cjs/cli/{dataLoader/patchMFConfig.js → routes/utils.js} +18 -10
- package/dist/cjs/cli/server/{dataLoaderPlugin.js → fetchRoutePlugin.js} +7 -7
- package/dist/cjs/runtime/{dataLoader → routes}/plugin-inject-assets.js +3 -3
- package/dist/cjs/runtime/{dataLoader → routes}/plugin.js +1 -1
- package/dist/cjs/types/routes.js +16 -0
- package/dist/esm/cli/index.js +6 -4
- package/dist/esm/cli/routes/exportRoutePlugin.js +79 -0
- package/dist/esm/cli/routes/importRoutesPlugin.js +122 -0
- package/dist/esm/cli/{dataLoader → routes}/plugin.js +72 -149
- package/dist/esm/cli/{dataLoader/generateRouteFile.js → routes/traverseRouteFiles.js} +23 -18
- package/dist/esm/cli/{dataLoader/patchMFConfig.js → routes/utils.js} +12 -6
- package/dist/esm/cli/server/{dataLoaderPlugin.js → fetchRoutePlugin.js} +4 -4
- package/dist/esm/runtime/{dataLoader → routes}/plugin-inject-assets.js +3 -3
- package/dist/esm/runtime/{dataLoader → routes}/plugin.js +1 -1
- package/dist/esm/types/routes.js +0 -0
- package/dist/esm-node/cli/index.js +5 -4
- package/dist/esm-node/cli/routes/exportRoutePlugin.js +53 -0
- package/dist/esm-node/cli/routes/importRoutesPlugin.js +96 -0
- package/dist/esm-node/cli/routes/plugin.js +121 -0
- package/dist/esm-node/cli/{dataLoader/generateRouteFile.js → routes/traverseRouteFiles.js} +8 -8
- package/dist/esm-node/cli/{dataLoader/patchMFConfig.js → routes/utils.js} +13 -6
- package/dist/esm-node/cli/server/{dataLoaderPlugin.js → fetchRoutePlugin.js} +4 -4
- package/dist/esm-node/runtime/{dataLoader → routes}/plugin-inject-assets.js +3 -3
- package/dist/esm-node/runtime/{dataLoader → routes}/plugin.js +1 -1
- package/dist/esm-node/types/routes.js +0 -0
- package/dist/types/cli/routes/exportRoutePlugin.d.ts +4 -0
- package/dist/types/cli/routes/importRoutesPlugin.d.ts +4 -0
- package/dist/types/cli/routes/plugin.d.ts +4 -0
- package/dist/types/cli/{dataLoader/generateRouteFile.d.ts → routes/traverseRouteFiles.d.ts} +4 -3
- package/dist/types/cli/{dataLoader/patchMFConfig.d.ts → routes/utils.d.ts} +2 -1
- package/dist/types/cli/server/{dataLoaderPlugin.d.ts → fetchRoutePlugin.d.ts} +2 -2
- package/dist/types/types/index.d.ts +2 -8
- package/dist/types/types/routes.d.ts +19 -0
- package/package.json +32 -32
- package/dist/cjs/cli/dataLoader/plugin.js +0 -219
- package/dist/esm-node/cli/dataLoader/plugin.js +0 -184
- package/dist/types/cli/dataLoader/plugin.d.ts +0 -6
- /package/dist/cjs/cli/{dataLoader → routes}/ast/constant.js +0 -0
- /package/dist/cjs/cli/{dataLoader → routes}/ast/generateRoutes.js +0 -0
- /package/dist/cjs/cli/{dataLoader → routes}/ast/index.js +0 -0
- /package/dist/cjs/cli/{dataLoader → routes}/clearMFCache.js +0 -0
- /package/dist/cjs/runtime/{dataLoader → routes}/utils.js +0 -0
- /package/dist/esm/cli/{dataLoader → routes}/ast/constant.js +0 -0
- /package/dist/esm/cli/{dataLoader → routes}/ast/generateRoutes.js +0 -0
- /package/dist/esm/cli/{dataLoader → routes}/ast/index.js +0 -0
- /package/dist/esm/cli/{dataLoader → routes}/clearMFCache.js +0 -0
- /package/dist/esm/runtime/{dataLoader → routes}/utils.js +0 -0
- /package/dist/esm-node/cli/{dataLoader → routes}/ast/constant.js +0 -0
- /package/dist/esm-node/cli/{dataLoader → routes}/ast/generateRoutes.js +0 -0
- /package/dist/esm-node/cli/{dataLoader → routes}/ast/index.js +0 -0
- /package/dist/esm-node/cli/{dataLoader → routes}/clearMFCache.js +0 -0
- /package/dist/esm-node/runtime/{dataLoader → routes}/utils.js +0 -0
- /package/dist/types/cli/{dataLoader → routes}/ast/constant.d.ts +0 -0
- /package/dist/types/cli/{dataLoader → routes}/ast/generateRoutes.d.ts +0 -0
- /package/dist/types/cli/{dataLoader → routes}/ast/index.d.ts +0 -0
- /package/dist/types/cli/{dataLoader → routes}/clearMFCache.d.ts +0 -0
- /package/dist/types/runtime/{dataLoader → routes}/plugin-inject-assets.d.ts +0 -0
- /package/dist/types/runtime/{dataLoader → routes}/plugin.d.ts +0 -0
- /package/dist/types/runtime/{dataLoader → routes}/utils.d.ts +0 -0
|
@@ -14,9 +14,9 @@ function traverseRoutes(routes, remoteNames) {
|
|
|
14
14
|
var ssrDataLoaderInjectAssetsPlugin = function(param) {
|
|
15
15
|
var metaName = param.metaName;
|
|
16
16
|
return {
|
|
17
|
-
name: "@modern-js/plugin-mf-
|
|
17
|
+
name: "@modern-js/plugin-mf-routes-inject-assets",
|
|
18
18
|
pre: [
|
|
19
|
-
"@modern-js/plugin-mf-
|
|
19
|
+
"@modern-js/plugin-mf-routes"
|
|
20
20
|
],
|
|
21
21
|
post: [
|
|
22
22
|
"@module-federation/modern-js",
|
|
@@ -24,7 +24,7 @@ var ssrDataLoaderInjectAssetsPlugin = function(param) {
|
|
|
24
24
|
],
|
|
25
25
|
setup: function() {
|
|
26
26
|
return {
|
|
27
|
-
wrapRoot: function wrapRoot(
|
|
27
|
+
wrapRoot: function wrapRoot() {
|
|
28
28
|
var AppWrapper = function(props) {
|
|
29
29
|
var instance = getInstance();
|
|
30
30
|
if (!instance || !instance.options.remotes.length) {
|
|
@@ -6,7 +6,7 @@ import { injectRemoteRoutes } from "./utils";
|
|
|
6
6
|
var ssrDataLoaderPlugin = function(param) {
|
|
7
7
|
var metaName = param.metaName, dataLoaderRemotes = param.dataLoaderRemotes;
|
|
8
8
|
return {
|
|
9
|
-
name: "@modern-js/plugin-mf-
|
|
9
|
+
name: "@modern-js/plugin-mf-routes",
|
|
10
10
|
post: [
|
|
11
11
|
"@".concat(metaName, "/plugin-router"),
|
|
12
12
|
"@module-federation/modern-js"
|
|
File without changes
|
|
@@ -2,7 +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 {
|
|
5
|
+
import { moduleFederationRoutesPlugin } from "./routes/plugin";
|
|
6
6
|
const moduleFederationPlugin = (userConfig = {}) => {
|
|
7
7
|
const internalModernPluginOptions = {
|
|
8
8
|
csrConfig: void 0,
|
|
@@ -11,7 +11,7 @@ const moduleFederationPlugin = (userConfig = {}) => {
|
|
|
11
11
|
nodePlugin: void 0,
|
|
12
12
|
distOutputDir: "",
|
|
13
13
|
originPluginOptions: userConfig,
|
|
14
|
-
remoteIpStrategy:
|
|
14
|
+
remoteIpStrategy: void 0
|
|
15
15
|
};
|
|
16
16
|
return {
|
|
17
17
|
name: "@modern-js/plugin-module-federation",
|
|
@@ -55,8 +55,9 @@ const moduleFederationPlugin = (userConfig = {}) => {
|
|
|
55
55
|
usePlugins: [
|
|
56
56
|
moduleFederationConfigPlugin(internalModernPluginOptions),
|
|
57
57
|
moduleFederationSSRPlugin(internalModernPluginOptions),
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
moduleFederationRoutesPlugin({
|
|
59
|
+
userConfig,
|
|
60
|
+
internalOptions: internalModernPluginOptions
|
|
60
61
|
})
|
|
61
62
|
]
|
|
62
63
|
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { META_NAME } from "../../constant";
|
|
2
|
+
import { addExpose } from "./utils";
|
|
3
|
+
const moduleFederationExportRoutePlugin = ({ userConfig, internalOptions, entries }) => ({
|
|
4
|
+
name: "@modern-js/plugin-module-federation-export-routes",
|
|
5
|
+
pre: [
|
|
6
|
+
"@modern-js/plugin-module-federation-route"
|
|
7
|
+
],
|
|
8
|
+
post: [
|
|
9
|
+
"@modern-js/plugin-router",
|
|
10
|
+
"@modern-js/plugin-module-federation"
|
|
11
|
+
],
|
|
12
|
+
setup: async ({ useAppContext }) => {
|
|
13
|
+
if (!userConfig.exportRoutes) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const appContext = useAppContext();
|
|
17
|
+
const { metaName = META_NAME } = internalOptions;
|
|
18
|
+
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
19
|
+
return {
|
|
20
|
+
config: async () => {
|
|
21
|
+
return {
|
|
22
|
+
tools: {
|
|
23
|
+
// bundlerChain can not keep target order
|
|
24
|
+
rspack(_config, { isServer }) {
|
|
25
|
+
addExpose({
|
|
26
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
27
|
+
metaName,
|
|
28
|
+
isServer,
|
|
29
|
+
internalDirectory,
|
|
30
|
+
entries
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
// bundlerChain can not keep target order
|
|
34
|
+
webpack(_config, { isServer }) {
|
|
35
|
+
addExpose({
|
|
36
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
37
|
+
metaName,
|
|
38
|
+
isServer,
|
|
39
|
+
internalDirectory,
|
|
40
|
+
entries
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
var exportRoutePlugin_default = moduleFederationExportRoutePlugin;
|
|
50
|
+
export {
|
|
51
|
+
exportRoutePlugin_default as default,
|
|
52
|
+
moduleFederationExportRoutePlugin
|
|
53
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { META_NAME } from "../../constant";
|
|
2
|
+
import { clearMFCache } from "./clearMFCache";
|
|
3
|
+
function _transformRuntimeOptions(buildOptions) {
|
|
4
|
+
const remotes = buildOptions.remotes || {};
|
|
5
|
+
const runtimeRemotes = Object.entries(remotes).map((remote) => {
|
|
6
|
+
const [alias, nameAndEntry] = remote;
|
|
7
|
+
const [name, entry] = nameAndEntry.split("@");
|
|
8
|
+
return {
|
|
9
|
+
name,
|
|
10
|
+
entry,
|
|
11
|
+
alias
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
name: buildOptions.name,
|
|
16
|
+
remotes: runtimeRemotes
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const moduleFederationImportRoutePlugin = ({ userConfig, internalOptions, remotePathMap, serverPlugin = "@module-federation/modern-js/fetch-router-server-plugin", ssrByRouteIdsMap, transformRuntimeOptions }) => ({
|
|
20
|
+
name: "@modern-js/plugin-module-federation-import-routes",
|
|
21
|
+
pre: [
|
|
22
|
+
"@modern-js/plugin-module-federation-route"
|
|
23
|
+
],
|
|
24
|
+
post: [
|
|
25
|
+
"@modern-js/plugin-router",
|
|
26
|
+
"@modern-js/plugin-module-federation"
|
|
27
|
+
],
|
|
28
|
+
setup: async () => {
|
|
29
|
+
if (!userConfig.importRoutes) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const { metaName = META_NAME } = internalOptions;
|
|
33
|
+
const transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
|
|
34
|
+
const dataLoaderRemotes = [
|
|
35
|
+
...new Set(Object.values(remotePathMap).map((i) => i.name))
|
|
36
|
+
];
|
|
37
|
+
const serverPluginOptions = {
|
|
38
|
+
runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
|
|
39
|
+
dataLoaderRemotes,
|
|
40
|
+
ssrByRouteIdsMap
|
|
41
|
+
};
|
|
42
|
+
const hasDataLoaderRemotes = () => Boolean(serverPluginOptions.dataLoaderRemotes.length);
|
|
43
|
+
return {
|
|
44
|
+
_internalRuntimePlugins: ({ entrypoint, plugins }) => {
|
|
45
|
+
if (!hasDataLoaderRemotes()) {
|
|
46
|
+
return {
|
|
47
|
+
entrypoint,
|
|
48
|
+
plugins
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
plugins.push({
|
|
52
|
+
name: "ssrDataLoader",
|
|
53
|
+
path: "@module-federation/modern-js/routes",
|
|
54
|
+
config: {
|
|
55
|
+
metaName,
|
|
56
|
+
dataLoaderRemotes
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
plugins.push({
|
|
60
|
+
name: "ssrDataLoaderInjectAssets",
|
|
61
|
+
path: "@module-federation/modern-js/routes-inject-assets",
|
|
62
|
+
config: {
|
|
63
|
+
metaName,
|
|
64
|
+
dataLoaderRemotes
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
entrypoint,
|
|
69
|
+
plugins
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
_internalServerPlugins({ plugins }) {
|
|
73
|
+
if (!hasDataLoaderRemotes()) {
|
|
74
|
+
return {
|
|
75
|
+
plugins
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
plugins.push({
|
|
79
|
+
name: serverPlugin,
|
|
80
|
+
options: serverPluginOptions
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
plugins
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
async afterDev() {
|
|
87
|
+
clearMFCache();
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
var importRoutesPlugin_default = moduleFederationImportRoutePlugin;
|
|
93
|
+
export {
|
|
94
|
+
importRoutesPlugin_default as default,
|
|
95
|
+
moduleFederationImportRoutePlugin
|
|
96
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import { transformName2Prefix } from "../../runtime/routes/utils";
|
|
4
|
+
import { META_NAME } from "../../constant";
|
|
5
|
+
import { traverseRouteFiles } from "./traverseRouteFiles";
|
|
6
|
+
import { moduleFederationExportRoutePlugin } from "./exportRoutePlugin";
|
|
7
|
+
import { moduleFederationImportRoutePlugin } from "./importRoutesPlugin";
|
|
8
|
+
import { addShared } from "./utils";
|
|
9
|
+
import { DEFAULT_ENTRY, ROUTE_ID } from "../../constant";
|
|
10
|
+
const moduleFederationRoutesPlugin = ({ userConfig, internalOptions }) => {
|
|
11
|
+
const entries = /* @__PURE__ */ new Set();
|
|
12
|
+
const remotePathMap = {};
|
|
13
|
+
const ssrByRouteIdsMap = {};
|
|
14
|
+
const internalRoutesPluginOptions = {
|
|
15
|
+
userConfig,
|
|
16
|
+
internalOptions,
|
|
17
|
+
entries,
|
|
18
|
+
remotePathMap,
|
|
19
|
+
ssrByRouteIdsMap
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
name: "@modern-js/plugin-module-federation-route",
|
|
23
|
+
pre: [
|
|
24
|
+
"@modern-js/plugin-module-federation-config"
|
|
25
|
+
],
|
|
26
|
+
setup: async ({ useAppContext }) => {
|
|
27
|
+
if (!userConfig.exportRoutes && !userConfig.importRoutes) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const appContext = useAppContext();
|
|
31
|
+
const name = internalOptions.csrConfig.name;
|
|
32
|
+
const { metaName = META_NAME } = internalOptions;
|
|
33
|
+
const internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
|
|
34
|
+
const csrConfig = internalOptions.csrConfig;
|
|
35
|
+
if (typeof csrConfig.remotes === "object" && Object.keys(csrConfig.remotes).length) {
|
|
36
|
+
const { entries: scanEntries } = await traverseRouteFiles({
|
|
37
|
+
appDirectory: appContext.appDirectory,
|
|
38
|
+
generateRouteFile: userConfig.importRoutes,
|
|
39
|
+
remotePathMap
|
|
40
|
+
});
|
|
41
|
+
scanEntries.forEach((e) => entries.add(e));
|
|
42
|
+
} else {
|
|
43
|
+
entries.add(DEFAULT_ENTRY);
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
config: async () => {
|
|
47
|
+
return {
|
|
48
|
+
tools: {
|
|
49
|
+
// bundlerChain can not keep target order
|
|
50
|
+
rspack(_config, { isServer }) {
|
|
51
|
+
addShared({
|
|
52
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
53
|
+
metaName,
|
|
54
|
+
isServer,
|
|
55
|
+
internalDirectory,
|
|
56
|
+
entries
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
// bundlerChain can not keep target order
|
|
60
|
+
webpack(_config, { isServer }) {
|
|
61
|
+
addShared({
|
|
62
|
+
mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
|
|
63
|
+
metaName,
|
|
64
|
+
isServer,
|
|
65
|
+
internalDirectory,
|
|
66
|
+
entries
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
source: {
|
|
71
|
+
define: {
|
|
72
|
+
MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
async modifyFileSystemRoutes(options) {
|
|
78
|
+
const { routes, entrypoint } = options;
|
|
79
|
+
const { entryName } = entrypoint;
|
|
80
|
+
const prefix = transformName2Prefix(internalOptions.csrConfig.name);
|
|
81
|
+
const modifyRouteIds = async (route, pathName) => {
|
|
82
|
+
const currentPathName = route.path ? path.join("/", pathName, route.path) : pathName;
|
|
83
|
+
if (route.id) {
|
|
84
|
+
const originalId = route.id;
|
|
85
|
+
route.originalId = originalId;
|
|
86
|
+
const remoteInfo = remotePathMap[currentPathName];
|
|
87
|
+
if (remoteInfo) {
|
|
88
|
+
const { path: path2, name: name2 } = remoteInfo;
|
|
89
|
+
const remoteIdPrefix = transformName2Prefix(name2);
|
|
90
|
+
const isLayout = Boolean(route.children);
|
|
91
|
+
const newId = `${remoteIdPrefix}${isLayout ? "layout" : "page"}`;
|
|
92
|
+
ssrByRouteIdsMap[originalId] = newId;
|
|
93
|
+
const content = fs.readFileSync(path2, "utf-8");
|
|
94
|
+
await fs.writeFile(path2, content.replace(ROUTE_ID, originalId));
|
|
95
|
+
route.id = newId;
|
|
96
|
+
} else {
|
|
97
|
+
route.id = `${prefix}${route.id}`;
|
|
98
|
+
}
|
|
99
|
+
route.children && await Promise.all(route.children.map(async (r) => {
|
|
100
|
+
return await modifyRouteIds(r, currentPathName);
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
await Promise.all(routes.map(async (route) => {
|
|
105
|
+
await modifyRouteIds(route, entryName === DEFAULT_ENTRY ? "" : entryName);
|
|
106
|
+
}));
|
|
107
|
+
return options;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
usePlugins: [
|
|
112
|
+
moduleFederationExportRoutePlugin(internalRoutesPluginOptions),
|
|
113
|
+
moduleFederationImportRoutePlugin(internalRoutesPluginOptions)
|
|
114
|
+
]
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
var plugin_default = moduleFederationRoutesPlugin;
|
|
118
|
+
export {
|
|
119
|
+
plugin_default as default,
|
|
120
|
+
moduleFederationRoutesPlugin
|
|
121
|
+
};
|
|
@@ -2,7 +2,7 @@ import { fs, normalizeToPosixPath } from "@modern-js/utils";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import os from "os";
|
|
4
4
|
import { DEFAULT_ENTRY, MF_JSON, ROUTE_ID } from "../../constant";
|
|
5
|
-
import { getRemoteLayoutId } from "../../runtime/
|
|
5
|
+
import { getRemoteLayoutId } from "../../runtime/routes/utils";
|
|
6
6
|
const ROUTES = "routes";
|
|
7
7
|
function calcPathName(filePath, rootDir, entry) {
|
|
8
8
|
const relativePath = path.relative(rootDir, filePath);
|
|
@@ -31,10 +31,9 @@ function calcPathName(filePath, rootDir, entry) {
|
|
|
31
31
|
}, []);
|
|
32
32
|
return path.join("/", entry, splitPaths.filter((p) => !!p).join("/"));
|
|
33
33
|
}
|
|
34
|
-
async function
|
|
35
|
-
const { appDirectory } = options;
|
|
34
|
+
async function traverseRouteFiles(options) {
|
|
35
|
+
const { appDirectory, generateRouteFile, remotePathMap } = options;
|
|
36
36
|
const entries = [];
|
|
37
|
-
const remotePathMap = {};
|
|
38
37
|
const srcPath = `${appDirectory}/src`;
|
|
39
38
|
const traverse = async (filepath, rootDir, entry) => {
|
|
40
39
|
if (!await fs.pathExists(filepath)) {
|
|
@@ -88,7 +87,9 @@ async function generateRouteFile(options) {
|
|
|
88
87
|
const entry = path.relative(srcPath, dir);
|
|
89
88
|
entries.push(entry);
|
|
90
89
|
const rootDir = `${dir}/${ROUTES}`;
|
|
91
|
-
|
|
90
|
+
if (generateRouteFile) {
|
|
91
|
+
await traverse(rootDir, rootDir, entry);
|
|
92
|
+
}
|
|
92
93
|
} else {
|
|
93
94
|
await Promise.all(items.map(async (item) => {
|
|
94
95
|
await traverseRoutes(path.join(dir, item), level++);
|
|
@@ -103,11 +104,10 @@ async function generateRouteFile(options) {
|
|
|
103
104
|
return entry;
|
|
104
105
|
});
|
|
105
106
|
return {
|
|
106
|
-
entries: normalizedEntries
|
|
107
|
-
remotePathMap
|
|
107
|
+
entries: normalizedEntries
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
110
|
export {
|
|
111
111
|
calcPathName,
|
|
112
|
-
|
|
112
|
+
traverseRouteFiles
|
|
113
113
|
};
|
|
@@ -76,7 +76,8 @@ function addExpose(options) {
|
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
function addShared(options) {
|
|
79
|
-
const {
|
|
79
|
+
const { mfConfig } = options;
|
|
80
|
+
const styledComponents = `styled-components`;
|
|
80
81
|
const reactRouterDom = `react-router-dom`;
|
|
81
82
|
const reactRouterDomServer = `react-router-dom/server`;
|
|
82
83
|
if (!mfConfig.shared) {
|
|
@@ -86,6 +87,9 @@ function addShared(options) {
|
|
|
86
87
|
},
|
|
87
88
|
[reactRouterDomServer]: {
|
|
88
89
|
singleton: true
|
|
90
|
+
},
|
|
91
|
+
[styledComponents]: {
|
|
92
|
+
singleton: true
|
|
89
93
|
}
|
|
90
94
|
};
|
|
91
95
|
} else {
|
|
@@ -96,6 +100,9 @@ function addShared(options) {
|
|
|
96
100
|
mfConfig.shared[reactRouterDomServer] = {
|
|
97
101
|
singleton: true
|
|
98
102
|
};
|
|
103
|
+
mfConfig.shared[styledComponents] = {
|
|
104
|
+
singleton: true
|
|
105
|
+
};
|
|
99
106
|
} else {
|
|
100
107
|
mfConfig.shared = mfConfig.shared.reduce((sum, cur) => {
|
|
101
108
|
if (typeof cur === "string") {
|
|
@@ -114,13 +121,13 @@ function addShared(options) {
|
|
|
114
121
|
mfConfig.shared[reactRouterDomServer] = {
|
|
115
122
|
singleton: true
|
|
116
123
|
};
|
|
124
|
+
mfConfig.shared[styledComponents] = {
|
|
125
|
+
singleton: true
|
|
126
|
+
};
|
|
117
127
|
}
|
|
118
128
|
}
|
|
119
129
|
}
|
|
120
|
-
function patchMFConfig(options) {
|
|
121
|
-
addShared(options);
|
|
122
|
-
addExpose(options);
|
|
123
|
-
}
|
|
124
130
|
export {
|
|
125
|
-
|
|
131
|
+
addExpose,
|
|
132
|
+
addShared
|
|
126
133
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { init } from "@module-federation/enhanced/runtime";
|
|
2
2
|
import { isBrowserEnv } from "@module-federation/sdk";
|
|
3
3
|
import { fetchRemoteFullRoutesMap, getSSRByRouteIds } from "./fetchRemoteFullRoutesMap";
|
|
4
|
-
import { injectRemoteRoutes } from "../../runtime/
|
|
4
|
+
import { injectRemoteRoutes } from "../../runtime/routes/utils";
|
|
5
5
|
import { MF_ROUTES, DEFAULT_ENTRY, MODERN_JS_ROUTE_SERVER_LOADER } from "../../constant";
|
|
6
|
-
var
|
|
7
|
-
name: "
|
|
6
|
+
var fetchRoutePlugin_default = ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap }) => ({
|
|
7
|
+
name: "@module-federation/modern-js-fetch-route",
|
|
8
8
|
pre: [
|
|
9
9
|
"@modern-js/plugin-inject-resource"
|
|
10
10
|
],
|
|
@@ -57,5 +57,5 @@ var dataLoaderPlugin_default = ({ runtimeOptions, dataLoaderRemotes, ssrByRouteI
|
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
export {
|
|
60
|
-
|
|
60
|
+
fetchRoutePlugin_default as default
|
|
61
61
|
};
|
|
@@ -12,9 +12,9 @@ function traverseRoutes(routes, remoteNames) {
|
|
|
12
12
|
}
|
|
13
13
|
const ssrDataLoaderInjectAssetsPlugin = ({ metaName }) => {
|
|
14
14
|
return {
|
|
15
|
-
name: "@modern-js/plugin-mf-
|
|
15
|
+
name: "@modern-js/plugin-mf-routes-inject-assets",
|
|
16
16
|
pre: [
|
|
17
|
-
"@modern-js/plugin-mf-
|
|
17
|
+
"@modern-js/plugin-mf-routes"
|
|
18
18
|
],
|
|
19
19
|
post: [
|
|
20
20
|
"@module-federation/modern-js",
|
|
@@ -22,7 +22,7 @@ const ssrDataLoaderInjectAssetsPlugin = ({ metaName }) => {
|
|
|
22
22
|
],
|
|
23
23
|
setup: () => {
|
|
24
24
|
return {
|
|
25
|
-
wrapRoot(
|
|
25
|
+
wrapRoot() {
|
|
26
26
|
const AppWrapper = (props) => {
|
|
27
27
|
const instance = getInstance();
|
|
28
28
|
if (!instance || !instance.options.remotes.length) {
|
|
@@ -3,7 +3,7 @@ import { MF_ROUTES, DEFAULT_ENTRY } from "../../constant";
|
|
|
3
3
|
import { injectRemoteRoutes } from "./utils";
|
|
4
4
|
const ssrDataLoaderPlugin = ({ metaName, dataLoaderRemotes }) => {
|
|
5
5
|
return {
|
|
6
|
-
name: "@modern-js/plugin-mf-
|
|
6
|
+
name: "@modern-js/plugin-mf-routes",
|
|
7
7
|
post: [
|
|
8
8
|
`@${metaName}/plugin-router`,
|
|
9
9
|
"@module-federation/modern-js"
|
|
File without changes
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { InternalRoutesPluginOptions } from '../../types/routes';
|
|
3
|
+
export declare const moduleFederationExportRoutePlugin: ({ userConfig, internalOptions, entries, }: InternalRoutesPluginOptions) => CliPlugin<AppTools>;
|
|
4
|
+
export default moduleFederationExportRoutePlugin;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { InternalRoutesPluginOptions } from '../../types/routes';
|
|
3
|
+
export declare const moduleFederationImportRoutePlugin: ({ userConfig, internalOptions, remotePathMap, serverPlugin, ssrByRouteIdsMap, transformRuntimeOptions, }: InternalRoutesPluginOptions) => CliPlugin<AppTools>;
|
|
4
|
+
export default moduleFederationImportRoutePlugin;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CliPlugin, AppTools } from '@modern-js/app-tools';
|
|
2
|
+
import type { RoutesPluginOptions } from '../../types/routes';
|
|
3
|
+
export declare const moduleFederationRoutesPlugin: ({ userConfig, internalOptions, }: RoutesPluginOptions) => CliPlugin<AppTools>;
|
|
4
|
+
export default moduleFederationRoutesPlugin;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export declare function calcPathName(filePath: string, rootDir: string, entry: string): string;
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function traverseRouteFiles(options: {
|
|
3
3
|
appDirectory: string;
|
|
4
|
-
}): Promise<{
|
|
5
|
-
entries: string[];
|
|
6
4
|
remotePathMap: Record<string, {
|
|
7
5
|
name: string;
|
|
8
6
|
path: string;
|
|
9
7
|
}>;
|
|
8
|
+
generateRouteFile?: boolean;
|
|
9
|
+
}): Promise<{
|
|
10
|
+
entries: string[];
|
|
10
11
|
}>;
|
|
@@ -6,5 +6,6 @@ type PatchMFConfigOptions = {
|
|
|
6
6
|
internalDirectory: string;
|
|
7
7
|
entries: Set<string>;
|
|
8
8
|
};
|
|
9
|
-
export declare function
|
|
9
|
+
export declare function addExpose(options: PatchMFConfigOptions): void;
|
|
10
|
+
export declare function addShared(options: PatchMFConfigOptions): void;
|
|
10
11
|
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { ServerPlugin } from '@modern-js/server-core';
|
|
2
2
|
import { init } from '@module-federation/enhanced/runtime';
|
|
3
3
|
type MFRuntimeOptions = Parameters<typeof init>[0];
|
|
4
|
-
export type
|
|
4
|
+
export type FetchRouteServerPluginOptions = {
|
|
5
5
|
runtimeOptions: MFRuntimeOptions;
|
|
6
6
|
dataLoaderRemotes: string[];
|
|
7
7
|
ssrByRouteIdsMap: Record<string, string>;
|
|
8
8
|
};
|
|
9
|
-
declare const _default: ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap, }:
|
|
9
|
+
declare const _default: ({ runtimeOptions, dataLoaderRemotes, ssrByRouteIdsMap, }: FetchRouteServerPluginOptions) => ServerPlugin;
|
|
10
10
|
export default _default;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
2
2
|
import type { ModuleFederationPlugin as WebpackModuleFederationPlugin } from '@module-federation/enhanced';
|
|
3
3
|
import type { ModuleFederationPlugin as RspackModuleFederationPlugin } from '@module-federation/enhanced/rspack';
|
|
4
|
-
import type { init } from '@module-federation/enhanced/runtime';
|
|
5
4
|
export interface PluginOptions {
|
|
6
5
|
config?: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
7
6
|
configPath?: string;
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
exportRoutes?: boolean;
|
|
8
|
+
importRoutes?: boolean;
|
|
10
9
|
}
|
|
11
10
|
export interface InternalModernPluginOptions {
|
|
12
11
|
metaName?: string;
|
|
@@ -19,8 +18,3 @@ export interface InternalModernPluginOptions {
|
|
|
19
18
|
remoteIpStrategy?: 'ipv4' | 'inherit';
|
|
20
19
|
}
|
|
21
20
|
export type BundlerPlugin = WebpackModuleFederationPlugin | RspackModuleFederationPlugin;
|
|
22
|
-
export type TransformRuntimeOptions = (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions) => Parameters<typeof init>[0];
|
|
23
|
-
export type DataLoaderOptions = {
|
|
24
|
-
serverPlugin?: string;
|
|
25
|
-
transformRuntimeOptions?: TransformRuntimeOptions;
|
|
26
|
-
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PluginOptions, InternalModernPluginOptions } from './index';
|
|
2
|
+
import type { init } from '@module-federation/enhanced/runtime';
|
|
3
|
+
import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
4
|
+
type TransformRuntimeOptions = (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions) => Parameters<typeof init>[0];
|
|
5
|
+
export type RoutesPluginOptions = {
|
|
6
|
+
userConfig: PluginOptions;
|
|
7
|
+
internalOptions: InternalModernPluginOptions;
|
|
8
|
+
serverPlugin?: string;
|
|
9
|
+
transformRuntimeOptions?: TransformRuntimeOptions;
|
|
10
|
+
};
|
|
11
|
+
export type InternalRoutesPluginOptions = {
|
|
12
|
+
entries: Set<string>;
|
|
13
|
+
remotePathMap: Record<string, {
|
|
14
|
+
name: string;
|
|
15
|
+
path: string;
|
|
16
|
+
}>;
|
|
17
|
+
ssrByRouteIdsMap: Record<string, string>;
|
|
18
|
+
} & RoutesPluginOptions;
|
|
19
|
+
export {};
|