@modern-js/runtime 2.53.0 → 2.54.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.
- 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,417 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
+
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
5
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { fs, getEntryOptions, isSSGEntry, slash } from "@modern-js/utils";
|
|
8
|
+
import { ROUTE_MODULES } from "@modern-js/utils/universal/constants";
|
|
9
|
+
import { TEMP_LOADERS_DIR } from "../constants";
|
|
10
|
+
import { getServerLoadersFile } from "./utils";
|
|
11
|
+
var routesForServer = function(param) {
|
|
12
|
+
var routes = param.routes;
|
|
13
|
+
var loaders = [];
|
|
14
|
+
var actions = [];
|
|
15
|
+
var loadersMap = {};
|
|
16
|
+
var traverseRouteTree = function(route2) {
|
|
17
|
+
var children;
|
|
18
|
+
if ("children" in route2 && route2.children) {
|
|
19
|
+
var _route_children;
|
|
20
|
+
children = route2 === null || route2 === void 0 ? void 0 : (_route_children = route2.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
21
|
+
}
|
|
22
|
+
var loader;
|
|
23
|
+
var action;
|
|
24
|
+
if (route2.type === "nested") {
|
|
25
|
+
if (route2.loader || route2.data) {
|
|
26
|
+
loaders.push(route2.loader);
|
|
27
|
+
var loaderId = loaders.length - 1;
|
|
28
|
+
loader = "loader_".concat(loaderId);
|
|
29
|
+
var inline = Boolean(route2.data);
|
|
30
|
+
loadersMap[loader] = {
|
|
31
|
+
loaderId,
|
|
32
|
+
routeId: route2.id,
|
|
33
|
+
filePath: route2.data || route2.loader,
|
|
34
|
+
clientData: Boolean(route2.clientData),
|
|
35
|
+
route: route2,
|
|
36
|
+
inline
|
|
37
|
+
};
|
|
38
|
+
if (route2.action) {
|
|
39
|
+
actions.push(route2.action);
|
|
40
|
+
action = "action_".concat(loaders.length - 1);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
var finalRoute = _object_spread_props(_object_spread({}, route2), {
|
|
45
|
+
loader,
|
|
46
|
+
action,
|
|
47
|
+
children
|
|
48
|
+
});
|
|
49
|
+
return finalRoute;
|
|
50
|
+
};
|
|
51
|
+
var routesCode = "\n export const routes = [\n ";
|
|
52
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
53
|
+
try {
|
|
54
|
+
for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
55
|
+
var route = _step.value;
|
|
56
|
+
if ("type" in route) {
|
|
57
|
+
var keywords = [
|
|
58
|
+
"loader",
|
|
59
|
+
"action"
|
|
60
|
+
];
|
|
61
|
+
var regs = keywords.map(createMatchReg);
|
|
62
|
+
var newRoute = traverseRouteTree(route);
|
|
63
|
+
var routeStr = JSON.stringify(newRoute, null, 2);
|
|
64
|
+
routesCode += regs.reduce(function(acc, reg) {
|
|
65
|
+
return acc.replace(reg, "$1$2");
|
|
66
|
+
}, routeStr).replace(/\\"/g, '"');
|
|
67
|
+
} else {
|
|
68
|
+
routesCode += "".concat(JSON.stringify(route, null, 2));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
} catch (err) {
|
|
72
|
+
_didIteratorError = true;
|
|
73
|
+
_iteratorError = err;
|
|
74
|
+
} finally {
|
|
75
|
+
try {
|
|
76
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
77
|
+
_iterator.return();
|
|
78
|
+
}
|
|
79
|
+
} finally {
|
|
80
|
+
if (_didIteratorError) {
|
|
81
|
+
throw _iteratorError;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
routesCode += "\n];";
|
|
86
|
+
var importLoadersCode = "";
|
|
87
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
|
|
88
|
+
try {
|
|
89
|
+
for (var _iterator1 = Object.entries(loadersMap)[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
|
|
90
|
+
var _step_value = _sliced_to_array(_step1.value, 2), key = _step_value[0], loaderInfo = _step_value[1];
|
|
91
|
+
if (loaderInfo.inline) {
|
|
92
|
+
var route1 = loaderInfo.route;
|
|
93
|
+
if (route1.action) {
|
|
94
|
+
importLoadersCode += "import { loader as ".concat(key, ", action as action_").concat(loaderInfo.loaderId, ' } from "').concat(slash(loaderInfo.filePath), '";\n');
|
|
95
|
+
} else {
|
|
96
|
+
importLoadersCode += "import { loader as ".concat(key, ' } from "').concat(slash(loaderInfo.filePath), '";\n');
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
importLoadersCode += "import ".concat(key, ' from "').concat(slash(loaderInfo.filePath), '";\n');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} catch (err) {
|
|
103
|
+
_didIteratorError1 = true;
|
|
104
|
+
_iteratorError1 = err;
|
|
105
|
+
} finally {
|
|
106
|
+
try {
|
|
107
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
108
|
+
_iterator1.return();
|
|
109
|
+
}
|
|
110
|
+
} finally {
|
|
111
|
+
if (_didIteratorError1) {
|
|
112
|
+
throw _iteratorError1;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return "\n ".concat(importLoadersCode, "\n ").concat(routesCode, "\n ");
|
|
117
|
+
};
|
|
118
|
+
var createMatchReg = function(keyword) {
|
|
119
|
+
return new RegExp('("'.concat(keyword, '":\\s)"([^\n]+)"'), "g");
|
|
120
|
+
};
|
|
121
|
+
var fileSystemRoutes = function() {
|
|
122
|
+
var _ref = _async_to_generator(function(param) {
|
|
123
|
+
var routes, ssrMode, nestedRoutesEntry, entryName, internalDirectory, _param_splitRouteChunks, splitRouteChunks, components, loadings, errors, loaders, loadersMap, configs, configsMap, loadersMapFile, importLazyCode, rootLayoutCode, getDataLoaderPath, traverseRouteTree, routeComponentsCode, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, newRoute, routeStr, keywords, regs, newRouteStr, component, finalRoute, keywords1, routeStr1, regs1, newRouteStr1, importLoadingCode, importComponentsCode, importErrorComponentsCode, importLoadersCode, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, _step_value, key, loaderInfo, route1, importConfigsCode, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _step_value1, key1, configPath, importRuntimeRouterCode, routeModulesCode;
|
|
124
|
+
return _ts_generator(this, function(_state) {
|
|
125
|
+
switch (_state.label) {
|
|
126
|
+
case 0:
|
|
127
|
+
routes = param.routes, ssrMode = param.ssrMode, nestedRoutesEntry = param.nestedRoutesEntry, entryName = param.entryName, internalDirectory = param.internalDirectory, _param_splitRouteChunks = param.splitRouteChunks, splitRouteChunks = _param_splitRouteChunks === void 0 ? true : _param_splitRouteChunks;
|
|
128
|
+
components = [];
|
|
129
|
+
loadings = [];
|
|
130
|
+
errors = [];
|
|
131
|
+
loaders = [];
|
|
132
|
+
loadersMap = {};
|
|
133
|
+
configs = [];
|
|
134
|
+
configsMap = {};
|
|
135
|
+
loadersMapFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, "map.json");
|
|
136
|
+
importLazyCode = '\n import { lazy } from "react";\n import loadable, { lazy as loadableLazy } from "@modern-js/runtime/loadable"\n ';
|
|
137
|
+
rootLayoutCode = "";
|
|
138
|
+
getDataLoaderPath = function(param2) {
|
|
139
|
+
var loaderId = param2.loaderId, clientData = param2.clientData, action = param2.action, inline = param2.inline, routeId = param2.routeId;
|
|
140
|
+
if (!ssrMode) {
|
|
141
|
+
return "";
|
|
142
|
+
}
|
|
143
|
+
var clientDataStr = clientData ? "&clientData=".concat(clientData) : "";
|
|
144
|
+
if (nestedRoutesEntry) {
|
|
145
|
+
return "?loaderId=".concat(loaderId).concat(clientDataStr, "&action=").concat(action ? slash(action) : action, "&inline=").concat(inline, "&routeId=").concat(routeId);
|
|
146
|
+
}
|
|
147
|
+
return "";
|
|
148
|
+
};
|
|
149
|
+
traverseRouteTree = function(route2) {
|
|
150
|
+
var children;
|
|
151
|
+
if ("children" in route2 && route2.children) {
|
|
152
|
+
var _route_children;
|
|
153
|
+
children = route2 === null || route2 === void 0 ? void 0 : (_route_children = route2.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
|
|
154
|
+
}
|
|
155
|
+
var loading;
|
|
156
|
+
var error;
|
|
157
|
+
var loader;
|
|
158
|
+
var action;
|
|
159
|
+
var config;
|
|
160
|
+
var component2 = "";
|
|
161
|
+
var lazyImport = null;
|
|
162
|
+
if (route2.type === "nested") {
|
|
163
|
+
if (route2.loading) {
|
|
164
|
+
loadings.push(route2.loading);
|
|
165
|
+
loading = "loading_".concat(loadings.length - 1);
|
|
166
|
+
}
|
|
167
|
+
if (route2.error) {
|
|
168
|
+
errors.push(route2.error);
|
|
169
|
+
error = "error_".concat(errors.length - 1);
|
|
170
|
+
}
|
|
171
|
+
if (route2.loader || route2.data) {
|
|
172
|
+
loaders.push(route2.loader);
|
|
173
|
+
var loaderId = loaders.length - 1;
|
|
174
|
+
loader = "loader_".concat(loaderId);
|
|
175
|
+
var inline = Boolean(route2.data);
|
|
176
|
+
loadersMap[loader] = {
|
|
177
|
+
loaderId,
|
|
178
|
+
routeId: route2.id,
|
|
179
|
+
filePath: route2.data || route2.loader,
|
|
180
|
+
clientData: Boolean(route2.clientData),
|
|
181
|
+
route: route2,
|
|
182
|
+
inline
|
|
183
|
+
};
|
|
184
|
+
loader = "loader_".concat(loaderId);
|
|
185
|
+
if (route2.action) {
|
|
186
|
+
action = "action_".concat(loaderId);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (typeof route2.config === "string") {
|
|
190
|
+
configs.push(route2.config);
|
|
191
|
+
var configId = configs.length - 1;
|
|
192
|
+
config = "config_".concat(configId);
|
|
193
|
+
configsMap[config] = route2.config;
|
|
194
|
+
}
|
|
195
|
+
if (route2._component) {
|
|
196
|
+
if (splitRouteChunks) {
|
|
197
|
+
if (route2.isRoot) {
|
|
198
|
+
lazyImport = "() => import('".concat(route2._component, `').then(routeModule => handleRouteModule(routeModule, "`).concat(route2.id, '")).catch(handleRouteModuleError) ');
|
|
199
|
+
rootLayoutCode = "import RootLayout from '".concat(route2._component, "'");
|
|
200
|
+
component2 = "RootLayout";
|
|
201
|
+
} else if (ssrMode === "string") {
|
|
202
|
+
lazyImport = '() => import(/* webpackChunkName: "'.concat(route2.id, `" */ '`).concat(route2._component, `').then(routeModule => handleRouteModule(routeModule, "`).concat(route2.id, '")).catch(handleRouteModuleError) ');
|
|
203
|
+
component2 = "loadable(".concat(lazyImport, ")");
|
|
204
|
+
} else {
|
|
205
|
+
lazyImport = '() => import(/* webpackChunkName: "'.concat(route2.id, `" */ '`).concat(route2._component, `').then(routeModule => handleRouteModule(routeModule, "`).concat(route2.id, '")).catch(handleRouteModuleError) ');
|
|
206
|
+
component2 = "lazy(".concat(lazyImport, ")");
|
|
207
|
+
}
|
|
208
|
+
} else if (ssrMode === "string") {
|
|
209
|
+
components.push(route2._component);
|
|
210
|
+
component2 = "component_".concat(components.length - 1);
|
|
211
|
+
} else {
|
|
212
|
+
lazyImport = `() => import(/* webpackMode: "eager" */ '`.concat(route2._component, `').then(routeModule => handleRouteModule(routeModule, "`).concat(route2.id, '")).catch(handleRouteModuleError) ');
|
|
213
|
+
component2 = "lazy(".concat(lazyImport, ")");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
} else if (route2._component) {
|
|
217
|
+
if (splitRouteChunks) {
|
|
218
|
+
lazyImport = "() => import('".concat(route2._component, "')");
|
|
219
|
+
component2 = "loadable(".concat(lazyImport, ")");
|
|
220
|
+
} else {
|
|
221
|
+
components.push(route2._component);
|
|
222
|
+
component2 = "component_".concat(components.length - 1);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
var finalRoute2 = _object_spread_props(_object_spread({}, route2), {
|
|
226
|
+
lazyImport,
|
|
227
|
+
loading,
|
|
228
|
+
loader,
|
|
229
|
+
action,
|
|
230
|
+
config,
|
|
231
|
+
error,
|
|
232
|
+
children
|
|
233
|
+
});
|
|
234
|
+
if (route2._component) {
|
|
235
|
+
finalRoute2.component = component2;
|
|
236
|
+
}
|
|
237
|
+
if (route2.type === "nested" && route2._component && (route2.loader || route2.data)) {
|
|
238
|
+
finalRoute2.shouldRevalidate = 'createShouldRevalidate("'.concat(route2.id, '")');
|
|
239
|
+
}
|
|
240
|
+
return finalRoute2;
|
|
241
|
+
};
|
|
242
|
+
routeComponentsCode = "\n export const routes = [\n ";
|
|
243
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
244
|
+
try {
|
|
245
|
+
for (_iterator = routes[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
246
|
+
route = _step.value;
|
|
247
|
+
if ("type" in route) {
|
|
248
|
+
newRoute = traverseRouteTree(route);
|
|
249
|
+
routeStr = JSON.stringify(newRoute, null, 2);
|
|
250
|
+
keywords = [
|
|
251
|
+
"component",
|
|
252
|
+
"lazyImport",
|
|
253
|
+
"shouldRevalidate",
|
|
254
|
+
"loader",
|
|
255
|
+
"action",
|
|
256
|
+
"loading",
|
|
257
|
+
"error",
|
|
258
|
+
"config"
|
|
259
|
+
];
|
|
260
|
+
regs = keywords.map(createMatchReg);
|
|
261
|
+
newRouteStr = regs.reduce(function(acc, reg) {
|
|
262
|
+
return acc.replace(reg, "$1$2");
|
|
263
|
+
}, routeStr).replace(/"(RootLayout)"/g, "$1").replace(/\\"/g, '"');
|
|
264
|
+
routeComponentsCode += "".concat(newRouteStr, ",");
|
|
265
|
+
} else {
|
|
266
|
+
component = "loadable(() => import('".concat(route._component, "'))");
|
|
267
|
+
finalRoute = _object_spread_props(_object_spread({}, route), {
|
|
268
|
+
component
|
|
269
|
+
});
|
|
270
|
+
keywords1 = [
|
|
271
|
+
"component",
|
|
272
|
+
"lazyImport"
|
|
273
|
+
];
|
|
274
|
+
routeStr1 = JSON.stringify(finalRoute, null, 2);
|
|
275
|
+
regs1 = keywords1.map(createMatchReg);
|
|
276
|
+
newRouteStr1 = regs1.reduce(function(acc, reg) {
|
|
277
|
+
return acc.replace(reg, "$1$2");
|
|
278
|
+
}, routeStr1).replace(/\\"/g, '"');
|
|
279
|
+
routeComponentsCode += "".concat(newRouteStr1, ",");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
} catch (err) {
|
|
283
|
+
_didIteratorError = true;
|
|
284
|
+
_iteratorError = err;
|
|
285
|
+
} finally {
|
|
286
|
+
try {
|
|
287
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
288
|
+
_iterator.return();
|
|
289
|
+
}
|
|
290
|
+
} finally {
|
|
291
|
+
if (_didIteratorError) {
|
|
292
|
+
throw _iteratorError;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
routeComponentsCode += "\n];";
|
|
297
|
+
importLoadingCode = loadings.map(function(loading, index) {
|
|
298
|
+
return "import loading_".concat(index, " from '").concat(loading, "';\n");
|
|
299
|
+
}).join("");
|
|
300
|
+
importComponentsCode = components.map(function(component2, index) {
|
|
301
|
+
return "import component_".concat(index, " from '").concat(component2, "';\n");
|
|
302
|
+
}).join("");
|
|
303
|
+
importErrorComponentsCode = errors.map(function(error, index) {
|
|
304
|
+
return "import error_".concat(index, " from '").concat(error, "';\n");
|
|
305
|
+
}).join("");
|
|
306
|
+
importLoadersCode = "";
|
|
307
|
+
_iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
|
|
308
|
+
try {
|
|
309
|
+
for (_iterator1 = Object.entries(loadersMap)[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
|
|
310
|
+
_step_value = _sliced_to_array(_step1.value, 2), key = _step_value[0], loaderInfo = _step_value[1];
|
|
311
|
+
if (loaderInfo.inline) {
|
|
312
|
+
route1 = loaderInfo.route;
|
|
313
|
+
if (route1.action) {
|
|
314
|
+
importLoadersCode += "import { loader as ".concat(key, ", action as action_").concat(loaderInfo.loaderId, ' } from "').concat(slash(loaderInfo.filePath)).concat(getDataLoaderPath({
|
|
315
|
+
loaderId: key,
|
|
316
|
+
clientData: loaderInfo.clientData,
|
|
317
|
+
action: route1.action,
|
|
318
|
+
inline: loaderInfo.inline,
|
|
319
|
+
routeId: loaderInfo.routeId
|
|
320
|
+
}), '";\n');
|
|
321
|
+
} else {
|
|
322
|
+
importLoadersCode += "import { loader as ".concat(key, ' } from "').concat(slash(loaderInfo.filePath)).concat(getDataLoaderPath({
|
|
323
|
+
loaderId: key,
|
|
324
|
+
clientData: loaderInfo.clientData,
|
|
325
|
+
action: false,
|
|
326
|
+
inline: loaderInfo.inline,
|
|
327
|
+
routeId: route1.id
|
|
328
|
+
}), '";\n');
|
|
329
|
+
}
|
|
330
|
+
} else {
|
|
331
|
+
importLoadersCode += "import ".concat(key, ' from "').concat(slash(loaderInfo.filePath)).concat(getDataLoaderPath({
|
|
332
|
+
loaderId: key,
|
|
333
|
+
clientData: loaderInfo.clientData,
|
|
334
|
+
action: false,
|
|
335
|
+
inline: loaderInfo.inline,
|
|
336
|
+
routeId: loaderInfo.routeId
|
|
337
|
+
}), '";\n');
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
} catch (err) {
|
|
341
|
+
_didIteratorError1 = true;
|
|
342
|
+
_iteratorError1 = err;
|
|
343
|
+
} finally {
|
|
344
|
+
try {
|
|
345
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
346
|
+
_iterator1.return();
|
|
347
|
+
}
|
|
348
|
+
} finally {
|
|
349
|
+
if (_didIteratorError1) {
|
|
350
|
+
throw _iteratorError1;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
importConfigsCode = "";
|
|
355
|
+
_iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = void 0;
|
|
356
|
+
try {
|
|
357
|
+
for (_iterator2 = Object.entries(configsMap)[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
|
358
|
+
_step_value1 = _sliced_to_array(_step2.value, 2), key1 = _step_value1[0], configPath = _step_value1[1];
|
|
359
|
+
importConfigsCode += "import * as ".concat(key1, ' from "').concat(slash(configPath), '";\n');
|
|
360
|
+
}
|
|
361
|
+
} catch (err) {
|
|
362
|
+
_didIteratorError2 = true;
|
|
363
|
+
_iteratorError2 = err;
|
|
364
|
+
} finally {
|
|
365
|
+
try {
|
|
366
|
+
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
|
367
|
+
_iterator2.return();
|
|
368
|
+
}
|
|
369
|
+
} finally {
|
|
370
|
+
if (_didIteratorError2) {
|
|
371
|
+
throw _iteratorError2;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return [
|
|
376
|
+
4,
|
|
377
|
+
fs.ensureFile(loadersMapFile)
|
|
378
|
+
];
|
|
379
|
+
case 1:
|
|
380
|
+
_state.sent();
|
|
381
|
+
return [
|
|
382
|
+
4,
|
|
383
|
+
fs.writeJSON(loadersMapFile, loadersMap)
|
|
384
|
+
];
|
|
385
|
+
case 2:
|
|
386
|
+
_state.sent();
|
|
387
|
+
importRuntimeRouterCode = "\n import { createShouldRevalidate, handleRouteModule, handleRouteModuleError} from '@modern-js/runtime/router';\n ";
|
|
388
|
+
routeModulesCode = "\n if(typeof document !== 'undefined'){\n window.".concat(ROUTE_MODULES, " = {}\n }\n ");
|
|
389
|
+
return [
|
|
390
|
+
2,
|
|
391
|
+
"\n ".concat(importLazyCode, "\n ").concat(importComponentsCode, "\n ").concat(importRuntimeRouterCode, "\n ").concat(rootLayoutCode, "\n ").concat(importLoadingCode, "\n ").concat(importErrorComponentsCode, "\n ").concat(importLoadersCode, "\n ").concat(importConfigsCode, "\n ").concat(routeModulesCode, "\n ").concat(routeComponentsCode, "\n ")
|
|
392
|
+
];
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
return function fileSystemRoutes2(_) {
|
|
397
|
+
return _ref.apply(this, arguments);
|
|
398
|
+
};
|
|
399
|
+
}();
|
|
400
|
+
function ssrLoaderCombinedModule(entrypoints, entrypoint, config, appContext) {
|
|
401
|
+
var entryName = entrypoint.entryName, isMainEntry = entrypoint.isMainEntry;
|
|
402
|
+
var packageName = appContext.packageName, internalDirectory = appContext.internalDirectory;
|
|
403
|
+
var ssr = getEntryOptions(entryName, isMainEntry, config.server.ssr, config.server.ssrByEntries, packageName);
|
|
404
|
+
var ssg = isSSGEntry(config, entryName, entrypoints);
|
|
405
|
+
if (entrypoint.nestedRoutesEntry && (ssr || ssg)) {
|
|
406
|
+
var serverLoaderRuntime = require.resolve("@modern-js/plugin-data-loader/runtime");
|
|
407
|
+
var serverLoadersFile = getServerLoadersFile(internalDirectory, entryName);
|
|
408
|
+
var combinedModule = 'export * from "'.concat(slash(serverLoaderRuntime), '"; export * from "').concat(slash(serverLoadersFile), '"');
|
|
409
|
+
return combinedModule;
|
|
410
|
+
}
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
export {
|
|
414
|
+
fileSystemRoutes,
|
|
415
|
+
routesForServer,
|
|
416
|
+
ssrLoaderCombinedModule
|
|
417
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
3
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { normalizeToPosixPath, fs as fse, JS_EXTENSIONS } from "@modern-js/utils";
|
|
8
|
+
import { transform } from "esbuild";
|
|
9
|
+
import { parse } from "es-module-lexer";
|
|
10
|
+
import { ACTION_EXPORT_NAME, LOADER_EXPORT_NAME } from "../constants";
|
|
11
|
+
var walkDirectory = function(dir) {
|
|
12
|
+
return fs.readdirSync(dir).reduce(function(previous, filename) {
|
|
13
|
+
var filePath = path.join(dir, filename);
|
|
14
|
+
if (fs.statSync(filePath).isDirectory()) {
|
|
15
|
+
return _to_consumable_array(previous).concat(_to_consumable_array(walkDirectory(filePath)));
|
|
16
|
+
} else {
|
|
17
|
+
return _to_consumable_array(previous).concat([
|
|
18
|
+
filePath
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
}, []);
|
|
22
|
+
};
|
|
23
|
+
var isPageComponentFile = function(filePath) {
|
|
24
|
+
if (/\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/.test(filePath)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if ([
|
|
28
|
+
".js",
|
|
29
|
+
".jsx",
|
|
30
|
+
".ts",
|
|
31
|
+
".tsx"
|
|
32
|
+
].includes(path.extname(filePath))) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
};
|
|
37
|
+
var replaceWithAlias = function(base, filePath, alias) {
|
|
38
|
+
if (filePath.includes(base)) {
|
|
39
|
+
return normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
|
|
40
|
+
} else {
|
|
41
|
+
return filePath;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var parseModule = function() {
|
|
45
|
+
var _ref = _async_to_generator(function(param) {
|
|
46
|
+
var source, filename, content, result;
|
|
47
|
+
return _ts_generator(this, function(_state) {
|
|
48
|
+
switch (_state.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
source = param.source, filename = param.filename;
|
|
51
|
+
content = source;
|
|
52
|
+
if (!JS_EXTENSIONS.some(function(ext) {
|
|
53
|
+
return filename.endsWith(ext);
|
|
54
|
+
}))
|
|
55
|
+
return [
|
|
56
|
+
3,
|
|
57
|
+
2
|
|
58
|
+
];
|
|
59
|
+
return [
|
|
60
|
+
4,
|
|
61
|
+
transform(content, {
|
|
62
|
+
loader: path.extname(filename).slice(1),
|
|
63
|
+
format: "esm"
|
|
64
|
+
})
|
|
65
|
+
];
|
|
66
|
+
case 1:
|
|
67
|
+
result = _state.sent();
|
|
68
|
+
content = result.code;
|
|
69
|
+
_state.label = 2;
|
|
70
|
+
case 2:
|
|
71
|
+
return [
|
|
72
|
+
2,
|
|
73
|
+
parse(content)
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
return function parseModule2(_) {
|
|
79
|
+
return _ref.apply(this, arguments);
|
|
80
|
+
};
|
|
81
|
+
}();
|
|
82
|
+
var hasLoader = function() {
|
|
83
|
+
var _ref = _async_to_generator(function(filename, source) {
|
|
84
|
+
var content, _ref2, moduleExports;
|
|
85
|
+
return _ts_generator(this, function(_state) {
|
|
86
|
+
switch (_state.label) {
|
|
87
|
+
case 0:
|
|
88
|
+
content = source;
|
|
89
|
+
if (!!source)
|
|
90
|
+
return [
|
|
91
|
+
3,
|
|
92
|
+
2
|
|
93
|
+
];
|
|
94
|
+
return [
|
|
95
|
+
4,
|
|
96
|
+
fse.readFile(filename, "utf-8")
|
|
97
|
+
];
|
|
98
|
+
case 1:
|
|
99
|
+
content = _state.sent().toString();
|
|
100
|
+
_state.label = 2;
|
|
101
|
+
case 2:
|
|
102
|
+
if (!content)
|
|
103
|
+
return [
|
|
104
|
+
3,
|
|
105
|
+
4
|
|
106
|
+
];
|
|
107
|
+
return [
|
|
108
|
+
4,
|
|
109
|
+
parseModule({
|
|
110
|
+
source: content.toString(),
|
|
111
|
+
filename
|
|
112
|
+
})
|
|
113
|
+
];
|
|
114
|
+
case 3:
|
|
115
|
+
_ref2 = _sliced_to_array.apply(void 0, [
|
|
116
|
+
_state.sent(),
|
|
117
|
+
2
|
|
118
|
+
]), moduleExports = _ref2[1];
|
|
119
|
+
return [
|
|
120
|
+
2,
|
|
121
|
+
moduleExports.some(function(e) {
|
|
122
|
+
return e.n === LOADER_EXPORT_NAME;
|
|
123
|
+
})
|
|
124
|
+
];
|
|
125
|
+
case 4:
|
|
126
|
+
return [
|
|
127
|
+
2,
|
|
128
|
+
false
|
|
129
|
+
];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
return function hasLoader2(filename, source) {
|
|
134
|
+
return _ref.apply(this, arguments);
|
|
135
|
+
};
|
|
136
|
+
}();
|
|
137
|
+
var hasAction = function() {
|
|
138
|
+
var _ref = _async_to_generator(function(filename, source) {
|
|
139
|
+
var content, _ref2, moduleExports;
|
|
140
|
+
return _ts_generator(this, function(_state) {
|
|
141
|
+
switch (_state.label) {
|
|
142
|
+
case 0:
|
|
143
|
+
content = source;
|
|
144
|
+
if (!!source)
|
|
145
|
+
return [
|
|
146
|
+
3,
|
|
147
|
+
2
|
|
148
|
+
];
|
|
149
|
+
return [
|
|
150
|
+
4,
|
|
151
|
+
fse.readFile(filename, "utf-8")
|
|
152
|
+
];
|
|
153
|
+
case 1:
|
|
154
|
+
content = _state.sent().toString();
|
|
155
|
+
_state.label = 2;
|
|
156
|
+
case 2:
|
|
157
|
+
if (!content)
|
|
158
|
+
return [
|
|
159
|
+
3,
|
|
160
|
+
4
|
|
161
|
+
];
|
|
162
|
+
return [
|
|
163
|
+
4,
|
|
164
|
+
parseModule({
|
|
165
|
+
source: content.toString(),
|
|
166
|
+
filename
|
|
167
|
+
})
|
|
168
|
+
];
|
|
169
|
+
case 3:
|
|
170
|
+
_ref2 = _sliced_to_array.apply(void 0, [
|
|
171
|
+
_state.sent(),
|
|
172
|
+
2
|
|
173
|
+
]), moduleExports = _ref2[1];
|
|
174
|
+
return [
|
|
175
|
+
2,
|
|
176
|
+
moduleExports.some(function(e) {
|
|
177
|
+
return e.n === ACTION_EXPORT_NAME;
|
|
178
|
+
})
|
|
179
|
+
];
|
|
180
|
+
case 4:
|
|
181
|
+
return [
|
|
182
|
+
2,
|
|
183
|
+
false
|
|
184
|
+
];
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
return function hasAction2(filename, source) {
|
|
189
|
+
return _ref.apply(this, arguments);
|
|
190
|
+
};
|
|
191
|
+
}();
|
|
192
|
+
var getServerLoadersFile = function(internalDirectory, entryName) {
|
|
193
|
+
return path.join(internalDirectory, entryName, "route-server-loaders.js");
|
|
194
|
+
};
|
|
195
|
+
var getServerCombinedModueFile = function(internalDirectory, entryName) {
|
|
196
|
+
return path.join(internalDirectory, entryName, "server-loader-combined.js");
|
|
197
|
+
};
|
|
198
|
+
var getPathWithoutExt = function(filename) {
|
|
199
|
+
var extname = path.extname(filename);
|
|
200
|
+
return filename.slice(0, -extname.length);
|
|
201
|
+
};
|
|
202
|
+
export {
|
|
203
|
+
getPathWithoutExt,
|
|
204
|
+
getServerCombinedModueFile,
|
|
205
|
+
getServerLoadersFile,
|
|
206
|
+
hasAction,
|
|
207
|
+
hasLoader,
|
|
208
|
+
isPageComponentFile,
|
|
209
|
+
parseModule,
|
|
210
|
+
replaceWithAlias,
|
|
211
|
+
walkDirectory
|
|
212
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var PAGES_DIR_NAME = "pages";
|
|
2
|
+
var NESTED_ROUTES_DIR = "routes";
|
|
3
|
+
var FILE_SYSTEM_ROUTES_FILE_NAME = "routes.js";
|
|
4
|
+
var LOADER_EXPORT_NAME = "loader";
|
|
5
|
+
var ACTION_EXPORT_NAME = "action";
|
|
6
|
+
var TEMP_LOADERS_DIR = "__loaders__";
|
|
7
|
+
var FILE_SYSTEM_ROUTES_LAYOUT = "_layout";
|
|
8
|
+
var FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = "_app";
|
|
9
|
+
var FILE_SYSTEM_ROUTES_INDEX = "index";
|
|
10
|
+
var FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
|
|
11
|
+
var FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
|
|
12
|
+
var FILE_SYSTEM_ROUTES_COMPONENTS_DIR = "internal_components";
|
|
13
|
+
var NESTED_ROUTE = {
|
|
14
|
+
LAYOUT_FILE: "layout",
|
|
15
|
+
LAYOUT_CONFIG_FILE: "layout.config",
|
|
16
|
+
LAYOUT_LOADER_FILE: "layout.loader",
|
|
17
|
+
LAYOUT_DATA_FILE: "layout.data",
|
|
18
|
+
LAYOUT_CLIENT_LOADER: "layout.data.client",
|
|
19
|
+
PAGE_FILE: "page",
|
|
20
|
+
PAGE_CONFIG_FILE: "page.config",
|
|
21
|
+
PAGE_LOADER_FILE: "page.loader",
|
|
22
|
+
PAGE_DATA_FILE: "page.data",
|
|
23
|
+
PAGE_CLIENT_LOADER: "page.data.client",
|
|
24
|
+
SPLATE_FILE: "$",
|
|
25
|
+
SPLATE_CONFIG_FILE: "$.config",
|
|
26
|
+
SPLATE_LOADER_FILE: "$.loader",
|
|
27
|
+
SPLATE_DATA_FILE: "$.data",
|
|
28
|
+
SPLATE_CLIENT_DATA: "$.data.client",
|
|
29
|
+
LOADING_FILE: "loading",
|
|
30
|
+
ERROR_FILE: "error",
|
|
31
|
+
LOADER_FILE: "loader"
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
ACTION_EXPORT_NAME,
|
|
35
|
+
FILE_SYSTEM_ROUTES_COMPONENTS_DIR,
|
|
36
|
+
FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP,
|
|
37
|
+
FILE_SYSTEM_ROUTES_FILE_NAME,
|
|
38
|
+
FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT,
|
|
39
|
+
FILE_SYSTEM_ROUTES_IGNORED_REGEX,
|
|
40
|
+
FILE_SYSTEM_ROUTES_INDEX,
|
|
41
|
+
FILE_SYSTEM_ROUTES_LAYOUT,
|
|
42
|
+
LOADER_EXPORT_NAME,
|
|
43
|
+
NESTED_ROUTE,
|
|
44
|
+
NESTED_ROUTES_DIR,
|
|
45
|
+
PAGES_DIR_NAME,
|
|
46
|
+
TEMP_LOADERS_DIR
|
|
47
|
+
};
|