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

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 (230) hide show
  1. package/CHANGELOG.md +305 -0
  2. package/bin/modern.js +1 -0
  3. package/dist/js/modern/analyze/Builder.js +39 -0
  4. package/dist/js/modern/analyze/constants.js +38 -18
  5. package/dist/js/modern/analyze/generateCode.js +262 -221
  6. package/dist/js/modern/analyze/getBundleEntry.js +34 -32
  7. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +93 -49
  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 +43 -22
  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 +201 -160
  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 +106 -60
  18. package/dist/js/modern/analyze/templates.js +163 -81
  19. package/dist/js/modern/analyze/utils.js +87 -66
  20. package/dist/js/modern/builder/builderPlugins/compatModern.js +122 -109
  21. package/dist/js/modern/builder/index.js +123 -86
  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 +77 -27
  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/start.js +37 -15
  34. package/dist/js/modern/config/default.js +103 -113
  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 +9 -10
  38. package/dist/js/modern/config/initial/createSourceConfig.js +5 -2
  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 +6 -3
  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 +15 -4
  46. package/dist/js/modern/index.js +178 -90
  47. package/dist/js/modern/initialize/index.js +98 -51
  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 +51 -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 +5 -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/getSpecifiedEntries.js +46 -19
  62. package/dist/js/modern/utils/language.js +6 -3
  63. package/dist/js/modern/utils/printInstructions.js +27 -8
  64. package/dist/js/modern/utils/restart.js +43 -16
  65. package/dist/js/modern/utils/routes.js +29 -12
  66. package/dist/js/node/analyze/Builder.js +58 -0
  67. package/dist/js/node/analyze/constants.js +54 -39
  68. package/dist/js/node/analyze/generateCode.js +282 -239
  69. package/dist/js/node/analyze/getBundleEntry.js +58 -44
  70. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +132 -76
  71. package/dist/js/node/analyze/getClientRoutes/getRoutesLegacy.js +128 -74
  72. package/dist/js/node/analyze/getClientRoutes/index.js +23 -17
  73. package/dist/js/node/analyze/getClientRoutes/utils.js +44 -21
  74. package/dist/js/node/analyze/getFileSystemEntry.js +70 -39
  75. package/dist/js/node/analyze/getHtmlTemplate.js +116 -61
  76. package/dist/js/node/analyze/getServerRoutes.js +143 -137
  77. package/dist/js/node/analyze/index.js +247 -174
  78. package/dist/js/node/analyze/isDefaultExportFunction.js +55 -26
  79. package/dist/js/node/analyze/makeLegalIdentifier.js +27 -15
  80. package/dist/js/node/analyze/nestedRoutes.js +137 -75
  81. package/dist/js/node/analyze/templates.js +185 -90
  82. package/dist/js/node/analyze/utils.js +114 -81
  83. package/dist/js/node/builder/builderPlugins/compatModern.js +150 -122
  84. package/dist/js/node/builder/index.js +151 -95
  85. package/dist/js/node/builder/loaders/routerLoader.js +27 -18
  86. package/dist/js/node/builder/loaders/serverModuleLoader.js +22 -7
  87. package/dist/js/node/builder/share.js +46 -26
  88. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +39 -26
  89. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +47 -31
  90. package/dist/js/node/builder/webpackPlugins/routerPlugin.js +118 -90
  91. package/dist/js/node/commands/build.js +89 -52
  92. package/dist/js/node/commands/deploy.js +45 -10
  93. package/dist/js/node/commands/dev.js +99 -41
  94. package/dist/js/node/commands/index.js +19 -38
  95. package/dist/js/node/commands/inspect.js +48 -11
  96. package/dist/js/node/commands/start.js +65 -26
  97. package/dist/js/node/config/default.js +119 -117
  98. package/dist/js/node/config/index.js +25 -29
  99. package/dist/js/node/config/initial/createHtmlConfig.js +22 -6
  100. package/dist/js/node/config/initial/createOutputConfig.js +26 -14
  101. package/dist/js/node/config/initial/createSourceConfig.js +22 -6
  102. package/dist/js/node/config/initial/createToolsConfig.js +24 -10
  103. package/dist/js/node/config/initial/index.js +28 -17
  104. package/dist/js/node/config/initial/inits.js +126 -81
  105. package/dist/js/node/config/initial/transformNormalizedConfig.js +31 -15
  106. package/dist/js/node/defineConfig.js +43 -17
  107. package/dist/js/node/exports/server.js +21 -10
  108. package/dist/js/node/hooks.js +48 -29
  109. package/dist/js/node/index.js +225 -128
  110. package/dist/js/node/initialize/index.js +116 -61
  111. package/dist/js/node/locale/en.js +36 -25
  112. package/dist/js/node/locale/index.js +27 -15
  113. package/dist/js/node/locale/zh.js +37 -26
  114. package/dist/js/node/schema/Schema.js +23 -10
  115. package/dist/js/node/schema/index.js +77 -114
  116. package/dist/js/node/schema/legacy.js +117 -240
  117. package/dist/js/node/types/config/index.js +17 -16
  118. package/dist/js/node/types/index.js +19 -38
  119. package/dist/js/node/types/legacyConfig/output.js +0 -5
  120. package/dist/js/node/utils/commands.js +21 -6
  121. package/dist/js/node/utils/config.js +120 -51
  122. package/dist/js/node/utils/createFileWatcher.js +104 -61
  123. package/dist/js/node/utils/createServer.js +86 -25
  124. package/dist/js/node/utils/getSpecifiedEntries.js +64 -25
  125. package/dist/js/node/utils/language.js +24 -8
  126. package/dist/js/node/utils/printInstructions.js +47 -16
  127. package/dist/js/node/utils/restart.js +61 -21
  128. package/dist/js/node/utils/routes.js +53 -18
  129. package/dist/js/treeshaking/analyze/Builder.js +199 -0
  130. package/dist/js/treeshaking/analyze/constants.js +28 -18
  131. package/dist/js/treeshaking/analyze/generateCode.js +600 -406
  132. package/dist/js/treeshaking/analyze/getBundleEntry.js +55 -63
  133. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +214 -168
  134. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +215 -169
  135. package/dist/js/treeshaking/analyze/getClientRoutes/index.js +3 -2
  136. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +19 -20
  137. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +89 -86
  138. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +298 -125
  139. package/dist/js/treeshaking/analyze/getServerRoutes.js +210 -147
  140. package/dist/js/treeshaking/analyze/index.js +571 -304
  141. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +45 -26
  142. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +13 -16
  143. package/dist/js/treeshaking/analyze/nestedRoutes.js +411 -200
  144. package/dist/js/treeshaking/analyze/templates.js +433 -236
  145. package/dist/js/treeshaking/analyze/utils.js +358 -153
  146. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +282 -200
  147. package/dist/js/treeshaking/builder/index.js +371 -165
  148. package/dist/js/treeshaking/builder/loaders/routerLoader.js +11 -12
  149. package/dist/js/treeshaking/builder/loaders/serverModuleLoader.js +3 -2
  150. package/dist/js/treeshaking/builder/share.js +38 -44
  151. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +108 -44
  152. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +70 -39
  153. package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +334 -112
  154. package/dist/js/treeshaking/commands/build.js +286 -88
  155. package/dist/js/treeshaking/commands/deploy.js +153 -25
  156. package/dist/js/treeshaking/commands/dev.js +318 -131
  157. package/dist/js/treeshaking/commands/index.js +1 -1
  158. package/dist/js/treeshaking/commands/inspect.js +147 -32
  159. package/dist/js/treeshaking/commands/start.js +190 -67
  160. package/dist/js/treeshaking/config/default.js +210 -198
  161. package/dist/js/treeshaking/config/index.js +3 -2
  162. package/dist/js/treeshaking/config/initial/createHtmlConfig.js +19 -31
  163. package/dist/js/treeshaking/config/initial/createOutputConfig.js +43 -70
  164. package/dist/js/treeshaking/config/initial/createSourceConfig.js +37 -40
  165. package/dist/js/treeshaking/config/initial/createToolsConfig.js +23 -38
  166. package/dist/js/treeshaking/config/initial/index.js +10 -9
  167. package/dist/js/treeshaking/config/initial/inits.js +205 -106
  168. package/dist/js/treeshaking/config/initial/transformNormalizedConfig.js +27 -34
  169. package/dist/js/treeshaking/defineConfig.js +60 -13
  170. package/dist/js/treeshaking/exports/server.js +2 -1
  171. package/dist/js/treeshaking/hooks.js +30 -25
  172. package/dist/js/treeshaking/index.js +681 -256
  173. package/dist/js/treeshaking/initialize/index.js +290 -112
  174. package/dist/js/treeshaking/locale/en.js +34 -33
  175. package/dist/js/treeshaking/locale/index.js +5 -5
  176. package/dist/js/treeshaking/locale/zh.js +34 -33
  177. package/dist/js/treeshaking/schema/Schema.js +267 -69
  178. package/dist/js/treeshaking/schema/index.js +165 -121
  179. package/dist/js/treeshaking/schema/legacy.js +323 -256
  180. package/dist/js/treeshaking/types/config/deploy.js +1 -0
  181. package/dist/js/treeshaking/types/config/dev.js +1 -0
  182. package/dist/js/treeshaking/types/config/experiments.js +1 -0
  183. package/dist/js/treeshaking/types/config/html.js +1 -0
  184. package/dist/js/treeshaking/types/config/index.js +0 -1
  185. package/dist/js/treeshaking/types/config/output.js +1 -0
  186. package/dist/js/treeshaking/types/config/performance.js +1 -0
  187. package/dist/js/treeshaking/types/config/security.js +1 -0
  188. package/dist/js/treeshaking/types/config/source.js +1 -0
  189. package/dist/js/treeshaking/types/config/tools.js +1 -0
  190. package/dist/js/treeshaking/types/hooks.js +1 -0
  191. package/dist/js/treeshaking/types/index.js +0 -1
  192. package/dist/js/treeshaking/types/legacyConfig/deploy.js +1 -0
  193. package/dist/js/treeshaking/types/legacyConfig/dev.js +1 -0
  194. package/dist/js/treeshaking/types/legacyConfig/index.js +1 -0
  195. package/dist/js/treeshaking/types/legacyConfig/output.js +1 -1
  196. package/dist/js/treeshaking/types/legacyConfig/source.js +1 -0
  197. package/dist/js/treeshaking/types/legacyConfig/tools.js +1 -0
  198. package/dist/js/treeshaking/utils/commands.js +6 -5
  199. package/dist/js/treeshaking/utils/config.js +295 -117
  200. package/dist/js/treeshaking/utils/createFileWatcher.js +278 -118
  201. package/dist/js/treeshaking/utils/createServer.js +252 -67
  202. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +182 -55
  203. package/dist/js/treeshaking/utils/language.js +6 -5
  204. package/dist/js/treeshaking/utils/printInstructions.js +151 -29
  205. package/dist/js/treeshaking/utils/restart.js +184 -42
  206. package/dist/js/treeshaking/utils/routes.js +151 -27
  207. package/dist/js/treeshaking/utils/types.js +1 -0
  208. package/dist/types/analyze/Builder.d.ts +8 -0
  209. package/dist/types/analyze/index.d.ts +2 -0
  210. package/dist/types/analyze/templates.d.ts +3 -1
  211. package/dist/types/builder/builderPlugins/compatModern.d.ts +2 -1
  212. package/dist/types/builder/index.d.ts +3 -3
  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 +6 -1
  216. package/dist/types/initialize/index.d.ts +2 -0
  217. package/dist/types/types/config/dev.d.ts +2 -2
  218. package/dist/types/types/config/index.d.ts +1 -0
  219. package/dist/types/types/config/output.d.ts +0 -1
  220. package/dist/types/types/config/source.d.ts +1 -0
  221. package/dist/types/types/config/tools.d.ts +2 -0
  222. package/dist/types/types/hooks.d.ts +5 -0
  223. package/dist/types/types/legacyConfig/dev.d.ts +1 -0
  224. package/dist/types/types/legacyConfig/output.d.ts +1 -1
  225. package/dist/types/types/legacyConfig/source.d.ts +1 -0
  226. package/dist/types/types/legacyConfig/tools.d.ts +1 -0
  227. package/dist/types/utils/config.d.ts +1 -0
  228. package/dist/types/utils/createServer.d.ts +1 -0
  229. package/dist/types/utils/restart.d.ts +1 -1
  230. package/package.json +23 -22
@@ -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';
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";
5
31
  import { getCommand } 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,257 @@ 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) => {
95
+ const buildLoader = (entry, outfile) => __async(void 0, null, function* () {
71
96
  const command = getCommand();
72
- await esbuild.build({
73
- format: 'esm',
74
- platform: 'browser',
75
- target: 'esnext',
97
+ yield loaderBuilder.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(build) {
111
+ build.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
- const buildServerLoader = async (entry, outfile) => {
125
+ });
126
+ const buildServerLoader = (entry, outfile) => __async(void 0, null, function* () {
104
127
  const command = getCommand();
105
- await esbuild.build({
106
- format: 'cjs',
107
- platform: 'node',
108
- target: 'esnext',
128
+ yield serverLoaderBuilder.build({
129
+ format: "cjs",
130
+ platform: "node",
131
+ target: "esnext",
109
132
  loader,
110
- watch: command === 'dev' && {},
133
+ watch: command === "dev" && {},
111
134
  bundle: true,
112
- logLevel: 'error',
135
+ logLevel: "error",
113
136
  entryPoints: [entry],
114
137
  outfile
115
138
  });
116
- };
117
- export const generateCode = async (appContext, config, entrypoints, api) => {
118
- var _config$runtime, _config$runtime$route;
139
+ });
140
+ const generateCode = (appContext, config, entrypoints, api) => __async(void 0, null, function* () {
141
+ var _a, _b;
119
142
  const {
120
143
  internalDirectory,
121
144
  distDirectory,
122
145
  srcDirectory,
123
146
  internalDirAlias,
124
- internalSrcAlias
147
+ internalSrcAlias,
148
+ packageName
125
149
  } = appContext;
126
150
  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;
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;
131
153
  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({
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({
147
191
  entrypoint,
148
- srcDirectory,
149
- srcAlias: internalSrcAlias,
150
- internalDirectory,
151
- internalDirAlias
192
+ routes: initialRoutes
152
193
  });
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
- }
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";
163
204
  } else {
164
- logger.error('Nested routes is not supported in legacy mode.');
165
- // eslint-disable-next-line no-process-exit
166
- process.exit(1);
205
+ mode = false;
167
206
  }
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);
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
+ }
189
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
+ internalDirAlias
227
+ })
228
+ });
229
+ if (entrypoint.nestedRoutesEntry) {
230
+ const routesServerFile = path.join(
231
+ internalDirectory,
232
+ entryName,
233
+ "route-server-loaders.js"
234
+ );
235
+ const outputRoutesServerFile = path.join(
236
+ distDirectory,
237
+ LOADER_ROUTES_DIR,
238
+ entryName,
239
+ "index.js"
240
+ );
241
+ const code3 = templates.routesForServer({
242
+ routes,
243
+ internalDirectory,
244
+ entryName
245
+ });
246
+ yield fs.ensureFile(routesServerFile);
247
+ yield fs.writeFile(routesServerFile, code3);
248
+ const loaderEntryFile = path.join(
249
+ internalDirectory,
250
+ entryName,
251
+ TEMP_LOADERS_DIR,
252
+ "entry.js"
253
+ );
254
+ const loaderIndexFile = path.join(
255
+ internalDirectory,
256
+ entryName,
257
+ TEMP_LOADERS_DIR,
258
+ "index.js"
259
+ );
260
+ if (yield fs.pathExists(loaderEntryFile)) {
261
+ yield buildLoader(loaderEntryFile, loaderIndexFile);
262
+ }
263
+ if (yield fs.pathExists(routesServerFile)) {
264
+ yield buildServerLoader(routesServerFile, outputRoutesServerFile);
265
+ }
266
+ }
267
+ fs.outputFileSync(
268
+ path.resolve(
269
+ internalDirectory,
270
+ `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`
271
+ ),
272
+ code2,
273
+ "utf8"
274
+ );
190
275
  }
191
- const {
192
- code
193
- } = await hookRunners.beforeGenerateRoutes({
276
+ const { imports: importStatements } = yield hookRunners.modifyEntryImports({
194
277
  entrypoint,
195
- code: await templates.fileSystemRoutes({
196
- routes,
197
- ssrMode: mode,
198
- nestedRoutesEntry: entrypoint.nestedRoutesEntry,
199
- entryName: entrypoint.entryName,
278
+ imports: getDefaultImports({
279
+ entrypoint,
280
+ srcDirectory,
281
+ internalSrcAlias,
282
+ internalDirAlias,
200
283
  internalDirectory
201
284
  })
202
285
  });
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({
286
+ const { plugins } = yield hookRunners.modifyEntryRuntimePlugins({
233
287
  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({
288
+ plugins: []
289
+ });
290
+ const { code: renderFunction } = yield hookRunners.modifyEntryRenderFunction({
282
291
  entrypoint,
283
- code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
292
+ code: templates.renderFunction({
293
+ plugins,
294
+ customBootstrap,
295
+ fileSystemRoutes
296
+ })
284
297
  });
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');
298
+ const { exportStatement } = yield hookRunners.modifyEntryExport({
299
+ entrypoint,
300
+ exportStatement: "export default AppWrapper;"
301
+ });
302
+ const code = templates.index({
303
+ mountId,
304
+ imports: createImportStatements(importStatements),
305
+ renderFunction,
306
+ exportStatement
307
+ });
308
+ const entryFile = path.resolve(
309
+ internalDirectory,
310
+ `./${entryName}/${ENTRY_POINT_FILE_NAME}`
311
+ );
312
+ entrypoint.entry = entryFile;
313
+ if (config.source.enableAsyncEntry) {
314
+ const { code: asyncEntryCode } = yield hookRunners.modifyAsyncEntry({
315
+ entrypoint,
316
+ code: `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`
317
+ });
318
+ fs.outputFileSync(entryFile, asyncEntryCode, "utf8");
319
+ const bootstrapFile = path.resolve(
320
+ internalDirectory,
321
+ `./${entryName}/${ENTRY_BOOTSTRAP_FILE_NAME}`
322
+ );
323
+ fs.outputFileSync(bootstrapFile, code, "utf8");
324
+ } else {
325
+ fs.outputFileSync(entryFile, code, "utf8");
326
+ }
290
327
  }
291
- }
328
+ });
292
329
  }
293
- };
330
+ });
331
+ export {
332
+ createImportStatements,
333
+ generateCode
334
+ };
@@ -1,68 +1,70 @@
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
  };
51
+ if (entrypoint.fileSystemRoutes) {
52
+ entrypoint.nestedRoutesEntry = entrypoint.entry;
53
+ }
52
54
  if (!ifAlreadyExists(defaults, entrypoint)) {
53
55
  defaults.push(entrypoint);
54
56
  }
55
57
  });
56
58
  }
57
59
  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);
60
+ const entriesDirAbs = ensureAbsolutePath(appDirectory, entriesDir || "");
61
+ const found = defaults.find(
62
+ ({ entryName, entry, nestedRoutesEntry = "" }) => entryName === packageName || path.dirname(entry) === entriesDirAbs || path.dirname(nestedRoutesEntry) === entriesDirAbs
63
+ );
65
64
  found && (found.entryName = MAIN_ENTRY_NAME);
66
65
  }
67
66
  return defaults;
68
- };
67
+ };
68
+ export {
69
+ getBundleEntry
70
+ };