@modern-js/app-tools 2.15.0 → 2.17.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 (268) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/dist/cjs/analyze/constants.js +37 -61
  3. package/dist/cjs/analyze/generateCode.js +102 -127
  4. package/dist/cjs/analyze/getBundleEntry.js +33 -66
  5. package/dist/cjs/analyze/getClientRoutes/getRoutes.js +61 -117
  6. package/dist/cjs/analyze/getClientRoutes/getRoutesLegacy.js +61 -115
  7. package/dist/cjs/analyze/getClientRoutes/index.js +15 -28
  8. package/dist/cjs/analyze/getClientRoutes/utils.js +24 -48
  9. package/dist/cjs/analyze/getFileSystemEntry.js +39 -79
  10. package/dist/cjs/analyze/getHtmlTemplate.js +88 -98
  11. package/dist/cjs/analyze/getServerRoutes.js +67 -110
  12. package/dist/cjs/analyze/index.js +265 -257
  13. package/dist/cjs/analyze/isDefaultExportFunction.js +66 -44
  14. package/dist/cjs/analyze/makeLegalIdentifier.js +7 -25
  15. package/dist/cjs/analyze/nestedRoutes.js +99 -104
  16. package/dist/cjs/analyze/templates.js +49 -110
  17. package/dist/cjs/analyze/utils.js +98 -102
  18. package/dist/cjs/builder/builder-rspack/adapterCopy.js +58 -100
  19. package/dist/cjs/builder/builder-rspack/index.js +14 -30
  20. package/dist/cjs/builder/builder-webpack/adapterModern.js +33 -50
  21. package/dist/cjs/builder/builder-webpack/createCopyPattern.js +16 -32
  22. package/dist/cjs/builder/builder-webpack/index.js +65 -46
  23. package/dist/cjs/builder/generator/createBuilderOptions.js +10 -26
  24. package/dist/cjs/builder/generator/createBuilderProviderConfig.js +11 -27
  25. package/dist/cjs/builder/generator/getBuilderTargets.js +13 -29
  26. package/dist/cjs/builder/generator/index.js +61 -55
  27. package/dist/cjs/builder/index.js +53 -43
  28. package/dist/cjs/builder/shared/builderPlugins/adapterBasic.js +13 -36
  29. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +90 -90
  30. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +108 -111
  31. package/dist/cjs/builder/shared/builderPlugins/index.js +20 -19
  32. package/dist/cjs/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +30 -30
  33. package/dist/cjs/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +32 -38
  34. package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +86 -116
  35. package/dist/cjs/builder/shared/bundlerPlugins/index.js +20 -19
  36. package/dist/cjs/builder/shared/createCopyInfo.js +16 -42
  37. package/dist/cjs/builder/shared/index.js +20 -19
  38. package/dist/cjs/builder/shared/loaders/serverModuleLoader.js +8 -24
  39. package/dist/cjs/builder/shared/types.js +4 -15
  40. package/dist/cjs/commands/build.js +23 -40
  41. package/dist/cjs/commands/deploy.js +7 -25
  42. package/dist/cjs/commands/dev.js +38 -57
  43. package/dist/cjs/commands/index.js +20 -19
  44. package/dist/cjs/commands/inspect.js +10 -30
  45. package/dist/cjs/commands/serve.js +28 -66
  46. package/dist/cjs/config/default.js +34 -34
  47. package/dist/cjs/config/index.js +20 -19
  48. package/dist/cjs/config/initialize/index.js +11 -29
  49. package/dist/cjs/config/initialize/inits.js +103 -82
  50. package/dist/cjs/config/legacy/createHtmlConfig.js +8 -40
  51. package/dist/cjs/config/legacy/createOutputConfig.js +8 -51
  52. package/dist/cjs/config/legacy/createSourceConfig.js +8 -38
  53. package/dist/cjs/config/legacy/createToolsConfig.js +8 -43
  54. package/dist/cjs/config/legacy/index.js +22 -46
  55. package/dist/cjs/defineConfig.js +16 -31
  56. package/dist/cjs/exports/server.js +7 -25
  57. package/dist/cjs/hooks.js +33 -51
  58. package/dist/cjs/index.js +185 -166
  59. package/dist/cjs/initialize/index.js +84 -100
  60. package/dist/cjs/locale/en.js +13 -27
  61. package/dist/cjs/locale/index.js +18 -28
  62. package/dist/cjs/locale/zh.js +13 -27
  63. package/dist/cjs/schema/Schema.js +27 -31
  64. package/dist/cjs/schema/index.js +141 -71
  65. package/dist/cjs/schema/legacy.js +273 -96
  66. package/dist/cjs/types/config/deploy.js +4 -15
  67. package/dist/cjs/types/config/dev.js +4 -15
  68. package/dist/cjs/types/config/experiments.js +4 -15
  69. package/dist/cjs/types/config/html.js +4 -15
  70. package/dist/cjs/types/config/index.js +18 -17
  71. package/dist/cjs/types/config/output.js +4 -15
  72. package/dist/cjs/types/config/performance.js +4 -15
  73. package/dist/cjs/types/config/security.js +4 -15
  74. package/dist/cjs/types/config/source.js +4 -15
  75. package/dist/cjs/types/config/tools.js +4 -15
  76. package/dist/cjs/types/hooks.js +4 -15
  77. package/dist/cjs/types/index.js +20 -19
  78. package/dist/cjs/types/legacyConfig/deploy.js +4 -15
  79. package/dist/cjs/types/legacyConfig/dev.js +4 -15
  80. package/dist/cjs/types/legacyConfig/index.js +4 -15
  81. package/dist/cjs/types/legacyConfig/output.js +4 -15
  82. package/dist/cjs/types/legacyConfig/source.js +4 -15
  83. package/dist/cjs/types/legacyConfig/tools.js +4 -15
  84. package/dist/cjs/types/utils.js +4 -15
  85. package/dist/cjs/utils/config.js +74 -79
  86. package/dist/cjs/utils/createServer.js +16 -42
  87. package/dist/cjs/utils/env.js +7 -25
  88. package/dist/cjs/utils/generateWatchFiles.js +31 -52
  89. package/dist/cjs/utils/getSelectedEntries.js +13 -35
  90. package/dist/cjs/utils/getServerInternalPlugins.js +8 -29
  91. package/dist/cjs/utils/printInstructions.js +10 -28
  92. package/dist/cjs/utils/restart.js +13 -31
  93. package/dist/cjs/utils/routes.js +18 -39
  94. package/dist/cjs/utils/types.js +4 -15
  95. package/dist/esm/analyze/constants.js +40 -41
  96. package/dist/esm/analyze/generateCode.js +477 -455
  97. package/dist/esm/analyze/getBundleEntry.js +53 -54
  98. package/dist/esm/analyze/getClientRoutes/getRoutes.js +201 -202
  99. package/dist/esm/analyze/getClientRoutes/getRoutesLegacy.js +199 -200
  100. package/dist/esm/analyze/getClientRoutes/index.js +2 -3
  101. package/dist/esm/analyze/getClientRoutes/utils.js +15 -15
  102. package/dist/esm/analyze/getFileSystemEntry.js +86 -87
  103. package/dist/esm/analyze/getHtmlTemplate.js +289 -278
  104. package/dist/esm/analyze/getServerRoutes.js +203 -190
  105. package/dist/esm/analyze/index.js +679 -651
  106. package/dist/esm/analyze/isDefaultExportFunction.js +38 -39
  107. package/dist/esm/analyze/makeLegalIdentifier.js +8 -9
  108. package/dist/esm/analyze/nestedRoutes.js +400 -392
  109. package/dist/esm/analyze/templates.js +536 -509
  110. package/dist/esm/analyze/utils.js +357 -336
  111. package/dist/esm/builder/builder-rspack/adapterCopy.js +347 -330
  112. package/dist/esm/builder/builder-rspack/index.js +8 -9
  113. package/dist/esm/builder/builder-webpack/adapterModern.js +51 -43
  114. package/dist/esm/builder/builder-webpack/createCopyPattern.js +31 -32
  115. package/dist/esm/builder/builder-webpack/index.js +266 -249
  116. package/dist/esm/builder/generator/createBuilderOptions.js +37 -38
  117. package/dist/esm/builder/generator/createBuilderProviderConfig.js +62 -63
  118. package/dist/esm/builder/generator/getBuilderTargets.js +13 -14
  119. package/dist/esm/builder/generator/index.js +203 -196
  120. package/dist/esm/builder/index.js +183 -176
  121. package/dist/esm/builder/shared/builderPlugins/adapterBasic.js +90 -91
  122. package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +300 -291
  123. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +337 -329
  124. package/dist/esm/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +108 -97
  125. package/dist/esm/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +66 -64
  126. package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +364 -337
  127. package/dist/esm/builder/shared/createCopyInfo.js +9 -10
  128. package/dist/esm/builder/shared/loaders/serverModuleLoader.js +2 -3
  129. package/dist/esm/builder/shared/types.js +1 -1
  130. package/dist/esm/commands/build.js +279 -272
  131. package/dist/esm/commands/deploy.js +150 -144
  132. package/dist/esm/commands/dev.js +296 -289
  133. package/dist/esm/commands/inspect.js +144 -138
  134. package/dist/esm/commands/serve.js +241 -235
  135. package/dist/esm/config/default.js +202 -203
  136. package/dist/esm/config/initialize/index.js +7 -8
  137. package/dist/esm/config/initialize/inits.js +189 -181
  138. package/dist/esm/config/legacy/createHtmlConfig.js +17 -18
  139. package/dist/esm/config/legacy/createOutputConfig.js +41 -42
  140. package/dist/esm/config/legacy/createSourceConfig.js +38 -39
  141. package/dist/esm/config/legacy/createToolsConfig.js +24 -25
  142. package/dist/esm/config/legacy/index.js +31 -32
  143. package/dist/esm/defineConfig.js +52 -53
  144. package/dist/esm/exports/server.js +1 -2
  145. package/dist/esm/hooks.js +26 -27
  146. package/dist/esm/index.js +685 -671
  147. package/dist/esm/initialize/index.js +276 -269
  148. package/dist/esm/locale/en.js +36 -37
  149. package/dist/esm/locale/index.js +2 -2
  150. package/dist/esm/locale/zh.js +36 -37
  151. package/dist/esm/schema/Schema.js +255 -244
  152. package/dist/esm/schema/index.js +165 -165
  153. package/dist/esm/schema/legacy.js +316 -317
  154. package/dist/esm/types/config/deploy.js +1 -1
  155. package/dist/esm/types/config/dev.js +1 -1
  156. package/dist/esm/types/config/experiments.js +1 -1
  157. package/dist/esm/types/config/html.js +1 -1
  158. package/dist/esm/types/config/output.js +1 -1
  159. package/dist/esm/types/config/performance.js +1 -1
  160. package/dist/esm/types/config/security.js +1 -1
  161. package/dist/esm/types/config/source.js +1 -1
  162. package/dist/esm/types/config/tools.js +1 -1
  163. package/dist/esm/types/hooks.js +1 -1
  164. package/dist/esm/types/legacyConfig/deploy.js +1 -1
  165. package/dist/esm/types/legacyConfig/dev.js +1 -1
  166. package/dist/esm/types/legacyConfig/index.js +1 -1
  167. package/dist/esm/types/legacyConfig/output.js +1 -1
  168. package/dist/esm/types/legacyConfig/source.js +1 -1
  169. package/dist/esm/types/legacyConfig/tools.js +1 -1
  170. package/dist/esm/types/utils.js +1 -1
  171. package/dist/esm/utils/config.js +285 -278
  172. package/dist/esm/utils/createServer.js +242 -234
  173. package/dist/esm/utils/env.js +11 -12
  174. package/dist/esm/utils/generateWatchFiles.js +205 -190
  175. package/dist/esm/utils/getSelectedEntries.js +180 -173
  176. package/dist/esm/utils/getServerInternalPlugins.js +197 -191
  177. package/dist/esm/utils/printInstructions.js +147 -141
  178. package/dist/esm/utils/restart.js +179 -173
  179. package/dist/esm/utils/routes.js +146 -139
  180. package/dist/esm/utils/types.js +1 -1
  181. package/dist/esm-node/analyze/constants.js +31 -46
  182. package/dist/esm-node/analyze/generateCode.js +28 -94
  183. package/dist/esm-node/analyze/getBundleEntry.js +8 -27
  184. package/dist/esm-node/analyze/getClientRoutes/getRoutes.js +24 -65
  185. package/dist/esm-node/analyze/getClientRoutes/getRoutesLegacy.js +24 -63
  186. package/dist/esm-node/analyze/getClientRoutes/index.js +2 -6
  187. package/dist/esm-node/analyze/getClientRoutes/utils.js +5 -19
  188. package/dist/esm-node/analyze/getFileSystemEntry.js +10 -37
  189. package/dist/esm-node/analyze/getHtmlTemplate.js +29 -59
  190. package/dist/esm-node/analyze/getServerRoutes.js +43 -75
  191. package/dist/esm-node/analyze/index.js +208 -230
  192. package/dist/esm-node/analyze/isDefaultExportFunction.js +7 -5
  193. package/dist/esm-node/analyze/makeLegalIdentifier.js +1 -4
  194. package/dist/esm-node/analyze/nestedRoutes.js +23 -43
  195. package/dist/esm-node/analyze/templates.js +22 -69
  196. package/dist/esm-node/analyze/utils.js +66 -60
  197. package/dist/esm-node/builder/builder-rspack/adapterCopy.js +40 -62
  198. package/dist/esm-node/builder/builder-rspack/index.js +4 -5
  199. package/dist/esm-node/builder/builder-webpack/adapterModern.js +24 -26
  200. package/dist/esm-node/builder/builder-webpack/createCopyPattern.js +3 -10
  201. package/dist/esm-node/builder/builder-webpack/index.js +13 -11
  202. package/dist/esm-node/builder/generator/createBuilderOptions.js +4 -5
  203. package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +5 -6
  204. package/dist/esm-node/builder/generator/getBuilderTargets.js +5 -11
  205. package/dist/esm-node/builder/generator/index.js +7 -16
  206. package/dist/esm-node/builder/index.js +2 -7
  207. package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.js +2 -6
  208. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +34 -60
  209. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +54 -74
  210. package/dist/esm-node/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +23 -8
  211. package/dist/esm-node/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +25 -16
  212. package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +66 -76
  213. package/dist/esm-node/builder/shared/createCopyInfo.js +2 -8
  214. package/dist/esm-node/builder/shared/loaders/serverModuleLoader.js +1 -4
  215. package/dist/esm-node/builder/shared/types.js +1 -0
  216. package/dist/esm-node/commands/build.js +7 -9
  217. package/dist/esm-node/commands/deploy.js +1 -4
  218. package/dist/esm-node/commands/dev.js +19 -27
  219. package/dist/esm-node/commands/inspect.js +3 -8
  220. package/dist/esm-node/commands/serve.js +7 -25
  221. package/dist/esm-node/config/default.js +20 -11
  222. package/dist/esm-node/config/initialize/index.js +1 -4
  223. package/dist/esm-node/config/initialize/inits.js +48 -54
  224. package/dist/esm-node/config/legacy/createHtmlConfig.js +2 -19
  225. package/dist/esm-node/config/legacy/createOutputConfig.js +2 -30
  226. package/dist/esm-node/config/legacy/createSourceConfig.js +2 -17
  227. package/dist/esm-node/config/legacy/createToolsConfig.js +2 -22
  228. package/dist/esm-node/config/legacy/index.js +3 -18
  229. package/dist/esm-node/defineConfig.js +7 -13
  230. package/dist/esm-node/exports/server.js +1 -4
  231. package/dist/esm-node/hooks.js +2 -9
  232. package/dist/esm-node/index.js +95 -120
  233. package/dist/esm-node/initialize/index.js +74 -87
  234. package/dist/esm-node/locale/en.js +7 -6
  235. package/dist/esm-node/locale/index.js +5 -5
  236. package/dist/esm-node/locale/zh.js +7 -6
  237. package/dist/esm-node/schema/Schema.js +19 -8
  238. package/dist/esm-node/schema/index.js +121 -36
  239. package/dist/esm-node/schema/legacy.js +262 -72
  240. package/dist/esm-node/types/config/deploy.js +1 -0
  241. package/dist/esm-node/types/config/dev.js +1 -0
  242. package/dist/esm-node/types/config/experiments.js +1 -0
  243. package/dist/esm-node/types/config/html.js +1 -0
  244. package/dist/esm-node/types/config/output.js +1 -0
  245. package/dist/esm-node/types/config/performance.js +1 -0
  246. package/dist/esm-node/types/config/security.js +1 -0
  247. package/dist/esm-node/types/config/source.js +1 -0
  248. package/dist/esm-node/types/config/tools.js +1 -0
  249. package/dist/esm-node/types/hooks.js +1 -0
  250. package/dist/esm-node/types/legacyConfig/deploy.js +1 -0
  251. package/dist/esm-node/types/legacyConfig/dev.js +1 -0
  252. package/dist/esm-node/types/legacyConfig/index.js +1 -0
  253. package/dist/esm-node/types/legacyConfig/output.js +1 -0
  254. package/dist/esm-node/types/legacyConfig/source.js +1 -0
  255. package/dist/esm-node/types/legacyConfig/tools.js +1 -0
  256. package/dist/esm-node/types/utils.js +1 -0
  257. package/dist/esm-node/utils/config.js +16 -47
  258. package/dist/esm-node/utils/createServer.js +5 -12
  259. package/dist/esm-node/utils/env.js +1 -4
  260. package/dist/esm-node/utils/generateWatchFiles.js +13 -20
  261. package/dist/esm-node/utils/getSelectedEntries.js +2 -9
  262. package/dist/esm-node/utils/getServerInternalPlugins.js +2 -8
  263. package/dist/esm-node/utils/printInstructions.js +1 -4
  264. package/dist/esm-node/utils/restart.js +2 -11
  265. package/dist/esm-node/utils/routes.js +4 -4
  266. package/dist/esm-node/utils/types.js +1 -0
  267. package/dist/types/index.d.ts +2 -2
  268. package/package.json +29 -25
@@ -1,18 +1,11 @@
1
1
  import path from "path";
2
2
  import { fs } from "@modern-js/utils";
3
3
  import { makeLegalIdentifier } from "../makeLegalIdentifier";
4
- import {
5
- FILE_SYSTEM_ROUTES_COMPONENTS_DIR,
6
- FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP,
7
- FILE_SYSTEM_ROUTES_INDEX,
8
- FILE_SYSTEM_ROUTES_LAYOUT
9
- } from "../constants";
4
+ import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
10
5
  import { replaceWithAlias } from "../utils";
11
6
  import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
12
7
  const compName = (srcDirectory, filePath) => {
13
- const legalCompName = makeLegalIdentifier(
14
- path.relative(srcDirectory, filePath)
15
- );
8
+ const legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
16
9
  return `Comp_${legalCompName}`;
17
10
  };
18
11
  const layoutNameAbbr = (filePath) => {
@@ -21,13 +14,7 @@ const layoutNameAbbr = (filePath) => {
21
14
  return `${prefix}${makeLegalIdentifier(dirName)}`;
22
15
  };
23
16
  const parents = [];
24
- const recursiveReadDir = ({
25
- dir,
26
- routes,
27
- basePath = "/",
28
- srcDirectory,
29
- srcAlias
30
- }) => {
17
+ const recursiveReadDir = ({ dir, routes, basePath = "/", srcDirectory, srcAlias }) => {
31
18
  let hasDynamicRoute = false;
32
19
  let resetParent = false;
33
20
  let parent = parents[parents.length - 1];
@@ -62,9 +49,7 @@ const recursiveReadDir = ({
62
49
  const dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
63
50
  if (dynamicRouteMatched) {
64
51
  if (hasDynamicRoute) {
65
- throw new Error(
66
- `Can't set two dynamic route in one directory: ${dir}`
67
- );
52
+ throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
68
53
  } else {
69
54
  hasDynamicRoute = true;
70
55
  }
@@ -103,12 +88,9 @@ const recursiveReadDir = ({
103
88
  }
104
89
  };
105
90
  const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
106
- const flat = (routes) => routes.reduce(
107
- (memo, route) => memo.concat(
108
- Array.isArray(route.children) ? flat(route.children) : [route]
109
- ),
110
- []
111
- );
91
+ const flat = (routes) => routes.reduce((memo, route) => memo.concat(Array.isArray(route.children) ? flat(route.children) : [
92
+ route
93
+ ]), []);
112
94
  const generate = (route) => {
113
95
  const codes = [];
114
96
  let lastComponent = route.component;
@@ -121,50 +103,38 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
121
103
  const layoutComponentAbbr = layoutNameAbbr(route._component);
122
104
  imports.push(`import ${layoutComponent} from '${route._component}';`);
123
105
  const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
124
- codes.push(
125
- `const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`
126
- );
106
+ codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
127
107
  lastComponent = currentComponent;
128
108
  }
129
109
  const file = path.resolve(internalComponentsDir, `${lastComponent}.jsx`);
130
- fs.outputFileSync(
131
- file,
132
- `${imports.join("\n")}
133
- ${codes.join(
134
- "\n"
135
- )}
136
- export default ${lastComponent}`
137
- );
110
+ fs.outputFileSync(file, `${imports.join("\n")}
111
+ ${codes.join("\n")}
112
+ export default ${lastComponent}`);
138
113
  return {
139
114
  component: lastComponent,
140
115
  _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
141
116
  };
142
117
  };
143
- const normalized = flat(nested).map(
144
- (route) => route.parent ? { ...route, ...generate(route), parent: void 0 } : { ...route, parent: void 0 }
145
- );
118
+ const normalized = flat(nested).map((route) => route.parent ? {
119
+ ...route,
120
+ ...generate(route),
121
+ parent: void 0
122
+ } : {
123
+ ...route,
124
+ parent: void 0
125
+ });
146
126
  return normalized;
147
127
  };
148
- const getClientRoutes = ({
149
- entrypoint,
150
- srcDirectory,
151
- srcAlias,
152
- internalDirectory,
153
- internalDirAlias
154
- }) => {
128
+ export const getClientRoutes = ({ entrypoint, srcDirectory, srcAlias, internalDirectory, internalDirAlias }) => {
155
129
  const { entryName, pageRoutesEntry } = entrypoint;
156
130
  if (!pageRoutesEntry) {
157
131
  return [];
158
132
  }
159
133
  if (!fs.existsSync(pageRoutesEntry)) {
160
- throw new Error(
161
- `generate file system routes error, ${pageRoutesEntry} directory not found.`
162
- );
134
+ throw new Error(`generate file system routes error, ${pageRoutesEntry} directory not found.`);
163
135
  }
164
136
  if (!(fs.existsSync(pageRoutesEntry) && fs.statSync(pageRoutesEntry).isDirectory())) {
165
- throw new Error(
166
- `generate file system routes error, ${pageRoutesEntry} should be directory.`
167
- );
137
+ throw new Error(`generate file system routes error, ${pageRoutesEntry} should be directory.`);
168
138
  }
169
139
  let routes = [];
170
140
  recursiveReadDir({
@@ -174,17 +144,9 @@ const getClientRoutes = ({
174
144
  srcDirectory,
175
145
  srcAlias
176
146
  });
177
- const internalComponentsDir = path.resolve(
178
- internalDirectory,
179
- `${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`
180
- );
147
+ const internalComponentsDir = path.resolve(internalDirectory, `${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
181
148
  fs.emptyDirSync(internalComponentsDir);
182
- routes = normalizeNestedRoutes(
183
- routes,
184
- internalComponentsDir,
185
- internalDirectory,
186
- internalDirAlias
187
- );
149
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
188
150
  parents.length = 0;
189
151
  routes.sort((a, b) => {
190
152
  const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
@@ -196,6 +158,3 @@ const getClientRoutes = ({
196
158
  debug(`fileSystem routes: %o`, routes);
197
159
  return routes;
198
160
  };
199
- export {
200
- getClientRoutes
201
- };
@@ -1,18 +1,11 @@
1
1
  import path from "path";
2
2
  import { fs } from "@modern-js/utils";
3
3
  import { makeLegalIdentifier } from "../makeLegalIdentifier";
4
- import {
5
- FILE_SYSTEM_ROUTES_COMPONENTS_DIR,
6
- FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP,
7
- FILE_SYSTEM_ROUTES_INDEX,
8
- FILE_SYSTEM_ROUTES_LAYOUT
9
- } from "../constants";
4
+ import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
10
5
  import { replaceWithAlias } from "../utils";
11
6
  import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
12
7
  const compName = (srcDirectory, filePath) => {
13
- const legalCompName = makeLegalIdentifier(
14
- path.relative(srcDirectory, filePath)
15
- );
8
+ const legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
16
9
  return `Comp_${legalCompName}`;
17
10
  };
18
11
  const layoutNameAbbr = (filePath) => {
@@ -21,13 +14,7 @@ const layoutNameAbbr = (filePath) => {
21
14
  return `${prefix}${makeLegalIdentifier(dirName)}`;
22
15
  };
23
16
  const parents = [];
24
- const recursiveReadDirLegacy = ({
25
- dir,
26
- routes,
27
- basePath = "/",
28
- srcDirectory,
29
- srcAlias
30
- }) => {
17
+ const recursiveReadDirLegacy = ({ dir, routes, basePath = "/", srcDirectory, srcAlias }) => {
31
18
  let hasDynamicRoute = false;
32
19
  let resetParent = false;
33
20
  let parent = parents[parents.length - 1];
@@ -62,9 +49,7 @@ const recursiveReadDirLegacy = ({
62
49
  const dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
63
50
  if (dynamicRouteMatched) {
64
51
  if (hasDynamicRoute) {
65
- throw new Error(
66
- `Can't set two dynamic route in one directory: ${dir}`
67
- );
52
+ throw new Error(`Can't set two dynamic route in one directory: ${dir}`);
68
53
  } else {
69
54
  hasDynamicRoute = true;
70
55
  }
@@ -104,10 +89,9 @@ const recursiveReadDirLegacy = ({
104
89
  }
105
90
  };
106
91
  const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory, internalDirAlias) => {
107
- const flat = (routes) => routes.reduce(
108
- (memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [route]),
109
- []
110
- );
92
+ const flat = (routes) => routes.reduce((memo, route) => memo.concat(Array.isArray(route.routes) ? flat(route.routes) : [
93
+ route
94
+ ]), []);
111
95
  const generate = (route) => {
112
96
  const codes = [];
113
97
  let lastComponent = route.component;
@@ -120,47 +104,35 @@ const normalizeNestedRoutes = (nested, internalComponentsDir, internalDirectory,
120
104
  const layoutComponentAbbr = layoutNameAbbr(route._component);
121
105
  imports.push(`import ${layoutComponent} from '${route._component}';`);
122
106
  const currentComponent = `${layoutComponentAbbr}_${lastComponent}`;
123
- codes.push(
124
- `const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`
125
- );
107
+ codes.push(`const ${currentComponent} = props => <${layoutComponent} Component={${lastComponent}} {...props} />;`);
126
108
  lastComponent = currentComponent;
127
109
  }
128
110
  const file = path.resolve(internalComponentsDir, `${lastComponent}.jsx`);
129
- fs.outputFileSync(
130
- file,
131
- `${imports.join("\n")}
132
- ${codes.join(
133
- "\n"
134
- )}
135
- export default ${lastComponent}`
136
- );
111
+ fs.outputFileSync(file, `${imports.join("\n")}
112
+ ${codes.join("\n")}
113
+ export default ${lastComponent}`);
137
114
  return {
138
115
  component: lastComponent,
139
116
  _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
140
117
  };
141
118
  };
142
- const normalized = flat(nested).map(
143
- (route) => route.parent ? { ...route, ...generate(route), parent: void 0 } : { ...route, parent: void 0 }
144
- );
119
+ const normalized = flat(nested).map((route) => route.parent ? {
120
+ ...route,
121
+ ...generate(route),
122
+ parent: void 0
123
+ } : {
124
+ ...route,
125
+ parent: void 0
126
+ });
145
127
  return normalized;
146
128
  };
147
- const getClientRoutes = ({
148
- entrypoint,
149
- srcDirectory,
150
- srcAlias,
151
- internalDirectory,
152
- internalDirAlias
153
- }) => {
129
+ export const getClientRoutes = ({ entrypoint, srcDirectory, srcAlias, internalDirectory, internalDirAlias }) => {
154
130
  const { entry, entryName } = entrypoint;
155
131
  if (!fs.existsSync(entry)) {
156
- throw new Error(
157
- `generate file system routes error, ${entry} directory not found.`
158
- );
132
+ throw new Error(`generate file system routes error, ${entry} directory not found.`);
159
133
  }
160
134
  if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
161
- throw new Error(
162
- `generate file system routes error, ${entry} should be directory.`
163
- );
135
+ throw new Error(`generate file system routes error, ${entry} should be directory.`);
164
136
  }
165
137
  let routes = [];
166
138
  recursiveReadDirLegacy({
@@ -170,17 +142,9 @@ const getClientRoutes = ({
170
142
  srcDirectory,
171
143
  srcAlias
172
144
  });
173
- const internalComponentsDir = path.resolve(
174
- internalDirectory,
175
- `${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`
176
- );
145
+ const internalComponentsDir = path.resolve(internalDirectory, `${entryName}/${FILE_SYSTEM_ROUTES_COMPONENTS_DIR}`);
177
146
  fs.emptyDirSync(internalComponentsDir);
178
- routes = normalizeNestedRoutes(
179
- routes,
180
- internalComponentsDir,
181
- internalDirectory,
182
- internalDirAlias
183
- );
147
+ routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
184
148
  parents.length = 0;
185
149
  routes.sort((a, b) => {
186
150
  const delta = getRouteWeight(a.path) - getRouteWeight(b.path);
@@ -192,6 +156,3 @@ const getClientRoutes = ({
192
156
  debug(`fileSystem routes: %o`, routes);
193
157
  return routes;
194
158
  };
195
- export {
196
- getClientRoutes
197
- };
@@ -1,6 +1,2 @@
1
- import { getClientRoutes } from "./getRoutes";
2
- import { getClientRoutes as getClientRoutes2 } from "./getRoutesLegacy";
3
- export {
4
- getClientRoutes,
5
- getClientRoutes2 as getClientRoutesLegacy
6
- };
1
+ export { getClientRoutes } from "./getRoutes";
2
+ export { getClientRoutes as getClientRoutesLegacy } from "./getRoutesLegacy";
@@ -1,19 +1,11 @@
1
1
  import path from "path";
2
2
  import { createDebugger, findExists, fs } from "@modern-js/utils";
3
- import {
4
- JS_EXTENSIONS,
5
- FILE_SYSTEM_ROUTES_LAYOUT,
6
- FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT,
7
- FILE_SYSTEM_ROUTES_IGNORED_REGEX
8
- } from "../constants";
3
+ import { JS_EXTENSIONS, FILE_SYSTEM_ROUTES_LAYOUT, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, FILE_SYSTEM_ROUTES_IGNORED_REGEX } from "../constants";
9
4
  const debug = createDebugger("get-client-routes");
10
- const findLayout = (dir) => findExists(
11
- JS_EXTENSIONS.map(
12
- (ext) => path.resolve(dir, `${FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)
13
- )
14
- );
15
- const getRouteWeight = (route) => route === "*" ? 999 : route.split(":").length - 1;
16
- const shouldSkip = (file) => {
5
+ export { debug };
6
+ export const findLayout = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${FILE_SYSTEM_ROUTES_LAYOUT}${ext}`)));
7
+ export const getRouteWeight = (route) => route === "*" ? 999 : route.split(":").length - 1;
8
+ export const shouldSkip = (file) => {
17
9
  if (fs.statSync(file).isDirectory()) {
18
10
  return false;
19
11
  }
@@ -23,9 +15,3 @@ const shouldSkip = (file) => {
23
15
  }
24
16
  return false;
25
17
  };
26
- export {
27
- debug,
28
- findLayout,
29
- getRouteWeight,
30
- shouldSkip
31
- };
@@ -2,20 +2,9 @@ import fs from "fs";
2
2
  import path from "path";
3
3
  import { findExists, ensureAbsolutePath } from "@modern-js/utils";
4
4
  import { isDefaultExportFunction } from "./isDefaultExportFunction";
5
- import {
6
- JS_EXTENSIONS,
7
- INDEX_FILE_NAME,
8
- APP_FILE_NAME,
9
- PAGES_DIR_NAME,
10
- FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT,
11
- NESTED_ROUTES_DIR
12
- } from "./constants";
13
- const hasIndex = (dir) => findExists(
14
- JS_EXTENSIONS.map((ext) => path.resolve(dir, `${INDEX_FILE_NAME}${ext}`))
15
- );
16
- const hasApp = (dir) => findExists(
17
- JS_EXTENSIONS.map((ext) => path.resolve(dir, `${APP_FILE_NAME}${ext}`))
18
- );
5
+ import { JS_EXTENSIONS, INDEX_FILE_NAME, APP_FILE_NAME, PAGES_DIR_NAME, FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT, NESTED_ROUTES_DIR } from "./constants";
6
+ const hasIndex = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${INDEX_FILE_NAME}${ext}`)));
7
+ const hasApp = (dir) => findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `${APP_FILE_NAME}${ext}`)));
19
8
  const hasPages = (dir) => fs.existsSync(path.join(dir, PAGES_DIR_NAME));
20
9
  const hasNestedRoutes = (dir) => fs.existsSync(path.join(dir, NESTED_ROUTES_DIR));
21
10
  const isBundleEntry = (dir) => hasApp(dir) || hasPages(dir) || hasIndex(dir) || hasNestedRoutes(dir);
@@ -48,14 +37,7 @@ const scanDir = (dirs) => dirs.map((dir) => {
48
37
  entryName,
49
38
  entry: "",
50
39
  fileSystemRoutes: {
51
- globalApp: findExists(
52
- JS_EXTENSIONS.map(
53
- (ext) => path.resolve(
54
- dir,
55
- `./${PAGES_DIR_NAME}/${FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT}${ext}`
56
- )
57
- )
58
- )
40
+ globalApp: findExists(JS_EXTENSIONS.map((ext) => path.resolve(dir, `./${PAGES_DIR_NAME}/${FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT}${ext}`)))
59
41
  },
60
42
  isAutoMount: true,
61
43
  absoluteEntryDir: path.resolve(dir),
@@ -78,25 +60,19 @@ const scanDir = (dirs) => dirs.map((dir) => {
78
60
  isAutoMount: false
79
61
  };
80
62
  });
81
- const getFileSystemEntry = (appContext, config) => {
63
+ export const getFileSystemEntry = (appContext, config) => {
82
64
  const { appDirectory } = appContext;
83
- const {
84
- source: { entriesDir, disableEntryDirs }
85
- } = config;
65
+ const { source: { entriesDir, disableEntryDirs } } = config;
86
66
  let disabledDirs = [];
87
67
  if (disableEntryDirs && Array.isArray(disableEntryDirs)) {
88
- disabledDirs = disableEntryDirs == null ? void 0 : disableEntryDirs.map(
89
- (dir) => ensureAbsolutePath(appDirectory, dir)
90
- );
68
+ disabledDirs = disableEntryDirs === null || disableEntryDirs === void 0 ? void 0 : disableEntryDirs.map((dir) => ensureAbsolutePath(appDirectory, dir));
91
69
  }
92
70
  const src = ensureAbsolutePath(appDirectory, entriesDir || "");
93
71
  if (fs.existsSync(src)) {
94
72
  if (fs.statSync(src).isDirectory()) {
95
- return scanDir(
96
- isBundleEntry(src) ? [src] : fs.readdirSync(src).map((file) => path.join(src, file)).filter(
97
- (file) => fs.statSync(file).isDirectory() && isBundleEntry(file) && !disabledDirs.includes(file)
98
- )
99
- );
73
+ return scanDir(isBundleEntry(src) ? [
74
+ src
75
+ ] : fs.readdirSync(src).map((file) => path.join(src, file)).filter((file) => fs.statSync(file).isDirectory() && isBundleEntry(file) && !disabledDirs.includes(file)));
100
76
  } else {
101
77
  throw Error(`source.entriesDir accept a directory.`);
102
78
  }
@@ -104,6 +80,3 @@ const getFileSystemEntry = (appContext, config) => {
104
80
  throw Error(`src dir ${entriesDir} not found.`);
105
81
  }
106
82
  };
107
- export {
108
- getFileSystemEntry
109
- };
@@ -2,52 +2,34 @@ import path from "path";
2
2
  import { fs, findExists, MAIN_ENTRY_NAME } from "@modern-js/utils";
3
3
  import { HTML_PARTIALS_EXTENSIONS, HTML_PARTIALS_FOLDER } from "./constants";
4
4
  import * as templates from "./templates";
5
- var PartialPosition = /* @__PURE__ */ ((PartialPosition2) => {
5
+ var PartialPosition;
6
+ (function(PartialPosition2) {
6
7
  PartialPosition2["TOP"] = "top";
7
8
  PartialPosition2["HEAD"] = "head";
8
9
  PartialPosition2["BODY"] = "body";
9
10
  PartialPosition2["BOTTOM"] = "bottom";
10
11
  PartialPosition2["INDEX"] = "index";
11
- return PartialPosition2;
12
- })(PartialPosition || {});
12
+ })(PartialPosition || (PartialPosition = {}));
13
13
  const findPartials = (dir, entryName, position) => {
14
14
  if (fs.existsSync(dir)) {
15
- const base = findExists(
16
- HTML_PARTIALS_EXTENSIONS.map(
17
- (ext) => path.resolve(dir, `${position}${ext}`)
18
- )
19
- );
20
- const file = entryName ? findExists(
21
- HTML_PARTIALS_EXTENSIONS.map(
22
- (ext) => path.resolve(dir, entryName, `${position}${ext}`)
23
- )
24
- ) || base : base;
25
- return file ? { file, content: fs.readFileSync(file, "utf8") } : null;
15
+ const base = findExists(HTML_PARTIALS_EXTENSIONS.map((ext) => path.resolve(dir, `${position}${ext}`)));
16
+ const file = entryName ? findExists(HTML_PARTIALS_EXTENSIONS.map((ext) => path.resolve(dir, entryName, `${position}${ext}`))) || base : base;
17
+ return file ? {
18
+ file,
19
+ content: fs.readFileSync(file, "utf8")
20
+ } : null;
26
21
  }
27
22
  return null;
28
23
  };
29
- const getHtmlTemplate = async (entrypoints, api, {
30
- appContext,
31
- config
32
- }) => {
24
+ export const getHtmlTemplate = async (entrypoints, api, { appContext, config }) => {
33
25
  const { appDirectory, internalDirectory } = appContext;
34
- const {
35
- source: { configDir }
36
- } = config;
37
- const htmlDir = path.resolve(
38
- appDirectory,
39
- configDir || "",
40
- HTML_PARTIALS_FOLDER
41
- );
26
+ const { source: { configDir } } = config;
27
+ const htmlDir = path.resolve(appDirectory, configDir || "", HTML_PARTIALS_FOLDER);
42
28
  const htmlTemplates = {};
43
29
  for (const entrypoint of entrypoints) {
44
30
  const { entryName } = entrypoint;
45
31
  const name = entrypoints.length === 1 && entryName === MAIN_ENTRY_NAME ? "" : entryName;
46
- const customIndexTemplate = findPartials(
47
- htmlDir,
48
- name,
49
- "index" /* INDEX */
50
- );
32
+ const customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
51
33
  if (customIndexTemplate) {
52
34
  htmlTemplates[entryName] = customIndexTemplate.file;
53
35
  } else {
@@ -55,34 +37,25 @@ const getHtmlTemplate = async (entrypoints, api, {
55
37
  const { partials } = await hookRunners.htmlPartials({
56
38
  entrypoint,
57
39
  partials: [
58
- "top" /* TOP */,
59
- "head" /* HEAD */,
60
- "body" /* BODY */
61
- ].reduce(
62
- (previous, position) => {
63
- const found = findPartials(htmlDir, name, position);
64
- previous[position] = found ? [found.content] : [];
65
- return previous;
66
- },
67
- {
68
- top: [],
69
- head: [],
70
- body: []
71
- }
72
- )
40
+ PartialPosition.TOP,
41
+ PartialPosition.HEAD,
42
+ PartialPosition.BODY
43
+ ].reduce((previous, position) => {
44
+ const found = findPartials(htmlDir, name, position);
45
+ previous[position] = found ? [
46
+ found.content
47
+ ] : [];
48
+ return previous;
49
+ }, {
50
+ top: [],
51
+ head: [],
52
+ body: []
53
+ })
73
54
  });
74
- const templatePath = path.resolve(
75
- internalDirectory,
76
- entryName,
77
- "index.html"
78
- );
55
+ const templatePath = path.resolve(internalDirectory, entryName, "index.html");
79
56
  fs.outputFileSync(templatePath, templates.html(partials), "utf8");
80
57
  htmlTemplates[entryName] = templatePath;
81
- const bottomTemplate = findPartials(
82
- htmlDir,
83
- name,
84
- "bottom" /* BOTTOM */
85
- );
58
+ const bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
86
59
  if (bottomTemplate) {
87
60
  htmlTemplates[`__${entryName}-bottom__`] = bottomTemplate.content;
88
61
  }
@@ -90,6 +63,3 @@ const getHtmlTemplate = async (entrypoints, api, {
90
63
  }
91
64
  return htmlTemplates;
92
65
  };
93
- export {
94
- getHtmlTemplate
95
- };