@modern-js/app-tools 2.28.0 → 2.28.1-alpha.1

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.
@@ -74,11 +74,13 @@ ${initialize || ""}`);
74
74
  ${initialize || ""}`).join("\n");
75
75
  };
76
76
  const generateCode = async (appContext, config, entrypoints, api) => {
77
+ var _config, _config_runtime_router, _config_runtime, _config1;
77
78
  const { internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName } = appContext;
78
79
  const hookRunners = api.useHookRunners();
79
80
  const isV5 = (0, _utils.isRouterV5)(config);
80
81
  const getRoutes = isV5 ? _getClientRoutes.getClientRoutesLegacy : _getClientRoutes.getClientRoutes;
81
82
  const importsStatemets = /* @__PURE__ */ new Map();
83
+ const oldVersion = typeof ((_config = config) === null || _config === void 0 ? void 0 : _config.runtime.router) === "object" ? Boolean((_config1 = config) === null || _config1 === void 0 ? void 0 : (_config_runtime = _config1.runtime) === null || _config_runtime === void 0 ? void 0 : (_config_runtime_router = _config_runtime.router) === null || _config_runtime_router === void 0 ? void 0 : _config_runtime_router.oldVersion) : false;
82
84
  await Promise.all(entrypoints.map(generateEntryCode));
83
85
  return {
84
86
  importsStatemets
@@ -87,9 +89,9 @@ const generateCode = async (appContext, config, entrypoints, api) => {
87
89
  const { entryName, isMainEntry, isAutoMount, fileSystemRoutes } = entrypoint;
88
90
  if (isAutoMount) {
89
91
  if (fileSystemRoutes) {
90
- var _config_output, _config;
92
+ var _config_output, _config2;
91
93
  let initialRoutes = [];
92
- let nestedRoute = null;
94
+ let nestedRoutes = null;
93
95
  if (entrypoint.entry) {
94
96
  initialRoutes = getRoutes({
95
97
  entrypoint,
@@ -100,12 +102,19 @@ const generateCode = async (appContext, config, entrypoints, api) => {
100
102
  });
101
103
  }
102
104
  if (!isV5 && entrypoint.nestedRoutesEntry) {
103
- nestedRoute = await (0, _nestedRoutes.walk)(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
105
+ nestedRoutes = await (0, _nestedRoutes.walk)(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
104
106
  name: internalSrcAlias,
105
107
  basename: srcDirectory
106
- }, entrypoint.entryName, entrypoint.isMainEntry);
107
- if (nestedRoute) {
108
- initialRoutes.unshift(nestedRoute);
108
+ }, entrypoint.entryName, entrypoint.isMainEntry, oldVersion);
109
+ if (nestedRoutes) {
110
+ if (!Array.isArray(nestedRoutes)) {
111
+ nestedRoutes = [
112
+ nestedRoutes
113
+ ];
114
+ }
115
+ for (const route of nestedRoutes) {
116
+ initialRoutes.unshift(route);
117
+ }
109
118
  }
110
119
  }
111
120
  const { routes } = await hookRunners.modifyFileSystemRoutes({
@@ -134,7 +143,7 @@ const generateCode = async (appContext, config, entrypoints, api) => {
134
143
  nestedRoutesEntry: entrypoint.nestedRoutesEntry,
135
144
  entryName: entrypoint.entryName,
136
145
  internalDirectory,
137
- splitRouteChunks: (_config = config2) === null || _config === void 0 ? void 0 : (_config_output = _config.output) === null || _config_output === void 0 ? void 0 : _config_output.splitRouteChunks
146
+ splitRouteChunks: (_config2 = config2) === null || _config2 === void 0 ? void 0 : (_config_output = _config2.output) === null || _config_output === void 0 ? void 0 : _config_output.splitRouteChunks
138
147
  })
139
148
  });
140
149
  if (entrypoint.nestedRoutesEntry && (0, _utils.isUseSSRBundle)(config2)) {
@@ -13,6 +13,9 @@ _export(exports, {
13
13
  getRouteId: function() {
14
14
  return getRouteId;
15
15
  },
16
+ optimizeRoute: function() {
17
+ return optimizeRoute;
18
+ },
16
19
  walk: function() {
17
20
  return walk;
18
21
  }
@@ -56,8 +59,32 @@ const createRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
56
59
  type: "nested"
57
60
  };
58
61
  };
59
- const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
60
- var _routePath, _route_children;
62
+ const optimizeRoute = (routeTree) => {
63
+ if (!routeTree.children || routeTree.children.length === 0) {
64
+ return [
65
+ routeTree
66
+ ];
67
+ }
68
+ const { children } = routeTree;
69
+ if (!routeTree._component) {
70
+ const newRoutes = children.map((child) => {
71
+ var _routeTree_path;
72
+ const routePath = `${(_routeTree_path = routeTree.path) !== null && _routeTree_path !== void 0 ? _routeTree_path : ""}${child.path ? `/${child.path}` : ""}`;
73
+ return {
74
+ ...child,
75
+ path: routePath
76
+ };
77
+ });
78
+ return newRoutes.flatMap((newRoute) => optimizeRoute(newRoute));
79
+ } else {
80
+ routeTree.children = children.flatMap(optimizeRoute);
81
+ return [
82
+ routeTree
83
+ ];
84
+ }
85
+ };
86
+ const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
87
+ var _routePath, _finalRoute_children;
61
88
  if (!await _utils.fs.pathExists(dirname)) {
62
89
  return null;
63
90
  }
@@ -93,10 +120,10 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
93
120
  const itemWithoutExt = item.slice(0, -extname.length);
94
121
  const isDirectory2 = (await _utils.fs.stat(itemPath)).isDirectory();
95
122
  if (isDirectory2) {
96
- const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry);
97
- if (childRoute) {
98
- var _route_children1;
99
- (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(childRoute);
123
+ const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
124
+ if (childRoute && !Array.isArray(childRoute)) {
125
+ var _route_children;
126
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
100
127
  }
101
128
  }
102
129
  if (extname && (!_constants.JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
@@ -122,7 +149,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
122
149
  pageConfigFile = itemPath;
123
150
  }
124
151
  if (itemWithoutExt === _constants.NESTED_ROUTE.PAGE_FILE) {
125
- var _route_children2;
152
+ var _route_children1;
126
153
  pageRoute = createIndexRoute({
127
154
  _component: (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name)
128
155
  }, rootDir, itemPath, entryName, isMainEntry);
@@ -132,13 +159,13 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
132
159
  if (pageConfigFile) {
133
160
  pageRoute.config = pageConfigFile;
134
161
  }
135
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(pageRoute);
162
+ (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
136
163
  }
137
164
  if (itemWithoutExt === _constants.NESTED_ROUTE.SPLATE_LOADER_FILE) {
138
165
  splatLoaderFile = itemPath;
139
166
  }
140
167
  if (itemWithoutExt === _constants.NESTED_ROUTE.SPLATE_FILE) {
141
- var _route_children3;
168
+ var _route_children2;
142
169
  splatRoute = createRoute({
143
170
  _component: (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name),
144
171
  path: "*"
@@ -146,7 +173,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
146
173
  if (splatLoaderFile) {
147
174
  splatRoute.loader = splatLoaderFile;
148
175
  }
149
- (_route_children3 = route.children) === null || _route_children3 === void 0 ? void 0 : _route_children3.push(splatRoute);
176
+ (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
150
177
  }
151
178
  if (itemWithoutExt === _constants.NESTED_ROUTE.LOADING_FILE) {
152
179
  route.loading = (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name);
@@ -159,12 +186,12 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
159
186
  if (isPathlessLayout) {
160
187
  delete finalRoute.path;
161
188
  }
162
- route.children = (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.filter((childRoute) => childRoute);
163
- if (route.children && route.children.length === 0 && !route.index) {
189
+ const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
190
+ if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
164
191
  return null;
165
192
  }
166
- if (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
167
- const childRoute = finalRoute.children[0];
193
+ if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
194
+ const childRoute = childRoutes[0];
168
195
  if (childRoute.path === "*") {
169
196
  const path = `${finalRoute.path || ""}/${childRoute.path || ""}`;
170
197
  finalRoute = {
@@ -173,5 +200,18 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
173
200
  };
174
201
  }
175
202
  }
203
+ if (splatRoute) {
204
+ var _childRoutes;
205
+ const slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex((childRoute) => childRoute === splatRoute);
206
+ if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
207
+ var _childRoutes1, _childRoutes2;
208
+ (_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
209
+ (_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
210
+ }
211
+ }
212
+ if (isRoot && !oldVersion) {
213
+ const optimizedRoutes = optimizeRoute(finalRoute);
214
+ return optimizedRoutes;
215
+ }
176
216
  return finalRoute;
177
217
  };
@@ -94,13 +94,13 @@ export var createImportStatements = function(statements) {
94
94
  };
95
95
  export var generateCode = function() {
96
96
  var _ref = _async_to_generator(function(appContext, config, entrypoints, api) {
97
- var internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName, hookRunners, isV5, getRoutes, importsStatemets;
97
+ var _config, _config_runtime_router, _config_runtime, _config1, internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName, hookRunners, isV5, getRoutes, importsStatemets, oldVersion;
98
98
  function generateEntryCode(entrypoint) {
99
99
  return _generateEntryCode.apply(this, arguments);
100
100
  }
101
101
  function _generateEntryCode() {
102
102
  _generateEntryCode = _async_to_generator(function(entrypoint) {
103
- var entryName, isMainEntry, isAutoMount, fileSystemRoutes, _config_output, _config, initialRoutes, nestedRoute, routes, _$config, ssr, useSSG, mode, hasPageRoute, code, _, _tmp, routesServerFile, code1, serverLoaderCombined, serverLoaderFile, imports, entryFile;
103
+ var entryName, isMainEntry, isAutoMount, fileSystemRoutes, _config_output, _config2, initialRoutes, nestedRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, routes, _$config, ssr, useSSG, mode, hasPageRoute, code, _, _tmp, routesServerFile, code1, serverLoaderCombined, serverLoaderFile, imports, entryFile;
104
104
  return _ts_generator(this, function(_state) {
105
105
  switch (_state.label) {
106
106
  case 0:
@@ -116,7 +116,7 @@ export var generateCode = function() {
116
116
  11
117
117
  ];
118
118
  initialRoutes = [];
119
- nestedRoute = null;
119
+ nestedRoutes = null;
120
120
  if (entrypoint.entry) {
121
121
  initialRoutes = getRoutes({
122
122
  entrypoint: entrypoint,
@@ -136,12 +136,36 @@ export var generateCode = function() {
136
136
  walk(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
137
137
  name: internalSrcAlias,
138
138
  basename: srcDirectory
139
- }, entrypoint.entryName, entrypoint.isMainEntry)
139
+ }, entrypoint.entryName, entrypoint.isMainEntry, oldVersion)
140
140
  ];
141
141
  case 1:
142
- nestedRoute = _state.sent();
143
- if (nestedRoute) {
144
- initialRoutes.unshift(nestedRoute);
142
+ nestedRoutes = _state.sent();
143
+ if (nestedRoutes) {
144
+ if (!Array.isArray(nestedRoutes)) {
145
+ nestedRoutes = [
146
+ nestedRoutes
147
+ ];
148
+ }
149
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
150
+ try {
151
+ for (_iterator = nestedRoutes[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
152
+ route = _step.value;
153
+ initialRoutes.unshift(route);
154
+ }
155
+ } catch (err) {
156
+ _didIteratorError = true;
157
+ _iteratorError = err;
158
+ } finally {
159
+ try {
160
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
161
+ _iterator.return();
162
+ }
163
+ } finally {
164
+ if (_didIteratorError) {
165
+ throw _iteratorError;
166
+ }
167
+ }
168
+ }
145
169
  }
146
170
  _state.label = 2;
147
171
  case 2:
@@ -161,8 +185,8 @@ export var generateCode = function() {
161
185
  mode = typeof ssr === "object" ? ssr.mode || "string" : "string";
162
186
  }
163
187
  if (mode === "stream") {
164
- hasPageRoute = routes.some(function(route) {
165
- return "type" in route && route.type === "page";
188
+ hasPageRoute = routes.some(function(route2) {
189
+ return "type" in route2 && route2.type === "page";
166
190
  });
167
191
  if (hasPageRoute) {
168
192
  logger.error("Streaming ssr is not supported when pages dir exists");
@@ -181,7 +205,7 @@ export var generateCode = function() {
181
205
  nestedRoutesEntry: entrypoint.nestedRoutesEntry,
182
206
  entryName: entrypoint.entryName,
183
207
  internalDirectory: internalDirectory,
184
- splitRouteChunks: (_config = _$config) === null || _config === void 0 ? void 0 : (_config_output = _config.output) === null || _config_output === void 0 ? void 0 : _config_output.splitRouteChunks
208
+ splitRouteChunks: (_config2 = _$config) === null || _config2 === void 0 ? void 0 : (_config_output = _config2.output) === null || _config_output === void 0 ? void 0 : _config_output.splitRouteChunks
185
209
  })
186
210
  ];
187
211
  case 4:
@@ -270,6 +294,7 @@ export var generateCode = function() {
270
294
  isV5 = isRouterV5(config);
271
295
  getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
272
296
  importsStatemets = /* @__PURE__ */ new Map();
297
+ oldVersion = typeof ((_config = config) === null || _config === void 0 ? void 0 : _config.runtime.router) === "object" ? Boolean((_config1 = config) === null || _config1 === void 0 ? void 0 : (_config_runtime = _config1.runtime) === null || _config_runtime === void 0 ? void 0 : (_config_runtime_router = _config_runtime.router) === null || _config_runtime_router === void 0 ? void 0 : _config_runtime_router.oldVersion) : false;
273
298
  return [
274
299
  4,
275
300
  Promise.all(entrypoints.map(generateEntryCode))
@@ -38,9 +38,34 @@ var createRoute = function(routeInfo, rootDir, filename, entryName, isMainEntry)
38
38
  type: "nested"
39
39
  });
40
40
  };
41
+ export var optimizeRoute = function(routeTree) {
42
+ if (!routeTree.children || routeTree.children.length === 0) {
43
+ return [
44
+ routeTree
45
+ ];
46
+ }
47
+ var children = routeTree.children;
48
+ if (!routeTree._component) {
49
+ var newRoutes = children.map(function(child) {
50
+ var _routeTree_path;
51
+ var routePath = "".concat((_routeTree_path = routeTree.path) !== null && _routeTree_path !== void 0 ? _routeTree_path : "").concat(child.path ? "/".concat(child.path) : "");
52
+ return _object_spread_props(_object_spread({}, child), {
53
+ path: routePath
54
+ });
55
+ });
56
+ return newRoutes.flatMap(function(newRoute) {
57
+ return optimizeRoute(newRoute);
58
+ });
59
+ } else {
60
+ routeTree.children = children.flatMap(optimizeRoute);
61
+ return [
62
+ routeTree
63
+ ];
64
+ }
65
+ };
41
66
  export var walk = function() {
42
- var _ref = _async_to_generator(function(dirname, rootDir, alias, entryName, isMainEntry) {
43
- var _routePath, _route_children, isDirectory, relativeDir, pathSegments, lastSegment, isRoot, isPathlessLayout, isWithoutLayoutPath, routePath, route, pageLoaderFile, pageRoute, splatLoaderFile, splatRoute, pageConfigFile, items, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, item, itemPath, extname, itemWithoutExt, isDirectory1, childRoute, _route_children1, _route_children2, _route_children3, err, finalRoute, childRoute1, _$path;
67
+ var _ref = _async_to_generator(function(dirname, rootDir, alias, entryName, isMainEntry, oldVersion) {
68
+ var _routePath, _finalRoute_children, isDirectory, relativeDir, pathSegments, lastSegment, isRoot, isPathlessLayout, isWithoutLayoutPath, routePath, route, pageLoaderFile, pageRoute, splatLoaderFile, splatRoute, pageConfigFile, items, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, item, itemPath, extname, itemWithoutExt, isDirectory1, childRoute, _route_children, _route_children1, _route_children2, err, finalRoute, childRoutes, childRoute1, _$path, _childRoutes, slatRouteIndex, _childRoutes1, _childRoutes2, optimizedRoutes;
44
69
  return _ts_generator(this, function(_state) {
45
70
  switch (_state.label) {
46
71
  case 0:
@@ -128,13 +153,13 @@ export var walk = function() {
128
153
  ];
129
154
  return [
130
155
  4,
131
- walk(itemPath, rootDir, alias, entryName, isMainEntry)
156
+ walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion)
132
157
  ];
133
158
  case 7:
134
159
  childRoute = _state.sent();
135
- if (childRoute) {
160
+ if (childRoute && !Array.isArray(childRoute)) {
136
161
  ;
137
- (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(childRoute);
162
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
138
163
  }
139
164
  _state.label = 8;
140
165
  case 8:
@@ -174,7 +199,7 @@ export var walk = function() {
174
199
  if (pageConfigFile) {
175
200
  pageRoute.config = pageConfigFile;
176
201
  }
177
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(pageRoute);
202
+ (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
178
203
  }
179
204
  if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
180
205
  splatLoaderFile = itemPath;
@@ -188,7 +213,7 @@ export var walk = function() {
188
213
  if (splatLoaderFile) {
189
214
  splatRoute.loader = splatLoaderFile;
190
215
  }
191
- (_route_children3 = route.children) === null || _route_children3 === void 0 ? void 0 : _route_children3.push(splatRoute);
216
+ (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
192
217
  }
193
218
  if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
194
219
  route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
@@ -234,17 +259,17 @@ export var walk = function() {
234
259
  if (isPathlessLayout) {
235
260
  delete finalRoute.path;
236
261
  }
237
- route.children = (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.filter(function(childRoute2) {
262
+ childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter(function(childRoute2) {
238
263
  return childRoute2;
239
264
  });
240
- if (route.children && route.children.length === 0 && !route.index) {
265
+ if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
241
266
  return [
242
267
  2,
243
268
  null
244
269
  ];
245
270
  }
246
- if (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
247
- childRoute1 = finalRoute.children[0];
271
+ if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
272
+ childRoute1 = childRoutes[0];
248
273
  if (childRoute1.path === "*") {
249
274
  _$path = "".concat(finalRoute.path || "", "/").concat(childRoute1.path || "");
250
275
  finalRoute = _object_spread_props(_object_spread({}, childRoute1), {
@@ -252,6 +277,24 @@ export var walk = function() {
252
277
  });
253
278
  }
254
279
  }
280
+ if (splatRoute) {
281
+ ;
282
+ slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex(function(childRoute2) {
283
+ return childRoute2 === splatRoute;
284
+ });
285
+ if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
286
+ ;
287
+ (_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
288
+ (_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
289
+ }
290
+ }
291
+ if (isRoot && !oldVersion) {
292
+ optimizedRoutes = optimizeRoute(finalRoute);
293
+ return [
294
+ 2,
295
+ optimizedRoutes
296
+ ];
297
+ }
255
298
  return [
256
299
  2,
257
300
  finalRoute
@@ -259,7 +302,7 @@ export var walk = function() {
259
302
  }
260
303
  });
261
304
  });
262
- return function walk2(dirname, rootDir, alias, entryName, isMainEntry) {
305
+ return function walk2(dirname, rootDir, alias, entryName, isMainEntry, oldVersion) {
263
306
  return _ref.apply(this, arguments);
264
307
  };
265
308
  }();
@@ -50,11 +50,13 @@ ${initialize || ""}`);
50
50
  ${initialize || ""}`).join("\n");
51
51
  };
52
52
  export const generateCode = async (appContext, config, entrypoints, api) => {
53
+ var _config, _config_runtime_router, _config_runtime, _config1;
53
54
  const { internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName } = appContext;
54
55
  const hookRunners = api.useHookRunners();
55
56
  const isV5 = isRouterV5(config);
56
57
  const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
57
58
  const importsStatemets = /* @__PURE__ */ new Map();
59
+ const oldVersion = typeof ((_config = config) === null || _config === void 0 ? void 0 : _config.runtime.router) === "object" ? Boolean((_config1 = config) === null || _config1 === void 0 ? void 0 : (_config_runtime = _config1.runtime) === null || _config_runtime === void 0 ? void 0 : (_config_runtime_router = _config_runtime.router) === null || _config_runtime_router === void 0 ? void 0 : _config_runtime_router.oldVersion) : false;
58
60
  await Promise.all(entrypoints.map(generateEntryCode));
59
61
  return {
60
62
  importsStatemets
@@ -63,9 +65,9 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
63
65
  const { entryName, isMainEntry, isAutoMount, fileSystemRoutes } = entrypoint;
64
66
  if (isAutoMount) {
65
67
  if (fileSystemRoutes) {
66
- var _config_output, _config;
68
+ var _config_output, _config2;
67
69
  let initialRoutes = [];
68
- let nestedRoute = null;
70
+ let nestedRoutes = null;
69
71
  if (entrypoint.entry) {
70
72
  initialRoutes = getRoutes({
71
73
  entrypoint,
@@ -76,12 +78,19 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
76
78
  });
77
79
  }
78
80
  if (!isV5 && entrypoint.nestedRoutesEntry) {
79
- nestedRoute = await walk(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
81
+ nestedRoutes = await walk(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
80
82
  name: internalSrcAlias,
81
83
  basename: srcDirectory
82
- }, entrypoint.entryName, entrypoint.isMainEntry);
83
- if (nestedRoute) {
84
- initialRoutes.unshift(nestedRoute);
84
+ }, entrypoint.entryName, entrypoint.isMainEntry, oldVersion);
85
+ if (nestedRoutes) {
86
+ if (!Array.isArray(nestedRoutes)) {
87
+ nestedRoutes = [
88
+ nestedRoutes
89
+ ];
90
+ }
91
+ for (const route of nestedRoutes) {
92
+ initialRoutes.unshift(route);
93
+ }
85
94
  }
86
95
  }
87
96
  const { routes } = await hookRunners.modifyFileSystemRoutes({
@@ -110,7 +119,7 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
110
119
  nestedRoutesEntry: entrypoint.nestedRoutesEntry,
111
120
  entryName: entrypoint.entryName,
112
121
  internalDirectory,
113
- splitRouteChunks: (_config = config2) === null || _config === void 0 ? void 0 : (_config_output = _config.output) === null || _config_output === void 0 ? void 0 : _config_output.splitRouteChunks
122
+ splitRouteChunks: (_config2 = config2) === null || _config2 === void 0 ? void 0 : (_config_output = _config2.output) === null || _config_output === void 0 ? void 0 : _config_output.splitRouteChunks
114
123
  })
115
124
  });
116
125
  if (entrypoint.nestedRoutesEntry && isUseSSRBundle(config2)) {
@@ -36,8 +36,32 @@ const createRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
36
36
  type: "nested"
37
37
  };
38
38
  };
39
- export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
40
- var _routePath, _route_children;
39
+ export const optimizeRoute = (routeTree) => {
40
+ if (!routeTree.children || routeTree.children.length === 0) {
41
+ return [
42
+ routeTree
43
+ ];
44
+ }
45
+ const { children } = routeTree;
46
+ if (!routeTree._component) {
47
+ const newRoutes = children.map((child) => {
48
+ var _routeTree_path;
49
+ const routePath = `${(_routeTree_path = routeTree.path) !== null && _routeTree_path !== void 0 ? _routeTree_path : ""}${child.path ? `/${child.path}` : ""}`;
50
+ return {
51
+ ...child,
52
+ path: routePath
53
+ };
54
+ });
55
+ return newRoutes.flatMap((newRoute) => optimizeRoute(newRoute));
56
+ } else {
57
+ routeTree.children = children.flatMap(optimizeRoute);
58
+ return [
59
+ routeTree
60
+ ];
61
+ }
62
+ };
63
+ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
64
+ var _routePath, _finalRoute_children;
41
65
  if (!await fs.pathExists(dirname)) {
42
66
  return null;
43
67
  }
@@ -73,10 +97,10 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
73
97
  const itemWithoutExt = item.slice(0, -extname.length);
74
98
  const isDirectory2 = (await fs.stat(itemPath)).isDirectory();
75
99
  if (isDirectory2) {
76
- const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry);
77
- if (childRoute) {
78
- var _route_children1;
79
- (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(childRoute);
100
+ const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
101
+ if (childRoute && !Array.isArray(childRoute)) {
102
+ var _route_children;
103
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
80
104
  }
81
105
  }
82
106
  if (extname && (!JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
@@ -102,7 +126,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
102
126
  pageConfigFile = itemPath;
103
127
  }
104
128
  if (itemWithoutExt === NESTED_ROUTE.PAGE_FILE) {
105
- var _route_children2;
129
+ var _route_children1;
106
130
  pageRoute = createIndexRoute({
107
131
  _component: replaceWithAlias(alias.basename, itemPath, alias.name)
108
132
  }, rootDir, itemPath, entryName, isMainEntry);
@@ -112,13 +136,13 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
112
136
  if (pageConfigFile) {
113
137
  pageRoute.config = pageConfigFile;
114
138
  }
115
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(pageRoute);
139
+ (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
116
140
  }
117
141
  if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
118
142
  splatLoaderFile = itemPath;
119
143
  }
120
144
  if (itemWithoutExt === NESTED_ROUTE.SPLATE_FILE) {
121
- var _route_children3;
145
+ var _route_children2;
122
146
  splatRoute = createRoute({
123
147
  _component: replaceWithAlias(alias.basename, itemPath, alias.name),
124
148
  path: "*"
@@ -126,7 +150,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
126
150
  if (splatLoaderFile) {
127
151
  splatRoute.loader = splatLoaderFile;
128
152
  }
129
- (_route_children3 = route.children) === null || _route_children3 === void 0 ? void 0 : _route_children3.push(splatRoute);
153
+ (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
130
154
  }
131
155
  if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
132
156
  route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
@@ -139,12 +163,12 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
139
163
  if (isPathlessLayout) {
140
164
  delete finalRoute.path;
141
165
  }
142
- route.children = (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.filter((childRoute) => childRoute);
143
- if (route.children && route.children.length === 0 && !route.index) {
166
+ const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
167
+ if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
144
168
  return null;
145
169
  }
146
- if (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
147
- const childRoute = finalRoute.children[0];
170
+ if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
171
+ const childRoute = childRoutes[0];
148
172
  if (childRoute.path === "*") {
149
173
  const path2 = `${finalRoute.path || ""}/${childRoute.path || ""}`;
150
174
  finalRoute = {
@@ -153,5 +177,18 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
153
177
  };
154
178
  }
155
179
  }
180
+ if (splatRoute) {
181
+ var _childRoutes;
182
+ const slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex((childRoute) => childRoute === splatRoute);
183
+ if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
184
+ var _childRoutes1, _childRoutes2;
185
+ (_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
186
+ (_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
187
+ }
188
+ }
189
+ if (isRoot && !oldVersion) {
190
+ const optimizedRoutes = optimizeRoute(finalRoute);
191
+ return optimizedRoutes;
192
+ }
156
193
  return finalRoute;
157
194
  };
@@ -1,6 +1,7 @@
1
1
  import type { NestedRouteForCli } from '@modern-js/types';
2
2
  export declare const getRouteId: (componentPath: string, routesDir: string, entryName: string, isMainEntry: boolean) => string;
3
+ export declare const optimizeRoute: (routeTree: NestedRouteForCli) => NestedRouteForCli[];
3
4
  export declare const walk: (dirname: string, rootDir: string, alias: {
4
5
  name: string;
5
6
  basename: string;
6
- }, entryName: string, isMainEntry: boolean) => Promise<NestedRouteForCli | null>;
7
+ }, entryName: string, isMainEntry: boolean, oldVersion: boolean) => Promise<NestedRouteForCli | NestedRouteForCli[] | null>;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.28.0",
18
+ "version": "2.28.1-alpha.1",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -74,23 +74,23 @@
74
74
  "rspack-plugin-virtual-module": "0.1.0",
75
75
  "@swc/helpers": "0.5.1",
76
76
  "@modern-js/builder": "2.28.0",
77
- "@modern-js/builder-plugin-esbuild": "2.28.0",
78
- "@modern-js/builder-plugin-node-polyfill": "2.28.0",
79
77
  "@modern-js/builder-shared": "2.28.0",
80
78
  "@modern-js/builder-webpack-provider": "2.28.0",
81
79
  "@modern-js/core": "2.28.0",
82
- "@modern-js/new-action": "2.28.0",
83
80
  "@modern-js/node-bundle-require": "2.28.0",
84
81
  "@modern-js/plugin": "2.28.0",
85
- "@modern-js/plugin-data-loader": "2.28.0",
82
+ "@modern-js/builder-plugin-node-polyfill": "2.28.0",
83
+ "@modern-js/new-action": "2.28.0",
86
84
  "@modern-js/plugin-i18n": "2.28.0",
87
- "@modern-js/plugin-lint": "2.28.0",
88
85
  "@modern-js/prod-server": "2.28.0",
86
+ "@modern-js/plugin-lint": "2.28.0",
89
87
  "@modern-js/server": "2.28.0",
90
88
  "@modern-js/types": "2.28.0",
91
89
  "@modern-js/upgrade": "2.28.0",
92
90
  "@modern-js/utils": "2.28.0",
93
- "@modern-js/server-core": "2.28.0"
91
+ "@modern-js/server-core": "2.28.0",
92
+ "@modern-js/builder-plugin-esbuild": "2.28.0",
93
+ "@modern-js/plugin-data-loader": "2.28.0"
94
94
  },
95
95
  "devDependencies": {
96
96
  "@types/babel__traverse": "^7.14.2",
@@ -99,10 +99,10 @@
99
99
  "jest": "^29",
100
100
  "typescript": "^5",
101
101
  "webpack": "^5.88.1",
102
- "@modern-js/builder-plugin-swc": "2.28.0",
103
- "@modern-js/builder-rspack-provider": "2.28.0",
104
102
  "@scripts/build": "2.28.0",
105
- "@scripts/jest-config": "2.28.0"
103
+ "@scripts/jest-config": "2.28.0",
104
+ "@modern-js/builder-rspack-provider": "2.28.0",
105
+ "@modern-js/builder-plugin-swc": "2.28.0"
106
106
  },
107
107
  "peerDependencies": {
108
108
  "@modern-js/builder-rspack-provider": "^2.28.0"