@modern-js/app-tools 2.53.1-alpha.4 → 2.54.1-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. package/bin/modern.js +0 -2
  2. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +2 -1
  3. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +2 -2
  4. package/dist/cjs/commands/build.js +2 -0
  5. package/dist/cjs/commands/deploy.js +2 -2
  6. package/dist/cjs/commands/dev.js +19 -5
  7. package/dist/cjs/commands/index.js +113 -7
  8. package/dist/cjs/commands/serve.js +19 -5
  9. package/dist/cjs/hooks.js +5 -0
  10. package/dist/cjs/index.js +14 -83
  11. package/dist/cjs/plugins/analyze/constants.js +56 -0
  12. package/dist/cjs/{analyze → plugins/analyze}/generateCode.js +11 -84
  13. package/dist/cjs/{analyze → plugins/analyze}/getBundleEntry.js +11 -20
  14. package/dist/cjs/{analyze → plugins/analyze}/getFileSystemEntry.js +33 -47
  15. package/dist/cjs/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  16. package/dist/cjs/{analyze → plugins/analyze}/index.js +9 -24
  17. package/dist/cjs/plugins/analyze/templates.js +101 -0
  18. package/dist/cjs/{analyze → plugins/analyze}/utils.js +12 -55
  19. package/dist/cjs/plugins/deploy/platforms/netlify.js +2 -2
  20. package/dist/cjs/plugins/deploy/platforms/node.js +2 -2
  21. package/dist/cjs/plugins/deploy/platforms/vercel.js +2 -2
  22. package/dist/cjs/{initialize → plugins/initialize}/index.js +1 -1
  23. package/dist/cjs/plugins/serverBuild.js +10 -4
  24. package/dist/cjs/utils/createServer.js +1 -1
  25. package/dist/cjs/utils/{getServerInternalPlugins.js → loadPlugins.js} +18 -11
  26. package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +4 -1
  27. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +2 -2
  28. package/dist/esm/commands/build.js +16 -9
  29. package/dist/esm/commands/deploy.js +2 -2
  30. package/dist/esm/commands/dev.js +13 -9
  31. package/dist/esm/commands/index.js +411 -3
  32. package/dist/esm/commands/serve.js +12 -8
  33. package/dist/esm/hooks.js +5 -0
  34. package/dist/esm/index.js +12 -394
  35. package/dist/esm/plugins/analyze/constants.js +24 -0
  36. package/dist/esm/{analyze → plugins/analyze}/generateCode.js +27 -169
  37. package/dist/esm/plugins/analyze/getBundleEntry.js +101 -0
  38. package/dist/esm/plugins/analyze/getFileSystemEntry.js +195 -0
  39. package/dist/esm/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  40. package/dist/esm/{analyze → plugins/analyze}/index.js +42 -70
  41. package/dist/esm/plugins/analyze/templates.js +22 -0
  42. package/dist/esm/{analyze → plugins/analyze}/utils.js +13 -135
  43. package/dist/esm/plugins/deploy/platforms/netlify.js +5 -3
  44. package/dist/esm/plugins/deploy/platforms/node.js +5 -3
  45. package/dist/esm/plugins/deploy/platforms/vercel.js +5 -3
  46. package/dist/esm/{initialize → plugins/initialize}/index.js +1 -1
  47. package/dist/esm/plugins/serverBuild.js +13 -6
  48. package/dist/esm/utils/createServer.js +2 -2
  49. package/dist/esm/utils/loadPlugins.js +64 -0
  50. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +2 -1
  51. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +2 -2
  52. package/dist/esm-node/commands/build.js +2 -0
  53. package/dist/esm-node/commands/deploy.js +2 -2
  54. package/dist/esm-node/commands/dev.js +11 -7
  55. package/dist/esm-node/commands/index.js +92 -3
  56. package/dist/esm-node/commands/serve.js +10 -6
  57. package/dist/esm-node/hooks.js +5 -0
  58. package/dist/esm-node/index.js +12 -79
  59. package/dist/esm-node/plugins/analyze/constants.js +24 -0
  60. package/dist/esm-node/{analyze → plugins/analyze}/generateCode.js +14 -87
  61. package/dist/esm-node/{analyze → plugins/analyze}/getBundleEntry.js +11 -20
  62. package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +75 -0
  63. package/dist/esm-node/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  64. package/dist/esm-node/{analyze → plugins/analyze}/index.js +10 -25
  65. package/dist/esm-node/plugins/analyze/templates.js +75 -0
  66. package/dist/esm-node/{analyze → plugins/analyze}/utils.js +13 -52
  67. package/dist/esm-node/plugins/deploy/platforms/netlify.js +3 -3
  68. package/dist/esm-node/plugins/deploy/platforms/node.js +3 -3
  69. package/dist/esm-node/plugins/deploy/platforms/vercel.js +3 -3
  70. package/dist/esm-node/{initialize → plugins/initialize}/index.js +1 -1
  71. package/dist/esm-node/plugins/serverBuild.js +11 -5
  72. package/dist/esm-node/utils/createServer.js +2 -2
  73. package/dist/esm-node/utils/loadPlugins.js +21 -0
  74. package/dist/types/builder/builder-webpack/createCopyPattern.d.ts +2 -2
  75. package/dist/types/commands/index.d.ts +10 -3
  76. package/dist/types/config/initialize/inits.d.ts +1 -1
  77. package/dist/types/exports/server.d.ts +4 -0
  78. package/dist/types/index.d.ts +2 -5
  79. package/dist/types/plugins/analyze/constants.d.ts +9 -0
  80. package/dist/types/{analyze → plugins/analyze}/generateCode.d.ts +1 -1
  81. package/dist/types/plugins/analyze/getBundleEntry.d.ts +4 -0
  82. package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +5 -0
  83. package/dist/types/{analyze → plugins/analyze}/getHtmlTemplate.d.ts +1 -1
  84. package/dist/types/{analyze → plugins/analyze}/getServerRoutes.d.ts +1 -1
  85. package/dist/types/{analyze → plugins/analyze}/index.d.ts +1 -1
  86. package/dist/types/plugins/analyze/templates.d.ts +19 -0
  87. package/dist/types/{analyze → plugins/analyze}/utils.d.ts +4 -6
  88. package/dist/types/{initialize → plugins/initialize}/index.d.ts +1 -1
  89. package/dist/types/types/hooks.d.ts +11 -1
  90. package/dist/types/utils/loadPlugins.d.ts +5 -0
  91. package/package.json +22 -22
  92. package/dist/cjs/analyze/constants.js +0 -122
  93. package/dist/cjs/analyze/getClientRoutes/getRoutes.js +0 -197
  94. package/dist/cjs/analyze/getClientRoutes/getRoutesLegacy.js +0 -195
  95. package/dist/cjs/analyze/getClientRoutes/index.js +0 -31
  96. package/dist/cjs/analyze/getClientRoutes/utils.js +0 -59
  97. package/dist/cjs/analyze/makeLegalIdentifier.js +0 -37
  98. package/dist/cjs/analyze/nestedRoutes.js +0 -295
  99. package/dist/cjs/analyze/templates.js +0 -444
  100. package/dist/esm/analyze/constants.js +0 -76
  101. package/dist/esm/analyze/getBundleEntry.js +0 -75
  102. package/dist/esm/analyze/getClientRoutes/getRoutes.js +0 -185
  103. package/dist/esm/analyze/getClientRoutes/getRoutesLegacy.js +0 -183
  104. package/dist/esm/analyze/getClientRoutes/index.js +0 -6
  105. package/dist/esm/analyze/getClientRoutes/utils.js +0 -28
  106. package/dist/esm/analyze/getFileSystemEntry.js +0 -113
  107. package/dist/esm/analyze/makeLegalIdentifier.js +0 -15
  108. package/dist/esm/analyze/nestedRoutes.js +0 -398
  109. package/dist/esm/analyze/templates.js +0 -435
  110. package/dist/esm/utils/getServerInternalPlugins.js +0 -40
  111. package/dist/esm-node/analyze/constants.js +0 -76
  112. package/dist/esm-node/analyze/getClientRoutes/getRoutes.js +0 -163
  113. package/dist/esm-node/analyze/getClientRoutes/getRoutesLegacy.js +0 -161
  114. package/dist/esm-node/analyze/getClientRoutes/index.js +0 -6
  115. package/dist/esm-node/analyze/getClientRoutes/utils.js +0 -22
  116. package/dist/esm-node/analyze/getFileSystemEntry.js +0 -89
  117. package/dist/esm-node/analyze/makeLegalIdentifier.js +0 -13
  118. package/dist/esm-node/analyze/nestedRoutes.js +0 -259
  119. package/dist/esm-node/analyze/templates.js +0 -405
  120. package/dist/esm-node/utils/getServerInternalPlugins.js +0 -15
  121. package/dist/types/analyze/constants.d.ts +0 -42
  122. package/dist/types/analyze/getBundleEntry.d.ts +0 -3
  123. package/dist/types/analyze/getClientRoutes/getRoutes.d.ts +0 -8
  124. package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +0 -9
  125. package/dist/types/analyze/getClientRoutes/index.d.ts +0 -2
  126. package/dist/types/analyze/getClientRoutes/utils.d.ts +0 -5
  127. package/dist/types/analyze/getFileSystemEntry.d.ts +0 -4
  128. package/dist/types/analyze/makeLegalIdentifier.d.ts +0 -1
  129. package/dist/types/analyze/nestedRoutes.d.ts +0 -7
  130. package/dist/types/analyze/templates.d.ts +0 -30
  131. package/dist/types/utils/getServerInternalPlugins.d.ts +0 -2
  132. /package/dist/cjs/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  133. /package/dist/cjs/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  134. /package/dist/esm/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  135. /package/dist/esm/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  136. /package/dist/esm-node/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  137. /package/dist/esm-node/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  138. /package/dist/types/{analyze → plugins/analyze}/isDefaultExportFunction.d.ts +0 -0
@@ -1,259 +0,0 @@
1
- import * as path from "path";
2
- import { fs, normalizeToPosixPath } from "@modern-js/utils";
3
- import { getPathWithoutExt } from "../utils/routes";
4
- import { JS_EXTENSIONS, NESTED_ROUTE } from "./constants";
5
- import { hasAction, replaceWithAlias } from "./utils";
6
- const conventionNames = Object.values(NESTED_ROUTE);
7
- const replaceDynamicPath = (routePath) => {
8
- return routePath.replace(/\[(.*?)\]/g, ":$1");
9
- };
10
- const getRouteId = (componentPath, routesDir, entryName, isMainEntry) => {
11
- const relativePath = normalizeToPosixPath(path.relative(routesDir, componentPath));
12
- const pathWithoutExt = getPathWithoutExt(relativePath);
13
- let id = ``;
14
- if (isMainEntry) {
15
- id = pathWithoutExt;
16
- } else {
17
- id = `${entryName}_${pathWithoutExt}`;
18
- }
19
- return id.replace(/\[(.*?)\]/g, "($1)");
20
- };
21
- const createIndexRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
22
- return createRoute({
23
- ...routeInfo,
24
- index: true,
25
- children: void 0
26
- }, rootDir, filename, entryName, isMainEntry);
27
- };
28
- const createRoute = (routeInfo, rootDir, filename, entryName, isMainEntry) => {
29
- const id = getRouteId(filename, rootDir, entryName, isMainEntry);
30
- return {
31
- ...routeInfo,
32
- id,
33
- type: "nested"
34
- };
35
- };
36
- const optimizeRoute = (routeTree) => {
37
- if (!routeTree.children || routeTree.children.length === 0) {
38
- return [
39
- routeTree
40
- ];
41
- }
42
- const { children } = routeTree;
43
- if (!routeTree._component && !routeTree.error && !routeTree.loading && !routeTree.config && !routeTree.clientData) {
44
- const newRoutes = children.map((child) => {
45
- const routePath = `${routeTree.path ? routeTree.path : ""}${child.path ? `/${child.path}` : ""}`;
46
- const newRoute = {
47
- ...child,
48
- path: routePath.replace(/\/\//g, "/")
49
- };
50
- if (routePath.length > 0) {
51
- delete newRoute.index;
52
- } else {
53
- delete newRoute.path;
54
- }
55
- return newRoute;
56
- });
57
- return Array.from(new Set(newRoutes)).flatMap(optimizeRoute);
58
- } else {
59
- const optimizedChildren = routeTree.children.flatMap(optimizeRoute);
60
- return [
61
- {
62
- ...routeTree,
63
- children: optimizedChildren
64
- }
65
- ];
66
- }
67
- };
68
- const walk = async (dirname, rootDir, alias, entryName, isMainEntry, oldVersion) => {
69
- var _finalRoute_children;
70
- if (!await fs.pathExists(dirname)) {
71
- return null;
72
- }
73
- const isDirectory = (await fs.stat(dirname)).isDirectory();
74
- if (!isDirectory) {
75
- return null;
76
- }
77
- const relativeDir = path.relative(rootDir, dirname);
78
- const pathSegments = relativeDir.split(path.sep);
79
- const lastSegment = pathSegments[pathSegments.length - 1];
80
- const isRoot = lastSegment === "";
81
- const isPathlessLayout = lastSegment.startsWith("__");
82
- const isWithoutLayoutPath = lastSegment.includes(".");
83
- let routePath = isRoot || isPathlessLayout ? "/" : `${lastSegment}`;
84
- if (isWithoutLayoutPath) {
85
- routePath = lastSegment.split(".").join("/");
86
- }
87
- routePath = replaceDynamicPath(routePath);
88
- const route = {
89
- path: routePath === null || routePath === void 0 ? void 0 : routePath.replace(/\$$/, "?"),
90
- children: [],
91
- isRoot
92
- };
93
- let pageLoaderFile = "";
94
- let pageRoute = null;
95
- let pageConfigFile = "";
96
- let pageClientData = "";
97
- let pageData = "";
98
- let pageAction = "";
99
- let splatLoaderFile = "";
100
- let splatRoute = null;
101
- let splatConfigFile = "";
102
- let splatClientData = "";
103
- let splatData = "";
104
- let splatAction = "";
105
- const items = await fs.readdir(dirname);
106
- for (const item of items) {
107
- const itemPath = path.join(dirname, item);
108
- const itemPathWithAlias = getPathWithoutExt(replaceWithAlias(alias.basename, itemPath, alias.name));
109
- const extname = path.extname(item);
110
- const itemWithoutExt = item.slice(0, -extname.length);
111
- const isDirectory2 = (await fs.stat(itemPath)).isDirectory();
112
- if (isDirectory2) {
113
- const childRoute = await walk(itemPath, rootDir, alias, entryName, isMainEntry, oldVersion);
114
- if (childRoute && !Array.isArray(childRoute)) {
115
- var _route_children;
116
- (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.push(childRoute);
117
- }
118
- }
119
- if (extname && (!JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
120
- continue;
121
- }
122
- if (itemWithoutExt === NESTED_ROUTE.LAYOUT_LOADER_FILE) {
123
- if (!route.loader) {
124
- route.loader = itemPathWithAlias;
125
- }
126
- }
127
- if (itemWithoutExt === NESTED_ROUTE.LAYOUT_CLIENT_LOADER) {
128
- route.clientData = itemPathWithAlias;
129
- }
130
- if (itemWithoutExt === NESTED_ROUTE.LAYOUT_DATA_FILE) {
131
- route.data = itemPathWithAlias;
132
- if (await hasAction(itemPath)) {
133
- route.action = itemPathWithAlias;
134
- }
135
- }
136
- if (itemWithoutExt === NESTED_ROUTE.LAYOUT_CONFIG_FILE) {
137
- if (!route.config) {
138
- route.config = itemPathWithAlias;
139
- }
140
- }
141
- if (itemWithoutExt === NESTED_ROUTE.LAYOUT_FILE) {
142
- route._component = itemPathWithAlias;
143
- }
144
- if (itemWithoutExt === NESTED_ROUTE.PAGE_LOADER_FILE) {
145
- pageLoaderFile = itemPathWithAlias;
146
- }
147
- if (itemWithoutExt === NESTED_ROUTE.PAGE_CLIENT_LOADER) {
148
- pageClientData = itemPathWithAlias;
149
- }
150
- if (itemWithoutExt === NESTED_ROUTE.PAGE_DATA_FILE) {
151
- pageData = itemPathWithAlias;
152
- if (await hasAction(itemPath)) {
153
- pageAction = itemPathWithAlias;
154
- }
155
- }
156
- if (itemWithoutExt === NESTED_ROUTE.PAGE_CONFIG_FILE) {
157
- pageConfigFile = itemPathWithAlias;
158
- }
159
- if (itemWithoutExt === NESTED_ROUTE.PAGE_FILE) {
160
- var _route_children1;
161
- pageRoute = createIndexRoute({
162
- _component: itemPathWithAlias
163
- }, rootDir, itemPath, entryName, isMainEntry);
164
- if (pageLoaderFile) {
165
- pageRoute.loader = pageLoaderFile;
166
- }
167
- if (pageConfigFile) {
168
- pageRoute.config = pageConfigFile;
169
- }
170
- if (pageData) {
171
- pageRoute.data = pageData;
172
- }
173
- if (pageClientData) {
174
- pageRoute.clientData = pageClientData;
175
- }
176
- if (pageAction) {
177
- pageRoute.action = pageAction;
178
- }
179
- (_route_children1 = route.children) === null || _route_children1 === void 0 ? void 0 : _route_children1.push(pageRoute);
180
- }
181
- if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
182
- splatLoaderFile = itemPathWithAlias;
183
- }
184
- if (itemWithoutExt === NESTED_ROUTE.SPLATE_CLIENT_DATA) {
185
- splatClientData = itemPathWithAlias;
186
- }
187
- if (itemWithoutExt === NESTED_ROUTE.SPLATE_CONFIG_FILE) {
188
- if (!route.config) {
189
- splatConfigFile = replaceWithAlias(alias.basename, itemPath, alias.name);
190
- }
191
- }
192
- if (itemWithoutExt === NESTED_ROUTE.SPLATE_DATA_FILE) {
193
- splatData = itemPathWithAlias;
194
- if (await hasAction(itemPath)) {
195
- splatAction = itemPathWithAlias;
196
- }
197
- }
198
- if (itemWithoutExt === NESTED_ROUTE.SPLATE_FILE) {
199
- var _route_children2;
200
- splatRoute = createRoute({
201
- _component: itemPathWithAlias,
202
- path: "*"
203
- }, rootDir, itemPath, entryName, isMainEntry);
204
- if (splatLoaderFile) {
205
- splatRoute.loader = splatLoaderFile;
206
- }
207
- if (splatClientData) {
208
- splatRoute.clientData = splatClientData;
209
- }
210
- if (splatData) {
211
- splatRoute.data = splatData;
212
- }
213
- if (splatConfigFile) {
214
- splatRoute.config = splatConfigFile;
215
- }
216
- if (splatAction) {
217
- splatRoute.action = splatAction;
218
- }
219
- (_route_children2 = route.children) === null || _route_children2 === void 0 ? void 0 : _route_children2.push(splatRoute);
220
- }
221
- if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
222
- route.loading = itemPathWithAlias;
223
- }
224
- if (itemWithoutExt === NESTED_ROUTE.ERROR_FILE) {
225
- route.error = itemPathWithAlias;
226
- }
227
- }
228
- let finalRoute = createRoute(route, rootDir, path.join(dirname, `${NESTED_ROUTE.LAYOUT_FILE}.ts`), entryName, isMainEntry);
229
- if (isPathlessLayout) {
230
- delete finalRoute.path;
231
- }
232
- const childRoutes = finalRoute.children = (_finalRoute_children = finalRoute.children) === null || _finalRoute_children === void 0 ? void 0 : _finalRoute_children.filter((childRoute) => childRoute);
233
- if (childRoutes && childRoutes.length === 0 && !finalRoute.index && !finalRoute._component) {
234
- return null;
235
- }
236
- if (childRoutes && childRoutes.length === 1 && !finalRoute._component) {
237
- const childRoute = childRoutes[0];
238
- if (childRoute.path === "*") {
239
- const path2 = `${finalRoute.path || ""}/${childRoute.path || ""}`;
240
- finalRoute = {
241
- ...childRoute,
242
- path: path2
243
- };
244
- }
245
- }
246
- if (isRoot && !finalRoute._component) {
247
- throw new Error("The root layout component is required, make sure the routes/layout.tsx file exists.");
248
- }
249
- if (isRoot && !oldVersion) {
250
- const optimizedRoutes = optimizeRoute(finalRoute);
251
- return optimizedRoutes;
252
- }
253
- return finalRoute;
254
- };
255
- export {
256
- getRouteId,
257
- optimizeRoute,
258
- walk
259
- };
@@ -1,405 +0,0 @@
1
- import path from "path";
2
- import { fs, getEntryOptions, isSSGEntry, slash } from "@modern-js/utils";
3
- import { ROUTE_MODULES } from "@modern-js/utils/universal/constants";
4
- import { APP_CONFIG_NAME, TEMP_LOADERS_DIR } from "./constants";
5
- import { getServerLoadersFile } from "./utils";
6
- const index = ({ mountId, imports, renderFunction: renderFunction2, exportStatement }) => `
7
- const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
8
- const IS_REACT18 = process.env.IS_REACT18 === 'true';
9
- const MOUNT_ID = '${mountId}';
10
-
11
- ${imports}
12
-
13
- let AppWrapper = null;
14
-
15
- let root = null;
16
-
17
- function render() {
18
- ${renderFunction2}
19
- }
20
-
21
- AppWrapper = render();
22
-
23
- ${exportStatement};
24
- `;
25
- const renderFunction = ({ plugins, customBootstrap, fileSystemRoutes: fileSystemRoutes2 }) => {
26
- const bootstrap = "bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM)";
27
- return `
28
- const finalAppConfig = {
29
- ...App.config,
30
- ...typeof ${APP_CONFIG_NAME} === 'function' ? ${APP_CONFIG_NAME}() : {},
31
- }
32
-
33
- AppWrapper = createApp({
34
- plugins: [
35
- ${plugins.map(({ name, options, args }) => `${name}({...${options}, ...finalAppConfig?.${args || name}}),`).join("\n")}
36
- ]
37
- })(${fileSystemRoutes2 ? "" : `App`})
38
-
39
-
40
- if(!AppWrapper.init && typeof appInit !== 'undefined') {
41
- AppWrapper.init = appInit;
42
- }
43
-
44
-
45
- if (IS_BROWSER) {
46
- ${customBootstrap ? `customBootstrap(AppWrapper, () => ${bootstrap});` : `${bootstrap};`}
47
- }
48
-
49
- return AppWrapper
50
- `;
51
- };
52
- const html = (partials) => `
53
- <!DOCTYPE html>
54
- <html>
55
- <head>
56
-
57
- ${partials.top.join("\n")}
58
-
59
- ${partials.head.join("\n")}
60
-
61
- </head>
62
-
63
- <body>
64
- <div id="<%= mountId %>"><!--<?- html ?>--></div>
65
- ${partials.body.join("\n")}
66
- <!--<?- chunksMap.js ?>-->
67
- <!--<?- SSRDataScript ?>-->
68
- <!--<?- bottomTemplate ?>-->
69
- </body>
70
-
71
- </html>
72
- `;
73
- const routesForServer = ({ routes }) => {
74
- const loaders = [];
75
- const actions = [];
76
- const loadersMap = {};
77
- const traverseRouteTree = (route) => {
78
- let children;
79
- if ("children" in route && route.children) {
80
- var _route_children;
81
- children = route === null || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
82
- }
83
- let loader;
84
- let action;
85
- if (route.type === "nested") {
86
- if (route.loader || route.data) {
87
- loaders.push(route.loader);
88
- const loaderId = loaders.length - 1;
89
- loader = `loader_${loaderId}`;
90
- const inline = Boolean(route.data);
91
- loadersMap[loader] = {
92
- loaderId,
93
- routeId: route.id,
94
- filePath: route.data || route.loader,
95
- clientData: Boolean(route.clientData),
96
- route,
97
- inline
98
- };
99
- if (route.action) {
100
- actions.push(route.action);
101
- action = `action_${loaders.length - 1}`;
102
- }
103
- }
104
- }
105
- const finalRoute = {
106
- ...route,
107
- loader,
108
- action,
109
- children
110
- };
111
- return finalRoute;
112
- };
113
- let routesCode = `
114
- export const routes = [
115
- `;
116
- for (const route of routes) {
117
- if ("type" in route) {
118
- const keywords = [
119
- "loader",
120
- "action"
121
- ];
122
- const regs = keywords.map(createMatchReg);
123
- const newRoute = traverseRouteTree(route);
124
- const routeStr = JSON.stringify(newRoute, null, 2);
125
- routesCode += regs.reduce((acc, reg) => acc.replace(reg, "$1$2"), routeStr).replace(/\\"/g, '"');
126
- } else {
127
- routesCode += `${JSON.stringify(route, null, 2)}`;
128
- }
129
- }
130
- routesCode += `
131
- ];`;
132
- let importLoadersCode = "";
133
- for (const [key, loaderInfo] of Object.entries(loadersMap)) {
134
- if (loaderInfo.inline) {
135
- const { route } = loaderInfo;
136
- if (route.action) {
137
- importLoadersCode += `import { loader as ${key}, action as action_${loaderInfo.loaderId} } from "${slash(loaderInfo.filePath)}";
138
- `;
139
- } else {
140
- importLoadersCode += `import { loader as ${key} } from "${slash(loaderInfo.filePath)}";
141
- `;
142
- }
143
- } else {
144
- importLoadersCode += `import ${key} from "${slash(loaderInfo.filePath)}";
145
- `;
146
- }
147
- }
148
- return `
149
- ${importLoadersCode}
150
- ${routesCode}
151
- `;
152
- };
153
- const createMatchReg = (keyword) => new RegExp(`("${keyword}":\\s)"([^
154
- ]+)"`, "g");
155
- const fileSystemRoutes = async ({ routes, ssrMode, nestedRoutesEntry, entryName, internalDirectory, splitRouteChunks = true }) => {
156
- const components = [];
157
- const loadings = [];
158
- const errors = [];
159
- const loaders = [];
160
- const loadersMap = {};
161
- const configs = [];
162
- const configsMap = {};
163
- const loadersMapFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, "map.json");
164
- const importLazyCode = `
165
- import { lazy } from "react";
166
- import loadable, { lazy as loadableLazy } from "@modern-js/runtime/loadable"
167
- `;
168
- let rootLayoutCode = ``;
169
- const getDataLoaderPath = ({ loaderId, clientData, action, inline, routeId }) => {
170
- if (!ssrMode) {
171
- return "";
172
- }
173
- const clientDataStr = clientData ? `&clientData=${clientData}` : "";
174
- if (nestedRoutesEntry) {
175
- return `?loaderId=${loaderId}${clientDataStr}&action=${action ? slash(action) : action}&inline=${inline}&routeId=${routeId}`;
176
- }
177
- return "";
178
- };
179
- const traverseRouteTree = (route) => {
180
- let children;
181
- if ("children" in route && route.children) {
182
- var _route_children;
183
- children = route === null || route === void 0 ? void 0 : (_route_children = route.children) === null || _route_children === void 0 ? void 0 : _route_children.map(traverseRouteTree);
184
- }
185
- let loading;
186
- let error;
187
- let loader;
188
- let action;
189
- let config;
190
- let component = "";
191
- let lazyImport = null;
192
- if (route.type === "nested") {
193
- if (route.loading) {
194
- loadings.push(route.loading);
195
- loading = `loading_${loadings.length - 1}`;
196
- }
197
- if (route.error) {
198
- errors.push(route.error);
199
- error = `error_${errors.length - 1}`;
200
- }
201
- if (route.loader || route.data) {
202
- loaders.push(route.loader);
203
- const loaderId = loaders.length - 1;
204
- loader = `loader_${loaderId}`;
205
- const inline = Boolean(route.data);
206
- loadersMap[loader] = {
207
- loaderId,
208
- routeId: route.id,
209
- filePath: route.data || route.loader,
210
- clientData: Boolean(route.clientData),
211
- route,
212
- inline
213
- };
214
- loader = `loader_${loaderId}`;
215
- if (route.action) {
216
- action = `action_${loaderId}`;
217
- }
218
- }
219
- if (typeof route.config === "string") {
220
- configs.push(route.config);
221
- const configId = configs.length - 1;
222
- config = `config_${configId}`;
223
- configsMap[config] = route.config;
224
- }
225
- if (route._component) {
226
- if (splitRouteChunks) {
227
- if (route.isRoot) {
228
- lazyImport = `() => import('${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
229
- rootLayoutCode = `import RootLayout from '${route._component}'`;
230
- component = `RootLayout`;
231
- } else if (ssrMode === "string") {
232
- lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
233
- component = `loadable(${lazyImport})`;
234
- } else {
235
- lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
236
- component = `lazy(${lazyImport})`;
237
- }
238
- } else if (ssrMode === "string") {
239
- components.push(route._component);
240
- component = `component_${components.length - 1}`;
241
- } else {
242
- lazyImport = `() => import(/* webpackMode: "eager" */ '${route._component}').then(routeModule => handleRouteModule(routeModule, "${route.id}")).catch(handleRouteModuleError) `;
243
- component = `lazy(${lazyImport})`;
244
- }
245
- }
246
- } else if (route._component) {
247
- if (splitRouteChunks) {
248
- lazyImport = `() => import('${route._component}')`;
249
- component = `loadable(${lazyImport})`;
250
- } else {
251
- components.push(route._component);
252
- component = `component_${components.length - 1}`;
253
- }
254
- }
255
- const finalRoute = {
256
- ...route,
257
- lazyImport,
258
- loading,
259
- loader,
260
- action,
261
- config,
262
- error,
263
- children
264
- };
265
- if (route._component) {
266
- finalRoute.component = component;
267
- }
268
- if (route.type === "nested" && route._component && (route.loader || route.data)) {
269
- finalRoute.shouldRevalidate = `createShouldRevalidate("${route.id}")`;
270
- }
271
- return finalRoute;
272
- };
273
- let routeComponentsCode = `
274
- export const routes = [
275
- `;
276
- for (const route of routes) {
277
- if ("type" in route) {
278
- const newRoute = traverseRouteTree(route);
279
- const routeStr = JSON.stringify(newRoute, null, 2);
280
- const keywords = [
281
- "component",
282
- "lazyImport",
283
- "shouldRevalidate",
284
- "loader",
285
- "action",
286
- "loading",
287
- "error",
288
- "config"
289
- ];
290
- const regs = keywords.map(createMatchReg);
291
- const newRouteStr = regs.reduce((acc, reg) => acc.replace(reg, "$1$2"), routeStr).replace(/"(RootLayout)"/g, "$1").replace(/\\"/g, '"');
292
- routeComponentsCode += `${newRouteStr},`;
293
- } else {
294
- const component = `loadable(() => import('${route._component}'))`;
295
- const finalRoute = {
296
- ...route,
297
- component
298
- };
299
- const keywords = [
300
- "component",
301
- "lazyImport"
302
- ];
303
- const routeStr = JSON.stringify(finalRoute, null, 2);
304
- const regs = keywords.map(createMatchReg);
305
- const newRouteStr = regs.reduce((acc, reg) => acc.replace(reg, "$1$2"), routeStr).replace(/\\"/g, '"');
306
- routeComponentsCode += `${newRouteStr},`;
307
- }
308
- }
309
- routeComponentsCode += `
310
- ];`;
311
- const importLoadingCode = loadings.map((loading, index2) => {
312
- return `import loading_${index2} from '${loading}';
313
- `;
314
- }).join("");
315
- const importComponentsCode = components.map((component, index2) => {
316
- return `import component_${index2} from '${component}';
317
- `;
318
- }).join("");
319
- const importErrorComponentsCode = errors.map((error, index2) => {
320
- return `import error_${index2} from '${error}';
321
- `;
322
- }).join("");
323
- let importLoadersCode = "";
324
- for (const [key, loaderInfo] of Object.entries(loadersMap)) {
325
- if (loaderInfo.inline) {
326
- const { route } = loaderInfo;
327
- if (route.action) {
328
- importLoadersCode += `import { loader as ${key}, action as action_${loaderInfo.loaderId} } from "${slash(loaderInfo.filePath)}${getDataLoaderPath({
329
- loaderId: key,
330
- clientData: loaderInfo.clientData,
331
- action: route.action,
332
- inline: loaderInfo.inline,
333
- routeId: loaderInfo.routeId
334
- })}";
335
- `;
336
- } else {
337
- importLoadersCode += `import { loader as ${key} } from "${slash(loaderInfo.filePath)}${getDataLoaderPath({
338
- loaderId: key,
339
- clientData: loaderInfo.clientData,
340
- action: false,
341
- inline: loaderInfo.inline,
342
- routeId: route.id
343
- })}";
344
- `;
345
- }
346
- } else {
347
- importLoadersCode += `import ${key} from "${slash(loaderInfo.filePath)}${getDataLoaderPath({
348
- loaderId: key,
349
- clientData: loaderInfo.clientData,
350
- action: false,
351
- inline: loaderInfo.inline,
352
- routeId: loaderInfo.routeId
353
- })}";
354
- `;
355
- }
356
- }
357
- let importConfigsCode = "";
358
- for (const [key, configPath] of Object.entries(configsMap)) {
359
- importConfigsCode += `import * as ${key} from "${slash(configPath)}";
360
- `;
361
- }
362
- await fs.ensureFile(loadersMapFile);
363
- await fs.writeJSON(loadersMapFile, loadersMap);
364
- const importRuntimeRouterCode = `
365
- import { createShouldRevalidate, handleRouteModule, handleRouteModuleError} from '@modern-js/runtime/router';
366
- `;
367
- const routeModulesCode = `
368
- if(typeof document !== 'undefined'){
369
- window.${ROUTE_MODULES} = {}
370
- }
371
- `;
372
- return `
373
- ${importLazyCode}
374
- ${importComponentsCode}
375
- ${importRuntimeRouterCode}
376
- ${rootLayoutCode}
377
- ${importLoadingCode}
378
- ${importErrorComponentsCode}
379
- ${importLoadersCode}
380
- ${importConfigsCode}
381
- ${routeModulesCode}
382
- ${routeComponentsCode}
383
- `;
384
- };
385
- function ssrLoaderCombinedModule(entrypoints, entrypoint, config, appContext) {
386
- const { entryName, isMainEntry } = entrypoint;
387
- const { packageName, internalDirectory } = appContext;
388
- const ssr = getEntryOptions(entryName, isMainEntry, config.server.ssr, config.server.ssrByEntries, packageName);
389
- const ssg = isSSGEntry(config, entryName, entrypoints);
390
- if (entrypoint.nestedRoutesEntry && (ssr || ssg)) {
391
- const serverLoaderRuntime = require.resolve("@modern-js/plugin-data-loader/runtime");
392
- const serverLoadersFile = getServerLoadersFile(internalDirectory, entryName);
393
- const combinedModule = `export * from "${slash(serverLoaderRuntime)}"; export * from "${slash(serverLoadersFile)}"`;
394
- return combinedModule;
395
- }
396
- return null;
397
- }
398
- export {
399
- fileSystemRoutes,
400
- html,
401
- index,
402
- renderFunction,
403
- routesForServer,
404
- ssrLoaderCombinedModule
405
- };
@@ -1,15 +0,0 @@
1
- async function getServerInternalPlugins(api) {
2
- const hookRunners = api.useHookRunners();
3
- const { plugins: serverPlugins } = await hookRunners.collectServerPlugins({
4
- plugins: []
5
- });
6
- const serverInternalPlugins = serverPlugins.reduce((result, plugin) => Object.assign(result, plugin), {});
7
- api.setAppContext({
8
- ...api.useAppContext(),
9
- serverInternalPlugins
10
- });
11
- return serverInternalPlugins;
12
- }
13
- export {
14
- getServerInternalPlugins
15
- };