@modern-js/runtime 2.52.0 → 2.54.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.
- package/dist/cjs/{runtimeContext.js → cli/constants.js} +6 -10
- package/dist/cjs/cli/entry.js +42 -0
- package/dist/cjs/cli/index.js +10 -0
- package/dist/cjs/core/compatible.js +13 -29
- package/dist/cjs/core/{appConfig.js → config.js} +6 -4
- package/dist/cjs/core/context/index.js +28 -0
- package/dist/cjs/core/context/runtime.js +42 -0
- package/dist/cjs/core/index.js +8 -12
- package/dist/cjs/core/loader/useLoader.js +2 -2
- package/dist/cjs/core/{plugin.js → plugin/base.js} +5 -25
- package/dist/cjs/core/plugin/index.js +39 -0
- package/dist/cjs/core/plugin/runner.js +42 -0
- package/dist/cjs/index.js +2 -4
- package/dist/cjs/router/cli/code/getClientRoutes/getRoutes.js +197 -0
- package/dist/cjs/router/cli/code/getClientRoutes/getRoutesLegacy.js +195 -0
- package/dist/cjs/router/cli/code/getClientRoutes/index.js +31 -0
- package/dist/cjs/router/cli/code/getClientRoutes/utils.js +59 -0
- package/dist/cjs/router/cli/code/index.js +134 -0
- package/dist/cjs/router/cli/code/makeLegalIdentifier.js +37 -0
- package/dist/cjs/router/cli/code/nestedRoutes.js +294 -0
- package/dist/cjs/router/cli/code/templates.js +371 -0
- package/dist/cjs/router/cli/code/utils.js +143 -0
- package/dist/cjs/router/cli/constants.js +83 -0
- package/dist/cjs/router/cli/entry.js +87 -0
- package/dist/cjs/router/cli/handler.js +71 -0
- package/dist/cjs/router/cli/index.js +25 -0
- package/dist/cjs/router/runtime/plugin.js +7 -4
- package/dist/cjs/router/runtime/plugin.node.js +12 -5
- package/dist/cjs/ssr/index.node.js +1 -0
- package/dist/cjs/state/runtime/plugin.js +3 -2
- package/dist/esm/cli/constants.js +4 -0
- package/dist/esm/cli/entry.js +14 -0
- package/dist/esm/cli/index.js +10 -0
- package/dist/esm/core/compatible.js +11 -30
- package/dist/esm/core/{appConfig.js → config.js} +4 -1
- package/dist/esm/core/context/index.js +4 -0
- package/dist/esm/core/context/runtime.js +18 -0
- package/dist/esm/core/index.js +4 -6
- package/dist/esm/core/loader/useLoader.js +1 -1
- package/dist/esm/core/plugin/base.js +21 -0
- package/dist/esm/core/plugin/index.js +15 -0
- package/dist/esm/core/plugin/runner.js +17 -0
- package/dist/esm/index.js +2 -3
- package/dist/esm/router/cli/code/getClientRoutes/getRoutes.js +185 -0
- package/dist/esm/router/cli/code/getClientRoutes/getRoutesLegacy.js +183 -0
- package/dist/esm/router/cli/code/getClientRoutes/index.js +6 -0
- package/dist/esm/router/cli/code/getClientRoutes/utils.js +28 -0
- package/dist/esm/router/cli/code/index.js +214 -0
- package/dist/esm/router/cli/code/makeLegalIdentifier.js +15 -0
- package/dist/esm/router/cli/code/nestedRoutes.js +397 -0
- package/dist/esm/router/cli/code/templates.js +417 -0
- package/dist/esm/router/cli/code/utils.js +212 -0
- package/dist/esm/router/cli/constants.js +47 -0
- package/dist/esm/router/cli/entry.js +57 -0
- package/dist/esm/router/cli/handler.js +103 -0
- package/dist/esm/router/cli/index.js +57 -0
- package/dist/esm/router/runtime/plugin.js +7 -4
- package/dist/esm/router/runtime/plugin.node.js +13 -6
- package/dist/esm/ssr/index.node.js +1 -0
- package/dist/esm/state/runtime/plugin.js +3 -2
- package/dist/esm-node/cli/constants.js +4 -0
- package/dist/esm-node/cli/entry.js +8 -0
- package/dist/esm-node/cli/index.js +9 -0
- package/dist/esm-node/core/compatible.js +12 -28
- package/dist/esm-node/core/{appConfig.js → config.js} +2 -1
- package/dist/esm-node/core/context/index.js +4 -0
- package/dist/esm-node/core/context/runtime.js +16 -0
- package/dist/esm-node/core/index.js +4 -6
- package/dist/esm-node/core/loader/useLoader.js +1 -1
- package/dist/esm-node/core/plugin/base.js +19 -0
- package/dist/esm-node/core/plugin/index.js +13 -0
- package/dist/esm-node/core/plugin/runner.js +17 -0
- package/dist/esm-node/index.js +2 -3
- package/dist/esm-node/router/cli/code/getClientRoutes/getRoutes.js +163 -0
- package/dist/esm-node/router/cli/code/getClientRoutes/getRoutesLegacy.js +161 -0
- package/dist/esm-node/router/cli/code/getClientRoutes/index.js +6 -0
- package/dist/esm-node/router/cli/code/getClientRoutes/utils.js +22 -0
- package/dist/esm-node/router/cli/code/index.js +100 -0
- package/dist/esm-node/router/cli/code/makeLegalIdentifier.js +13 -0
- package/dist/esm-node/router/cli/code/nestedRoutes.js +258 -0
- package/dist/esm-node/router/cli/code/templates.js +335 -0
- package/dist/esm-node/router/cli/code/utils.js +101 -0
- package/dist/esm-node/router/cli/constants.js +47 -0
- package/dist/esm-node/router/cli/entry.js +50 -0
- package/dist/esm-node/router/cli/handler.js +36 -0
- package/dist/esm-node/router/cli/index.js +22 -0
- package/dist/esm-node/router/runtime/plugin.js +7 -4
- package/dist/esm-node/router/runtime/plugin.node.js +12 -5
- package/dist/esm-node/ssr/index.node.js +1 -0
- package/dist/esm-node/state/runtime/plugin.js +3 -2
- package/dist/types/cli/constants.d.ts +1 -0
- package/dist/types/cli/entry.d.ts +1 -0
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/core/compatible.d.ts +7 -3
- package/dist/types/core/config.d.ts +14 -0
- package/dist/types/core/context/index.d.ts +1 -0
- package/dist/types/{runtimeContext.d.ts → core/context/runtime.d.ts} +5 -4
- package/dist/types/core/index.d.ts +4 -4
- package/dist/types/core/plugin/base.d.ts +80 -0
- package/dist/types/core/plugin/index.d.ts +29 -0
- package/dist/types/core/plugin/runner.d.ts +26 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/router/cli/code/getClientRoutes/getRoutes.d.ts +8 -0
- package/dist/types/router/cli/code/getClientRoutes/getRoutesLegacy.d.ts +9 -0
- package/dist/types/router/cli/code/getClientRoutes/index.d.ts +2 -0
- package/dist/types/router/cli/code/getClientRoutes/utils.d.ts +5 -0
- package/dist/types/router/cli/code/index.d.ts +6 -0
- package/dist/types/router/cli/code/makeLegalIdentifier.d.ts +1 -0
- package/dist/types/router/cli/code/nestedRoutes.d.ts +7 -0
- package/dist/types/router/cli/code/templates.d.ts +14 -0
- package/dist/types/router/cli/code/utils.d.ts +12 -0
- package/dist/types/router/cli/constants.d.ts +32 -0
- package/dist/types/router/cli/entry.d.ts +5 -0
- package/dist/types/router/cli/handler.d.ts +5 -0
- package/dist/types/router/cli/index.d.ts +3 -1
- package/dist/types/ssr/serverRender/types.d.ts +3 -0
- package/package.json +37 -11
- package/dist/esm/core/plugin.js +0 -59
- package/dist/esm/runtimeContext.js +0 -7
- package/dist/esm-node/core/plugin.js +0 -37
- package/dist/esm-node/runtimeContext.js +0 -7
- package/dist/types/core/appConfig.d.ts +0 -4
- package/dist/types/core/plugin.d.ts +0 -204
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var nestedRoutes_exports = {};
|
|
30
|
+
__export(nestedRoutes_exports, {
|
|
31
|
+
getRouteId: () => getRouteId,
|
|
32
|
+
optimizeRoute: () => optimizeRoute,
|
|
33
|
+
walk: () => walk
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(nestedRoutes_exports);
|
|
36
|
+
var path = __toESM(require("path"));
|
|
37
|
+
var import_utils = require("@modern-js/utils");
|
|
38
|
+
var import_constants = require("../constants");
|
|
39
|
+
var import_utils2 = require("./utils");
|
|
40
|
+
const conventionNames = Object.values(import_constants.NESTED_ROUTE);
|
|
41
|
+
const replaceDynamicPath = (routePath) => {
|
|
42
|
+
return routePath.replace(/\[(.*?)\]/g, ":$1");
|
|
43
|
+
};
|
|
44
|
+
const getRouteId = (componentPath, routesDir, entryName, isMainEntry) => {
|
|
45
|
+
const relativePath = (0, import_utils.normalizeToPosixPath)(path.relative(routesDir, componentPath));
|
|
46
|
+
const pathWithoutExt = (0, import_utils2.getPathWithoutExt)(relativePath);
|
|
47
|
+
let id = ``;
|
|
48
|
+
if (isMainEntry) {
|
|
49
|
+
id = pathWithoutExt;
|
|
50
|
+
} else {
|
|
51
|
+
id = `${entryName}_${pathWithoutExt}`;
|
|
52
|
+
}
|
|
53
|
+
return id.replace(/\[(.*?)\]/g, "($1)");
|
|
54
|
+
};
|
|
55
|
+
const createIndexRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
|
|
56
|
+
return createRoute({
|
|
57
|
+
...routeInfo,
|
|
58
|
+
index: true,
|
|
59
|
+
children: void 0
|
|
60
|
+
}, rootDir, filename, entryName, isMainEntry);
|
|
61
|
+
};
|
|
62
|
+
const createRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
|
|
63
|
+
const id = getRouteId(filename, rootDir, entryName, isMainEntry);
|
|
64
|
+
return {
|
|
65
|
+
...routeInfo,
|
|
66
|
+
id,
|
|
67
|
+
type: "nested"
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
const optimizeRoute = (routeTree) => {
|
|
71
|
+
if (!routeTree.children || routeTree.children.length === 0) {
|
|
72
|
+
return [
|
|
73
|
+
routeTree
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
const { children } = routeTree;
|
|
77
|
+
if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData) {
|
|
78
|
+
const newRoutes = children.map((child) => {
|
|
79
|
+
const routePath = `${routeTree.path ? routeTree.path : ""}${child.path ? `/${child.path}` : ""}`;
|
|
80
|
+
const newRoute = {
|
|
81
|
+
...child,
|
|
82
|
+
path: routePath.replace(/\/\//g, "/")
|
|
83
|
+
};
|
|
84
|
+
if (routePath.length > 0) {
|
|
85
|
+
delete newRoute.index;
|
|
86
|
+
} else {
|
|
87
|
+
delete newRoute.path;
|
|
88
|
+
}
|
|
89
|
+
return newRoute;
|
|
90
|
+
});
|
|
91
|
+
return Array.from(new Set(newRoutes)).flatMap(optimizeRoute);
|
|
92
|
+
} else {
|
|
93
|
+
const optimizedChildren = routeTree.children.flatMap(optimizeRoute);
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
96
|
+
...routeTree,
|
|
97
|
+
children: optimizedChildren
|
|
98
|
+
}
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
|
|
103
|
+
var _finalRoute_children;
|
|
104
|
+
if (!await import_utils.fs.pathExists(dirname)) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const isDirectory = (await import_utils.fs.stat(dirname)).isDirectory();
|
|
108
|
+
if (!isDirectory) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const relativeDir = path.relative(rootDir, dirname);
|
|
112
|
+
const pathSegments = relativeDir.split(path.sep);
|
|
113
|
+
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
114
|
+
const isRoot = lastSegment === "";
|
|
115
|
+
const isPathlessLayout = lastSegment.startsWith("__");
|
|
116
|
+
const isWithoutLayoutPath = lastSegment.includes(".");
|
|
117
|
+
let routePath = isRoot || isPathlessLayout ? "/" : `${lastSegment}`;
|
|
118
|
+
if (isWithoutLayoutPath) {
|
|
119
|
+
routePath = lastSegment.split(".").join("/");
|
|
120
|
+
}
|
|
121
|
+
routePath = replaceDynamicPath(routePath);
|
|
122
|
+
const route = {
|
|
123
|
+
path: routePath === null || routePath === void 0 ? void 0 : routePath.replace(/\$$/, "?"),
|
|
124
|
+
children: [],
|
|
125
|
+
isRoot
|
|
126
|
+
};
|
|
127
|
+
let pageLoaderFile = "";
|
|
128
|
+
let pageRoute = null;
|
|
129
|
+
let pageConfigFile = "";
|
|
130
|
+
let pageClientData = "";
|
|
131
|
+
let pageData = "";
|
|
132
|
+
let pageAction = "";
|
|
133
|
+
let splatLoaderFile = "";
|
|
134
|
+
let splatRoute = null;
|
|
135
|
+
let splatConfigFile = "";
|
|
136
|
+
let splatClientData = "";
|
|
137
|
+
let splatData = "";
|
|
138
|
+
let splatAction = "";
|
|
139
|
+
const items = await import_utils.fs.readdir(dirname);
|
|
140
|
+
for (const item of items) {
|
|
141
|
+
const itemPath = path.join(dirname, item);
|
|
142
|
+
const itemPathWithAlias = (0, import_utils2.getPathWithoutExt)((0, import_utils2.replaceWithAlias)(alias.basename, itemPath, alias.name));
|
|
143
|
+
const extname = path.extname(item);
|
|
144
|
+
const itemWithoutExt = item.slice(0, -extname.length);
|
|
145
|
+
const isDirectory2 = (await import_utils.fs.stat(itemPath)).isDirectory();
|
|
146
|
+
if (isDirectory2) {
|
|
147
|
+
const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
|
|
148
|
+
if (childRoute && !Array.isArray(childRoute)) {
|
|
149
|
+
var _route_children;
|
|
150
|
+
(_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (extname && (!import_utils.JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.LAYOUT_LOADER_FILE) {
|
|
157
|
+
if (!route.loader) {
|
|
158
|
+
route.loader = itemPathWithAlias;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.LAYOUT_CLIENT_LOADER) {
|
|
162
|
+
route.clientData = itemPathWithAlias;
|
|
163
|
+
}
|
|
164
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.LAYOUT_DATA_FILE) {
|
|
165
|
+
route.data = itemPathWithAlias;
|
|
166
|
+
if (await (0, import_utils2.hasAction)(itemPath)) {
|
|
167
|
+
route.action = itemPathWithAlias;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.LAYOUT_CONFIG_FILE) {
|
|
171
|
+
if (!route.config) {
|
|
172
|
+
route.config = itemPathWithAlias;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.LAYOUT_FILE) {
|
|
176
|
+
route._component = itemPathWithAlias;
|
|
177
|
+
}
|
|
178
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.PAGE_LOADER_FILE) {
|
|
179
|
+
pageLoaderFile = itemPathWithAlias;
|
|
180
|
+
}
|
|
181
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.PAGE_CLIENT_LOADER) {
|
|
182
|
+
pageClientData = itemPathWithAlias;
|
|
183
|
+
}
|
|
184
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.PAGE_DATA_FILE) {
|
|
185
|
+
pageData = itemPathWithAlias;
|
|
186
|
+
if (await (0, import_utils2.hasAction)(itemPath)) {
|
|
187
|
+
pageAction = itemPathWithAlias;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.PAGE_CONFIG_FILE) {
|
|
191
|
+
pageConfigFile = itemPathWithAlias;
|
|
192
|
+
}
|
|
193
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.PAGE_FILE) {
|
|
194
|
+
var _route_children1;
|
|
195
|
+
pageRoute = createIndexRoute({
|
|
196
|
+
_component: itemPathWithAlias
|
|
197
|
+
}, rootDir, itemPath, entryName, isMainEntry);
|
|
198
|
+
if (pageLoaderFile) {
|
|
199
|
+
pageRoute.loader = pageLoaderFile;
|
|
200
|
+
}
|
|
201
|
+
if (pageConfigFile) {
|
|
202
|
+
pageRoute.config = pageConfigFile;
|
|
203
|
+
}
|
|
204
|
+
if (pageData) {
|
|
205
|
+
pageRoute.data = pageData;
|
|
206
|
+
}
|
|
207
|
+
if (pageClientData) {
|
|
208
|
+
pageRoute.clientData = pageClientData;
|
|
209
|
+
}
|
|
210
|
+
if (pageAction) {
|
|
211
|
+
pageRoute.action = pageAction;
|
|
212
|
+
}
|
|
213
|
+
(_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
|
|
214
|
+
}
|
|
215
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.SPLATE_LOADER_FILE) {
|
|
216
|
+
splatLoaderFile = itemPathWithAlias;
|
|
217
|
+
}
|
|
218
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.SPLATE_CLIENT_DATA) {
|
|
219
|
+
splatClientData = itemPathWithAlias;
|
|
220
|
+
}
|
|
221
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.SPLATE_CONFIG_FILE) {
|
|
222
|
+
if (!route.config) {
|
|
223
|
+
splatConfigFile = (0, import_utils2.replaceWithAlias)(alias.basename, itemPath, alias.name);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.SPLATE_DATA_FILE) {
|
|
227
|
+
splatData = itemPathWithAlias;
|
|
228
|
+
if (await (0, import_utils2.hasAction)(itemPath)) {
|
|
229
|
+
splatAction = itemPathWithAlias;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.SPLATE_FILE) {
|
|
233
|
+
var _route_children2;
|
|
234
|
+
splatRoute = createRoute({
|
|
235
|
+
_component: itemPathWithAlias,
|
|
236
|
+
path: "*"
|
|
237
|
+
}, rootDir, itemPath, entryName, isMainEntry);
|
|
238
|
+
if (splatLoaderFile) {
|
|
239
|
+
splatRoute.loader = splatLoaderFile;
|
|
240
|
+
}
|
|
241
|
+
if (splatClientData) {
|
|
242
|
+
splatRoute.clientData = splatClientData;
|
|
243
|
+
}
|
|
244
|
+
if (splatData) {
|
|
245
|
+
splatRoute.data = splatData;
|
|
246
|
+
}
|
|
247
|
+
if (splatConfigFile) {
|
|
248
|
+
splatRoute.config = splatConfigFile;
|
|
249
|
+
}
|
|
250
|
+
if (splatAction) {
|
|
251
|
+
splatRoute.action = splatAction;
|
|
252
|
+
}
|
|
253
|
+
(_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
|
|
254
|
+
}
|
|
255
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.LOADING_FILE) {
|
|
256
|
+
route.loading = itemPathWithAlias;
|
|
257
|
+
}
|
|
258
|
+
if (itemWithoutExt === import_constants.NESTED_ROUTE.ERROR_FILE) {
|
|
259
|
+
route.error = itemPathWithAlias;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
let finalRoute = createRoute(route, rootDir, path.join(dirname, `${import_constants.NESTED_ROUTE.LAYOUT_FILE}.ts`), entryName, isMainEntry);
|
|
263
|
+
if (isPathlessLayout) {
|
|
264
|
+
delete finalRoute.path;
|
|
265
|
+
}
|
|
266
|
+
const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
|
|
267
|
+
if (childRoutes && childRoutes.length === 0 && !finalRoute.index && !finalRoute._component) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
|
|
271
|
+
const childRoute = childRoutes[0];
|
|
272
|
+
if (childRoute.path === "*") {
|
|
273
|
+
const path2 = `${finalRoute.path || ""}/${childRoute.path || ""}`;
|
|
274
|
+
finalRoute = {
|
|
275
|
+
...childRoute,
|
|
276
|
+
path: path2
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (isRoot && !finalRoute._component) {
|
|
281
|
+
throw new Error("The root layout component is required, make sure the routes/layout.tsx file exists.");
|
|
282
|
+
}
|
|
283
|
+
if (isRoot && !oldVersion) {
|
|
284
|
+
const optimizedRoutes = optimizeRoute(finalRoute);
|
|
285
|
+
return optimizedRoutes;
|
|
286
|
+
}
|
|
287
|
+
return finalRoute;
|
|
288
|
+
};
|
|
289
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
290
|
+
0 && (module.exports = {
|
|
291
|
+
getRouteId,
|
|
292
|
+
optimizeRoute,
|
|
293
|
+
walk
|
|
294
|
+
});
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var templates_exports = {};
|
|
30
|
+
__export(templates_exports, {
|
|
31
|
+
fileSystemRoutes: () => fileSystemRoutes,
|
|
32
|
+
routesForServer: () => routesForServer,
|
|
33
|
+
ssrLoaderCombinedModule: () => ssrLoaderCombinedModule
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(templates_exports);
|
|
36
|
+
var import_path = __toESM(require("path"));
|
|
37
|
+
var import_utils = require("@modern-js/utils");
|
|
38
|
+
var import_constants = require("@modern-js/utils/universal/constants");
|
|
39
|
+
var import_constants2 = require("../constants");
|
|
40
|
+
var import_utils2 = require("./utils");
|
|
41
|
+
const routesForServer = ({ routes }) => {
|
|
42
|
+
const loaders = [];
|
|
43
|
+
const actions = [];
|
|
44
|
+
const loadersMap = {};
|
|
45
|
+
const traverseRouteTree = (route) => {
|
|
46
|
+
let children;
|
|
47
|
+
if ("children" in route && route.children) {
|
|
48
|
+
var _route_children;
|
|
49
|
+
children = route === null || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
50
|
+
}
|
|
51
|
+
let loader;
|
|
52
|
+
let action;
|
|
53
|
+
if (route.type === "nested") {
|
|
54
|
+
if (route.loader || route.data) {
|
|
55
|
+
loaders.push(route.loader);
|
|
56
|
+
const loaderId = loaders.length - 1;
|
|
57
|
+
loader = `loader_${loaderId}`;
|
|
58
|
+
const inline = Boolean(route.data);
|
|
59
|
+
loadersMap[loader] = {
|
|
60
|
+
loaderId,
|
|
61
|
+
routeId: route.id,
|
|
62
|
+
filePath: route.data || route.loader,
|
|
63
|
+
clientData: Boolean(route.clientData),
|
|
64
|
+
route,
|
|
65
|
+
inline
|
|
66
|
+
};
|
|
67
|
+
if (route.action) {
|
|
68
|
+
actions.push(route.action);
|
|
69
|
+
action = `action_${loaders.length - 1}`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const finalRoute = {
|
|
74
|
+
...route,
|
|
75
|
+
loader,
|
|
76
|
+
action,
|
|
77
|
+
children
|
|
78
|
+
};
|
|
79
|
+
return finalRoute;
|
|
80
|
+
};
|
|
81
|
+
let routesCode = `
|
|
82
|
+
export const routes = [
|
|
83
|
+
`;
|
|
84
|
+
for (const route of routes) {
|
|
85
|
+
if ("type" in route) {
|
|
86
|
+
const keywords = [
|
|
87
|
+
"loader",
|
|
88
|
+
"action"
|
|
89
|
+
];
|
|
90
|
+
const regs = keywords.map(createMatchReg);
|
|
91
|
+
const newRoute = traverseRouteTree(route);
|
|
92
|
+
const routeStr = JSON.stringify(newRoute, null, 2);
|
|
93
|
+
routesCode += regs.reduce((acc, reg) => acc.replace(reg, "$1$2"), routeStr).replace(/\\"/g, '"');
|
|
94
|
+
} else {
|
|
95
|
+
routesCode += `${JSON.stringify(route, null, 2)}`;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
routesCode += `
|
|
99
|
+
];`;
|
|
100
|
+
let importLoadersCode = "";
|
|
101
|
+
for (const [key, loaderInfo] of Object.entries(loadersMap)) {
|
|
102
|
+
if (loaderInfo.inline) {
|
|
103
|
+
const { route } = loaderInfo;
|
|
104
|
+
if (route.action) {
|
|
105
|
+
importLoadersCode += `import { loader as ${key}, action as action_${loaderInfo.loaderId} } from "${(0, import_utils.slash)(loaderInfo.filePath)}";
|
|
106
|
+
`;
|
|
107
|
+
} else {
|
|
108
|
+
importLoadersCode += `import { loader as ${key} } from "${(0, import_utils.slash)(loaderInfo.filePath)}";
|
|
109
|
+
`;
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
importLoadersCode += `import ${key} from "${(0, import_utils.slash)(loaderInfo.filePath)}";
|
|
113
|
+
`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return `
|
|
117
|
+
${importLoadersCode}
|
|
118
|
+
${routesCode}
|
|
119
|
+
`;
|
|
120
|
+
};
|
|
121
|
+
const createMatchReg = (keyword) => new RegExp(`("${keyword}":\\s)"([^
|
|
122
|
+
]+)"`, "g");
|
|
123
|
+
const fileSystemRoutes = async ({ routes, ssrMode, nestedRoutesEntry, entryName, internalDirectory, splitRouteChunks = true }) => {
|
|
124
|
+
const components = [];
|
|
125
|
+
const loadings = [];
|
|
126
|
+
const errors = [];
|
|
127
|
+
const loaders = [];
|
|
128
|
+
const loadersMap = {};
|
|
129
|
+
const configs = [];
|
|
130
|
+
const configsMap = {};
|
|
131
|
+
const loadersMapFile = import_path.default.join(internalDirectory, entryName, import_constants2.TEMP_LOADERS_DIR, "map.json");
|
|
132
|
+
const importLazyCode = `
|
|
133
|
+
import { lazy } from "react";
|
|
134
|
+
import loadable, { lazy as loadableLazy } from "@modern-js/runtime/loadable"
|
|
135
|
+
`;
|
|
136
|
+
let rootLayoutCode = ``;
|
|
137
|
+
const getDataLoaderPath = ({ loaderId, clientData, action, inline, routeId }) => {
|
|
138
|
+
if (!ssrMode) {
|
|
139
|
+
return "";
|
|
140
|
+
}
|
|
141
|
+
const clientDataStr = clientData ? `&clientData=${clientData}` : "";
|
|
142
|
+
if (nestedRoutesEntry) {
|
|
143
|
+
return `?loaderId=${loaderId}${clientDataStr}&action=${action ? (0, import_utils.slash)(action) : action}&inline=${inline}&routeId=${routeId}`;
|
|
144
|
+
}
|
|
145
|
+
return "";
|
|
146
|
+
};
|
|
147
|
+
const traverseRouteTree = (route) => {
|
|
148
|
+
let children;
|
|
149
|
+
if ("children" in route && route.children) {
|
|
150
|
+
var _route_children;
|
|
151
|
+
children = route === null || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
152
|
+
}
|
|
153
|
+
let loading;
|
|
154
|
+
let error;
|
|
155
|
+
let loader;
|
|
156
|
+
let action;
|
|
157
|
+
let config;
|
|
158
|
+
let component = "";
|
|
159
|
+
let lazyImport = null;
|
|
160
|
+
if (route.type === "nested") {
|
|
161
|
+
if (route.loading) {
|
|
162
|
+
loadings.push(route.loading);
|
|
163
|
+
loading = `loading_${loadings.length - 1}`;
|
|
164
|
+
}
|
|
165
|
+
if (route.error) {
|
|
166
|
+
errors.push(route.error);
|
|
167
|
+
error = `error_${errors.length - 1}`;
|
|
168
|
+
}
|
|
169
|
+
if (route.loader || route.data) {
|
|
170
|
+
loaders.push(route.loader);
|
|
171
|
+
const loaderId = loaders.length - 1;
|
|
172
|
+
loader = `loader_${loaderId}`;
|
|
173
|
+
const inline = Boolean(route.data);
|
|
174
|
+
loadersMap[loader] = {
|
|
175
|
+
loaderId,
|
|
176
|
+
routeId: route.id,
|
|
177
|
+
filePath: route.data || route.loader,
|
|
178
|
+
clientData: Boolean(route.clientData),
|
|
179
|
+
route,
|
|
180
|
+
inline
|
|
181
|
+
};
|
|
182
|
+
loader = `loader_${loaderId}`;
|
|
183
|
+
if (route.action) {
|
|
184
|
+
action = `action_${loaderId}`;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (typeof route.config === "string") {
|
|
188
|
+
configs.push(route.config);
|
|
189
|
+
const configId = configs.length - 1;
|
|
190
|
+
config = `config_${configId}`;
|
|
191
|
+
configsMap[config] = route.config;
|
|
192
|
+
}
|
|
193
|
+
if (route._component) {
|
|
194
|
+
if (splitRouteChunks) {
|
|
195
|
+
if (route.isRoot) {
|
|
196
|
+
lazyImport = `() => import('${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
|
|
197
|
+
rootLayoutCode = `import RootLayout from '${route._component}'`;
|
|
198
|
+
component = `RootLayout`;
|
|
199
|
+
} else if (ssrMode === "string") {
|
|
200
|
+
lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
|
|
201
|
+
component = `loadable(${lazyImport})`;
|
|
202
|
+
} else {
|
|
203
|
+
lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
|
|
204
|
+
component = `lazy(${lazyImport})`;
|
|
205
|
+
}
|
|
206
|
+
} else if (ssrMode === "string") {
|
|
207
|
+
components.push(route._component);
|
|
208
|
+
component = `component_${components.length - 1}`;
|
|
209
|
+
} else {
|
|
210
|
+
lazyImport = `() => import(/* webpackMode: "eager" */ '${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
|
|
211
|
+
component = `lazy(${lazyImport})`;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
} else if (route._component) {
|
|
215
|
+
if (splitRouteChunks) {
|
|
216
|
+
lazyImport = `() => import('${route._component}')`;
|
|
217
|
+
component = `loadable(${lazyImport})`;
|
|
218
|
+
} else {
|
|
219
|
+
components.push(route._component);
|
|
220
|
+
component = `component_${components.length - 1}`;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const finalRoute = {
|
|
224
|
+
...route,
|
|
225
|
+
lazyImport,
|
|
226
|
+
loading,
|
|
227
|
+
loader,
|
|
228
|
+
action,
|
|
229
|
+
config,
|
|
230
|
+
error,
|
|
231
|
+
children
|
|
232
|
+
};
|
|
233
|
+
if (route._component) {
|
|
234
|
+
finalRoute.component = component;
|
|
235
|
+
}
|
|
236
|
+
if (route.type === "nested" && route._component && (route.loader || route.data)) {
|
|
237
|
+
finalRoute.shouldRevalidate = `createShouldRevalidate("${route.id}")`;
|
|
238
|
+
}
|
|
239
|
+
return finalRoute;
|
|
240
|
+
};
|
|
241
|
+
let routeComponentsCode = `
|
|
242
|
+
export const routes = [
|
|
243
|
+
`;
|
|
244
|
+
for (const route of routes) {
|
|
245
|
+
if ("type" in route) {
|
|
246
|
+
const newRoute = traverseRouteTree(route);
|
|
247
|
+
const routeStr = JSON.stringify(newRoute, null, 2);
|
|
248
|
+
const keywords = [
|
|
249
|
+
"component",
|
|
250
|
+
"lazyImport",
|
|
251
|
+
"shouldRevalidate",
|
|
252
|
+
"loader",
|
|
253
|
+
"action",
|
|
254
|
+
"loading",
|
|
255
|
+
"error",
|
|
256
|
+
"config"
|
|
257
|
+
];
|
|
258
|
+
const regs = keywords.map(createMatchReg);
|
|
259
|
+
const newRouteStr = regs.reduce((acc, reg) => acc.replace(reg, "$1$2"), routeStr).replace(/"(RootLayout)"/g, "$1").replace(/\\"/g, '"');
|
|
260
|
+
routeComponentsCode += `${newRouteStr},`;
|
|
261
|
+
} else {
|
|
262
|
+
const component = `loadable(() => import('${route._component}'))`;
|
|
263
|
+
const finalRoute = {
|
|
264
|
+
...route,
|
|
265
|
+
component
|
|
266
|
+
};
|
|
267
|
+
const keywords = [
|
|
268
|
+
"component",
|
|
269
|
+
"lazyImport"
|
|
270
|
+
];
|
|
271
|
+
const routeStr = JSON.stringify(finalRoute, null, 2);
|
|
272
|
+
const regs = keywords.map(createMatchReg);
|
|
273
|
+
const newRouteStr = regs.reduce((acc, reg) => acc.replace(reg, "$1$2"), routeStr).replace(/\\"/g, '"');
|
|
274
|
+
routeComponentsCode += `${newRouteStr},`;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
routeComponentsCode += `
|
|
278
|
+
];`;
|
|
279
|
+
const importLoadingCode = loadings.map((loading, index) => {
|
|
280
|
+
return `import loading_${index} from '${loading}';
|
|
281
|
+
`;
|
|
282
|
+
}).join("");
|
|
283
|
+
const importComponentsCode = components.map((component, index) => {
|
|
284
|
+
return `import component_${index} from '${component}';
|
|
285
|
+
`;
|
|
286
|
+
}).join("");
|
|
287
|
+
const importErrorComponentsCode = errors.map((error, index) => {
|
|
288
|
+
return `import error_${index} from '${error}';
|
|
289
|
+
`;
|
|
290
|
+
}).join("");
|
|
291
|
+
let importLoadersCode = "";
|
|
292
|
+
for (const [key, loaderInfo] of Object.entries(loadersMap)) {
|
|
293
|
+
if (loaderInfo.inline) {
|
|
294
|
+
const { route } = loaderInfo;
|
|
295
|
+
if (route.action) {
|
|
296
|
+
importLoadersCode += `import { loader as ${key}, action as action_${loaderInfo.loaderId} } from "${(0, import_utils.slash)(loaderInfo.filePath)}${getDataLoaderPath({
|
|
297
|
+
loaderId: key,
|
|
298
|
+
clientData: loaderInfo.clientData,
|
|
299
|
+
action: route.action,
|
|
300
|
+
inline: loaderInfo.inline,
|
|
301
|
+
routeId: loaderInfo.routeId
|
|
302
|
+
})}";
|
|
303
|
+
`;
|
|
304
|
+
} else {
|
|
305
|
+
importLoadersCode += `import { loader as ${key} } from "${(0, import_utils.slash)(loaderInfo.filePath)}${getDataLoaderPath({
|
|
306
|
+
loaderId: key,
|
|
307
|
+
clientData: loaderInfo.clientData,
|
|
308
|
+
action: false,
|
|
309
|
+
inline: loaderInfo.inline,
|
|
310
|
+
routeId: route.id
|
|
311
|
+
})}";
|
|
312
|
+
`;
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
importLoadersCode += `import ${key} from "${(0, import_utils.slash)(loaderInfo.filePath)}${getDataLoaderPath({
|
|
316
|
+
loaderId: key,
|
|
317
|
+
clientData: loaderInfo.clientData,
|
|
318
|
+
action: false,
|
|
319
|
+
inline: loaderInfo.inline,
|
|
320
|
+
routeId: loaderInfo.routeId
|
|
321
|
+
})}";
|
|
322
|
+
`;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
let importConfigsCode = "";
|
|
326
|
+
for (const [key, configPath] of Object.entries(configsMap)) {
|
|
327
|
+
importConfigsCode += `import * as ${key} from "${(0, import_utils.slash)(configPath)}";
|
|
328
|
+
`;
|
|
329
|
+
}
|
|
330
|
+
await import_utils.fs.ensureFile(loadersMapFile);
|
|
331
|
+
await import_utils.fs.writeJSON(loadersMapFile, loadersMap);
|
|
332
|
+
const importRuntimeRouterCode = `
|
|
333
|
+
import { createShouldRevalidate, handleRouteModule, handleRouteModuleError} from '@modern-js/runtime/router';
|
|
334
|
+
`;
|
|
335
|
+
const routeModulesCode = `
|
|
336
|
+
if(typeof document !== 'undefined'){
|
|
337
|
+
window.${import_constants.ROUTE_MODULES} = {}
|
|
338
|
+
}
|
|
339
|
+
`;
|
|
340
|
+
return `
|
|
341
|
+
${importLazyCode}
|
|
342
|
+
${importComponentsCode}
|
|
343
|
+
${importRuntimeRouterCode}
|
|
344
|
+
${rootLayoutCode}
|
|
345
|
+
${importLoadingCode}
|
|
346
|
+
${importErrorComponentsCode}
|
|
347
|
+
${importLoadersCode}
|
|
348
|
+
${importConfigsCode}
|
|
349
|
+
${routeModulesCode}
|
|
350
|
+
${routeComponentsCode}
|
|
351
|
+
`;
|
|
352
|
+
};
|
|
353
|
+
function ssrLoaderCombinedModule(entrypoints, entrypoint, config, appContext) {
|
|
354
|
+
const { entryName, isMainEntry } = entrypoint;
|
|
355
|
+
const { packageName, internalDirectory } = appContext;
|
|
356
|
+
const ssr = (0, import_utils.getEntryOptions)(entryName, isMainEntry, config.server.ssr, config.server.ssrByEntries, packageName);
|
|
357
|
+
const ssg = (0, import_utils.isSSGEntry)(config, entryName, entrypoints);
|
|
358
|
+
if (entrypoint.nestedRoutesEntry && (ssr || ssg)) {
|
|
359
|
+
const serverLoaderRuntime = require.resolve("@modern-js/plugin-data-loader/runtime");
|
|
360
|
+
const serverLoadersFile = (0, import_utils2.getServerLoadersFile)(internalDirectory, entryName);
|
|
361
|
+
const combinedModule = `export * from "${(0, import_utils.slash)(serverLoaderRuntime)}"; export * from "${(0, import_utils.slash)(serverLoadersFile)}"`;
|
|
362
|
+
return combinedModule;
|
|
363
|
+
}
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
367
|
+
0 && (module.exports = {
|
|
368
|
+
fileSystemRoutes,
|
|
369
|
+
routesForServer,
|
|
370
|
+
ssrLoaderCombinedModule
|
|
371
|
+
});
|