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