@modern-js/app-tools 2.0.0-beta.3 → 2.0.0-beta.6

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 (261) hide show
  1. package/CHANGELOG.md +619 -0
  2. package/bin/modern.js +3 -0
  3. package/dist/js/modern/analyze/Builder.js +39 -0
  4. package/dist/js/modern/analyze/constants.js +48 -18
  5. package/dist/js/modern/analyze/generateCode.js +264 -225
  6. package/dist/js/modern/analyze/getBundleEntry.js +34 -32
  7. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +99 -52
  8. package/dist/js/modern/analyze/getClientRoutes/getRoutesLegacy.js +89 -47
  9. package/dist/js/modern/analyze/getClientRoutes/index.js +6 -2
  10. package/dist/js/modern/analyze/getClientRoutes/utils.js +23 -10
  11. package/dist/js/modern/analyze/getFileSystemEntry.js +52 -23
  12. package/dist/js/modern/analyze/getHtmlTemplate.js +89 -49
  13. package/dist/js/modern/analyze/getServerRoutes.js +122 -126
  14. package/dist/js/modern/analyze/index.js +204 -161
  15. package/dist/js/modern/analyze/isDefaultExportFunction.js +30 -16
  16. package/dist/js/modern/analyze/makeLegalIdentifier.js +10 -11
  17. package/dist/js/modern/analyze/nestedRoutes.js +118 -65
  18. package/dist/js/modern/analyze/templates.js +170 -81
  19. package/dist/js/modern/analyze/utils.js +104 -67
  20. package/dist/js/modern/builder/builderPlugins/compatModern.js +128 -109
  21. package/dist/js/modern/builder/index.js +120 -89
  22. package/dist/js/modern/builder/loaders/routerLoader.js +9 -12
  23. package/dist/js/modern/builder/loaders/serverModuleLoader.js +4 -1
  24. package/dist/js/modern/builder/share.js +23 -21
  25. package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +23 -22
  26. package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +31 -27
  27. package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +100 -84
  28. package/dist/js/modern/commands/build.js +67 -42
  29. package/dist/js/modern/commands/deploy.js +27 -4
  30. package/dist/js/modern/commands/dev.js +81 -34
  31. package/dist/js/modern/commands/index.js +1 -1
  32. package/dist/js/modern/commands/inspect.js +30 -5
  33. package/dist/js/modern/commands/serve.js +54 -0
  34. package/dist/js/modern/config/default.js +112 -145
  35. package/dist/js/modern/config/index.js +8 -2
  36. package/dist/js/modern/config/initial/createHtmlConfig.js +5 -2
  37. package/dist/js/modern/config/initial/createOutputConfig.js +10 -13
  38. package/dist/js/modern/config/initial/createSourceConfig.js +10 -3
  39. package/dist/js/modern/config/initial/createToolsConfig.js +7 -6
  40. package/dist/js/modern/config/initial/index.js +9 -4
  41. package/dist/js/modern/config/initial/inits.js +109 -73
  42. package/dist/js/modern/config/initial/transformNormalizedConfig.js +11 -4
  43. package/dist/js/modern/defineConfig.js +26 -11
  44. package/dist/js/modern/exports/server.js +4 -1
  45. package/dist/js/modern/hooks.js +16 -4
  46. package/dist/js/modern/index.js +178 -90
  47. package/dist/js/modern/initialize/index.js +100 -52
  48. package/dist/js/modern/locale/en.js +20 -21
  49. package/dist/js/modern/locale/index.js +6 -6
  50. package/dist/js/modern/locale/zh.js +21 -22
  51. package/dist/js/modern/schema/Schema.js +6 -5
  52. package/dist/js/modern/schema/index.js +53 -100
  53. package/dist/js/modern/schema/legacy.js +96 -231
  54. package/dist/js/modern/types/config/index.js +0 -1
  55. package/dist/js/modern/types/index.js +0 -1
  56. package/dist/js/modern/types/legacyConfig/output.js +0 -1
  57. package/dist/js/modern/utils/commands.js +10 -2
  58. package/dist/js/modern/utils/config.js +102 -41
  59. package/dist/js/modern/utils/createFileWatcher.js +84 -51
  60. package/dist/js/modern/utils/createServer.js +63 -17
  61. package/dist/js/modern/utils/getServerInternalPlugins.js +58 -0
  62. package/dist/js/modern/utils/getSpecifiedEntries.js +46 -19
  63. package/dist/js/modern/utils/language.js +6 -3
  64. package/dist/js/modern/utils/printInstructions.js +27 -8
  65. package/dist/js/modern/utils/restart.js +43 -16
  66. package/dist/js/modern/utils/routes.js +29 -12
  67. package/dist/js/node/analyze/Builder.js +64 -0
  68. package/dist/js/node/analyze/constants.js +86 -39
  69. package/dist/js/node/analyze/generateCode.js +285 -239
  70. package/dist/js/node/analyze/getBundleEntry.js +62 -44
  71. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +138 -77
  72. package/dist/js/node/analyze/getClientRoutes/getRoutesLegacy.js +130 -74
  73. package/dist/js/node/analyze/getClientRoutes/index.js +27 -16
  74. package/dist/js/node/analyze/getClientRoutes/utils.js +51 -21
  75. package/dist/js/node/analyze/getFileSystemEntry.js +82 -39
  76. package/dist/js/node/analyze/getHtmlTemplate.js +120 -61
  77. package/dist/js/node/analyze/getServerRoutes.js +145 -137
  78. package/dist/js/node/analyze/index.js +233 -174
  79. package/dist/js/node/analyze/isDefaultExportFunction.js +59 -26
  80. package/dist/js/node/analyze/makeLegalIdentifier.js +31 -15
  81. package/dist/js/node/analyze/nestedRoutes.js +150 -79
  82. package/dist/js/node/analyze/templates.js +198 -90
  83. package/dist/js/node/analyze/utils.js +136 -81
  84. package/dist/js/node/builder/builderPlugins/compatModern.js +158 -122
  85. package/dist/js/node/builder/index.js +142 -98
  86. package/dist/js/node/builder/loaders/routerLoader.js +29 -18
  87. package/dist/js/node/builder/loaders/serverModuleLoader.js +24 -7
  88. package/dist/js/node/builder/share.js +50 -26
  89. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +43 -26
  90. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +51 -31
  91. package/dist/js/node/builder/webpackPlugins/routerPlugin.js +120 -90
  92. package/dist/js/node/commands/build.js +91 -52
  93. package/dist/js/node/commands/deploy.js +49 -10
  94. package/dist/js/node/commands/dev.js +105 -48
  95. package/dist/js/node/commands/index.js +19 -38
  96. package/dist/js/node/commands/inspect.js +52 -11
  97. package/dist/js/node/commands/serve.js +83 -0
  98. package/dist/js/node/config/default.js +131 -149
  99. package/dist/js/node/config/index.js +31 -28
  100. package/dist/js/node/config/initial/createHtmlConfig.js +26 -6
  101. package/dist/js/node/config/initial/createOutputConfig.js +31 -17
  102. package/dist/js/node/config/initial/createSourceConfig.js +31 -7
  103. package/dist/js/node/config/initial/createToolsConfig.js +28 -10
  104. package/dist/js/node/config/initial/index.js +34 -17
  105. package/dist/js/node/config/initial/inits.js +130 -81
  106. package/dist/js/node/config/initial/transformNormalizedConfig.js +40 -16
  107. package/dist/js/node/defineConfig.js +46 -17
  108. package/dist/js/node/exports/server.js +25 -10
  109. package/dist/js/node/hooks.js +53 -29
  110. package/dist/js/node/index.js +213 -129
  111. package/dist/js/node/initialize/index.js +117 -61
  112. package/dist/js/node/locale/en.js +40 -25
  113. package/dist/js/node/locale/index.js +31 -14
  114. package/dist/js/node/locale/zh.js +41 -26
  115. package/dist/js/node/schema/Schema.js +27 -10
  116. package/dist/js/node/schema/index.js +84 -114
  117. package/dist/js/node/schema/legacy.js +119 -240
  118. package/dist/js/node/types/config/deploy.js +15 -0
  119. package/dist/js/node/types/config/dev.js +15 -0
  120. package/dist/js/node/types/config/experiments.js +15 -0
  121. package/dist/js/node/types/config/html.js +15 -0
  122. package/dist/js/node/types/config/index.js +17 -16
  123. package/dist/js/node/types/config/output.js +15 -0
  124. package/dist/js/node/types/config/performance.js +15 -0
  125. package/dist/js/node/types/config/security.js +15 -0
  126. package/dist/js/node/types/config/source.js +15 -0
  127. package/dist/js/node/types/config/tools.js +15 -0
  128. package/dist/js/node/types/hooks.js +15 -0
  129. package/dist/js/node/types/index.js +19 -38
  130. package/dist/js/node/types/legacyConfig/deploy.js +15 -0
  131. package/dist/js/node/types/legacyConfig/dev.js +15 -0
  132. package/dist/js/node/types/legacyConfig/index.js +15 -0
  133. package/dist/js/node/types/legacyConfig/output.js +15 -5
  134. package/dist/js/node/types/legacyConfig/source.js +15 -0
  135. package/dist/js/node/types/legacyConfig/tools.js +15 -0
  136. package/dist/js/node/utils/commands.js +31 -6
  137. package/dist/js/node/utils/config.js +125 -51
  138. package/dist/js/node/utils/createFileWatcher.js +109 -61
  139. package/dist/js/node/utils/createServer.js +92 -25
  140. package/dist/js/node/utils/getServerInternalPlugins.js +79 -0
  141. package/dist/js/node/utils/getSpecifiedEntries.js +68 -25
  142. package/dist/js/node/utils/language.js +28 -8
  143. package/dist/js/node/utils/printInstructions.js +51 -16
  144. package/dist/js/node/utils/restart.js +65 -21
  145. package/dist/js/node/utils/routes.js +58 -19
  146. package/dist/js/node/utils/types.js +15 -0
  147. package/dist/js/treeshaking/analyze/Builder.js +199 -0
  148. package/dist/js/treeshaking/analyze/constants.js +37 -18
  149. package/dist/js/treeshaking/analyze/generateCode.js +597 -407
  150. package/dist/js/treeshaking/analyze/getBundleEntry.js +55 -63
  151. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +217 -168
  152. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +215 -169
  153. package/dist/js/treeshaking/analyze/getClientRoutes/index.js +3 -2
  154. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +19 -20
  155. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +97 -86
  156. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +298 -125
  157. package/dist/js/treeshaking/analyze/getServerRoutes.js +210 -147
  158. package/dist/js/treeshaking/analyze/index.js +575 -305
  159. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +45 -26
  160. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +13 -16
  161. package/dist/js/treeshaking/analyze/nestedRoutes.js +419 -201
  162. package/dist/js/treeshaking/analyze/templates.js +438 -236
  163. package/dist/js/treeshaking/analyze/utils.js +367 -153
  164. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +285 -199
  165. package/dist/js/treeshaking/builder/index.js +365 -165
  166. package/dist/js/treeshaking/builder/loaders/routerLoader.js +11 -12
  167. package/dist/js/treeshaking/builder/loaders/serverModuleLoader.js +3 -2
  168. package/dist/js/treeshaking/builder/share.js +38 -44
  169. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +108 -44
  170. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +70 -39
  171. package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +334 -112
  172. package/dist/js/treeshaking/commands/build.js +286 -88
  173. package/dist/js/treeshaking/commands/deploy.js +153 -25
  174. package/dist/js/treeshaking/commands/dev.js +313 -131
  175. package/dist/js/treeshaking/commands/index.js +1 -1
  176. package/dist/js/treeshaking/commands/inspect.js +147 -32
  177. package/dist/js/treeshaking/commands/serve.js +199 -0
  178. package/dist/js/treeshaking/config/default.js +207 -198
  179. package/dist/js/treeshaking/config/index.js +3 -2
  180. package/dist/js/treeshaking/config/initial/createHtmlConfig.js +19 -31
  181. package/dist/js/treeshaking/config/initial/createOutputConfig.js +41 -70
  182. package/dist/js/treeshaking/config/initial/createSourceConfig.js +41 -40
  183. package/dist/js/treeshaking/config/initial/createToolsConfig.js +23 -38
  184. package/dist/js/treeshaking/config/initial/index.js +10 -9
  185. package/dist/js/treeshaking/config/initial/inits.js +205 -106
  186. package/dist/js/treeshaking/config/initial/transformNormalizedConfig.js +31 -34
  187. package/dist/js/treeshaking/defineConfig.js +60 -13
  188. package/dist/js/treeshaking/exports/server.js +2 -1
  189. package/dist/js/treeshaking/hooks.js +31 -25
  190. package/dist/js/treeshaking/index.js +681 -256
  191. package/dist/js/treeshaking/initialize/index.js +292 -113
  192. package/dist/js/treeshaking/locale/en.js +34 -33
  193. package/dist/js/treeshaking/locale/index.js +5 -5
  194. package/dist/js/treeshaking/locale/zh.js +34 -33
  195. package/dist/js/treeshaking/schema/Schema.js +267 -69
  196. package/dist/js/treeshaking/schema/index.js +171 -121
  197. package/dist/js/treeshaking/schema/legacy.js +323 -256
  198. package/dist/js/treeshaking/types/config/deploy.js +1 -0
  199. package/dist/js/treeshaking/types/config/dev.js +1 -0
  200. package/dist/js/treeshaking/types/config/experiments.js +1 -0
  201. package/dist/js/treeshaking/types/config/html.js +1 -0
  202. package/dist/js/treeshaking/types/config/index.js +0 -1
  203. package/dist/js/treeshaking/types/config/output.js +1 -0
  204. package/dist/js/treeshaking/types/config/performance.js +1 -0
  205. package/dist/js/treeshaking/types/config/security.js +1 -0
  206. package/dist/js/treeshaking/types/config/source.js +1 -0
  207. package/dist/js/treeshaking/types/config/tools.js +1 -0
  208. package/dist/js/treeshaking/types/hooks.js +1 -0
  209. package/dist/js/treeshaking/types/index.js +0 -1
  210. package/dist/js/treeshaking/types/legacyConfig/deploy.js +1 -0
  211. package/dist/js/treeshaking/types/legacyConfig/dev.js +1 -0
  212. package/dist/js/treeshaking/types/legacyConfig/index.js +1 -0
  213. package/dist/js/treeshaking/types/legacyConfig/output.js +1 -1
  214. package/dist/js/treeshaking/types/legacyConfig/source.js +1 -0
  215. package/dist/js/treeshaking/types/legacyConfig/tools.js +1 -0
  216. package/dist/js/treeshaking/utils/commands.js +10 -5
  217. package/dist/js/treeshaking/utils/config.js +295 -117
  218. package/dist/js/treeshaking/utils/createFileWatcher.js +278 -118
  219. package/dist/js/treeshaking/utils/createServer.js +252 -67
  220. package/dist/js/treeshaking/utils/getServerInternalPlugins.js +210 -0
  221. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +182 -55
  222. package/dist/js/treeshaking/utils/language.js +6 -5
  223. package/dist/js/treeshaking/utils/printInstructions.js +151 -29
  224. package/dist/js/treeshaking/utils/restart.js +184 -42
  225. package/dist/js/treeshaking/utils/routes.js +151 -27
  226. package/dist/js/treeshaking/utils/types.js +1 -0
  227. package/dist/types/analyze/Builder.d.ts +8 -0
  228. package/dist/types/analyze/constants.d.ts +10 -1
  229. package/dist/types/analyze/index.d.ts +2 -0
  230. package/dist/types/analyze/templates.d.ts +3 -1
  231. package/dist/types/analyze/utils.d.ts +2 -1
  232. package/dist/types/builder/builderPlugins/compatModern.d.ts +2 -1
  233. package/dist/types/builder/index.d.ts +3 -3
  234. package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +1 -7
  235. package/dist/types/commands/index.d.ts +1 -1
  236. package/dist/types/commands/{start.d.ts → serve.d.ts} +0 -0
  237. package/dist/types/defineConfig.d.ts +1 -0
  238. package/dist/types/index.d.ts +7 -1
  239. package/dist/types/initialize/index.d.ts +2 -0
  240. package/dist/types/locale/en.d.ts +1 -1
  241. package/dist/types/locale/index.d.ts +2 -2
  242. package/dist/types/locale/zh.d.ts +1 -1
  243. package/dist/types/types/config/dev.d.ts +2 -2
  244. package/dist/types/types/config/index.d.ts +4 -0
  245. package/dist/types/types/config/output.d.ts +0 -1
  246. package/dist/types/types/config/source.d.ts +2 -0
  247. package/dist/types/types/config/tools.d.ts +2 -0
  248. package/dist/types/types/hooks.d.ts +8 -0
  249. package/dist/types/types/legacyConfig/dev.d.ts +1 -0
  250. package/dist/types/types/legacyConfig/output.d.ts +1 -1
  251. package/dist/types/types/legacyConfig/source.d.ts +1 -0
  252. package/dist/types/types/legacyConfig/tools.d.ts +1 -0
  253. package/dist/types/utils/commands.d.ts +2 -1
  254. package/dist/types/utils/config.d.ts +1 -0
  255. package/dist/types/utils/createServer.d.ts +1 -0
  256. package/dist/types/utils/getServerInternalPlugins.d.ts +2 -0
  257. package/dist/types/utils/restart.d.ts +1 -1
  258. package/package.json +26 -22
  259. package/dist/js/modern/commands/start.js +0 -30
  260. package/dist/js/node/commands/start.js +0 -38
  261. package/dist/js/treeshaking/commands/start.js +0 -69
@@ -1,18 +1,48 @@
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 NESTED_ROUTES_DIR = 'routes';
6
- export const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
7
- export const LOADER_EXPORT_NAME = 'loader';
8
- export const TEMP_LOADERS_DIR = '__loaders__';
9
- export const ENTRY_POINT_FILE_NAME = 'index.js';
10
- export const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
11
- export const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
12
- export const FILE_SYSTEM_ROUTES_LAYOUT = '_layout';
13
- export const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = '_app';
14
- export const FILE_SYSTEM_ROUTES_INDEX = 'index';
15
- export const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
16
- export const HTML_PARTIALS_FOLDER = 'html';
17
- export const HTML_PARTIALS_EXTENSIONS = ['.htm', '.html', '.ejs'];
18
- export const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = 'internal_components';
1
+ const JS_EXTENSIONS = [".js", ".ts", ".jsx", ".tsx"];
2
+ const INDEX_FILE_NAME = "index";
3
+ const APP_FILE_NAME = "App";
4
+ const PAGES_DIR_NAME = "pages";
5
+ const NESTED_ROUTES_DIR = "routes";
6
+ const FILE_SYSTEM_ROUTES_FILE_NAME = "routes.js";
7
+ const LOADER_EXPORT_NAME = "loader";
8
+ const TEMP_LOADERS_DIR = "__loaders__";
9
+ const ENTRY_POINT_FILE_NAME = "index.js";
10
+ const ENTRY_BOOTSTRAP_FILE_NAME = "bootstrap.js";
11
+ const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
12
+ const FILE_SYSTEM_ROUTES_LAYOUT = "_layout";
13
+ const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = "_app";
14
+ const FILE_SYSTEM_ROUTES_INDEX = "index";
15
+ const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
16
+ const HTML_PARTIALS_FOLDER = "html";
17
+ const HTML_PARTIALS_EXTENSIONS = [".htm", ".html", ".ejs"];
18
+ const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = "internal_components";
19
+ const NESTED_ROUTE = {
20
+ LAYOUT_FILE: "layout",
21
+ LAYOUT_LOADER_FILE: "layout.loader",
22
+ PAGE_FILE: "page",
23
+ PAGE_LOADER_FILE: "page.loader",
24
+ LOADING_FILE: "loading",
25
+ ERROR_FILE: "error",
26
+ LOADER_FILE: "loader"
27
+ };
28
+ export {
29
+ APP_FILE_NAME,
30
+ ENTRY_BOOTSTRAP_FILE_NAME,
31
+ ENTRY_POINT_FILE_NAME,
32
+ FILE_SYSTEM_ROUTES_COMPONENTS_DIR,
33
+ FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP,
34
+ FILE_SYSTEM_ROUTES_FILE_NAME,
35
+ FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT,
36
+ FILE_SYSTEM_ROUTES_IGNORED_REGEX,
37
+ FILE_SYSTEM_ROUTES_INDEX,
38
+ FILE_SYSTEM_ROUTES_LAYOUT,
39
+ HTML_PARTIALS_EXTENSIONS,
40
+ HTML_PARTIALS_FOLDER,
41
+ INDEX_FILE_NAME,
42
+ JS_EXTENSIONS,
43
+ LOADER_EXPORT_NAME,
44
+ NESTED_ROUTE,
45
+ NESTED_ROUTES_DIR,
46
+ PAGES_DIR_NAME,
47
+ TEMP_LOADERS_DIR
48
+ };
@@ -1,25 +1,54 @@
1
- import path from 'path';
2
- import { fs, LOADER_ROUTES_DIR, logger } from '@modern-js/utils';
3
- import { useResolvedConfigContext } from '@modern-js/core';
4
- import esbuild from 'esbuild';
5
- import { getCommand } from "../utils/commands";
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import path from "path";
22
+ import {
23
+ fs,
24
+ getEntryOptions,
25
+ LOADER_ROUTES_DIR,
26
+ logger
27
+ } from "@modern-js/utils";
28
+ import {
29
+ useResolvedConfigContext
30
+ } from "@modern-js/core";
31
+ import { isDevCommand } from "../utils/commands";
6
32
  import * as templates from "./templates";
7
33
  import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
8
- import { FILE_SYSTEM_ROUTES_FILE_NAME, ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME, TEMP_LOADERS_DIR } from "./constants";
34
+ import {
35
+ FILE_SYSTEM_ROUTES_FILE_NAME,
36
+ ENTRY_POINT_FILE_NAME,
37
+ ENTRY_BOOTSTRAP_FILE_NAME,
38
+ TEMP_LOADERS_DIR
39
+ } from "./constants";
9
40
  import { getDefaultImports } from "./utils";
10
41
  import { walk } from "./nestedRoutes";
42
+ import { loaderBuilder, serverLoaderBuilder } from "./Builder";
11
43
  const loader = {
12
- '.js': 'jsx',
13
- '.ts': 'tsx'
44
+ ".js": "jsx",
45
+ ".ts": "tsx"
14
46
  };
15
47
  const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
16
- const createImportSpecifier = specifiers => {
17
- let defaults = '';
48
+ const createImportSpecifier = (specifiers) => {
49
+ let defaults = "";
18
50
  const named = [];
19
- for (const {
20
- local,
21
- imported
22
- } of specifiers) {
51
+ for (const { local, imported } of specifiers) {
23
52
  if (local && imported) {
24
53
  named.push(`${imported} as ${local}`);
25
54
  } else if (local) {
@@ -29,22 +58,18 @@ const createImportSpecifier = specifiers => {
29
58
  }
30
59
  }
31
60
  if (defaults && named.length) {
32
- return `${defaults}, { ${named.join(', ')} }`;
61
+ return `${defaults}, { ${named.join(", ")} }`;
33
62
  } else if (defaults) {
34
63
  return defaults;
35
64
  } else {
36
- return `{ ${named.join(', ')} }`;
65
+ return `{ ${named.join(", ")} }`;
37
66
  }
38
67
  };
39
- export const createImportStatements = statements => {
40
- // merge import statements with the same value.
68
+ const createImportStatements = (statements) => {
69
+ var _a, _b;
41
70
  const deDuplicated = [];
42
- const seen = new Map();
43
- for (const {
44
- value,
45
- specifiers,
46
- initialize
47
- } of statements) {
71
+ const seen = /* @__PURE__ */ new Map();
72
+ for (const { value, specifiers, initialize } of statements) {
48
73
  if (!seen.has(value)) {
49
74
  deDuplicated.push({
50
75
  value,
@@ -53,241 +78,255 @@ export const createImportStatements = statements => {
53
78
  });
54
79
  seen.set(value, specifiers);
55
80
  } else {
56
- var _deDuplicated$modifyI, _deDuplicated$modifyI2;
57
81
  seen.get(value).push(...specifiers);
58
- // make "initialize" param can be connected when multiple plugins were imported from same package
59
- const modifyIndex = deDuplicated.findIndex(v => v.value === value);
60
- const originInitialize = (_deDuplicated$modifyI = (_deDuplicated$modifyI2 = deDuplicated[modifyIndex]) === null || _deDuplicated$modifyI2 === void 0 ? void 0 : _deDuplicated$modifyI2.initialize) !== null && _deDuplicated$modifyI !== void 0 ? _deDuplicated$modifyI : '';
61
- deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
82
+ const modifyIndex = deDuplicated.findIndex((v) => v.value === value);
83
+ const originInitialize = (_b = (_a = deDuplicated[modifyIndex]) == null ? void 0 : _a.initialize) != null ? _b : "";
84
+ deDuplicated[modifyIndex].initialize = originInitialize.concat(
85
+ `
86
+ ${initialize || ""}`
87
+ );
62
88
  }
63
89
  }
64
- return deDuplicated.map(({
65
- value,
66
- specifiers,
67
- initialize
68
- }) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
90
+ return deDuplicated.map(
91
+ ({ value, specifiers, initialize }) => `import ${createImportSpecifier(specifiers)} from '${value}';
92
+ ${initialize || ""}`
93
+ ).join("\n");
69
94
  };
70
- const buildLoader = async (entry, outfile) => {
71
- const command = getCommand();
72
- await esbuild.build({
73
- format: 'esm',
74
- platform: 'browser',
75
- target: 'esnext',
95
+ const buildLoader = (entry, outfile) => __async(void 0, null, function* () {
96
+ yield loaderBuilder.build({
97
+ format: "esm",
98
+ platform: "browser",
99
+ target: "esnext",
76
100
  loader,
77
- watch: command === 'dev' && {},
101
+ watch: isDevCommand() && {},
78
102
  bundle: true,
79
- logLevel: 'error',
103
+ logLevel: "error",
80
104
  entryPoints: [entry],
81
105
  outfile,
82
- plugins: [{
83
- name: 'make-all-packages-external',
84
- setup(build) {
85
- // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
86
- build.onResolve({
87
- filter: EXTERNAL_REGEXP
88
- }, args => {
89
- let external = true;
90
- // FIXME: windows external entrypoint
91
- if (args.kind === 'entry-point') {
92
- external = false;
93
- }
94
- return {
95
- path: args.path,
96
- external
97
- };
98
- });
106
+ plugins: [
107
+ {
108
+ name: "make-all-packages-external",
109
+ setup(build) {
110
+ build.onResolve({ filter: EXTERNAL_REGEXP }, (args) => {
111
+ let external = true;
112
+ if (args.kind === "entry-point") {
113
+ external = false;
114
+ }
115
+ return {
116
+ path: args.path,
117
+ external
118
+ };
119
+ });
120
+ }
99
121
  }
100
- }]
122
+ ]
101
123
  });
102
- };
103
- const buildServerLoader = async (entry, outfile) => {
104
- const command = getCommand();
105
- await esbuild.build({
106
- format: 'cjs',
107
- platform: 'node',
108
- target: 'esnext',
124
+ });
125
+ const buildServerLoader = (entry, outfile) => __async(void 0, null, function* () {
126
+ yield serverLoaderBuilder.build({
127
+ format: "cjs",
128
+ platform: "node",
129
+ target: "esnext",
109
130
  loader,
110
- watch: command === 'dev' && {},
131
+ watch: isDevCommand() && {},
111
132
  bundle: true,
112
- logLevel: 'error',
133
+ logLevel: "error",
113
134
  entryPoints: [entry],
114
135
  outfile
115
136
  });
116
- };
117
- export const generateCode = async (appContext, config, entrypoints, api) => {
118
- var _config$runtime, _config$runtime$route;
137
+ });
138
+ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, null, function* () {
139
+ var _a, _b, _c;
119
140
  const {
120
141
  internalDirectory,
121
142
  distDirectory,
122
143
  srcDirectory,
123
144
  internalDirAlias,
124
- internalSrcAlias
145
+ internalSrcAlias,
146
+ packageName
125
147
  } = appContext;
126
148
  const hookRunners = api.useHookRunners();
127
- const islegacy = Boolean(config === null || config === void 0 ? void 0 : (_config$runtime = config.runtime) === null || _config$runtime === void 0 ? void 0 : (_config$runtime$route = _config$runtime.router) === null || _config$runtime$route === void 0 ? void 0 : _config$runtime$route.legacy);
128
- const {
129
- mountId
130
- } = config.html;
131
- const getRoutes = islegacy ? getClientRoutesLegacy : getClientRoutes;
132
- await Promise.all(entrypoints.map(generateEntryCode));
133
- async function generateEntryCode(entrypoint) {
134
- const {
135
- entryName,
136
- isAutoMount,
137
- customBootstrap,
138
- fileSystemRoutes
139
- } = entrypoint;
140
- if (isAutoMount) {
141
- // generate routes file for file system routes entrypoint.
142
- if (fileSystemRoutes) {
143
- let initialRoutes = [];
144
- let nestedRoute = null;
145
- if (entrypoint.entry) {
146
- initialRoutes = getRoutes({
149
+ const isV5 = typeof ((_a = config.runtime) == null ? void 0 : _a.router) !== "boolean" && ((_c = (_b = config == null ? void 0 : config.runtime) == null ? void 0 : _b.router) == null ? void 0 : _c.mode) === "react-router-5";
150
+ const { mountId } = config.html;
151
+ const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
152
+ yield Promise.all(entrypoints.map(generateEntryCode));
153
+ function generateEntryCode(entrypoint) {
154
+ return __async(this, null, function* () {
155
+ const { entryName, isAutoMount, customBootstrap, fileSystemRoutes } = entrypoint;
156
+ if (isAutoMount) {
157
+ if (fileSystemRoutes) {
158
+ let initialRoutes = [];
159
+ let nestedRoute = null;
160
+ if (entrypoint.entry) {
161
+ initialRoutes = getRoutes({
162
+ entrypoint,
163
+ srcDirectory,
164
+ srcAlias: internalSrcAlias,
165
+ internalDirectory,
166
+ internalDirAlias
167
+ });
168
+ }
169
+ if (entrypoint.nestedRoutesEntry) {
170
+ if (!isV5) {
171
+ nestedRoute = yield walk(
172
+ entrypoint.nestedRoutesEntry,
173
+ entrypoint.nestedRoutesEntry,
174
+ {
175
+ name: internalSrcAlias,
176
+ basename: srcDirectory
177
+ },
178
+ entrypoint.entryName
179
+ );
180
+ if (nestedRoute) {
181
+ initialRoutes.unshift(nestedRoute);
182
+ }
183
+ } else {
184
+ logger.error("Nested routes is not supported in legacy mode.");
185
+ process.exit(1);
186
+ }
187
+ }
188
+ const { routes } = yield hookRunners.modifyFileSystemRoutes({
147
189
  entrypoint,
148
- srcDirectory,
149
- srcAlias: internalSrcAlias,
150
- internalDirectory,
151
- internalDirAlias
190
+ routes: initialRoutes
152
191
  });
153
- }
154
- if (entrypoint.nestedRoutesEntry) {
155
- if (!islegacy) {
156
- nestedRoute = await walk(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
157
- name: internalSrcAlias,
158
- basename: srcDirectory
159
- }, entrypoint.entryName);
160
- if (nestedRoute) {
161
- initialRoutes.unshift(nestedRoute);
162
- }
192
+ const config2 = useResolvedConfigContext();
193
+ const ssr = getEntryOptions(
194
+ entryName,
195
+ config2.server.ssr,
196
+ config2.server.ssrByEntries,
197
+ packageName
198
+ );
199
+ let mode;
200
+ if (ssr) {
201
+ mode = typeof ssr === "object" ? ssr.mode || "string" : "string";
163
202
  } else {
164
- logger.error('Nested routes is not supported in legacy mode.');
165
- // eslint-disable-next-line no-process-exit
166
- process.exit(1);
203
+ mode = false;
167
204
  }
168
- }
169
- const {
170
- routes
171
- } = await hookRunners.modifyFileSystemRoutes({
172
- entrypoint,
173
- routes: initialRoutes
174
- });
175
- const config = useResolvedConfigContext();
176
- const ssr = config === null || config === void 0 ? void 0 : config.server.ssr;
177
- let mode;
178
- if (ssr) {
179
- mode = typeof ssr === 'object' ? ssr.mode || 'string' : 'string';
180
- } else {
181
- mode = false;
182
- }
183
- if (mode === 'stream') {
184
- const hasPageRoute = routes.some(route => 'type' in route && route.type === 'page');
185
- if (hasPageRoute) {
186
- logger.error('streaming ssr is not supported when pages dir exists');
187
- // eslint-disable-next-line no-process-exit
188
- process.exit(1);
205
+ if (mode === "stream") {
206
+ const hasPageRoute = routes.some(
207
+ (route) => "type" in route && route.type === "page"
208
+ );
209
+ if (hasPageRoute) {
210
+ logger.error(
211
+ "Streaming ssr is not supported when pages dir exists"
212
+ );
213
+ process.exit(1);
214
+ }
189
215
  }
216
+ const { code: code2 } = yield hookRunners.beforeGenerateRoutes({
217
+ entrypoint,
218
+ code: yield templates.fileSystemRoutes({
219
+ routes,
220
+ ssrMode: mode,
221
+ nestedRoutesEntry: entrypoint.nestedRoutesEntry,
222
+ entryName: entrypoint.entryName,
223
+ internalDirectory,
224
+ internalDirAlias
225
+ })
226
+ });
227
+ if (entrypoint.nestedRoutesEntry) {
228
+ const routesServerFile = path.join(
229
+ internalDirectory,
230
+ entryName,
231
+ "route-server-loaders.js"
232
+ );
233
+ const outputRoutesServerFile = path.join(
234
+ distDirectory,
235
+ LOADER_ROUTES_DIR,
236
+ entryName,
237
+ "index.js"
238
+ );
239
+ const code3 = templates.routesForServer({
240
+ routes,
241
+ internalDirectory,
242
+ entryName
243
+ });
244
+ yield fs.ensureFile(routesServerFile);
245
+ yield fs.writeFile(routesServerFile, code3);
246
+ const loaderEntryFile = path.join(
247
+ internalDirectory,
248
+ entryName,
249
+ TEMP_LOADERS_DIR,
250
+ "entry.js"
251
+ );
252
+ const loaderIndexFile = path.join(
253
+ internalDirectory,
254
+ entryName,
255
+ TEMP_LOADERS_DIR,
256
+ "index.js"
257
+ );
258
+ if (yield fs.pathExists(loaderEntryFile)) {
259
+ yield buildLoader(loaderEntryFile, loaderIndexFile);
260
+ }
261
+ if (yield fs.pathExists(routesServerFile)) {
262
+ yield buildServerLoader(routesServerFile, outputRoutesServerFile);
263
+ }
264
+ }
265
+ fs.outputFileSync(
266
+ path.resolve(
267
+ internalDirectory,
268
+ `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`
269
+ ),
270
+ code2,
271
+ "utf8"
272
+ );
190
273
  }
191
- const {
192
- code
193
- } = await hookRunners.beforeGenerateRoutes({
274
+ const { imports: importStatements } = yield hookRunners.modifyEntryImports({
194
275
  entrypoint,
195
- code: await templates.fileSystemRoutes({
196
- routes,
197
- ssrMode: mode,
198
- nestedRoutesEntry: entrypoint.nestedRoutesEntry,
199
- entryName: entrypoint.entryName,
276
+ imports: getDefaultImports({
277
+ entrypoint,
278
+ srcDirectory,
279
+ internalSrcAlias,
280
+ internalDirAlias,
200
281
  internalDirectory
201
282
  })
202
283
  });
203
-
204
- // extract nested router loaders
205
- if (entrypoint.nestedRoutesEntry) {
206
- const routesServerFile = path.join(internalDirectory, entryName, 'route-server-loaders.js');
207
- const outputRoutesServerFile = path.join(distDirectory, LOADER_ROUTES_DIR, entryName, 'index.js');
208
- const code = templates.routesForServer({
209
- routes: routes,
210
- internalDirectory,
211
- entryName
212
- });
213
- await fs.ensureFile(routesServerFile);
214
- await fs.writeFile(routesServerFile, code);
215
- const loaderEntryFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, 'entry.js');
216
- const loaderIndexFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, 'index.js');
217
- if (await fs.pathExists(loaderEntryFile)) {
218
- await buildLoader(loaderEntryFile, loaderIndexFile);
219
- }
220
- if (await fs.pathExists(routesServerFile)) {
221
- await buildServerLoader(routesServerFile, outputRoutesServerFile);
222
- }
223
- }
224
- fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
225
- }
226
-
227
- // call modifyEntryImports hook
228
- const {
229
- imports: importStatements
230
- } = await hookRunners.modifyEntryImports({
231
- entrypoint,
232
- imports: getDefaultImports({
284
+ const { plugins } = yield hookRunners.modifyEntryRuntimePlugins({
233
285
  entrypoint,
234
- srcDirectory,
235
- internalSrcAlias,
236
- internalDirAlias,
237
- internalDirectory
238
- })
239
- });
240
-
241
- // call modifyEntryRuntimePlugins hook
242
- const {
243
- plugins
244
- } = await hookRunners.modifyEntryRuntimePlugins({
245
- entrypoint,
246
- plugins: []
247
- });
248
-
249
- // call modifyEntryRenderFunction hook
250
- const {
251
- code: renderFunction
252
- } = await hookRunners.modifyEntryRenderFunction({
253
- entrypoint,
254
- code: templates.renderFunction({
255
- plugins,
256
- customBootstrap,
257
- fileSystemRoutes
258
- })
259
- });
260
-
261
- // call modifyEntryExport hook
262
- const {
263
- exportStatement
264
- } = await hookRunners.modifyEntryExport({
265
- entrypoint,
266
- exportStatement: 'export default AppWrapper;'
267
- });
268
- const code = templates.index({
269
- mountId: mountId,
270
- imports: createImportStatements(importStatements),
271
- renderFunction,
272
- exportStatement
273
- });
274
- const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
275
- entrypoint.entry = entryFile;
276
-
277
- // generate entry file.
278
- if (config.source.enableAsyncEntry) {
279
- const {
280
- code: asyncEntryCode
281
- } = await hookRunners.modifyAsyncEntry({
286
+ plugins: []
287
+ });
288
+ const { code: renderFunction } = yield hookRunners.modifyEntryRenderFunction({
282
289
  entrypoint,
283
- code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
290
+ code: templates.renderFunction({
291
+ plugins,
292
+ customBootstrap,
293
+ fileSystemRoutes
294
+ })
284
295
  });
285
- fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
286
- const bootstrapFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`);
287
- fs.outputFileSync(bootstrapFile, code, 'utf8');
288
- } else {
289
- fs.outputFileSync(entryFile, code, 'utf8');
296
+ const { exportStatement } = yield hookRunners.modifyEntryExport({
297
+ entrypoint,
298
+ exportStatement: "export default AppWrapper;"
299
+ });
300
+ const code = templates.index({
301
+ mountId,
302
+ imports: createImportStatements(importStatements),
303
+ renderFunction,
304
+ exportStatement
305
+ });
306
+ const entryFile = path.resolve(
307
+ internalDirectory,
308
+ `./${entryName}/${ENTRY_POINT_FILE_NAME}`
309
+ );
310
+ entrypoint.entry = entryFile;
311
+ if (config.source.enableAsyncEntry) {
312
+ const { code: asyncEntryCode } = yield hookRunners.modifyAsyncEntry({
313
+ entrypoint,
314
+ code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
315
+ });
316
+ fs.outputFileSync(entryFile, asyncEntryCode, "utf8");
317
+ const bootstrapFile = path.resolve(
318
+ internalDirectory,
319
+ `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`
320
+ );
321
+ fs.outputFileSync(bootstrapFile, code, "utf8");
322
+ } else {
323
+ fs.outputFileSync(entryFile, code, "utf8");
324
+ }
290
325
  }
291
- }
326
+ });
292
327
  }
293
- };
328
+ });
329
+ export {
330
+ createImportStatements,
331
+ generateCode
332
+ };