@module-federation/modern-js 0.0.0-next-20240812114132 → 0.0.0-next-20240814073142

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 (63) hide show
  1. package/dist/cjs/cli/ast/constant.js +46 -0
  2. package/dist/cjs/cli/ast/generateRoutes.js +140 -0
  3. package/dist/cjs/cli/ast/generateSlimRoutes.js +106 -0
  4. package/dist/cjs/cli/ast/index.js +31 -0
  5. package/dist/cjs/cli/configPlugin.js +1 -0
  6. package/dist/cjs/cli/constant.js +3 -0
  7. package/dist/cjs/cli/dataLoaderPlugin.js +205 -0
  8. package/dist/cjs/cli/index.js +6 -1
  9. package/dist/cjs/cli/server/dataLoaderPlugin.js +87 -0
  10. package/dist/cjs/interfaces/route.js +16 -0
  11. package/dist/cjs/runtime/constant.js +34 -0
  12. package/dist/cjs/runtime/dataLoader.js +87 -0
  13. package/dist/cjs/runtime/index.js +4 -1
  14. package/dist/cjs/runtime/utils.js +43 -0
  15. package/dist/cjs/runtime/withMFRouteId.js +31 -0
  16. package/dist/cjs/ssr-runtime/plugin.js +5 -7
  17. package/dist/esm/cli/ast/constant.js +16 -0
  18. package/dist/esm/cli/ast/generateRoutes.js +107 -0
  19. package/dist/esm/cli/ast/generateSlimRoutes.js +75 -0
  20. package/dist/esm/cli/ast/index.js +6 -0
  21. package/dist/esm/cli/configPlugin.js +1 -0
  22. package/dist/esm/cli/constant.js +2 -0
  23. package/dist/esm/cli/dataLoaderPlugin.js +191 -0
  24. package/dist/esm/cli/index.js +6 -1
  25. package/dist/esm/cli/server/dataLoaderPlugin.js +132 -0
  26. package/dist/esm/interfaces/route.js +0 -0
  27. package/dist/esm/runtime/constant.js +8 -0
  28. package/dist/esm/runtime/dataLoader.js +125 -0
  29. package/dist/esm/runtime/index.js +3 -1
  30. package/dist/esm/runtime/utils.js +16 -0
  31. package/dist/esm/runtime/withMFRouteId.js +7 -0
  32. package/dist/esm/ssr-runtime/plugin.js +5 -10
  33. package/dist/esm-node/cli/ast/constant.js +16 -0
  34. package/dist/esm-node/cli/ast/generateRoutes.js +106 -0
  35. package/dist/esm-node/cli/ast/generateSlimRoutes.js +72 -0
  36. package/dist/esm-node/cli/ast/index.js +6 -0
  37. package/dist/esm-node/cli/configPlugin.js +1 -0
  38. package/dist/esm-node/cli/constant.js +2 -0
  39. package/dist/esm-node/cli/dataLoaderPlugin.js +169 -0
  40. package/dist/esm-node/cli/index.js +6 -1
  41. package/dist/esm-node/cli/server/dataLoaderPlugin.js +67 -0
  42. package/dist/esm-node/interfaces/route.js +0 -0
  43. package/dist/esm-node/runtime/constant.js +8 -0
  44. package/dist/esm-node/runtime/dataLoader.js +63 -0
  45. package/dist/esm-node/runtime/index.js +3 -1
  46. package/dist/esm-node/runtime/utils.js +19 -0
  47. package/dist/esm-node/runtime/withMFRouteId.js +7 -0
  48. package/dist/esm-node/ssr-runtime/plugin.js +5 -7
  49. package/dist/types/cli/ast/constant.d.ts +7 -0
  50. package/dist/types/cli/ast/generateRoutes.d.ts +7 -0
  51. package/dist/types/cli/ast/generateSlimRoutes.d.ts +7 -0
  52. package/dist/types/cli/ast/index.d.ts +2 -0
  53. package/dist/types/cli/constant.d.ts +1 -0
  54. package/dist/types/cli/dataLoaderPlugin.d.ts +6 -0
  55. package/dist/types/cli/server/dataLoaderPlugin.d.ts +4 -0
  56. package/dist/types/interfaces/route.d.ts +13 -0
  57. package/dist/types/runtime/constant.d.ts +3 -0
  58. package/dist/types/runtime/dataLoader.d.ts +7 -0
  59. package/dist/types/runtime/index.d.ts +1 -0
  60. package/dist/types/runtime/utils.d.ts +1 -0
  61. package/dist/types/runtime/withMFRouteId.d.ts +1 -0
  62. package/dist/types/types/index.d.ts +15 -0
  63. package/package.json +36 -10
@@ -1,9 +1,11 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
4
  import { ModuleFederationPlugin as WebpackModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
4
5
  import { ModuleFederationPlugin as RspackModuleFederationPlugin } from "@module-federation/enhanced/rspack";
5
6
  import { moduleFederationConfigPlugin } from "./configPlugin";
6
7
  import { moduleFederationSSRPlugin } from "./ssrPlugin";
8
+ import { moduleFederationDataLoaderPlugin } from "./dataLoaderPlugin";
7
9
  var moduleFederationPlugin = function() {
8
10
  var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
9
11
  var internalModernPluginOptions = {
@@ -79,7 +81,10 @@ var moduleFederationPlugin = function() {
79
81
  }(),
80
82
  usePlugins: [
81
83
  moduleFederationConfigPlugin(internalModernPluginOptions),
82
- moduleFederationSSRPlugin(internalModernPluginOptions)
84
+ moduleFederationSSRPlugin(internalModernPluginOptions),
85
+ moduleFederationDataLoaderPlugin(Boolean(userConfig.dataLoader), internalModernPluginOptions, _object_spread({
86
+ baseName: ""
87
+ }, userConfig.dataLoader))
83
88
  ]
84
89
  };
85
90
  };
@@ -0,0 +1,132 @@
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 { getInstance, init } from "@module-federation/enhanced/runtime";
6
+ import { MF_SLIM_ROUTES } from "../../runtime/constant";
7
+ function dataLoaderPlugin_default(options) {
8
+ return {
9
+ name: "MFDataLoaderServerPlugin",
10
+ pre: [
11
+ "@modern-js/plugin-inject-resource"
12
+ ],
13
+ setup: function setup(api) {
14
+ var mfConfig = options.csrConfig;
15
+ var remotes = mfConfig.remotes;
16
+ if (!remotes || !Object.keys(remotes).length) {
17
+ return {};
18
+ }
19
+ var runtimeRemotes = Object.entries(remotes).map(function(item) {
20
+ var _item = _sliced_to_array(item, 2), _alias = _item[0], nameAndEntry = _item[1];
21
+ var _nameAndEntry_split = _sliced_to_array(nameAndEntry.split("@"), 2), name = _nameAndEntry_split[0], entry = _nameAndEntry_split[1];
22
+ return {
23
+ name,
24
+ entry
25
+ };
26
+ });
27
+ var isHandled = false;
28
+ return {
29
+ prepare: function prepare() {
30
+ var middlewares = api.useAppContext().middlewares;
31
+ middlewares.push({
32
+ name: "MFDataLoaderServerPlugin",
33
+ handler: function() {
34
+ var _ref = _async_to_generator(function(c, next) {
35
+ var serverManifest, loaderBundles, instance, slimRoutes;
36
+ return _ts_generator(this, function(_state) {
37
+ switch (_state.label) {
38
+ case 0:
39
+ console.log("isHandled : ", isHandled);
40
+ serverManifest = c.get("serverManifest");
41
+ loaderBundles = serverManifest.loaderBundles;
42
+ console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
43
+ if (!isHandled)
44
+ return [
45
+ 3,
46
+ 2
47
+ ];
48
+ return [
49
+ 4,
50
+ next()
51
+ ];
52
+ case 1:
53
+ _state.sent();
54
+ return [
55
+ 3,
56
+ 5
57
+ ];
58
+ case 2:
59
+ instance = getInstance() || init({
60
+ name: mfConfig.name,
61
+ remotes: runtimeRemotes
62
+ });
63
+ return [
64
+ 4,
65
+ Promise.all(runtimeRemotes.map(function() {
66
+ var _ref2 = _async_to_generator(function(remote) {
67
+ var _ref3, routes, baseName;
68
+ return _ts_generator(this, function(_state2) {
69
+ switch (_state2.label) {
70
+ case 0:
71
+ return [
72
+ 4,
73
+ instance.loadRemote("".concat(remote.name, "/").concat(MF_SLIM_ROUTES))
74
+ ];
75
+ case 1:
76
+ _ref3 = _state2.sent(), routes = _ref3.routes, baseName = _ref3.baseName;
77
+ return [
78
+ 2,
79
+ {
80
+ routes,
81
+ baseName
82
+ }
83
+ ];
84
+ }
85
+ });
86
+ });
87
+ return function(remote) {
88
+ return _ref2.apply(this, arguments);
89
+ };
90
+ }()))
91
+ ];
92
+ case 3:
93
+ slimRoutes = _state.sent();
94
+ slimRoutes.forEach(function(slimRoute) {
95
+ var _loaderBundles_main_routes;
96
+ var routes = slimRoute.routes, baseName = slimRoute.baseName;
97
+ routes[0].path = "/".concat(baseName);
98
+ (_loaderBundles_main_routes = loaderBundles.main.routes).push.apply(_loaderBundles_main_routes, _to_consumable_array(routes));
99
+ });
100
+ console.log("loaderBundles.main.routes : ", loaderBundles.main.routes);
101
+ isHandled = true;
102
+ return [
103
+ 4,
104
+ next()
105
+ ];
106
+ case 4:
107
+ _state.sent();
108
+ _state.label = 5;
109
+ case 5:
110
+ return [
111
+ 2
112
+ ];
113
+ }
114
+ });
115
+ });
116
+ return function(c, next) {
117
+ return _ref.apply(this, arguments);
118
+ };
119
+ }(),
120
+ before: [
121
+ "render"
122
+ ]
123
+ });
124
+ }
125
+ };
126
+ }
127
+ };
128
+ }
129
+ ;
130
+ export {
131
+ dataLoaderPlugin_default as default
132
+ };
File without changes
@@ -0,0 +1,8 @@
1
+ var MF_SLIM_ROUTES = "mf-slim-routes";
2
+ var MF_FULL_ROUTES = "mf-full-routes";
3
+ var MF_ROUTES_META = "mf-routes-meta";
4
+ export {
5
+ MF_FULL_ROUTES,
6
+ MF_ROUTES_META,
7
+ MF_SLIM_ROUTES
8
+ };
@@ -0,0 +1,125 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ import { getInstance, loadRemote } from "@module-federation/enhanced/runtime";
6
+ import { MF_FULL_ROUTES } from "./constant";
7
+ globalThis.mfRemoteRoutes = globalThis.mfRemoteRoutes || [];
8
+ var _globalThis_mfHasLoadedRemoteRoutes;
9
+ globalThis.mfHasLoadedRemoteRoutes = (_globalThis_mfHasLoadedRemoteRoutes = globalThis.mfHasLoadedRemoteRoutes) !== null && _globalThis_mfHasLoadedRemoteRoutes !== void 0 ? _globalThis_mfHasLoadedRemoteRoutes : false;
10
+ function loadRoutes() {
11
+ return _loadRoutes.apply(this, arguments);
12
+ }
13
+ function _loadRoutes() {
14
+ _loadRoutes = _async_to_generator(function() {
15
+ var _getInstance, remotes, remoteModuleIds, traverse;
16
+ return _ts_generator(this, function(_state) {
17
+ switch (_state.label) {
18
+ case 0:
19
+ if (globalThis.mfHasLoadedRemoteRoutes) {
20
+ return [
21
+ 2
22
+ ];
23
+ }
24
+ remotes = (_getInstance = getInstance()) === null || _getInstance === void 0 ? void 0 : _getInstance.options.remotes;
25
+ if (!remotes) {
26
+ return [
27
+ 2
28
+ ];
29
+ }
30
+ remoteModuleIds = remotes.map(function(remote) {
31
+ return "".concat(remote.name, "/").concat(MF_FULL_ROUTES);
32
+ });
33
+ traverse = function(cRoutes, prefix) {
34
+ cRoutes.forEach(function(i) {
35
+ i.id = prefix + i.id;
36
+ var Comp = i.component;
37
+ i.element = /* @__PURE__ */ _jsx(Comp, {});
38
+ if (i.children) {
39
+ traverse(i.children, prefix);
40
+ }
41
+ });
42
+ };
43
+ return [
44
+ 4,
45
+ Promise.all(remoteModuleIds.map(function() {
46
+ var _ref = _async_to_generator(function(remoteModuleId) {
47
+ var _globalThis_mfRemoteRoutes, remoteName, _ref2, routes, baseName;
48
+ return _ts_generator(this, function(_state2) {
49
+ switch (_state2.label) {
50
+ case 0:
51
+ remoteName = remoteModuleId.split("/".concat(MF_FULL_ROUTES))[0];
52
+ return [
53
+ 4,
54
+ loadRemote(remoteModuleId)
55
+ ];
56
+ case 1:
57
+ _ref2 = _state2.sent(), routes = _ref2.routes, baseName = _ref2.baseName;
58
+ routes[0].path = "/".concat(baseName);
59
+ routes[0].isRoot = false;
60
+ (_globalThis_mfRemoteRoutes = globalThis.mfRemoteRoutes).push.apply(_globalThis_mfRemoteRoutes, _to_consumable_array(routes));
61
+ return [
62
+ 2
63
+ ];
64
+ }
65
+ });
66
+ });
67
+ return function(remoteModuleId) {
68
+ return _ref.apply(this, arguments);
69
+ };
70
+ }()))
71
+ ];
72
+ case 1:
73
+ _state.sent();
74
+ globalThis.mfHasLoadedRemoteRoutes = true;
75
+ return [
76
+ 2,
77
+ globalThis.mfRemoteRoutes
78
+ ];
79
+ }
80
+ });
81
+ });
82
+ return _loadRoutes.apply(this, arguments);
83
+ }
84
+ var ssrDataLoaderPlugin = function() {
85
+ return {
86
+ name: "@modern-js/plugin-mf-data-loader",
87
+ post: [
88
+ "@modern-js/plugin-router"
89
+ ],
90
+ setup: function() {
91
+ return {
92
+ beforeRender: function beforeRender(param) {
93
+ var context = param.context;
94
+ return _async_to_generator(function() {
95
+ return _ts_generator(this, function(_state) {
96
+ switch (_state.label) {
97
+ case 0:
98
+ console.log("init");
99
+ return [
100
+ 4,
101
+ loadRoutes()
102
+ ];
103
+ case 1:
104
+ _state.sent();
105
+ return [
106
+ 2
107
+ ];
108
+ }
109
+ });
110
+ })();
111
+ },
112
+ modifyRoutes: function(routes) {
113
+ var _routes;
114
+ console.log("modifyRoutes");
115
+ (_routes = routes).push.apply(_routes, _to_consumable_array(globalThis.mfRemoteRoutes));
116
+ console.log("routes: ", routes);
117
+ return routes;
118
+ }
119
+ };
120
+ }
121
+ };
122
+ };
123
+ export {
124
+ ssrDataLoaderPlugin
125
+ };
@@ -1,5 +1,7 @@
1
1
  export * from "@module-federation/enhanced/runtime";
2
2
  import { createRemoteSSRComponent } from "./createRemoteSSRComponent";
3
+ import { withMFRouteId } from "./withMFRouteId";
3
4
  export {
4
- createRemoteSSRComponent
5
+ createRemoteSSRComponent,
6
+ withMFRouteId
5
7
  };
@@ -0,0 +1,16 @@
1
+ import { _ as _define_property } from "@swc/helpers/_/_define_property";
2
+ function transformName2Prefix(name) {
3
+ var NameTransformSymbol = {
4
+ AT: "@",
5
+ HYPHEN: "-",
6
+ SLASH: "/",
7
+ UNDERLINE: "_"
8
+ };
9
+ var _obj;
10
+ var NameTransformMap = (_obj = {}, _define_property(_obj, NameTransformSymbol.AT, "SCOPE"), _define_property(_obj, NameTransformSymbol.HYPHEN, "HYPHEN"), _define_property(_obj, NameTransformSymbol.SLASH, "SLASH"), _define_property(_obj, NameTransformSymbol.UNDERLINE, "UNDERLINE"), _obj);
11
+ var SPLIT_SYMBOL = "@";
12
+ return "".concat(name.replace(new RegExp("".concat(NameTransformSymbol.AT), "g"), NameTransformMap[NameTransformSymbol.AT]).replace(new RegExp("".concat(NameTransformSymbol.HYPHEN), "g"), NameTransformMap[NameTransformSymbol.HYPHEN]).replace(new RegExp("".concat(NameTransformSymbol.SLASH), "g"), NameTransformMap[NameTransformSymbol.SLASH]).replace(new RegExp("".concat(NameTransformSymbol.UNDERLINE), "g"), NameTransformMap[NameTransformSymbol.UNDERLINE])).concat(SPLIT_SYMBOL);
13
+ }
14
+ export {
15
+ transformName2Prefix
16
+ };
@@ -0,0 +1,7 @@
1
+ function withMFRouteId(id) {
2
+ var prefix = typeof MODERN_ROUTER_ID_PREFIX === "string" ? MODERN_ROUTER_ID_PREFIX : "";
3
+ return prefix + id;
4
+ }
5
+ export {
6
+ withMFRouteId
7
+ };
@@ -4,24 +4,22 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
4
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
5
5
  import hoistNonReactStatics from "hoist-non-react-statics";
6
6
  import { SSRLiveReload } from "./SSRLiveReload";
7
+ console.log("mfSSRPlugin trigger");
7
8
  var mfSSRPlugin = function() {
8
9
  return {
9
10
  name: "@module-federation/modern-js",
10
11
  setup: function() {
11
12
  return {
12
- init: function init(param, next) {
13
- var context = param.context;
13
+ beforeRender: function beforeRender() {
14
14
  return _async_to_generator(function() {
15
15
  var nodeUtils, shouldUpdate;
16
16
  return _ts_generator(this, function(_state) {
17
17
  switch (_state.label) {
18
18
  case 0:
19
+ console.log(111, "beforeRender");
19
20
  if (typeof window !== "undefined") {
20
21
  return [
21
- 2,
22
- next({
23
- context
24
- })
22
+ 2
25
23
  ];
26
24
  }
27
25
  globalThis.shouldUpdate = false;
@@ -53,10 +51,7 @@ var mfSSRPlugin = function() {
53
51
  _state.label = 4;
54
52
  case 4:
55
53
  return [
56
- 2,
57
- next({
58
- context
59
- })
54
+ 2
60
55
  ];
61
56
  }
62
57
  });
@@ -0,0 +1,16 @@
1
+ const IS_ROOT = "isRoot";
2
+ const ID = "id";
3
+ const COMPONENT = "component";
4
+ const LAZY_COMPONENT = "lazyImport";
5
+ const SHOULD_REVALIDATE = "shouldRevalidate";
6
+ const PRIVATE_COMPONENT = "_component";
7
+ const ELEMENT = "element";
8
+ export {
9
+ COMPONENT,
10
+ ELEMENT,
11
+ ID,
12
+ IS_ROOT,
13
+ LAZY_COMPONENT,
14
+ PRIVATE_COMPONENT,
15
+ SHOULD_REVALIDATE
16
+ };
@@ -0,0 +1,106 @@
1
+ import fs from "fs";
2
+ import traverse from "@babel/traverse";
3
+ import * as babelParser from "@babel/parser";
4
+ import generate from "@babel/generator";
5
+ import * as t from "@babel/types";
6
+ function findTargetKeyNode(nodeProperties, key) {
7
+ return nodeProperties.find((p) => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === key);
8
+ }
9
+ function generateRoutes({ sourceCode, filePath, prefix, baseName }) {
10
+ const ast = babelParser.parse(sourceCode, {
11
+ sourceType: "module"
12
+ });
13
+ const lazyComponentDeclarations = [];
14
+ const componentDeclarations = [];
15
+ let componentId = 0;
16
+ traverse(ast, {
17
+ ImportDeclaration(path) {
18
+ const source = path.node.source.value;
19
+ const routeIdMatch = source.match(/routeId=([^&]+)/);
20
+ if (routeIdMatch) {
21
+ const originalRouteId = routeIdMatch[1];
22
+ const newRouteId = `${prefix}${originalRouteId}`;
23
+ const newSource = source.replace(/routeId=[^&]+/, `routeId=${newRouteId}`);
24
+ path.node.source = t.stringLiteral(newSource);
25
+ }
26
+ },
27
+ ObjectExpression(path) {
28
+ let componentName = "";
29
+ let lazyComponentName = "";
30
+ if (!Array.isArray(path.node.properties)) {
31
+ return;
32
+ }
33
+ const idNode = findTargetKeyNode(path.node.properties, "id");
34
+ if (idNode && t.isObjectProperty(idNode) && t.isStringLiteral(idNode.value)) {
35
+ idNode.value = t.stringLiteral(`${prefix}${idNode.value.value}`);
36
+ }
37
+ const isRootNode = findTargetKeyNode(path.node.properties, "isRoot");
38
+ if (isRootNode && t.isObjectProperty(isRootNode) && t.isBooleanLiteral(isRootNode.value)) {
39
+ isRootNode.value.value = false;
40
+ }
41
+ if (!isRootNode) {
42
+ const lazyComponentNode = findTargetKeyNode(path.node.properties, "lazyImport");
43
+ if (lazyComponentNode && t.isObjectProperty(lazyComponentNode) && t.isArrowFunctionExpression(lazyComponentNode.value)) {
44
+ lazyComponentName = `LazyComponent_${componentId}`;
45
+ const lazyDeclaration = t.variableDeclaration("const", [
46
+ t.variableDeclarator(t.identifier(lazyComponentName), lazyComponentNode.value)
47
+ ]);
48
+ lazyComponentNode.value = t.identifier(lazyComponentName);
49
+ const componentNode2 = findTargetKeyNode(path.node.properties, "component");
50
+ if (componentNode2 && t.isObjectProperty(componentNode2) && t.isCallExpression(componentNode2.value) && t.isIdentifier(componentNode2.value.callee)) {
51
+ componentNode2.value = t.callExpression(t.identifier("lazy"), [
52
+ t.identifier(lazyComponentName)
53
+ ]);
54
+ }
55
+ lazyComponentDeclarations.push(lazyDeclaration);
56
+ }
57
+ }
58
+ const componentNode = findTargetKeyNode(path.node.properties, "component");
59
+ if (componentNode && t.isObjectProperty(componentNode) && t.isCallExpression(componentNode.value) && t.isIdentifier(componentNode.value.callee)) {
60
+ componentName = `Component_${componentId}`;
61
+ const componentDeclaration = t.variableDeclaration("const", [
62
+ t.variableDeclarator(t.identifier(componentName), lazyComponentName ? t.callExpression(t.identifier("lazy"), [
63
+ t.identifier(lazyComponentName)
64
+ ]) : componentNode.value)
65
+ ]);
66
+ componentDeclarations.push(componentDeclaration);
67
+ componentNode.value = t.identifier(componentName);
68
+ }
69
+ if (lazyComponentName || componentName) {
70
+ componentId++;
71
+ if (componentName) {
72
+ const upperFirstName = componentName.slice(0, 1).toUpperCase() + componentName.slice(1);
73
+ const jsxElement = t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier(upperFirstName), [], true), null, [], true);
74
+ path.node.properties.push(t.objectProperty(t.identifier("element"), jsxElement));
75
+ }
76
+ }
77
+ }
78
+ });
79
+ traverse(ast, {
80
+ Program(path) {
81
+ const lastImportIndex = path.get("body").reduce((lastIndex, p, index) => {
82
+ if (t.isImportDeclaration(p.node)) {
83
+ lastIndex = index;
84
+ }
85
+ return lastIndex;
86
+ }, -1);
87
+ if (lastImportIndex >= 0) {
88
+ const lastImport = path.get(`body.${lastImportIndex}`);
89
+ [
90
+ ...componentDeclarations,
91
+ ...lazyComponentDeclarations
92
+ ].forEach((declaration) => {
93
+ if ("insertAfter" in lastImport) {
94
+ lastImport.insertAfter(declaration);
95
+ }
96
+ });
97
+ }
98
+ }
99
+ });
100
+ const { code: newCode } = generate(ast);
101
+ const finalCode = `${newCode}export const baseName = '${baseName}';`;
102
+ fs.writeFileSync(filePath, finalCode);
103
+ }
104
+ export {
105
+ generateRoutes
106
+ };
@@ -0,0 +1,72 @@
1
+ import fs from "fs";
2
+ import traverse from "@babel/traverse";
3
+ import * as babelParser from "@babel/parser";
4
+ import generate from "@babel/generator";
5
+ import * as t from "@babel/types";
6
+ import { COMPONENT, ID, SHOULD_REVALIDATE, LAZY_COMPONENT, PRIVATE_COMPONENT } from "./constant";
7
+ function generateSlimRoutes({ sourceCode, filePath, prefix, baseName }) {
8
+ const ast = babelParser.parse(sourceCode, {
9
+ sourceType: "module"
10
+ });
11
+ const removedKeys = [
12
+ COMPONENT,
13
+ SHOULD_REVALIDATE,
14
+ LAZY_COMPONENT,
15
+ PRIVATE_COMPONENT
16
+ ];
17
+ traverse(ast, {
18
+ ImportDeclaration(path) {
19
+ const source = path.node.source.value;
20
+ const routeIdMatch = source.match(/routeId=([^&]+)/);
21
+ if (routeIdMatch) {
22
+ const originalRouteId = routeIdMatch[1];
23
+ const newRouteId = `${prefix}${originalRouteId}`;
24
+ const newSource = source.replace(/routeId=[^&]+/, `routeId=${newRouteId}`);
25
+ path.node.source = t.stringLiteral(newSource);
26
+ }
27
+ },
28
+ ObjectExpression(path) {
29
+ if (!Array.isArray(path.node.properties)) {
30
+ return;
31
+ }
32
+ path.node.properties.forEach((prop) => {
33
+ if (t.isObjectProperty(prop) && t.isStringLiteral(prop.key) && t.isStringLiteral(prop.value) && prop.key.value === ID) {
34
+ prop.value = t.stringLiteral(`${prefix}${prop.value.value}`);
35
+ }
36
+ });
37
+ path.node.properties = path.node.properties.filter((p) => {
38
+ if (t.isObjectProperty(p) && t.isStringLiteral(p.key)) {
39
+ return !removedKeys.includes(p.key.value);
40
+ } else {
41
+ return true;
42
+ }
43
+ });
44
+ }
45
+ });
46
+ const tempCode = generate(ast).code;
47
+ const tempAst = babelParser.parse(tempCode, {
48
+ sourceType: "module"
49
+ });
50
+ const usedIdentifiers = /* @__PURE__ */ new Set();
51
+ traverse(tempAst, {
52
+ Identifier(path) {
53
+ if (t.isProperty(path.parent)) {
54
+ usedIdentifiers.add(path.node.name);
55
+ }
56
+ }
57
+ });
58
+ traverse(tempAst, {
59
+ ImportDeclaration(path) {
60
+ path.node.specifiers = path.node.specifiers.filter((specifier) => usedIdentifiers.has(specifier.local.name));
61
+ if (!path.node.specifiers.length) {
62
+ path.remove();
63
+ }
64
+ }
65
+ });
66
+ const { code: newCode } = generate(tempAst);
67
+ const finalCode = `${newCode}export const baseName = '${baseName}';`;
68
+ fs.writeFileSync(filePath, finalCode);
69
+ }
70
+ export {
71
+ generateSlimRoutes
72
+ };
@@ -0,0 +1,6 @@
1
+ import { generateRoutes } from "./generateRoutes";
2
+ import { generateSlimRoutes } from "./generateSlimRoutes";
3
+ export {
4
+ generateRoutes,
5
+ generateSlimRoutes
6
+ };
@@ -32,6 +32,7 @@ const moduleFederationConfigPlugin = (userConfig) => ({
32
32
  return {
33
33
  config: async () => {
34
34
  var _modernjsConfig_server, _modernjsConfig_dev;
35
+ console.log("config plugin config");
35
36
  const bundlerType = useAppContext().bundlerType === "rspack" ? "rspack" : "webpack";
36
37
  const ipv4 = getIPV4();
37
38
  const enableSSR = Boolean(modernjsConfig === null || modernjsConfig === void 0 ? void 0 : (_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
@@ -1,4 +1,6 @@
1
1
  const isDev = process.env.NODE_ENV === "development";
2
+ const MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME = "routes.js";
2
3
  export {
4
+ MODERN_JS_FILE_SYSTEM_ROUTES_FILE_NAME,
3
5
  isDev
4
6
  };