@modern-js/app-tools 2.4.0 → 2.5.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (276) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/dist/cjs/analyze/constants.js +100 -0
  3. package/dist/cjs/analyze/generateCode.js +251 -0
  4. package/dist/cjs/analyze/getBundleEntry.js +101 -0
  5. package/dist/cjs/analyze/getClientRoutes/getRoutes.js +225 -0
  6. package/dist/cjs/analyze/getClientRoutes/getRoutesLegacy.js +221 -0
  7. package/dist/cjs/analyze/getClientRoutes/index.js +30 -0
  8. package/dist/cjs/analyze/getClientRoutes/utils.js +58 -0
  9. package/dist/cjs/analyze/getFileSystemEntry.js +131 -0
  10. package/dist/cjs/analyze/getHtmlTemplate.js +124 -0
  11. package/dist/cjs/analyze/getServerRoutes.js +174 -0
  12. package/dist/cjs/analyze/index.js +294 -0
  13. package/dist/cjs/analyze/isDefaultExportFunction.js +71 -0
  14. package/dist/cjs/analyze/makeLegalIdentifier.js +36 -0
  15. package/dist/cjs/analyze/nestedRoutes.js +179 -0
  16. package/dist/cjs/analyze/templates.js +330 -0
  17. package/dist/cjs/analyze/utils.js +152 -0
  18. package/dist/cjs/builder/builder-rspack/index.js +31 -0
  19. package/dist/cjs/builder/builder-webpack/builderPlugins/compatModern.js +64 -0
  20. package/dist/cjs/builder/builder-webpack/index.js +78 -0
  21. package/dist/cjs/builder/builder-webpack/webpackPlugins/RouterPlugin.js +120 -0
  22. package/dist/cjs/builder/builder-webpack/webpackPlugins/index.js +17 -0
  23. package/dist/cjs/builder/generator/createBuilderOptions.js +47 -0
  24. package/dist/cjs/builder/generator/createBuilderProviderConfig.js +49 -0
  25. package/dist/cjs/builder/generator/getBuilderTargets.js +39 -0
  26. package/dist/cjs/builder/generator/index.js +58 -0
  27. package/dist/cjs/builder/index.js +46 -0
  28. package/dist/cjs/builder/shared/builderPlugins/adapterModern.js +230 -0
  29. package/dist/cjs/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +50 -0
  30. package/dist/cjs/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +57 -0
  31. package/dist/cjs/builder/shared/createCopyPattern.js +75 -0
  32. package/dist/cjs/builder/shared/index.js +19 -0
  33. package/dist/cjs/builder/shared/loaders/serverModuleLoader.js +28 -0
  34. package/dist/cjs/builder/shared/types.js +15 -0
  35. package/dist/cjs/commands/build.js +75 -0
  36. package/dist/cjs/commands/deploy.js +31 -0
  37. package/dist/cjs/commands/dev.js +91 -0
  38. package/dist/cjs/commands/index.js +19 -0
  39. package/dist/cjs/commands/inspect.js +41 -0
  40. package/dist/cjs/commands/serve.js +63 -0
  41. package/dist/cjs/config/default.js +186 -0
  42. package/dist/cjs/config/index.js +19 -0
  43. package/dist/cjs/config/initialize/index.js +35 -0
  44. package/dist/cjs/config/initialize/inits.js +161 -0
  45. package/dist/cjs/config/legacy/createHtmlConfig.js +58 -0
  46. package/dist/cjs/config/legacy/createOutputConfig.js +91 -0
  47. package/dist/cjs/config/legacy/createSourceConfig.js +62 -0
  48. package/dist/cjs/config/legacy/createToolsConfig.js +65 -0
  49. package/dist/cjs/config/legacy/index.js +75 -0
  50. package/dist/cjs/defineConfig.js +33 -0
  51. package/dist/cjs/exports/server.js +27 -0
  52. package/dist/cjs/hooks.js +54 -0
  53. package/dist/cjs/index.js +198 -0
  54. package/dist/cjs/initialize/index.js +102 -0
  55. package/dist/cjs/locale/en.js +59 -0
  56. package/dist/cjs/locale/index.js +33 -0
  57. package/dist/cjs/locale/zh.js +59 -0
  58. package/dist/cjs/schema/Schema.js +63 -0
  59. package/dist/cjs/schema/index.js +118 -0
  60. package/dist/cjs/schema/legacy.js +169 -0
  61. package/dist/cjs/types/config/deploy.js +15 -0
  62. package/dist/cjs/types/config/dev.js +15 -0
  63. package/dist/cjs/types/config/experiments.js +15 -0
  64. package/dist/cjs/types/config/html.js +15 -0
  65. package/dist/cjs/types/config/index.js +17 -0
  66. package/dist/cjs/types/config/output.js +15 -0
  67. package/dist/cjs/types/config/performance.js +15 -0
  68. package/dist/cjs/types/config/security.js +15 -0
  69. package/dist/cjs/types/config/source.js +15 -0
  70. package/dist/cjs/types/config/tools.js +15 -0
  71. package/dist/cjs/types/hooks.js +15 -0
  72. package/dist/cjs/types/index.js +19 -0
  73. package/dist/cjs/types/legacyConfig/deploy.js +15 -0
  74. package/dist/cjs/types/legacyConfig/dev.js +15 -0
  75. package/dist/cjs/types/legacyConfig/index.js +15 -0
  76. package/dist/cjs/types/legacyConfig/output.js +15 -0
  77. package/dist/cjs/types/legacyConfig/source.js +15 -0
  78. package/dist/cjs/types/legacyConfig/tools.js +15 -0
  79. package/dist/cjs/types/utils.js +15 -0
  80. package/dist/cjs/utils/config.js +115 -0
  81. package/dist/cjs/utils/createServer.js +70 -0
  82. package/dist/cjs/utils/env.js +38 -0
  83. package/dist/cjs/utils/generateWatchFiles.js +63 -0
  84. package/dist/cjs/utils/getSelectedEntries.js +61 -0
  85. package/dist/cjs/utils/getServerInternalPlugins.js +41 -0
  86. package/dist/cjs/utils/language.js +31 -0
  87. package/dist/cjs/utils/printInstructions.js +34 -0
  88. package/dist/cjs/utils/restart.js +45 -0
  89. package/dist/cjs/utils/routes.js +39 -0
  90. package/dist/cjs/utils/types.js +15 -0
  91. package/dist/esm/analyze/constants.js +42 -0
  92. package/dist/esm/analyze/generateCode.js +486 -0
  93. package/dist/esm/analyze/getBundleEntry.js +64 -0
  94. package/dist/esm/analyze/getClientRoutes/getRoutes.js +233 -0
  95. package/dist/esm/analyze/getClientRoutes/getRoutesLegacy.js +231 -0
  96. package/dist/esm/analyze/getClientRoutes/index.js +3 -0
  97. package/dist/esm/analyze/getClientRoutes/utils.js +23 -0
  98. package/dist/esm/analyze/getFileSystemEntry.js +107 -0
  99. package/dist/esm/analyze/getHtmlTemplate.js +302 -0
  100. package/dist/esm/analyze/getServerRoutes.js +224 -0
  101. package/dist/esm/analyze/index.js +703 -0
  102. package/dist/esm/analyze/isDefaultExportFunction.js +47 -0
  103. package/dist/esm/analyze/makeLegalIdentifier.js +13 -0
  104. package/dist/esm/analyze/nestedRoutes.js +409 -0
  105. package/dist/esm/analyze/templates.js +466 -0
  106. package/dist/esm/analyze/utils.js +374 -0
  107. package/dist/esm/builder/builder-rspack/index.js +6 -0
  108. package/dist/esm/builder/builder-webpack/builderPlugins/compatModern.js +64 -0
  109. package/dist/esm/builder/builder-webpack/index.js +301 -0
  110. package/dist/esm/builder/builder-webpack/webpackPlugins/RouterPlugin.js +346 -0
  111. package/dist/esm/builder/builder-webpack/webpackPlugins/index.js +1 -0
  112. package/dist/esm/builder/generator/createBuilderOptions.js +41 -0
  113. package/dist/esm/builder/generator/createBuilderProviderConfig.js +70 -0
  114. package/dist/esm/builder/generator/getBuilderTargets.js +16 -0
  115. package/dist/esm/builder/generator/index.js +199 -0
  116. package/dist/esm/builder/index.js +175 -0
  117. package/dist/esm/builder/shared/builderPlugins/adapterModern.js +300 -0
  118. package/dist/esm/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +110 -0
  119. package/dist/esm/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +72 -0
  120. package/dist/esm/builder/shared/createCopyPattern.js +40 -0
  121. package/dist/esm/builder/shared/index.js +3 -0
  122. package/dist/esm/builder/shared/loaders/serverModuleLoader.js +5 -0
  123. package/dist/esm/builder/shared/types.js +1 -0
  124. package/dist/esm/commands/build.js +291 -0
  125. package/dist/esm/commands/deploy.js +154 -0
  126. package/dist/esm/commands/dev.js +301 -0
  127. package/dist/esm/commands/index.js +3 -0
  128. package/dist/esm/commands/inspect.js +149 -0
  129. package/dist/esm/commands/serve.js +199 -0
  130. package/dist/esm/config/default.js +210 -0
  131. package/dist/esm/config/index.js +3 -0
  132. package/dist/esm/config/initialize/index.js +10 -0
  133. package/dist/esm/config/initialize/inits.js +211 -0
  134. package/dist/esm/config/legacy/createHtmlConfig.js +19 -0
  135. package/dist/esm/config/legacy/createOutputConfig.js +41 -0
  136. package/dist/esm/config/legacy/createSourceConfig.js +42 -0
  137. package/dist/esm/config/legacy/createToolsConfig.js +23 -0
  138. package/dist/esm/config/legacy/index.js +38 -0
  139. package/dist/esm/defineConfig.js +61 -0
  140. package/dist/esm/exports/server.js +2 -0
  141. package/dist/esm/hooks.js +29 -0
  142. package/dist/esm/index.js +713 -0
  143. package/dist/esm/initialize/index.js +284 -0
  144. package/dist/esm/locale/en.js +38 -0
  145. package/dist/esm/locale/index.js +9 -0
  146. package/dist/esm/locale/zh.js +38 -0
  147. package/dist/esm/schema/Schema.js +273 -0
  148. package/dist/esm/schema/index.js +181 -0
  149. package/dist/esm/schema/legacy.js +337 -0
  150. package/dist/esm/types/config/deploy.js +1 -0
  151. package/dist/esm/types/config/dev.js +1 -0
  152. package/dist/esm/types/config/experiments.js +1 -0
  153. package/dist/esm/types/config/html.js +1 -0
  154. package/dist/esm/types/config/index.js +1 -0
  155. package/dist/esm/types/config/output.js +1 -0
  156. package/dist/esm/types/config/performance.js +1 -0
  157. package/dist/esm/types/config/security.js +1 -0
  158. package/dist/esm/types/config/source.js +1 -0
  159. package/dist/esm/types/config/tools.js +1 -0
  160. package/dist/esm/types/hooks.js +1 -0
  161. package/dist/esm/types/index.js +3 -0
  162. package/dist/esm/types/legacyConfig/deploy.js +1 -0
  163. package/dist/esm/types/legacyConfig/dev.js +1 -0
  164. package/dist/esm/types/legacyConfig/index.js +1 -0
  165. package/dist/esm/types/legacyConfig/output.js +1 -0
  166. package/dist/esm/types/legacyConfig/source.js +1 -0
  167. package/dist/esm/types/legacyConfig/tools.js +1 -0
  168. package/dist/esm/types/utils.js +1 -0
  169. package/dist/esm/utils/config.js +302 -0
  170. package/dist/esm/utils/createServer.js +258 -0
  171. package/dist/esm/utils/env.js +13 -0
  172. package/dist/esm/utils/generateWatchFiles.js +214 -0
  173. package/dist/esm/utils/getSelectedEntries.js +186 -0
  174. package/dist/esm/utils/getServerInternalPlugins.js +210 -0
  175. package/dist/esm/utils/language.js +6 -0
  176. package/dist/esm/utils/printInstructions.js +152 -0
  177. package/dist/esm/utils/restart.js +187 -0
  178. package/dist/esm/utils/routes.js +153 -0
  179. package/dist/esm/utils/types.js +1 -0
  180. package/dist/esm-node/analyze/constants.js +56 -0
  181. package/dist/esm-node/analyze/generateCode.js +227 -0
  182. package/dist/esm-node/analyze/getBundleEntry.js +77 -0
  183. package/dist/esm-node/analyze/getClientRoutes/getRoutes.js +201 -0
  184. package/dist/esm-node/analyze/getClientRoutes/getRoutesLegacy.js +197 -0
  185. package/dist/esm-node/analyze/getClientRoutes/index.js +6 -0
  186. package/dist/esm-node/analyze/getClientRoutes/utils.js +31 -0
  187. package/dist/esm-node/analyze/getFileSystemEntry.js +109 -0
  188. package/dist/esm-node/analyze/getHtmlTemplate.js +95 -0
  189. package/dist/esm-node/analyze/getServerRoutes.js +154 -0
  190. package/dist/esm-node/analyze/index.js +285 -0
  191. package/dist/esm-node/analyze/isDefaultExportFunction.js +42 -0
  192. package/dist/esm-node/analyze/makeLegalIdentifier.js +13 -0
  193. package/dist/esm-node/analyze/nestedRoutes.js +150 -0
  194. package/dist/esm-node/analyze/templates.js +297 -0
  195. package/dist/esm-node/analyze/utils.js +121 -0
  196. package/dist/esm-node/builder/builder-rspack/index.js +8 -0
  197. package/dist/esm-node/builder/builder-webpack/builderPlugins/compatModern.js +41 -0
  198. package/dist/esm-node/builder/builder-webpack/index.js +51 -0
  199. package/dist/esm-node/builder/builder-webpack/webpackPlugins/RouterPlugin.js +96 -0
  200. package/dist/esm-node/builder/builder-webpack/webpackPlugins/index.js +1 -0
  201. package/dist/esm-node/builder/generator/createBuilderOptions.js +24 -0
  202. package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +26 -0
  203. package/dist/esm-node/builder/generator/getBuilderTargets.js +21 -0
  204. package/dist/esm-node/builder/generator/index.js +29 -0
  205. package/dist/esm-node/builder/index.js +17 -0
  206. package/dist/esm-node/builder/shared/builderPlugins/adapterModern.js +202 -0
  207. package/dist/esm-node/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +27 -0
  208. package/dist/esm-node/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +34 -0
  209. package/dist/esm-node/builder/shared/createCopyPattern.js +46 -0
  210. package/dist/esm-node/builder/shared/index.js +3 -0
  211. package/dist/esm-node/builder/shared/loaders/serverModuleLoader.js +7 -0
  212. package/dist/esm-node/builder/shared/types.js +0 -0
  213. package/dist/esm-node/commands/build.js +52 -0
  214. package/dist/esm-node/commands/deploy.js +8 -0
  215. package/dist/esm-node/commands/dev.js +72 -0
  216. package/dist/esm-node/commands/index.js +3 -0
  217. package/dist/esm-node/commands/inspect.js +18 -0
  218. package/dist/esm-node/commands/serve.js +34 -0
  219. package/dist/esm-node/config/default.js +162 -0
  220. package/dist/esm-node/config/index.js +3 -0
  221. package/dist/esm-node/config/initialize/index.js +12 -0
  222. package/dist/esm-node/config/initialize/inits.js +136 -0
  223. package/dist/esm-node/config/legacy/createHtmlConfig.js +35 -0
  224. package/dist/esm-node/config/legacy/createOutputConfig.js +68 -0
  225. package/dist/esm-node/config/legacy/createSourceConfig.js +39 -0
  226. package/dist/esm-node/config/legacy/createToolsConfig.js +42 -0
  227. package/dist/esm-node/config/legacy/index.js +51 -0
  228. package/dist/esm-node/defineConfig.js +9 -0
  229. package/dist/esm-node/exports/server.js +4 -0
  230. package/dist/esm-node/hooks.js +35 -0
  231. package/dist/esm-node/index.js +172 -0
  232. package/dist/esm-node/initialize/index.js +91 -0
  233. package/dist/esm-node/locale/en.js +36 -0
  234. package/dist/esm-node/locale/index.js +9 -0
  235. package/dist/esm-node/locale/zh.js +36 -0
  236. package/dist/esm-node/schema/Schema.js +40 -0
  237. package/dist/esm-node/schema/index.js +88 -0
  238. package/dist/esm-node/schema/legacy.js +148 -0
  239. package/dist/esm-node/types/config/deploy.js +0 -0
  240. package/dist/esm-node/types/config/dev.js +0 -0
  241. package/dist/esm-node/types/config/experiments.js +0 -0
  242. package/dist/esm-node/types/config/html.js +0 -0
  243. package/dist/esm-node/types/config/index.js +1 -0
  244. package/dist/esm-node/types/config/output.js +0 -0
  245. package/dist/esm-node/types/config/performance.js +0 -0
  246. package/dist/esm-node/types/config/security.js +0 -0
  247. package/dist/esm-node/types/config/source.js +0 -0
  248. package/dist/esm-node/types/config/tools.js +0 -0
  249. package/dist/esm-node/types/hooks.js +0 -0
  250. package/dist/esm-node/types/index.js +3 -0
  251. package/dist/esm-node/types/legacyConfig/deploy.js +0 -0
  252. package/dist/esm-node/types/legacyConfig/dev.js +0 -0
  253. package/dist/esm-node/types/legacyConfig/index.js +0 -0
  254. package/dist/esm-node/types/legacyConfig/output.js +0 -0
  255. package/dist/esm-node/types/legacyConfig/source.js +0 -0
  256. package/dist/esm-node/types/legacyConfig/tools.js +0 -0
  257. package/dist/esm-node/types/utils.js +0 -0
  258. package/dist/esm-node/utils/config.js +88 -0
  259. package/dist/esm-node/utils/createServer.js +37 -0
  260. package/dist/esm-node/utils/env.js +15 -0
  261. package/dist/esm-node/utils/generateWatchFiles.js +33 -0
  262. package/dist/esm-node/utils/getSelectedEntries.js +38 -0
  263. package/dist/esm-node/utils/getServerInternalPlugins.js +18 -0
  264. package/dist/esm-node/utils/language.js +8 -0
  265. package/dist/esm-node/utils/printInstructions.js +11 -0
  266. package/dist/esm-node/utils/restart.js +22 -0
  267. package/dist/esm-node/utils/routes.js +10 -0
  268. package/dist/esm-node/utils/types.js +0 -0
  269. package/dist/js/modern/analyze/nestedRoutes.js +1 -10
  270. package/dist/js/node/analyze/nestedRoutes.js +1 -10
  271. package/dist/js/treeshaking/analyze/nestedRoutes.js +1 -10
  272. package/dist/types/analyze/templates.d.ts +3 -1
  273. package/dist/types/types/config/tools.d.ts +1 -1
  274. package/dist/types/types/hooks.d.ts +5 -4
  275. package/dist/types/types/index.d.ts +3 -1
  276. package/package.json +30 -31
@@ -0,0 +1,42 @@
1
+ import fs from "fs";
2
+ import { parse } from "@babel/parser";
3
+ import traverse from "@babel/traverse";
4
+ import * as t from "@babel/types";
5
+ const isFunction = (node) => t.isFunctionDeclaration(node) || t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
6
+ const isDefaultExportFunction = (file) => {
7
+ if (!file || !fs.existsSync(file)) {
8
+ return false;
9
+ }
10
+ const ast = parse(fs.readFileSync(file, "utf8"), {
11
+ sourceType: "unambiguous",
12
+ plugins: [
13
+ "jsx",
14
+ "typescript",
15
+ "classProperties",
16
+ "dynamicImport",
17
+ "exportDefaultFrom",
18
+ "exportNamespaceFrom",
19
+ "decorators-legacy",
20
+ "functionBind",
21
+ "classPrivateMethods",
22
+ ["pipelineOperator", { proposal: "minimal" }],
23
+ "optionalChaining",
24
+ "optionalCatchBinding",
25
+ "objectRestSpread",
26
+ "numericSeparator"
27
+ ]
28
+ });
29
+ let isExportFunction = false;
30
+ traverse(ast, {
31
+ ExportDefaultDeclaration: (path) => {
32
+ const { declaration } = path.node;
33
+ if (isFunction(declaration)) {
34
+ isExportFunction = true;
35
+ }
36
+ }
37
+ });
38
+ return isExportFunction;
39
+ };
40
+ export {
41
+ isDefaultExportFunction
42
+ };
@@ -0,0 +1,13 @@
1
+ const reservedWords = "break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public";
2
+ const builtins = "arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl";
3
+ const forbidList = new Set(`${reservedWords} ${builtins}`.split(" "));
4
+ function makeLegalIdentifier(str) {
5
+ const identifier = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, "_");
6
+ if (/\d/.test(identifier[0]) || forbidList.has(identifier)) {
7
+ return `_${identifier}`;
8
+ }
9
+ return identifier || "_";
10
+ }
11
+ export {
12
+ makeLegalIdentifier
13
+ };
@@ -0,0 +1,150 @@
1
+ import * as path from "path";
2
+ import { fs, getRouteId } from "@modern-js/utils";
3
+ import { JS_EXTENSIONS, NESTED_ROUTE } from "./constants";
4
+ import { replaceWithAlias } from "./utils";
5
+ const conventionNames = Object.values(NESTED_ROUTE);
6
+ const replaceDynamicPath = (routePath) => {
7
+ return routePath.replace(/\[(.*?)\]/g, ":$1");
8
+ };
9
+ const createIndexRoute = (routeInfo, rootDir, filename, entryName) => {
10
+ return createRoute(
11
+ {
12
+ ...routeInfo,
13
+ index: true,
14
+ children: void 0
15
+ },
16
+ rootDir,
17
+ filename,
18
+ entryName
19
+ );
20
+ };
21
+ const createRoute = (routeInfo, rootDir, filename, entryName) => {
22
+ const id = getRouteId(filename, rootDir, entryName);
23
+ return {
24
+ ...routeInfo,
25
+ id,
26
+ type: "nested"
27
+ };
28
+ };
29
+ const walk = async (dirname, rootDir, alias, entryName) => {
30
+ var _a, _b, _c, _d;
31
+ if (!await fs.pathExists(dirname)) {
32
+ return null;
33
+ }
34
+ const isDirectory = (await fs.stat(dirname)).isDirectory();
35
+ if (!isDirectory) {
36
+ return null;
37
+ }
38
+ const relativeDir = path.relative(rootDir, dirname);
39
+ const pathSegments = relativeDir.split(path.sep);
40
+ const lastSegment = pathSegments[pathSegments.length - 1];
41
+ const isRoot = lastSegment === "";
42
+ const isPathlessLayout = lastSegment.startsWith("__");
43
+ const isWithoutLayoutPath = lastSegment.includes(".");
44
+ let routePath = isRoot || isPathlessLayout ? "/" : `${lastSegment}`;
45
+ if (isWithoutLayoutPath) {
46
+ routePath = lastSegment.split(".").join("/");
47
+ }
48
+ routePath = replaceDynamicPath(routePath);
49
+ const route = {
50
+ path: routePath,
51
+ children: [],
52
+ isRoot
53
+ };
54
+ let pageLoaderFile = "";
55
+ let pageRoute = null;
56
+ let splatLoaderFile = "";
57
+ let splatRoute = null;
58
+ const items = await fs.readdir(dirname);
59
+ for (const item of items) {
60
+ const itemPath = path.join(dirname, item);
61
+ const extname = path.extname(item);
62
+ const itemWithoutExt = item.slice(0, -extname.length);
63
+ const isDirectory2 = (await fs.stat(itemPath)).isDirectory();
64
+ if (isDirectory2) {
65
+ const childRoute = await walk(itemPath, rootDir, alias, entryName);
66
+ if (childRoute) {
67
+ (_a = route.children) == null ? void 0 : _a.push(childRoute);
68
+ }
69
+ }
70
+ if (extname && (!JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
71
+ continue;
72
+ }
73
+ if (itemWithoutExt === NESTED_ROUTE.LAYOUT_LOADER_FILE) {
74
+ if (!route.loader) {
75
+ route.loader = itemPath;
76
+ }
77
+ }
78
+ if (itemWithoutExt === NESTED_ROUTE.LAYOUT_FILE) {
79
+ route._component = replaceWithAlias(alias.basename, itemPath, alias.name);
80
+ }
81
+ if (itemWithoutExt === NESTED_ROUTE.PAGE_LOADER_FILE) {
82
+ pageLoaderFile = itemPath;
83
+ }
84
+ if (itemWithoutExt === NESTED_ROUTE.PAGE_FILE) {
85
+ pageRoute = createIndexRoute(
86
+ {
87
+ _component: replaceWithAlias(alias.basename, itemPath, alias.name)
88
+ },
89
+ rootDir,
90
+ itemPath,
91
+ entryName
92
+ );
93
+ if (pageLoaderFile) {
94
+ pageRoute.loader = pageLoaderFile;
95
+ }
96
+ (_b = route.children) == null ? void 0 : _b.push(pageRoute);
97
+ }
98
+ if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
99
+ splatLoaderFile = itemPath;
100
+ }
101
+ if (itemWithoutExt === NESTED_ROUTE.SPLATE_FILE) {
102
+ splatRoute = createRoute(
103
+ {
104
+ _component: replaceWithAlias(alias.basename, itemPath, alias.name),
105
+ path: "*"
106
+ },
107
+ rootDir,
108
+ itemPath,
109
+ entryName
110
+ );
111
+ if (splatLoaderFile) {
112
+ splatRoute.loader = splatLoaderFile;
113
+ }
114
+ (_c = route.children) == null ? void 0 : _c.push(splatRoute);
115
+ }
116
+ if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
117
+ route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
118
+ }
119
+ if (itemWithoutExt === NESTED_ROUTE.ERROR_FILE) {
120
+ route.error = replaceWithAlias(alias.basename, itemPath, alias.name);
121
+ }
122
+ }
123
+ let finalRoute = createRoute(
124
+ route,
125
+ rootDir,
126
+ path.join(dirname, `${NESTED_ROUTE.LAYOUT_FILE}.ts`),
127
+ entryName
128
+ );
129
+ if (isPathlessLayout) {
130
+ delete finalRoute.path;
131
+ }
132
+ route.children = (_d = route.children) == null ? void 0 : _d.filter((childRoute) => childRoute);
133
+ if (route.children && route.children.length === 0 && !route.index) {
134
+ return null;
135
+ }
136
+ if (finalRoute.children && finalRoute.children.length === 1 && !finalRoute._component) {
137
+ const childRoute = finalRoute.children[0];
138
+ if (childRoute.path === "*") {
139
+ const path2 = `${finalRoute.path || ""}/${childRoute.path || ""}`;
140
+ finalRoute = {
141
+ ...childRoute,
142
+ path: path2
143
+ };
144
+ }
145
+ }
146
+ return finalRoute;
147
+ };
148
+ export {
149
+ walk
150
+ };
@@ -0,0 +1,297 @@
1
+ import path from "path";
2
+ import { fs, slash } from "@modern-js/utils";
3
+ import { APP_CONFIG_NAME, TEMP_LOADERS_DIR } from "./constants";
4
+ const index = ({
5
+ mountId,
6
+ imports,
7
+ renderFunction: renderFunction2,
8
+ exportStatement
9
+ }) => `
10
+ const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
11
+ const IS_REACT18 = process.env.IS_REACT18 === 'true';
12
+ const MOUNT_ID = '${mountId}';
13
+
14
+ ${imports}
15
+
16
+ let AppWrapper = null;
17
+
18
+ let root = null;
19
+
20
+ function render() {
21
+ ${renderFunction2}
22
+ }
23
+
24
+ AppWrapper = render();
25
+
26
+ ${exportStatement};
27
+ `;
28
+ const renderFunction = ({
29
+ plugins,
30
+ customBootstrap,
31
+ fileSystemRoutes: fileSystemRoutes2
32
+ }) => {
33
+ const bootstrap = "bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM)";
34
+ return `
35
+ const finalAppConfig = {
36
+ ...App.config,
37
+ ...typeof ${APP_CONFIG_NAME} === 'function' ? ${APP_CONFIG_NAME}() : {},
38
+ }
39
+
40
+ AppWrapper = createApp({
41
+ plugins: [
42
+ ${plugins.map(
43
+ ({ name, options, args }) => `${name}({...${options}, ...finalAppConfig?.${args || name}}),`
44
+ ).join("\n")}
45
+ ]
46
+ })(${fileSystemRoutes2 ? "" : `App`})
47
+
48
+
49
+ if(!AppWrapper.init && typeof appInit !== 'undefined') {
50
+ AppWrapper.init = appInit;
51
+ }
52
+
53
+
54
+ if (IS_BROWSER) {
55
+ ${customBootstrap ? `customBootstrap(AppWrapper, () => ${bootstrap});` : `${bootstrap};`}
56
+ }
57
+
58
+ return AppWrapper
59
+ `;
60
+ };
61
+ const html = (partials) => `
62
+ <!DOCTYPE html>
63
+ <html>
64
+ <head>
65
+ <%= meta %>
66
+ <title><%= title %></title>
67
+
68
+ ${partials.top.join("\n")}
69
+
70
+ <script>
71
+ window.__assetPrefix__ = '<%= assetPrefix %>';
72
+ <\/script>
73
+ ${partials.head.join("\n")}
74
+
75
+ <!--<?- chunksMap.css ?>-->
76
+ </head>
77
+
78
+ <body>
79
+ <noscript>
80
+ We're sorry but react app doesn't work properly without JavaScript enabled. Please enable it to continue.
81
+ </noscript>
82
+ <div id="<%= mountId %>"><!--<?- html ?>--></div>
83
+ ${partials.body.join("\n")}
84
+ <!--<?- chunksMap.js ?>-->
85
+ <!--<?- SSRDataScript ?>-->
86
+ <!--<?- bottomTemplate ?>-->
87
+ </body>
88
+
89
+ </html>
90
+ `;
91
+ const routesForServer = ({
92
+ routes,
93
+ metaName
94
+ }) => {
95
+ const loaders = [];
96
+ const traverseRouteTree = (route) => {
97
+ var _a;
98
+ let children;
99
+ if ("children" in route && route.children) {
100
+ children = (_a = route == null ? void 0 : route.children) == null ? void 0 : _a.map(traverseRouteTree);
101
+ }
102
+ let loader;
103
+ if (route.type === "nested") {
104
+ if (route.loader) {
105
+ loaders.push(route.loader);
106
+ loader = `loader_${loaders.length - 1}`;
107
+ }
108
+ }
109
+ const finalRoute = {
110
+ ...route,
111
+ loader,
112
+ children
113
+ };
114
+ return finalRoute;
115
+ };
116
+ let routesCode = `
117
+ export const routes = [
118
+ `;
119
+ for (const route of routes) {
120
+ if ("type" in route) {
121
+ const newRoute = traverseRouteTree(route);
122
+ routesCode += `${JSON.stringify(newRoute, null, 2).replace(
123
+ /"(loader_[^"])"/g,
124
+ "$1"
125
+ )},`;
126
+ } else {
127
+ routesCode += `${JSON.stringify(route, null, 2)}`;
128
+ }
129
+ }
130
+ routesCode += `
131
+ ];`;
132
+ let importLoadersCode = "";
133
+ if (loaders.length > 0) {
134
+ importLoadersCode = loaders.map((loader, index2) => {
135
+ return `import loader_${index2} from "${slash(loader)}"`;
136
+ }).join("\n");
137
+ }
138
+ const exportCode = `
139
+ export { handleRequest } from '@modern-js/plugin-data-loader/runtime';
140
+ `;
141
+ return `
142
+ ${importLoadersCode}
143
+ ${routesCode}
144
+ ${exportCode}
145
+ `;
146
+ };
147
+ const fileSystemRoutes = async ({
148
+ routes,
149
+ ssrMode,
150
+ nestedRoutesEntry,
151
+ entryName,
152
+ internalDirectory
153
+ }) => {
154
+ const loadings = [];
155
+ const errors = [];
156
+ const loaders = [];
157
+ const loadersMap = {};
158
+ const loadersMapFile = path.join(
159
+ internalDirectory,
160
+ entryName,
161
+ TEMP_LOADERS_DIR,
162
+ "map.json"
163
+ );
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) => {
170
+ if (!ssrMode) {
171
+ return "";
172
+ }
173
+ let dataLoaderPath = require.resolve("@modern-js/plugin-data-loader/loader");
174
+ if (nestedRoutesEntry) {
175
+ dataLoaderPath = `${slash(dataLoaderPath)}?mapFile=${slash(
176
+ loadersMapFile
177
+ )}&loaderId=${loaderId}!`;
178
+ }
179
+ return dataLoaderPath;
180
+ };
181
+ const traverseRouteTree = (route) => {
182
+ var _a;
183
+ let children;
184
+ if ("children" in route && route.children) {
185
+ children = (_a = route == null ? void 0 : route.children) == null ? void 0 : _a.map(traverseRouteTree);
186
+ }
187
+ let loading;
188
+ let error;
189
+ let loader;
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) {
202
+ loaders.push(route.loader);
203
+ const loaderId = loaders.length - 1;
204
+ loader = `loader_${loaderId}`;
205
+ loadersMap[loader] = {
206
+ routeId: route.id,
207
+ filePath: route.loader,
208
+ inline: false
209
+ };
210
+ }
211
+ if (route._component) {
212
+ if (route.isRoot) {
213
+ rootLayoutCode = `import RootLayout from '${route._component}'`;
214
+ component = `RootLayout`;
215
+ } else if (ssrMode === "string") {
216
+ lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}')`;
217
+ component = `loadable(${lazyImport})`;
218
+ } else {
219
+ lazyImport = `() => import(/* webpackChunkName: "${route.id}" */ '${route._component}')`;
220
+ component = `lazy(${lazyImport})`;
221
+ }
222
+ }
223
+ } else if (route._component) {
224
+ lazyImport = `() => import('${route._component}')`;
225
+ component = `loadable(${lazyImport})`;
226
+ }
227
+ const finalRoute = {
228
+ ...route,
229
+ lazyImport,
230
+ loading,
231
+ loader,
232
+ error,
233
+ children
234
+ };
235
+ if (route._component) {
236
+ finalRoute.component = component;
237
+ }
238
+ return finalRoute;
239
+ };
240
+ let routeComponentsCode = `
241
+ export const routes = [
242
+ `;
243
+ for (const route of routes) {
244
+ if ("type" in route) {
245
+ const newRoute = traverseRouteTree(route);
246
+ routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable.*\))"/g, "$1").replace(/"(loadableLazy.*\))"/g, "$1").replace(/"(\(\)[^,]+)",/g, "$1,").replace(/"(lazy\(.*\))"/g, "$1").replace(/"(loading_[^"])"/g, "$1").replace(/"(loader_[^"])"/g, "$1").replace(/"(RootLayout)"/g, "$1").replace(/"(error_[^"])"/g, "$1").replace(/\\"/g, '"')},`;
247
+ } else {
248
+ const component = `loadable(() => import('${route._component}'))`;
249
+ const finalRoute = {
250
+ ...route,
251
+ component
252
+ };
253
+ routeComponentsCode += `${JSON.stringify(finalRoute, null, 2).replace(/"(loadable[^"]*)"/g, "$1").replace(/"(lazy[^"]*)"/g, "$1")},`;
254
+ }
255
+ }
256
+ routeComponentsCode += `
257
+ ];`;
258
+ const importLoadingCode = loadings.map((loading, index2) => {
259
+ return `import loading_${index2} from '${loading}';
260
+ `;
261
+ }).join("");
262
+ const importErrorComponentsCode = errors.map((error, index2) => {
263
+ return `import error_${index2} from '${error}';
264
+ `;
265
+ }).join("");
266
+ let importLoadersCode = "";
267
+ for (const [key, loaderInfo] of Object.entries(loadersMap)) {
268
+ if (loaderInfo.inline) {
269
+ importLoadersCode += `import { loader as ${key} } from "${getDataLoaderPath(
270
+ key
271
+ )}${slash(loaderInfo.filePath)}";
272
+ `;
273
+ } else {
274
+ importLoadersCode += `import ${key} from "${getDataLoaderPath(
275
+ key
276
+ )}${slash(loaderInfo.filePath)}";
277
+ `;
278
+ }
279
+ }
280
+ await fs.ensureFile(loadersMapFile);
281
+ await fs.writeJSON(loadersMapFile, loadersMap);
282
+ return `
283
+ ${importLazyCode}
284
+ ${rootLayoutCode}
285
+ ${importLoadingCode}
286
+ ${importErrorComponentsCode}
287
+ ${importLoadersCode}
288
+ ${routeComponentsCode}
289
+ `;
290
+ };
291
+ export {
292
+ fileSystemRoutes,
293
+ html,
294
+ index,
295
+ renderFunction,
296
+ routesForServer
297
+ };
@@ -0,0 +1,121 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { isReact18, normalizeToPosixPath, fs as fse } from "@modern-js/utils";
4
+ import { transform } from "esbuild";
5
+ import { parse } from "es-module-lexer";
6
+ import {
7
+ FILE_SYSTEM_ROUTES_FILE_NAME,
8
+ JS_EXTENSIONS,
9
+ LOADER_EXPORT_NAME
10
+ } from "./constants";
11
+ const walkDirectory = (dir) => fs.readdirSync(dir).reduce((previous, filename) => {
12
+ const filePath = path.join(dir, filename);
13
+ if (fs.statSync(filePath).isDirectory()) {
14
+ return [...previous, ...walkDirectory(filePath)];
15
+ } else {
16
+ return [...previous, filePath];
17
+ }
18
+ }, []);
19
+ const getDefaultImports = ({
20
+ entrypoint,
21
+ srcDirectory,
22
+ internalSrcAlias,
23
+ internalDirAlias,
24
+ internalDirectory
25
+ }) => {
26
+ const { entryName, fileSystemRoutes, customBootstrap, entry } = entrypoint;
27
+ const imports = [
28
+ {
29
+ specifiers: [{ local: "React" }],
30
+ value: "react"
31
+ },
32
+ {
33
+ specifiers: [{ local: "ReactDOM" }],
34
+ value: isReact18(path.join(internalDirectory, "../../")) ? "react-dom/client" : "react-dom"
35
+ },
36
+ {
37
+ specifiers: [{ imported: "createApp" }, { imported: "bootstrap" }],
38
+ value: "@modern-js/runtime"
39
+ },
40
+ customBootstrap && {
41
+ specifiers: [{ local: "customBootstrap" }],
42
+ value: normalizeToPosixPath(
43
+ customBootstrap.replace(srcDirectory, internalSrcAlias)
44
+ )
45
+ }
46
+ ].filter(Boolean);
47
+ if (fileSystemRoutes) {
48
+ const route = {
49
+ specifiers: [{ imported: "routes" }],
50
+ value: normalizeToPosixPath(
51
+ `${internalDirAlias}/${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME.replace(
52
+ ".js",
53
+ ""
54
+ )}`
55
+ )
56
+ };
57
+ if (fileSystemRoutes.globalApp) {
58
+ imports.push({
59
+ specifiers: [{ local: "App" }],
60
+ value: normalizeToPosixPath(
61
+ fileSystemRoutes.globalApp.replace(srcDirectory, internalSrcAlias)
62
+ )
63
+ });
64
+ } else {
65
+ route.initialize = "const App = false;";
66
+ }
67
+ imports.push(route);
68
+ } else {
69
+ imports.push({
70
+ specifiers: [{ local: "App" }],
71
+ value: normalizeToPosixPath(
72
+ entry.replace(srcDirectory, internalSrcAlias)
73
+ )
74
+ });
75
+ }
76
+ return imports;
77
+ };
78
+ const isPageComponentFile = (filePath) => {
79
+ if (/\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/.test(filePath)) {
80
+ return false;
81
+ }
82
+ if ([".js", ".jsx", ".ts", ".tsx"].includes(path.extname(filePath))) {
83
+ return true;
84
+ }
85
+ return false;
86
+ };
87
+ const replaceWithAlias = (base, filePath, alias) => normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
88
+ const parseModule = async ({
89
+ source,
90
+ filename
91
+ }) => {
92
+ let content = source;
93
+ if (JS_EXTENSIONS.some((ext) => filename.endsWith(ext))) {
94
+ const result = await transform(content, {
95
+ loader: path.extname(filename).slice(1),
96
+ format: "esm"
97
+ });
98
+ content = result.code;
99
+ }
100
+ return await parse(content);
101
+ };
102
+ const hasLoader = async (filename) => {
103
+ const source = await fse.readFile(filename);
104
+ const [, moduleExports] = await parseModule({
105
+ source: source.toString(),
106
+ filename
107
+ });
108
+ return moduleExports.some((e) => e.n === LOADER_EXPORT_NAME);
109
+ };
110
+ const getServerLoadersFile = (internalDirectory, entryName) => {
111
+ return path.join(internalDirectory, entryName, "route-server-loaders.js");
112
+ };
113
+ export {
114
+ getDefaultImports,
115
+ getServerLoadersFile,
116
+ hasLoader,
117
+ isPageComponentFile,
118
+ parseModule,
119
+ replaceWithAlias,
120
+ walkDirectory
121
+ };
@@ -0,0 +1,8 @@
1
+ import { builderRspackProvider } from "@modern-js/builder-rspack-provider";
2
+ import { generateBuilder } from "../generator";
3
+ function createRspackBuilderForModern(options) {
4
+ return generateBuilder(options, builderRspackProvider);
5
+ }
6
+ export {
7
+ createRspackBuilderForModern
8
+ };
@@ -0,0 +1,41 @@
1
+ import { join } from "path";
2
+ import { createCopyPattern } from "../../shared";
3
+ import { RouterPlugin } from "../webpackPlugins";
4
+ const PluginCompatModern = (options) => ({
5
+ name: "builder-plugin-compat-modern",
6
+ setup(api) {
7
+ const { normalizedConfig: modernConfig, appContext } = options;
8
+ api.modifyWebpackChain((chain, { CHAIN_ID }) => {
9
+ var _a;
10
+ chain.resolve.modules.add("node_modules").add(join(api.context.rootPath, "node_modules"));
11
+ if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
12
+ const defaultCopyPattern = createCopyPattern(
13
+ appContext,
14
+ modernConfig,
15
+ "public",
16
+ chain
17
+ );
18
+ chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
19
+ var _a2;
20
+ return [
21
+ {
22
+ patterns: [...((_a2 = args[0]) == null ? void 0 : _a2.patterns) || [], defaultCopyPattern]
23
+ }
24
+ ];
25
+ });
26
+ }
27
+ const { entrypoints } = appContext;
28
+ const existNestedRoutes = entrypoints.some(
29
+ (entrypoint) => entrypoint.nestedRoutesEntry
30
+ );
31
+ const routerConfig = (_a = modernConfig == null ? void 0 : modernConfig.runtime) == null ? void 0 : _a.router;
32
+ const routerManifest = Boolean(routerConfig == null ? void 0 : routerConfig.manifest);
33
+ if (existNestedRoutes || routerManifest) {
34
+ chain.plugin("route-plugin").use(RouterPlugin);
35
+ }
36
+ });
37
+ }
38
+ });
39
+ export {
40
+ PluginCompatModern
41
+ };