@modern-js/app-tools 2.53.1-alpha.3 → 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.
Files changed (214) 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/dependencies/index.js +16 -25
  20. package/dist/cjs/plugins/deploy/dependencies/utils.js +1 -2
  21. package/dist/cjs/plugins/deploy/platforms/netlify.js +5 -9
  22. package/dist/cjs/plugins/deploy/platforms/node.js +5 -13
  23. package/dist/cjs/plugins/deploy/platforms/vercel.js +5 -9
  24. package/dist/cjs/{initialize → plugins/initialize}/index.js +1 -1
  25. package/dist/cjs/plugins/serverBuild.js +10 -4
  26. package/dist/cjs/utils/createServer.js +1 -1
  27. package/dist/cjs/utils/{getServerInternalPlugins.js → loadPlugins.js} +18 -11
  28. package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +4 -1
  29. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +2 -2
  30. package/dist/esm/commands/build.js +16 -9
  31. package/dist/esm/commands/deploy.js +2 -2
  32. package/dist/esm/commands/dev.js +13 -9
  33. package/dist/esm/commands/index.js +411 -3
  34. package/dist/esm/commands/serve.js +12 -8
  35. package/dist/esm/hooks.js +5 -0
  36. package/dist/esm/index.js +12 -394
  37. package/dist/esm/plugins/analyze/constants.js +24 -0
  38. package/dist/esm/{analyze → plugins/analyze}/generateCode.js +27 -169
  39. package/dist/esm/plugins/analyze/getBundleEntry.js +101 -0
  40. package/dist/esm/plugins/analyze/getFileSystemEntry.js +195 -0
  41. package/dist/esm/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  42. package/dist/esm/{analyze → plugins/analyze}/index.js +42 -70
  43. package/dist/esm/plugins/analyze/templates.js +22 -0
  44. package/dist/esm/{analyze → plugins/analyze}/utils.js +13 -135
  45. package/dist/esm/plugins/deploy/dependencies/index.js +69 -94
  46. package/dist/esm/plugins/deploy/dependencies/utils.js +3 -4
  47. package/dist/esm/plugins/deploy/platforms/netlify.js +8 -10
  48. package/dist/esm/plugins/deploy/platforms/node.js +8 -14
  49. package/dist/esm/plugins/deploy/platforms/vercel.js +8 -10
  50. package/dist/esm/{initialize → plugins/initialize}/index.js +1 -1
  51. package/dist/esm/plugins/serverBuild.js +13 -6
  52. package/dist/esm/utils/createServer.js +2 -2
  53. package/dist/esm/utils/loadPlugins.js +64 -0
  54. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +2 -1
  55. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +2 -2
  56. package/dist/esm-node/commands/build.js +2 -0
  57. package/dist/esm-node/commands/deploy.js +2 -2
  58. package/dist/esm-node/commands/dev.js +11 -7
  59. package/dist/esm-node/commands/index.js +92 -3
  60. package/dist/esm-node/commands/serve.js +10 -6
  61. package/dist/esm-node/hooks.js +5 -0
  62. package/dist/esm-node/index.js +12 -79
  63. package/dist/esm-node/plugins/analyze/constants.js +24 -0
  64. package/dist/esm-node/{analyze → plugins/analyze}/generateCode.js +14 -87
  65. package/dist/esm-node/{analyze → plugins/analyze}/getBundleEntry.js +11 -20
  66. package/dist/esm-node/plugins/analyze/getFileSystemEntry.js +75 -0
  67. package/dist/esm-node/{analyze → plugins/analyze}/getServerRoutes.js +1 -1
  68. package/dist/esm-node/{analyze → plugins/analyze}/index.js +10 -25
  69. package/dist/esm-node/plugins/analyze/templates.js +75 -0
  70. package/dist/esm-node/{analyze → plugins/analyze}/utils.js +13 -52
  71. package/dist/esm-node/plugins/deploy/dependencies/index.js +18 -27
  72. package/dist/esm-node/plugins/deploy/dependencies/utils.js +1 -2
  73. package/dist/esm-node/plugins/deploy/platforms/netlify.js +6 -10
  74. package/dist/esm-node/plugins/deploy/platforms/node.js +6 -14
  75. package/dist/esm-node/plugins/deploy/platforms/vercel.js +6 -10
  76. package/dist/esm-node/{initialize → plugins/initialize}/index.js +1 -1
  77. package/dist/esm-node/plugins/serverBuild.js +11 -5
  78. package/dist/esm-node/utils/createServer.js +2 -2
  79. package/dist/esm-node/utils/loadPlugins.js +21 -0
  80. package/dist/types/builder/builder-webpack/createCopyPattern.d.ts +2 -2
  81. package/dist/types/commands/index.d.ts +10 -3
  82. package/dist/types/config/initialize/inits.d.ts +1 -1
  83. package/dist/types/exports/server.d.ts +4 -0
  84. package/dist/types/index.d.ts +2 -5
  85. package/dist/types/plugins/analyze/constants.d.ts +9 -0
  86. package/dist/types/{analyze → plugins/analyze}/generateCode.d.ts +1 -1
  87. package/dist/types/plugins/analyze/getBundleEntry.d.ts +4 -0
  88. package/dist/types/plugins/analyze/getFileSystemEntry.d.ts +5 -0
  89. package/dist/types/{analyze → plugins/analyze}/getHtmlTemplate.d.ts +1 -1
  90. package/dist/types/{analyze → plugins/analyze}/getServerRoutes.d.ts +1 -1
  91. package/dist/types/{analyze → plugins/analyze}/index.d.ts +1 -1
  92. package/dist/types/plugins/analyze/templates.d.ts +19 -0
  93. package/dist/types/{analyze → plugins/analyze}/utils.d.ts +4 -6
  94. package/dist/types/plugins/deploy/dependencies/index.d.ts +1 -9
  95. package/dist/types/plugins/deploy/dependencies/utils.d.ts +1 -7
  96. package/dist/types/{initialize → plugins/initialize}/index.d.ts +1 -1
  97. package/dist/types/types/hooks.d.ts +11 -1
  98. package/dist/types/utils/loadPlugins.d.ts +5 -0
  99. package/package.json +22 -30
  100. package/dist/cjs/analyze/constants.js +0 -122
  101. package/dist/cjs/analyze/getClientRoutes/getRoutes.js +0 -197
  102. package/dist/cjs/analyze/getClientRoutes/getRoutesLegacy.js +0 -195
  103. package/dist/cjs/analyze/getClientRoutes/index.js +0 -31
  104. package/dist/cjs/analyze/getClientRoutes/utils.js +0 -59
  105. package/dist/cjs/analyze/makeLegalIdentifier.js +0 -37
  106. package/dist/cjs/analyze/nestedRoutes.js +0 -295
  107. package/dist/cjs/analyze/templates.js +0 -444
  108. package/dist/cjs/plugins/deploy/exports.js +0 -28
  109. package/dist/esm/analyze/constants.js +0 -76
  110. package/dist/esm/analyze/getBundleEntry.js +0 -75
  111. package/dist/esm/analyze/getClientRoutes/getRoutes.js +0 -185
  112. package/dist/esm/analyze/getClientRoutes/getRoutesLegacy.js +0 -183
  113. package/dist/esm/analyze/getClientRoutes/index.js +0 -6
  114. package/dist/esm/analyze/getClientRoutes/utils.js +0 -28
  115. package/dist/esm/analyze/getFileSystemEntry.js +0 -113
  116. package/dist/esm/analyze/makeLegalIdentifier.js +0 -15
  117. package/dist/esm/analyze/nestedRoutes.js +0 -398
  118. package/dist/esm/analyze/templates.js +0 -435
  119. package/dist/esm/plugins/deploy/exports.js +0 -4
  120. package/dist/esm/utils/getServerInternalPlugins.js +0 -40
  121. package/dist/esm-node/analyze/constants.js +0 -76
  122. package/dist/esm-node/analyze/getClientRoutes/getRoutes.js +0 -163
  123. package/dist/esm-node/analyze/getClientRoutes/getRoutesLegacy.js +0 -161
  124. package/dist/esm-node/analyze/getClientRoutes/index.js +0 -6
  125. package/dist/esm-node/analyze/getClientRoutes/utils.js +0 -22
  126. package/dist/esm-node/analyze/getFileSystemEntry.js +0 -89
  127. package/dist/esm-node/analyze/makeLegalIdentifier.js +0 -13
  128. package/dist/esm-node/analyze/nestedRoutes.js +0 -259
  129. package/dist/esm-node/analyze/templates.js +0 -405
  130. package/dist/esm-node/plugins/deploy/exports.js +0 -4
  131. package/dist/esm-node/utils/getServerInternalPlugins.js +0 -15
  132. package/dist/js/modern/analyze/constants.js +0 -15
  133. package/dist/js/modern/analyze/generateCode.js +0 -179
  134. package/dist/js/modern/analyze/getBundleEntry.js +0 -75
  135. package/dist/js/modern/analyze/getClientRoutes.js +0 -219
  136. package/dist/js/modern/analyze/getFileSystemEntry.js +0 -74
  137. package/dist/js/modern/analyze/getHtmlTemplate.js +0 -82
  138. package/dist/js/modern/analyze/getServerRoutes.js +0 -192
  139. package/dist/js/modern/analyze/index.js +0 -148
  140. package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -32
  141. package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -16
  142. package/dist/js/modern/analyze/templates.js +0 -88
  143. package/dist/js/modern/analyze/utils.js +0 -92
  144. package/dist/js/modern/commands/build.js +0 -154
  145. package/dist/js/modern/commands/deploy.js +0 -5
  146. package/dist/js/modern/commands/dev.js +0 -95
  147. package/dist/js/modern/commands/index.js +0 -3
  148. package/dist/js/modern/commands/inspect.js +0 -69
  149. package/dist/js/modern/commands/start.js +0 -31
  150. package/dist/js/modern/exports/server.js +0 -1
  151. package/dist/js/modern/hooks.js +0 -21
  152. package/dist/js/modern/index.js +0 -109
  153. package/dist/js/modern/locale/en.js +0 -35
  154. package/dist/js/modern/locale/index.js +0 -9
  155. package/dist/js/modern/locale/zh.js +0 -35
  156. package/dist/js/modern/utils/config.js +0 -78
  157. package/dist/js/modern/utils/createCompiler.js +0 -61
  158. package/dist/js/modern/utils/createServer.js +0 -18
  159. package/dist/js/modern/utils/getSpecifiedEntries.js +0 -36
  160. package/dist/js/modern/utils/language.js +0 -5
  161. package/dist/js/modern/utils/printInstructions.js +0 -11
  162. package/dist/js/modern/utils/routes.js +0 -15
  163. package/dist/js/modern/utils/types.js +0 -0
  164. package/dist/js/node/analyze/constants.js +0 -36
  165. package/dist/js/node/analyze/generateCode.js +0 -208
  166. package/dist/js/node/analyze/getBundleEntry.js +0 -89
  167. package/dist/js/node/analyze/getClientRoutes.js +0 -241
  168. package/dist/js/node/analyze/getFileSystemEntry.js +0 -90
  169. package/dist/js/node/analyze/getHtmlTemplate.js +0 -106
  170. package/dist/js/node/analyze/getServerRoutes.js +0 -208
  171. package/dist/js/node/analyze/index.js +0 -178
  172. package/dist/js/node/analyze/isDefaultExportFunction.js +0 -50
  173. package/dist/js/node/analyze/makeLegalIdentifier.js +0 -24
  174. package/dist/js/node/analyze/templates.js +0 -106
  175. package/dist/js/node/analyze/utils.js +0 -113
  176. package/dist/js/node/commands/build.js +0 -174
  177. package/dist/js/node/commands/deploy.js +0 -14
  178. package/dist/js/node/commands/dev.js +0 -120
  179. package/dist/js/node/commands/index.js +0 -44
  180. package/dist/js/node/commands/inspect.js +0 -98
  181. package/dist/js/node/commands/start.js +0 -47
  182. package/dist/js/node/exports/server.js +0 -13
  183. package/dist/js/node/hooks.js +0 -39
  184. package/dist/js/node/index.js +0 -141
  185. package/dist/js/node/locale/en.js +0 -42
  186. package/dist/js/node/locale/index.js +0 -20
  187. package/dist/js/node/locale/zh.js +0 -42
  188. package/dist/js/node/utils/config.js +0 -103
  189. package/dist/js/node/utils/createCompiler.js +0 -81
  190. package/dist/js/node/utils/createServer.js +0 -35
  191. package/dist/js/node/utils/getSpecifiedEntries.js +0 -46
  192. package/dist/js/node/utils/language.js +0 -13
  193. package/dist/js/node/utils/printInstructions.js +0 -22
  194. package/dist/js/node/utils/routes.js +0 -25
  195. package/dist/js/node/utils/types.js +0 -0
  196. package/dist/types/analyze/constants.d.ts +0 -42
  197. package/dist/types/analyze/getBundleEntry.d.ts +0 -3
  198. package/dist/types/analyze/getClientRoutes/getRoutes.d.ts +0 -8
  199. package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +0 -9
  200. package/dist/types/analyze/getClientRoutes/index.d.ts +0 -2
  201. package/dist/types/analyze/getClientRoutes/utils.d.ts +0 -5
  202. package/dist/types/analyze/getFileSystemEntry.d.ts +0 -4
  203. package/dist/types/analyze/makeLegalIdentifier.d.ts +0 -1
  204. package/dist/types/analyze/nestedRoutes.d.ts +0 -7
  205. package/dist/types/analyze/templates.d.ts +0 -30
  206. package/dist/types/plugins/deploy/exports.d.ts +0 -1
  207. package/dist/types/utils/getServerInternalPlugins.d.ts +0 -2
  208. /package/dist/cjs/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  209. /package/dist/cjs/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  210. /package/dist/esm/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  211. /package/dist/esm/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  212. /package/dist/esm-node/{analyze → plugins/analyze}/getHtmlTemplate.js +0 -0
  213. /package/dist/esm-node/{analyze → plugins/analyze}/isDefaultExportFunction.js +0 -0
  214. /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,4 +0,0 @@
1
- import { handleDependencies } from "./dependencies";
2
- export {
3
- handleDependencies
4
- };
@@ -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
- };
@@ -1,15 +0,0 @@
1
- export const JS_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx'];
2
- export const INDEX_FILE_NAME = 'index';
3
- export const APP_FILE_NAME = 'App';
4
- export const PAGES_DIR_NAME = 'pages';
5
- export const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
6
- export const ENTRY_POINT_FILE_NAME = 'index.js';
7
- export const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
8
- export const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
9
- export const FILE_SYSTEM_ROUTES_LAYOUT = '_layout';
10
- export const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = '_app';
11
- export const FILE_SYSTEM_ROUTES_INDEX = 'index';
12
- export const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
13
- export const HTML_PARTIALS_FOLDER = 'html';
14
- export const HTML_PARTIALS_EXTENSIONS = ['.htm', '.html', '.ejs'];
15
- export const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = 'internal_components';