@module-federation/modern-js 0.0.0-next-20241106033302 → 0.0.0-next-20241106063644

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.
Files changed (115) hide show
  1. package/dist/cjs/cli/index.js +7 -2
  2. package/dist/cjs/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +6 -6
  3. package/dist/cjs/cli/mfRuntimePlugins/node.js +41 -0
  4. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  5. package/dist/cjs/cli/routes/ast/constant.js +49 -0
  6. package/dist/cjs/cli/routes/ast/generateRoutes.js +143 -0
  7. package/dist/cjs/cli/{constant.js → routes/ast/index.js} +6 -6
  8. package/dist/cjs/cli/routes/clearMFCache.js +57 -0
  9. package/dist/cjs/cli/routes/exportRoutePlugin.js +77 -0
  10. package/dist/cjs/cli/routes/importRoutesPlugin.js +120 -0
  11. package/dist/cjs/cli/routes/plugin.js +157 -0
  12. package/dist/cjs/cli/routes/traverseRouteFiles.js +148 -0
  13. package/dist/cjs/cli/routes/utils.js +168 -0
  14. package/dist/cjs/cli/server/fetchRemoteFullRoutesMap.js +72 -0
  15. package/dist/cjs/cli/server/fetchRoutePlugin.js +82 -0
  16. package/dist/cjs/cli/ssrPlugin.js +5 -2
  17. package/dist/cjs/cli/utils.js +11 -6
  18. package/dist/cjs/cli/utils.spec.js +5 -6
  19. package/dist/cjs/constant.js +35 -2
  20. package/dist/cjs/interfaces/route.js +16 -0
  21. package/dist/cjs/runtime/index.js +4 -1
  22. package/dist/cjs/runtime/routes/plugin-inject-assets.js +92 -0
  23. package/dist/cjs/runtime/routes/plugin.js +75 -0
  24. package/dist/cjs/runtime/routes/utils.js +115 -0
  25. package/dist/cjs/{cli/mfRuntimePlugins/shared-strategy.js → runtime/withMFRouteId.js} +11 -23
  26. package/dist/cjs/ssr-runtime/plugin.js +7 -8
  27. package/dist/cjs/types/modern.js +16 -0
  28. package/dist/cjs/types/routes.js +16 -0
  29. package/dist/esm/cli/index.js +7 -2
  30. package/dist/esm/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  31. package/dist/esm/cli/mfRuntimePlugins/node.js +13 -0
  32. package/dist/esm/cli/routes/ast/constant.js +18 -0
  33. package/dist/esm/cli/routes/ast/generateRoutes.js +110 -0
  34. package/dist/esm/cli/routes/ast/index.js +4 -0
  35. package/dist/esm/cli/routes/clearMFCache.js +34 -0
  36. package/dist/esm/cli/routes/exportRoutePlugin.js +79 -0
  37. package/dist/esm/cli/routes/importRoutesPlugin.js +122 -0
  38. package/dist/esm/cli/routes/plugin.js +266 -0
  39. package/dist/esm/cli/routes/traverseRouteFiles.js +313 -0
  40. package/dist/esm/cli/routes/utils.js +130 -0
  41. package/dist/esm/cli/server/fetchRemoteFullRoutesMap.js +93 -0
  42. package/dist/esm/cli/server/fetchRoutePlugin.js +105 -0
  43. package/dist/esm/cli/ssrPlugin.js +6 -3
  44. package/dist/esm/cli/utils.js +9 -5
  45. package/dist/esm/cli/utils.spec.js +5 -6
  46. package/dist/esm/constant.js +23 -1
  47. package/dist/esm/interfaces/route.js +0 -0
  48. package/dist/esm/runtime/index.js +3 -1
  49. package/dist/esm/runtime/routes/plugin-inject-assets.js +71 -0
  50. package/dist/esm/runtime/routes/plugin.js +97 -0
  51. package/dist/esm/runtime/routes/utils.js +86 -0
  52. package/dist/esm/runtime/withMFRouteId.js +7 -0
  53. package/dist/esm/ssr-runtime/plugin.js +8 -11
  54. package/dist/esm/types/modern.js +0 -0
  55. package/dist/esm/types/routes.js +0 -0
  56. package/dist/esm-node/cli/index.js +7 -2
  57. package/dist/esm-node/cli/mfRuntimePlugins/{inject-node-fetch.js → node-fetch.js} +3 -3
  58. package/dist/esm-node/cli/mfRuntimePlugins/node.js +11 -0
  59. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +40 -38
  60. package/dist/esm-node/cli/routes/ast/constant.js +18 -0
  61. package/dist/esm-node/cli/routes/ast/generateRoutes.js +109 -0
  62. package/dist/esm-node/cli/routes/ast/index.js +4 -0
  63. package/dist/esm-node/cli/routes/clearMFCache.js +33 -0
  64. package/dist/esm-node/cli/routes/exportRoutePlugin.js +53 -0
  65. package/dist/esm-node/cli/routes/importRoutesPlugin.js +96 -0
  66. package/dist/esm-node/cli/routes/plugin.js +123 -0
  67. package/dist/esm-node/cli/routes/traverseRouteFiles.js +113 -0
  68. package/dist/esm-node/cli/routes/utils.js +133 -0
  69. package/dist/esm-node/cli/server/fetchRemoteFullRoutesMap.js +47 -0
  70. package/dist/esm-node/cli/server/fetchRoutePlugin.js +62 -0
  71. package/dist/esm-node/cli/ssrPlugin.js +5 -2
  72. package/dist/esm-node/cli/utils.js +9 -5
  73. package/dist/esm-node/cli/utils.spec.js +5 -6
  74. package/dist/esm-node/constant.js +23 -1
  75. package/dist/esm-node/interfaces/route.js +0 -0
  76. package/dist/esm-node/runtime/index.js +3 -1
  77. package/dist/esm-node/runtime/routes/plugin-inject-assets.js +68 -0
  78. package/dist/esm-node/runtime/routes/plugin.js +51 -0
  79. package/dist/esm-node/runtime/routes/utils.js +86 -0
  80. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  81. package/dist/esm-node/ssr-runtime/plugin.js +7 -8
  82. package/dist/esm-node/types/modern.js +0 -0
  83. package/dist/esm-node/types/routes.js +0 -0
  84. package/dist/types/cli/mfRuntimePlugins/node.d.ts +3 -0
  85. package/dist/types/cli/routes/ast/constant.d.ts +8 -0
  86. package/dist/types/cli/routes/ast/generateRoutes.d.ts +5 -0
  87. package/dist/types/cli/routes/ast/index.d.ts +1 -0
  88. package/dist/types/cli/routes/clearMFCache.d.ts +1 -0
  89. package/dist/types/cli/routes/exportRoutePlugin.d.ts +4 -0
  90. package/dist/types/cli/routes/importRoutesPlugin.d.ts +4 -0
  91. package/dist/types/cli/routes/plugin.d.ts +4 -0
  92. package/dist/types/cli/routes/traverseRouteFiles.d.ts +11 -0
  93. package/dist/types/cli/routes/utils.d.ts +11 -0
  94. package/dist/types/cli/server/fetchRemoteFullRoutesMap.d.ts +11 -0
  95. package/dist/types/cli/server/fetchRoutePlugin.d.ts +10 -0
  96. package/dist/types/cli/utils.d.ts +2 -0
  97. package/dist/types/constant.d.ts +11 -0
  98. package/dist/types/interfaces/route.d.ts +12 -0
  99. package/dist/types/runtime/index.d.ts +1 -0
  100. package/dist/types/runtime/routes/plugin-inject-assets.d.ts +4 -0
  101. package/dist/types/runtime/routes/plugin.d.ts +5 -0
  102. package/dist/types/runtime/routes/utils.d.ts +22 -0
  103. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  104. package/dist/types/ssr-runtime/plugin.d.ts +3 -1
  105. package/dist/types/types/index.d.ts +3 -1
  106. package/dist/types/types/modern.d.ts +12 -0
  107. package/dist/types/types/routes.d.ts +19 -0
  108. package/package.json +42 -5
  109. package/dist/esm/cli/constant.js +0 -4
  110. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  111. package/dist/esm-node/cli/constant.js +0 -4
  112. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  113. package/dist/types/cli/constant.d.ts +0 -1
  114. package/dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts +0 -3
  115. /package/dist/types/cli/mfRuntimePlugins/{inject-node-fetch.d.ts → node-fetch.d.ts} +0 -0
@@ -0,0 +1,110 @@
1
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ import fs from "fs";
3
+ import traverse from "@babel/traverse";
4
+ import * as babelParser from "@babel/parser";
5
+ import generate from "@babel/generator";
6
+ import * as t from "@babel/types";
7
+ function findTargetKeyNode(nodeProperties, key) {
8
+ return nodeProperties.find(function(p) {
9
+ return t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key;
10
+ });
11
+ }
12
+ function generateRoutes(param) {
13
+ var sourceCode = param.sourceCode, filePath = param.filePath;
14
+ var ast = babelParser.parse(sourceCode, {
15
+ sourceType: "module"
16
+ });
17
+ var lazyComponentDeclarations = [];
18
+ var componentDeclarations = [];
19
+ var componentId = 0;
20
+ traverse(ast, {
21
+ // ImportDeclaration(path) {
22
+ // const source = path.node.source.value;
23
+ // const routeIdMatch = source.match(/routeId=([^&]+)/);
24
+ // if (routeIdMatch) {
25
+ // const originalRouteId = routeIdMatch[1];
26
+ // const newRouteId = `${prefix}${originalRouteId}`;
27
+ // const newSource = source.replace(
28
+ // /routeId=[^&]+/,
29
+ // `routeId=${newRouteId}`,
30
+ // );
31
+ // path.node.source = t.stringLiteral(newSource);
32
+ // }
33
+ // },
34
+ ObjectExpression: function ObjectExpression(path) {
35
+ var componentName = "";
36
+ var lazyComponentName = "";
37
+ if (!Array.isArray(path.node.properties)) {
38
+ return;
39
+ }
40
+ var idNode = findTargetKeyNode(path.node.properties, "id");
41
+ if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
42
+ }
43
+ var isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
44
+ if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
45
+ isRootNode.value.value = false;
46
+ }
47
+ var lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
48
+ if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
49
+ lazyComponentName = "LazyComponent_".concat(componentId);
50
+ var lazyDeclaration = t.variableDeclaration("const", [
51
+ t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
52
+ ]);
53
+ lazyComponentNode.value = t.identifier(lazyComponentName);
54
+ var componentNode = findTargetKeyNode(path.node.properties, "component");
55
+ if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
56
+ componentNode.value = t.callExpression(t.identifier("lazy"), [
57
+ t.identifier(lazyComponentName)
58
+ ]);
59
+ }
60
+ lazyComponentDeclarations.push(lazyDeclaration);
61
+ }
62
+ var componentNode1 = findTargetKeyNode(path.node.properties, "component");
63
+ if (componentNode1 && t.isObjectProperty(componentNode1) && t.isCallExpression(componentNode1.value) && t.isIdentifier(componentNode1.value.callee)) {
64
+ componentName = "Component_".concat(componentId);
65
+ var componentDeclaration = t.variableDeclaration("const", [
66
+ t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
67
+ t.identifier(lazyComponentName)
68
+ ]) : componentNode1.value)
69
+ ]);
70
+ componentDeclarations.push(componentDeclaration);
71
+ componentNode1.value = t.identifier(componentName);
72
+ }
73
+ if (lazyComponentName || componentName) {
74
+ componentId++;
75
+ if (componentName) {
76
+ var upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
77
+ var jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
78
+ path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
79
+ } else {
80
+ path.node.properties.push(t.objectProperty(t.identifier("Component"), t.callExpression(t.identifier("lazy"), [
81
+ t.identifier(lazyComponentName)
82
+ ])));
83
+ }
84
+ }
85
+ }
86
+ });
87
+ traverse(ast, {
88
+ Program: function Program(path) {
89
+ var lastImportIndex = path.get("body").reduce(function(lastIndex, p, index) {
90
+ if (t.isImportDeclaration(p.node)) {
91
+ lastIndex = index;
92
+ }
93
+ return lastIndex;
94
+ }, -1);
95
+ if (lastImportIndex >= 0) {
96
+ var lastImport = path.get("body.".concat(lastImportIndex));
97
+ _to_consumable_array(componentDeclarations).concat(_to_consumable_array(lazyComponentDeclarations)).forEach(function(declaration) {
98
+ if ("insertAfter" in lastImport) {
99
+ lastImport.insertAfter(declaration);
100
+ }
101
+ });
102
+ }
103
+ }
104
+ });
105
+ var code = generate(ast).code;
106
+ fs.writeFileSync(filePath, code);
107
+ }
108
+ export {
109
+ generateRoutes
110
+ };
@@ -0,0 +1,4 @@
1
+ import { generateRoutes } from "./generateRoutes";
2
+ export {
3
+ generateRoutes
4
+ };
@@ -0,0 +1,34 @@
1
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
2
+ function clearMFCache() {
3
+ if (!globalThis.__FEDERATION__) {
4
+ console.log("clearMFCache no globalThis.__FEDERATION__");
5
+ return;
6
+ }
7
+ globalThis.__FEDERATION__.__INSTANCES__.map(function(i) {
8
+ i.moduleCache.forEach(function(mc) {
9
+ if (mc.remoteInfo && mc.remoteInfo.entryGlobalName) {
10
+ delete globalThis[mc.remoteInfo.entryGlobalName];
11
+ }
12
+ });
13
+ i.moduleCache.clear();
14
+ if (globalThis[i.name]) {
15
+ delete globalThis[i.name];
16
+ }
17
+ });
18
+ Object.keys(globalThis.__FEDERATION__).forEach(function(key) {
19
+ if (Array.isArray(globalThis.__FEDERATION__[key])) {
20
+ globalThis.__FEDERATION__[key] = [];
21
+ } else if (key === "__PRELOADED_MAP__") {
22
+ globalThis.__FEDERATION__[key] = /* @__PURE__ */ new Map();
23
+ } else if (_type_of(globalThis.__FEDERATION__[key]) === "object") {
24
+ globalThis.__FEDERATION__[key] = {};
25
+ }
26
+ });
27
+ if (globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__) {
28
+ globalThis.__GLOBAL_LOADING_REMOTE_ENTRY__ = {};
29
+ }
30
+ globalThis.FORCE_MF_REFRESH = true;
31
+ }
32
+ export {
33
+ clearMFCache
34
+ };
@@ -0,0 +1,79 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { META_NAME } from "../../constant";
4
+ import { addExpose } from "./utils";
5
+ var moduleFederationExportRoutePlugin = function(param) {
6
+ var userConfig = param.userConfig, internalOptions = param.internalOptions, entries = param.entries;
7
+ return {
8
+ name: "@modern-js/plugin-module-federation-export-routes",
9
+ pre: [
10
+ "@modern-js/plugin-module-federation-route"
11
+ ],
12
+ post: [
13
+ "@modern-js/plugin-router",
14
+ "@modern-js/plugin-module-federation"
15
+ ],
16
+ setup: function() {
17
+ var _ref = _async_to_generator(function(param2) {
18
+ var useAppContext, appContext, _internalOptions_metaName, metaName, internalDirectory;
19
+ return _ts_generator(this, function(_state) {
20
+ useAppContext = param2.useAppContext;
21
+ if (!userConfig.exportRoutes) {
22
+ return [
23
+ 2
24
+ ];
25
+ }
26
+ appContext = useAppContext();
27
+ _internalOptions_metaName = internalOptions.metaName, metaName = _internalOptions_metaName === void 0 ? META_NAME : _internalOptions_metaName;
28
+ internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
29
+ return [
30
+ 2,
31
+ {
32
+ config: /* @__PURE__ */ _async_to_generator(function() {
33
+ return _ts_generator(this, function(_state2) {
34
+ return [
35
+ 2,
36
+ {
37
+ tools: {
38
+ // bundlerChain can not keep target order
39
+ rspack: function rspack(_config, param3) {
40
+ var isServer = param3.isServer;
41
+ addExpose({
42
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
43
+ metaName,
44
+ isServer,
45
+ internalDirectory,
46
+ entries
47
+ });
48
+ },
49
+ // bundlerChain can not keep target order
50
+ webpack: function webpack(_config, param3) {
51
+ var isServer = param3.isServer;
52
+ addExpose({
53
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
54
+ metaName,
55
+ isServer,
56
+ internalDirectory,
57
+ entries
58
+ });
59
+ }
60
+ }
61
+ }
62
+ ];
63
+ });
64
+ })
65
+ }
66
+ ];
67
+ });
68
+ });
69
+ return function(_) {
70
+ return _ref.apply(this, arguments);
71
+ };
72
+ }()
73
+ };
74
+ };
75
+ var exportRoutePlugin_default = moduleFederationExportRoutePlugin;
76
+ export {
77
+ exportRoutePlugin_default as default,
78
+ moduleFederationExportRoutePlugin
79
+ };
@@ -0,0 +1,122 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
3
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
+ import { META_NAME } from "../../constant";
6
+ import { clearMFCache } from "./clearMFCache";
7
+ function _transformRuntimeOptions(buildOptions) {
8
+ var remotes = buildOptions.remotes || {};
9
+ var runtimeRemotes = Object.entries(remotes).map(function(remote) {
10
+ var _remote = _sliced_to_array(remote, 2), alias = _remote[0], nameAndEntry = _remote[1];
11
+ var _nameAndEntry_split = _sliced_to_array(nameAndEntry.split("@"), 2), name = _nameAndEntry_split[0], entry = _nameAndEntry_split[1];
12
+ return {
13
+ name,
14
+ entry,
15
+ alias
16
+ };
17
+ });
18
+ return {
19
+ name: buildOptions.name,
20
+ remotes: runtimeRemotes
21
+ };
22
+ }
23
+ var moduleFederationImportRoutePlugin = function(param) {
24
+ var userConfig = param.userConfig, internalOptions = param.internalOptions, remotePathMap = param.remotePathMap, _param_serverPlugin = param.serverPlugin, serverPlugin = _param_serverPlugin === void 0 ? "@module-federation/modern-js/fetch-router-server-plugin" : _param_serverPlugin, ssrByRouteIdsMap = param.ssrByRouteIdsMap, transformRuntimeOptions = param.transformRuntimeOptions;
25
+ return {
26
+ name: "@modern-js/plugin-module-federation-import-routes",
27
+ pre: [
28
+ "@modern-js/plugin-module-federation-route"
29
+ ],
30
+ post: [
31
+ "@modern-js/plugin-router",
32
+ "@modern-js/plugin-module-federation"
33
+ ],
34
+ setup: /* @__PURE__ */ _async_to_generator(function() {
35
+ var _internalOptions_metaName, metaName, transformRuntimeFn, dataLoaderRemotes, serverPluginOptions, hasDataLoaderRemotes;
36
+ return _ts_generator(this, function(_state) {
37
+ if (!userConfig.importRoutes) {
38
+ return [
39
+ 2
40
+ ];
41
+ }
42
+ _internalOptions_metaName = internalOptions.metaName, metaName = _internalOptions_metaName === void 0 ? META_NAME : _internalOptions_metaName;
43
+ transformRuntimeFn = transformRuntimeOptions || _transformRuntimeOptions;
44
+ dataLoaderRemotes = _to_consumable_array(new Set(Object.values(remotePathMap).map(function(i) {
45
+ return i.name;
46
+ })));
47
+ serverPluginOptions = {
48
+ runtimeOptions: transformRuntimeFn(internalOptions.csrConfig),
49
+ dataLoaderRemotes,
50
+ ssrByRouteIdsMap
51
+ };
52
+ hasDataLoaderRemotes = function() {
53
+ return Boolean(serverPluginOptions.dataLoaderRemotes.length);
54
+ };
55
+ return [
56
+ 2,
57
+ {
58
+ _internalRuntimePlugins: function(param2) {
59
+ var entrypoint = param2.entrypoint, plugins = param2.plugins;
60
+ if (!hasDataLoaderRemotes()) {
61
+ return {
62
+ entrypoint,
63
+ plugins
64
+ };
65
+ }
66
+ plugins.push({
67
+ name: "ssrDataLoader",
68
+ path: "@module-federation/modern-js/routes",
69
+ config: {
70
+ metaName,
71
+ dataLoaderRemotes
72
+ }
73
+ });
74
+ plugins.push({
75
+ name: "ssrDataLoaderInjectAssets",
76
+ path: "@module-federation/modern-js/routes-inject-assets",
77
+ config: {
78
+ metaName,
79
+ dataLoaderRemotes
80
+ }
81
+ });
82
+ return {
83
+ entrypoint,
84
+ plugins
85
+ };
86
+ },
87
+ _internalServerPlugins: function _internalServerPlugins(param2) {
88
+ var plugins = param2.plugins;
89
+ if (!hasDataLoaderRemotes()) {
90
+ return {
91
+ plugins
92
+ };
93
+ }
94
+ plugins.push({
95
+ name: serverPlugin,
96
+ options: serverPluginOptions
97
+ });
98
+ return {
99
+ plugins
100
+ };
101
+ },
102
+ afterDev: function afterDev() {
103
+ return _async_to_generator(function() {
104
+ return _ts_generator(this, function(_state2) {
105
+ clearMFCache();
106
+ return [
107
+ 2
108
+ ];
109
+ });
110
+ })();
111
+ }
112
+ }
113
+ ];
114
+ });
115
+ })
116
+ };
117
+ };
118
+ var importRoutesPlugin_default = moduleFederationImportRoutePlugin;
119
+ export {
120
+ importRoutesPlugin_default as default,
121
+ moduleFederationImportRoutePlugin
122
+ };
@@ -0,0 +1,266 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _type_of } from "@swc/helpers/_/_type_of";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import path from "path";
5
+ import { fs } from "@modern-js/utils";
6
+ import { transformName2Prefix } from "../../runtime/routes/utils";
7
+ import { META_NAME } from "../../constant";
8
+ import { traverseRouteFiles } from "./traverseRouteFiles";
9
+ import { moduleFederationExportRoutePlugin } from "./exportRoutePlugin";
10
+ import { moduleFederationImportRoutePlugin } from "./importRoutesPlugin";
11
+ import { addShared } from "./utils";
12
+ import { DEFAULT_ENTRY, ROUTE_ID } from "../../constant";
13
+ var moduleFederationRoutesPlugin = function(param) {
14
+ var userConfig = param.userConfig, internalOptions = param.internalOptions, serverPlugin = param.serverPlugin, transformRuntimeOptions = param.transformRuntimeOptions;
15
+ var entries = /* @__PURE__ */ new Set();
16
+ var remotePathMap = {};
17
+ var ssrByRouteIdsMap = {};
18
+ var internalRoutesPluginOptions = {
19
+ userConfig,
20
+ internalOptions,
21
+ entries,
22
+ remotePathMap,
23
+ ssrByRouteIdsMap,
24
+ serverPlugin,
25
+ transformRuntimeOptions
26
+ };
27
+ return {
28
+ name: "@modern-js/plugin-module-federation-route",
29
+ pre: [
30
+ "@modern-js/plugin-module-federation-config"
31
+ ],
32
+ setup: function() {
33
+ var _ref = _async_to_generator(function(param2) {
34
+ var useAppContext, appContext, name, _internalOptions_metaName, metaName, internalDirectory, csrConfig, _ref2, scanEntries;
35
+ return _ts_generator(this, function(_state) {
36
+ switch (_state.label) {
37
+ case 0:
38
+ useAppContext = param2.useAppContext;
39
+ if (!userConfig.exportRoutes && !userConfig.importRoutes) {
40
+ return [
41
+ 2
42
+ ];
43
+ }
44
+ appContext = useAppContext();
45
+ name = internalOptions.csrConfig.name;
46
+ _internalOptions_metaName = internalOptions.metaName, metaName = _internalOptions_metaName === void 0 ? META_NAME : _internalOptions_metaName;
47
+ internalDirectory = appContext.internalDirectory.replace(META_NAME, metaName || META_NAME);
48
+ csrConfig = internalOptions.csrConfig;
49
+ if (!(_type_of(csrConfig.remotes) === "object" && Object.keys(csrConfig.remotes).length))
50
+ return [
51
+ 3,
52
+ 2
53
+ ];
54
+ return [
55
+ 4,
56
+ traverseRouteFiles({
57
+ appDirectory: appContext.appDirectory,
58
+ generateRouteFile: userConfig.importRoutes,
59
+ remotePathMap
60
+ })
61
+ ];
62
+ case 1:
63
+ _ref2 = _state.sent(), scanEntries = _ref2.entries;
64
+ scanEntries.forEach(function(e) {
65
+ return entries.add(e);
66
+ });
67
+ return [
68
+ 3,
69
+ 3
70
+ ];
71
+ case 2:
72
+ entries.add(DEFAULT_ENTRY);
73
+ _state.label = 3;
74
+ case 3:
75
+ return [
76
+ 2,
77
+ {
78
+ config: /* @__PURE__ */ _async_to_generator(function() {
79
+ return _ts_generator(this, function(_state2) {
80
+ return [
81
+ 2,
82
+ {
83
+ tools: {
84
+ // bundlerChain can not keep target order
85
+ rspack: function rspack(_config, param3) {
86
+ var isServer = param3.isServer;
87
+ addShared({
88
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
89
+ metaName,
90
+ isServer,
91
+ internalDirectory,
92
+ entries
93
+ });
94
+ },
95
+ // bundlerChain can not keep target order
96
+ webpack: function webpack(_config, param3) {
97
+ var isServer = param3.isServer;
98
+ addShared({
99
+ mfConfig: isServer ? internalOptions.ssrConfig : internalOptions.csrConfig,
100
+ metaName,
101
+ isServer,
102
+ internalDirectory,
103
+ entries
104
+ });
105
+ }
106
+ },
107
+ source: {
108
+ define: {
109
+ MODERN_ROUTER_ID_PREFIX: JSON.stringify(transformName2Prefix(name))
110
+ }
111
+ }
112
+ }
113
+ ];
114
+ });
115
+ }),
116
+ modifyFileSystemRoutes: function modifyFileSystemRoutes(options) {
117
+ return _async_to_generator(function() {
118
+ var routes, entrypoint, entryName, prefix, modifyRouteIds;
119
+ return _ts_generator(this, function(_state2) {
120
+ switch (_state2.label) {
121
+ case 0:
122
+ routes = options.routes, entrypoint = options.entrypoint;
123
+ entryName = entrypoint.entryName;
124
+ prefix = transformName2Prefix(internalOptions.csrConfig.name);
125
+ modifyRouteIds = function() {
126
+ var _ref3 = _async_to_generator(function(route, pathName) {
127
+ var currentPathName, originalId, remoteInfo, _$path, name2, remoteIdPrefix, isLayout, newId, content, _tmp;
128
+ return _ts_generator(this, function(_state3) {
129
+ switch (_state3.label) {
130
+ case 0:
131
+ currentPathName = route.path ? path.join("/", pathName, route.path) : pathName;
132
+ if (!route.id)
133
+ return [
134
+ 3,
135
+ 6
136
+ ];
137
+ originalId = route.id;
138
+ route.originalId = originalId;
139
+ remoteInfo = remotePathMap[currentPathName];
140
+ if (!remoteInfo)
141
+ return [
142
+ 3,
143
+ 2
144
+ ];
145
+ _$path = remoteInfo.path, name2 = remoteInfo.name;
146
+ remoteIdPrefix = transformName2Prefix(name2);
147
+ isLayout = Boolean(route.children);
148
+ newId = "".concat(remoteIdPrefix).concat(isLayout ? "layout" : "page");
149
+ ssrByRouteIdsMap[originalId] = newId;
150
+ content = fs.readFileSync(_$path, "utf-8");
151
+ return [
152
+ 4,
153
+ fs.writeFile(_$path, content.replace(ROUTE_ID, originalId))
154
+ ];
155
+ case 1:
156
+ _state3.sent();
157
+ route.id = newId;
158
+ return [
159
+ 3,
160
+ 3
161
+ ];
162
+ case 2:
163
+ route.id = "".concat(prefix).concat(route.id);
164
+ _state3.label = 3;
165
+ case 3:
166
+ _tmp = route.children;
167
+ if (!_tmp)
168
+ return [
169
+ 3,
170
+ 5
171
+ ];
172
+ return [
173
+ 4,
174
+ Promise.all(route.children.map(function() {
175
+ var _ref4 = _async_to_generator(function(r) {
176
+ return _ts_generator(this, function(_state4) {
177
+ switch (_state4.label) {
178
+ case 0:
179
+ return [
180
+ 4,
181
+ modifyRouteIds(r, currentPathName)
182
+ ];
183
+ case 1:
184
+ return [
185
+ 2,
186
+ _state4.sent()
187
+ ];
188
+ }
189
+ });
190
+ });
191
+ return function(r) {
192
+ return _ref4.apply(this, arguments);
193
+ };
194
+ }()))
195
+ ];
196
+ case 4:
197
+ _tmp = _state3.sent();
198
+ _state3.label = 5;
199
+ case 5:
200
+ _tmp;
201
+ _state3.label = 6;
202
+ case 6:
203
+ return [
204
+ 2
205
+ ];
206
+ }
207
+ });
208
+ });
209
+ return function modifyRouteIds2(route, pathName) {
210
+ return _ref3.apply(this, arguments);
211
+ };
212
+ }();
213
+ return [
214
+ 4,
215
+ Promise.all(routes.map(function() {
216
+ var _ref3 = _async_to_generator(function(route) {
217
+ return _ts_generator(this, function(_state3) {
218
+ switch (_state3.label) {
219
+ case 0:
220
+ return [
221
+ 4,
222
+ modifyRouteIds(route, entryName === DEFAULT_ENTRY ? "" : entryName)
223
+ ];
224
+ case 1:
225
+ _state3.sent();
226
+ return [
227
+ 2
228
+ ];
229
+ }
230
+ });
231
+ });
232
+ return function(route) {
233
+ return _ref3.apply(this, arguments);
234
+ };
235
+ }()))
236
+ ];
237
+ case 1:
238
+ _state2.sent();
239
+ return [
240
+ 2,
241
+ options
242
+ ];
243
+ }
244
+ });
245
+ })();
246
+ }
247
+ }
248
+ ];
249
+ }
250
+ });
251
+ });
252
+ return function(_) {
253
+ return _ref.apply(this, arguments);
254
+ };
255
+ }(),
256
+ usePlugins: [
257
+ moduleFederationExportRoutePlugin(internalRoutesPluginOptions),
258
+ moduleFederationImportRoutePlugin(internalRoutesPluginOptions)
259
+ ]
260
+ };
261
+ };
262
+ var plugin_default = moduleFederationRoutesPlugin;
263
+ export {
264
+ plugin_default as default,
265
+ moduleFederationRoutesPlugin
266
+ };