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

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,35 @@ 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
+ if (newRoutes.length > 1) {
79
+ return newRoutes.flatMap((newRoute) => optimizeRoute(newRoute));
80
+ }
81
+ return newRoutes;
82
+ } else {
83
+ routeTree.children = children.flatMap(optimizeRoute);
84
+ return [
85
+ routeTree
86
+ ];
87
+ }
88
+ };
89
+ const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
90
+ var _routePath, _finalRoute_children;
61
91
  if (!await _utils.fs.pathExists(dirname)) {
62
92
  return null;
63
93
  }
@@ -93,10 +123,10 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
93
123
  const itemWithoutExt = item.slice(0, -extname.length);
94
124
  const isDirectory2 = (await _utils.fs.stat(itemPath)).isDirectory();
95
125
  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);
126
+ const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
127
+ if (childRoute && !Array.isArray(childRoute)) {
128
+ var _route_children;
129
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
100
130
  }
101
131
  }
102
132
  if (extname && (!_constants.JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
@@ -122,7 +152,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
122
152
  pageConfigFile = itemPath;
123
153
  }
124
154
  if (itemWithoutExt === _constants.NESTED_ROUTE.PAGE_FILE) {
125
- var _route_children2;
155
+ var _route_children1;
126
156
  pageRoute = createIndexRoute({
127
157
  _component: (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name)
128
158
  }, rootDir, itemPath, entryName, isMainEntry);
@@ -132,13 +162,13 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
132
162
  if (pageConfigFile) {
133
163
  pageRoute.config = pageConfigFile;
134
164
  }
135
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(pageRoute);
165
+ (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
136
166
  }
137
167
  if (itemWithoutExt === _constants.NESTED_ROUTE.SPLATE_LOADER_FILE) {
138
168
  splatLoaderFile = itemPath;
139
169
  }
140
170
  if (itemWithoutExt === _constants.NESTED_ROUTE.SPLATE_FILE) {
141
- var _route_children3;
171
+ var _route_children2;
142
172
  splatRoute = createRoute({
143
173
  _component: (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name),
144
174
  path: "*"
@@ -146,7 +176,7 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
146
176
  if (splatLoaderFile) {
147
177
  splatRoute.loader = splatLoaderFile;
148
178
  }
149
- (_route_children3 = route.children) === null || _route_children3 === void 0 ? void 0 : _route_children3.push(splatRoute);
179
+ (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
150
180
  }
151
181
  if (itemWithoutExt === _constants.NESTED_ROUTE.LOADING_FILE) {
152
182
  route.loading = (0, _utils1.replaceWithAlias)(alias.basename, itemPath, alias.name);
@@ -159,12 +189,12 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
159
189
  if (isPathlessLayout) {
160
190
  delete finalRoute.path;
161
191
  }
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) {
192
+ const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
193
+ if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
164
194
  return null;
165
195
  }
166
- if (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
167
- const childRoute = finalRoute.children[0];
196
+ if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
197
+ const childRoute = childRoutes[0];
168
198
  if (childRoute.path === "*") {
169
199
  const path = `${finalRoute.path || ""}/${childRoute.path || ""}`;
170
200
  finalRoute = {
@@ -173,5 +203,18 @@ const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
173
203
  };
174
204
  }
175
205
  }
206
+ if (splatRoute) {
207
+ var _childRoutes;
208
+ const slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex((childRoute) => childRoute === splatRoute);
209
+ if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
210
+ var _childRoutes1, _childRoutes2;
211
+ (_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
212
+ (_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
213
+ }
214
+ }
215
+ if (isRoot && !oldVersion) {
216
+ const optimizedRoutes = optimizeRoute(finalRoute);
217
+ return optimizedRoutes;
218
+ }
176
219
  return finalRoute;
177
220
  };
@@ -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,37 @@ 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
+ if (newRoutes.length > 1) {
57
+ return newRoutes.flatMap(function(newRoute) {
58
+ return optimizeRoute(newRoute);
59
+ });
60
+ }
61
+ return newRoutes;
62
+ } else {
63
+ routeTree.children = children.flatMap(optimizeRoute);
64
+ return [
65
+ routeTree
66
+ ];
67
+ }
68
+ };
41
69
  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;
70
+ var _ref = _async_to_generator(function(dirname, rootDir, alias, entryName, isMainEntry, oldVersion) {
71
+ 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
72
  return _ts_generator(this, function(_state) {
45
73
  switch (_state.label) {
46
74
  case 0:
@@ -128,13 +156,13 @@ export var walk = function() {
128
156
  ];
129
157
  return [
130
158
  4,
131
- walk(itemPath, rootDir, alias, entryName, isMainEntry)
159
+ walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion)
132
160
  ];
133
161
  case 7:
134
162
  childRoute = _state.sent();
135
- if (childRoute) {
163
+ if (childRoute && !Array.isArray(childRoute)) {
136
164
  ;
137
- (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(childRoute);
165
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
138
166
  }
139
167
  _state.label = 8;
140
168
  case 8:
@@ -174,7 +202,7 @@ export var walk = function() {
174
202
  if (pageConfigFile) {
175
203
  pageRoute.config = pageConfigFile;
176
204
  }
177
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(pageRoute);
205
+ (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
178
206
  }
179
207
  if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
180
208
  splatLoaderFile = itemPath;
@@ -188,7 +216,7 @@ export var walk = function() {
188
216
  if (splatLoaderFile) {
189
217
  splatRoute.loader = splatLoaderFile;
190
218
  }
191
- (_route_children3 = route.children) === null || _route_children3 === void 0 ? void 0 : _route_children3.push(splatRoute);
219
+ (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
192
220
  }
193
221
  if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
194
222
  route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
@@ -234,17 +262,17 @@ export var walk = function() {
234
262
  if (isPathlessLayout) {
235
263
  delete finalRoute.path;
236
264
  }
237
- route.children = (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.filter(function(childRoute2) {
265
+ childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter(function(childRoute2) {
238
266
  return childRoute2;
239
267
  });
240
- if (route.children && route.children.length === 0 && !route.index) {
268
+ if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
241
269
  return [
242
270
  2,
243
271
  null
244
272
  ];
245
273
  }
246
- if (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
247
- childRoute1 = finalRoute.children[0];
274
+ if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
275
+ childRoute1 = childRoutes[0];
248
276
  if (childRoute1.path === "*") {
249
277
  _$path = "".concat(finalRoute.path || "", "/").concat(childRoute1.path || "");
250
278
  finalRoute = _object_spread_props(_object_spread({}, childRoute1), {
@@ -252,6 +280,24 @@ export var walk = function() {
252
280
  });
253
281
  }
254
282
  }
283
+ if (splatRoute) {
284
+ ;
285
+ slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex(function(childRoute2) {
286
+ return childRoute2 === splatRoute;
287
+ });
288
+ if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
289
+ ;
290
+ (_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
291
+ (_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
292
+ }
293
+ }
294
+ if (isRoot && !oldVersion) {
295
+ optimizedRoutes = optimizeRoute(finalRoute);
296
+ return [
297
+ 2,
298
+ optimizedRoutes
299
+ ];
300
+ }
255
301
  return [
256
302
  2,
257
303
  finalRoute
@@ -259,7 +305,7 @@ export var walk = function() {
259
305
  }
260
306
  });
261
307
  });
262
- return function walk2(dirname, rootDir, alias, entryName, isMainEntry) {
308
+ return function walk2(dirname, rootDir, alias, entryName, isMainEntry, oldVersion) {
263
309
  return _ref.apply(this, arguments);
264
310
  };
265
311
  }();
@@ -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,35 @@ 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
+ if (newRoutes.length > 1) {
56
+ return newRoutes.flatMap((newRoute) => optimizeRoute(newRoute));
57
+ }
58
+ return newRoutes;
59
+ } else {
60
+ routeTree.children = children.flatMap(optimizeRoute);
61
+ return [
62
+ routeTree
63
+ ];
64
+ }
65
+ };
66
+ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
67
+ var _routePath, _finalRoute_children;
41
68
  if (!await fs.pathExists(dirname)) {
42
69
  return null;
43
70
  }
@@ -73,10 +100,10 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
73
100
  const itemWithoutExt = item.slice(0, -extname.length);
74
101
  const isDirectory2 = (await fs.stat(itemPath)).isDirectory();
75
102
  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);
103
+ const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
104
+ if (childRoute && !Array.isArray(childRoute)) {
105
+ var _route_children;
106
+ (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
80
107
  }
81
108
  }
82
109
  if (extname && (!JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
@@ -102,7 +129,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
102
129
  pageConfigFile = itemPath;
103
130
  }
104
131
  if (itemWithoutExt === NESTED_ROUTE.PAGE_FILE) {
105
- var _route_children2;
132
+ var _route_children1;
106
133
  pageRoute = createIndexRoute({
107
134
  _component: replaceWithAlias(alias.basename, itemPath, alias.name)
108
135
  }, rootDir, itemPath, entryName, isMainEntry);
@@ -112,13 +139,13 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
112
139
  if (pageConfigFile) {
113
140
  pageRoute.config = pageConfigFile;
114
141
  }
115
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(pageRoute);
142
+ (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
116
143
  }
117
144
  if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
118
145
  splatLoaderFile = itemPath;
119
146
  }
120
147
  if (itemWithoutExt === NESTED_ROUTE.SPLATE_FILE) {
121
- var _route_children3;
148
+ var _route_children2;
122
149
  splatRoute = createRoute({
123
150
  _component: replaceWithAlias(alias.basename, itemPath, alias.name),
124
151
  path: "*"
@@ -126,7 +153,7 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
126
153
  if (splatLoaderFile) {
127
154
  splatRoute.loader = splatLoaderFile;
128
155
  }
129
- (_route_children3 = route.children) === null || _route_children3 === void 0 ? void 0 : _route_children3.push(splatRoute);
156
+ (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
130
157
  }
131
158
  if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
132
159
  route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
@@ -139,12 +166,12 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
139
166
  if (isPathlessLayout) {
140
167
  delete finalRoute.path;
141
168
  }
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) {
169
+ const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
170
+ if (childRoutes && childRoutes.length === 0 && !finalRoute.index) {
144
171
  return null;
145
172
  }
146
- if (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
147
- const childRoute = finalRoute.children[0];
173
+ if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
174
+ const childRoute = childRoutes[0];
148
175
  if (childRoute.path === "*") {
149
176
  const path2 = `${finalRoute.path || ""}/${childRoute.path || ""}`;
150
177
  finalRoute = {
@@ -153,5 +180,18 @@ export const walk = async (dirname, rootDir, alias, entryName, isMainEntry) => {
153
180
  };
154
181
  }
155
182
  }
183
+ if (splatRoute) {
184
+ var _childRoutes;
185
+ const slatRouteIndex = (_childRoutes = childRoutes) === null || _childRoutes === void 0 ? void 0 : _childRoutes.findIndex((childRoute) => childRoute === splatRoute);
186
+ if (typeof slatRouteIndex === "number" && slatRouteIndex !== -1) {
187
+ var _childRoutes1, _childRoutes2;
188
+ (_childRoutes1 = childRoutes) === null || _childRoutes1 === void 0 ? void 0 : _childRoutes1.splice(slatRouteIndex, 1);
189
+ (_childRoutes2 = childRoutes) === null || _childRoutes2 === void 0 ? void 0 : _childRoutes2.push(splatRoute);
190
+ }
191
+ }
192
+ if (isRoot && !oldVersion) {
193
+ const optimizedRoutes = optimizeRoute(finalRoute);
194
+ return optimizedRoutes;
195
+ }
156
196
  return finalRoute;
157
197
  };
@@ -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.0",
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
80
  "@modern-js/new-action": "2.28.0",
83
81
  "@modern-js/node-bundle-require": "2.28.0",
84
82
  "@modern-js/plugin": "2.28.0",
85
- "@modern-js/plugin-data-loader": "2.28.0",
86
83
  "@modern-js/plugin-i18n": "2.28.0",
87
84
  "@modern-js/plugin-lint": "2.28.0",
88
85
  "@modern-js/prod-server": "2.28.0",
89
86
  "@modern-js/server": "2.28.0",
90
- "@modern-js/types": "2.28.0",
91
- "@modern-js/upgrade": "2.28.0",
92
87
  "@modern-js/utils": "2.28.0",
93
- "@modern-js/server-core": "2.28.0"
88
+ "@modern-js/upgrade": "2.28.0",
89
+ "@modern-js/builder-plugin-esbuild": "2.28.0",
90
+ "@modern-js/types": "2.28.0",
91
+ "@modern-js/server-core": "2.28.0",
92
+ "@modern-js/builder-plugin-node-polyfill": "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
102
  "@modern-js/builder-rspack-provider": "2.28.0",
104
103
  "@scripts/build": "2.28.0",
105
- "@scripts/jest-config": "2.28.0"
104
+ "@scripts/jest-config": "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"