@modern-js/app-tools 2.0.0-beta.0 → 2.0.0-beta.1

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 (126) hide show
  1. package/CHANGELOG.md +181 -0
  2. package/dist/js/modern/analyze/generateCode.js +29 -37
  3. package/dist/js/modern/analyze/getBundleEntry.js +4 -12
  4. package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +5 -30
  5. package/dist/js/modern/analyze/getClientRoutes/getRoutesLegacy.js +5 -30
  6. package/dist/js/modern/analyze/getClientRoutes/utils.js +0 -3
  7. package/dist/js/modern/analyze/getFileSystemEntry.js +4 -17
  8. package/dist/js/modern/analyze/getHtmlTemplate.js +2 -9
  9. package/dist/js/modern/analyze/getServerRoutes.js +15 -32
  10. package/dist/js/modern/analyze/index.js +79 -22
  11. package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -4
  12. package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -2
  13. package/dist/js/modern/analyze/nestedRoutes.js +8 -33
  14. package/dist/js/modern/analyze/templates.js +22 -37
  15. package/dist/js/modern/analyze/utils.js +0 -7
  16. package/dist/js/modern/builder/builderPlugins/compatModern.js +30 -48
  17. package/dist/js/modern/builder/createHtmlConfig.js +2 -2
  18. package/dist/js/modern/builder/createOutputConfig.js +7 -4
  19. package/dist/js/modern/builder/createSourceConfig.js +0 -11
  20. package/dist/js/modern/builder/createToolsConfig.js +3 -10
  21. package/dist/js/modern/builder/index.js +24 -22
  22. package/dist/js/modern/builder/share.js +0 -4
  23. package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -8
  24. package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +2 -13
  25. package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +97 -0
  26. package/dist/js/modern/commands/build.js +6 -46
  27. package/dist/js/modern/commands/dev.js +4 -16
  28. package/dist/js/modern/commands/inspect.js +3 -20
  29. package/dist/js/modern/commands/start.js +0 -2
  30. package/dist/js/modern/index.js +6 -15
  31. package/dist/js/modern/utils/config.js +1 -12
  32. package/dist/js/modern/utils/createServer.js +0 -5
  33. package/dist/js/modern/utils/getSpecifiedEntries.js +0 -6
  34. package/dist/js/modern/utils/printInstructions.js +2 -1
  35. package/dist/js/modern/utils/routes.js +0 -2
  36. package/dist/js/node/analyze/generateCode.js +29 -60
  37. package/dist/js/node/analyze/getBundleEntry.js +4 -18
  38. package/dist/js/node/analyze/getClientRoutes/getRoutes.js +5 -45
  39. package/dist/js/node/analyze/getClientRoutes/getRoutesLegacy.js +5 -45
  40. package/dist/js/node/analyze/getClientRoutes/index.js +0 -2
  41. package/dist/js/node/analyze/getClientRoutes/utils.js +0 -14
  42. package/dist/js/node/analyze/getFileSystemEntry.js +4 -25
  43. package/dist/js/node/analyze/getHtmlTemplate.js +2 -23
  44. package/dist/js/node/analyze/getServerRoutes.js +14 -40
  45. package/dist/js/node/analyze/index.js +79 -32
  46. package/dist/js/node/analyze/isDefaultExportFunction.js +0 -12
  47. package/dist/js/node/analyze/makeLegalIdentifier.js +0 -4
  48. package/dist/js/node/analyze/nestedRoutes.js +8 -41
  49. package/dist/js/node/analyze/templates.js +22 -47
  50. package/dist/js/node/analyze/utils.js +0 -20
  51. package/dist/js/node/builder/builderPlugins/compatModern.js +30 -59
  52. package/dist/js/node/builder/createHtmlConfig.js +2 -10
  53. package/dist/js/node/builder/createOutputConfig.js +7 -6
  54. package/dist/js/node/builder/createSourceConfig.js +0 -17
  55. package/dist/js/node/builder/createToolsConfig.js +3 -12
  56. package/dist/js/node/builder/index.js +24 -36
  57. package/dist/js/node/builder/share.js +0 -12
  58. package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -10
  59. package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +2 -15
  60. package/dist/js/node/builder/webpackPlugins/routerPlugin.js +105 -0
  61. package/dist/js/node/commands/build.js +4 -57
  62. package/dist/js/node/commands/deploy.js +0 -2
  63. package/dist/js/node/commands/dev.js +3 -27
  64. package/dist/js/node/commands/index.js +0 -6
  65. package/dist/js/node/commands/inspect.js +3 -27
  66. package/dist/js/node/commands/start.js +0 -11
  67. package/dist/js/node/exports/server.js +0 -1
  68. package/dist/js/node/hooks.js +0 -2
  69. package/dist/js/node/index.js +7 -34
  70. package/dist/js/node/locale/index.js +0 -4
  71. package/dist/js/node/utils/commands.js +0 -2
  72. package/dist/js/node/utils/config.js +1 -25
  73. package/dist/js/node/utils/createServer.js +0 -15
  74. package/dist/js/node/utils/getSpecifiedEntries.js +0 -9
  75. package/dist/js/node/utils/language.js +0 -2
  76. package/dist/js/node/utils/printInstructions.js +2 -5
  77. package/dist/js/node/utils/routes.js +0 -5
  78. package/dist/js/treeshaking/analyze/generateCode.js +57 -100
  79. package/dist/js/treeshaking/analyze/getBundleEntry.js +11 -19
  80. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +16 -42
  81. package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +16 -42
  82. package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +0 -3
  83. package/dist/js/treeshaking/analyze/getFileSystemEntry.js +4 -17
  84. package/dist/js/treeshaking/analyze/getHtmlTemplate.js +2 -26
  85. package/dist/js/treeshaking/analyze/getServerRoutes.js +24 -37
  86. package/dist/js/treeshaking/analyze/index.js +185 -65
  87. package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +0 -4
  88. package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +0 -2
  89. package/dist/js/treeshaking/analyze/nestedRoutes.js +9 -51
  90. package/dist/js/treeshaking/analyze/templates.js +31 -50
  91. package/dist/js/treeshaking/analyze/utils.js +7 -14
  92. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +40 -59
  93. package/dist/js/treeshaking/builder/createHtmlConfig.js +15 -15
  94. package/dist/js/treeshaking/builder/createOutputConfig.js +26 -23
  95. package/dist/js/treeshaking/builder/createSourceConfig.js +7 -19
  96. package/dist/js/treeshaking/builder/createToolsConfig.js +16 -21
  97. package/dist/js/treeshaking/builder/index.js +38 -49
  98. package/dist/js/treeshaking/builder/share.js +0 -4
  99. package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +1 -13
  100. package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +2 -14
  101. package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +121 -0
  102. package/dist/js/treeshaking/commands/build.js +26 -111
  103. package/dist/js/treeshaking/commands/deploy.js +0 -3
  104. package/dist/js/treeshaking/commands/dev.js +13 -35
  105. package/dist/js/treeshaking/commands/inspect.js +5 -28
  106. package/dist/js/treeshaking/commands/start.js +0 -9
  107. package/dist/js/treeshaking/index.js +2 -39
  108. package/dist/js/treeshaking/utils/config.js +1 -17
  109. package/dist/js/treeshaking/utils/createServer.js +0 -10
  110. package/dist/js/treeshaking/utils/getSpecifiedEntries.js +0 -10
  111. package/dist/js/treeshaking/utils/printInstructions.js +0 -5
  112. package/dist/js/treeshaking/utils/routes.js +0 -4
  113. package/dist/types/analyze/index.d.ts +2 -3
  114. package/dist/types/analyze/nestedRoutes.d.ts +1 -1
  115. package/dist/types/analyze/templates.d.ts +3 -1
  116. package/dist/types/builder/builderPlugins/compatModern.d.ts +0 -1
  117. package/dist/types/builder/index.d.ts +2 -6
  118. package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +10 -0
  119. package/dist/types/commands/inspect.d.ts +1 -4
  120. package/dist/types/index.d.ts +0 -2
  121. package/dist/types/utils/config.d.ts +0 -1
  122. package/package.json +23 -22
  123. package/dist/js/modern/utils/createCompiler.js +0 -58
  124. package/dist/js/node/utils/createCompiler.js +0 -75
  125. package/dist/js/treeshaking/utils/createCompiler.js +0 -117
  126. package/dist/types/utils/createCompiler.d.ts +0 -12
@@ -13,20 +13,16 @@ import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
13
13
  import { FILE_SYSTEM_ROUTES_FILE_NAME, ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME } from "./constants";
14
14
  import { getDefaultImports } from "./utils";
15
15
  import { walk } from "./nestedRoutes";
16
-
17
16
  var createImportSpecifier = function createImportSpecifier(specifiers) {
18
17
  var defaults = '';
19
18
  var named = [];
20
-
21
19
  var _iterator = _createForOfIteratorHelper(specifiers),
22
- _step;
23
-
20
+ _step;
24
21
  try {
25
22
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
26
23
  var _step$value = _step.value,
27
- local = _step$value.local,
28
- imported = _step$value.imported;
29
-
24
+ local = _step$value.local,
25
+ imported = _step$value.imported;
30
26
  if (local && imported) {
31
27
  named.push("".concat(imported, " as ").concat(local));
32
28
  } else if (local) {
@@ -40,7 +36,6 @@ var createImportSpecifier = function createImportSpecifier(specifiers) {
40
36
  } finally {
41
37
  _iterator.f();
42
38
  }
43
-
44
39
  if (defaults && named.length) {
45
40
  return "".concat(defaults, ", { ").concat(named.join(', '), " }");
46
41
  } else if (defaults) {
@@ -49,22 +44,18 @@ var createImportSpecifier = function createImportSpecifier(specifiers) {
49
44
  return "{ ".concat(named.join(', '), " }");
50
45
  }
51
46
  };
52
-
53
47
  export var createImportStatements = function createImportStatements(statements) {
54
48
  // merge import statements with the same value.
55
49
  var deDuplicated = [];
56
50
  var seen = new Map();
57
-
58
51
  var _iterator2 = _createForOfIteratorHelper(statements),
59
- _step2;
60
-
52
+ _step2;
61
53
  try {
62
54
  var _loop = function _loop() {
63
55
  var _step2$value = _step2.value,
64
- value = _step2$value.value,
65
- specifiers = _step2$value.specifiers,
66
- initialize = _step2$value.initialize;
67
-
56
+ value = _step2$value.value,
57
+ specifiers = _step2$value.specifiers,
58
+ initialize = _step2$value.initialize;
68
59
  if (!seen.has(value)) {
69
60
  deDuplicated.push({
70
61
  value: value,
@@ -74,10 +65,8 @@ export var createImportStatements = function createImportStatements(statements)
74
65
  seen.set(value, specifiers);
75
66
  } else {
76
67
  var _seen$get, _deDuplicated$modifyI, _deDuplicated$modifyI2;
77
-
78
- (_seen$get = seen.get(value)).push.apply(_seen$get, _toConsumableArray(specifiers)); // make "initialize" param can be connected when multiple plugins were imported from same package
79
-
80
-
68
+ (_seen$get = seen.get(value)).push.apply(_seen$get, _toConsumableArray(specifiers));
69
+ // make "initialize" param can be connected when multiple plugins were imported from same package
81
70
  var modifyIndex = deDuplicated.findIndex(function (v) {
82
71
  return v.value === value;
83
72
  });
@@ -85,7 +74,6 @@ export var createImportStatements = function createImportStatements(statements)
85
74
  deDuplicated[modifyIndex].initialize = originInitialize.concat("\n".concat(initialize || ''));
86
75
  }
87
76
  };
88
-
89
77
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
90
78
  _loop();
91
79
  }
@@ -94,15 +82,13 @@ export var createImportStatements = function createImportStatements(statements)
94
82
  } finally {
95
83
  _iterator2.f();
96
84
  }
97
-
98
85
  return deDuplicated.map(function (_ref) {
99
86
  var value = _ref.value,
100
- specifiers = _ref.specifiers,
101
- initialize = _ref.initialize;
87
+ specifiers = _ref.specifiers,
88
+ initialize = _ref.initialize;
102
89
  return "import ".concat(createImportSpecifier(specifiers), " from '").concat(value, "';\n").concat(initialize || '');
103
90
  }).join('\n');
104
91
  };
105
-
106
92
  var buildLoader = /*#__PURE__*/function () {
107
93
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(entry, outfile) {
108
94
  var loader, EXTERNAL_REGEXP, command;
@@ -134,12 +120,11 @@ var buildLoader = /*#__PURE__*/function () {
134
120
  build.onResolve({
135
121
  filter: EXTERNAL_REGEXP
136
122
  }, function (args) {
137
- var external = true; // FIXME: windows external entrypoint
138
-
123
+ var external = true;
124
+ // FIXME: windows external entrypoint
139
125
  if (args.kind === 'entry-point') {
140
126
  external = false;
141
127
  }
142
-
143
128
  return {
144
129
  path: args.path,
145
130
  external: external
@@ -148,7 +133,6 @@ var buildLoader = /*#__PURE__*/function () {
148
133
  }
149
134
  }]
150
135
  });
151
-
152
136
  case 5:
153
137
  case "end":
154
138
  return _context.stop();
@@ -156,45 +140,36 @@ var buildLoader = /*#__PURE__*/function () {
156
140
  }
157
141
  }, _callee);
158
142
  }));
159
-
160
143
  return function buildLoader(_x, _x2) {
161
144
  return _ref2.apply(this, arguments);
162
145
  };
163
146
  }();
164
-
165
147
  export var generateCode = /*#__PURE__*/function () {
166
148
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(appContext, config, entrypoints, api) {
167
149
  var _config$runtime, _config$runtime$route;
168
-
169
150
  var internalDirectory, distDirectory, srcDirectory, internalDirAlias, internalSrcAlias, hookRunners, islegacy, mountId, getRoutes, generateEntryCode, _generateEntryCode;
170
-
171
151
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
172
152
  while (1) {
173
153
  switch (_context3.prev = _context3.next) {
174
154
  case 0:
175
155
  _generateEntryCode = function _generateEntryCode3() {
176
156
  _generateEntryCode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(entrypoint) {
177
- var entryName, isAutoMount, customBootstrap, fileSystemRoutes, initialRoutes, nestedRoute, _yield$hookRunners$mo, routes, _config, ssr, mode, _yield$hookRunners$be, _code, routesServerFile, outputRoutesServerFile, _code2, _yield$hookRunners$mo2, importStatements, _yield$hookRunners$mo3, plugins, _yield$hookRunners$mo4, renderFunction, _yield$hookRunners$mo5, exportStatement, code, entryFile, _yield$hookRunners$mo6, asyncEntryCode, bootstrapFile;
178
-
157
+ var entryName, isAutoMount, customBootstrap, fileSystemRoutes, initialRoutes, nestedRoute, _yield$hookRunners$mo, routes, _config, ssr, mode, hasPageRoute, _yield$hookRunners$be, _code, routesServerFile, outputRoutesServerFile, _code2, _yield$hookRunners$mo2, importStatements, _yield$hookRunners$mo3, plugins, _yield$hookRunners$mo4, renderFunction, _yield$hookRunners$mo5, exportStatement, code, entryFile, _yield$hookRunners$mo6, asyncEntryCode, bootstrapFile;
179
158
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
180
159
  while (1) {
181
160
  switch (_context2.prev = _context2.next) {
182
161
  case 0:
183
162
  entryName = entrypoint.entryName, isAutoMount = entrypoint.isAutoMount, customBootstrap = entrypoint.customBootstrap, fileSystemRoutes = entrypoint.fileSystemRoutes;
184
-
185
163
  if (!isAutoMount) {
186
- _context2.next = 68;
164
+ _context2.next = 69;
187
165
  break;
188
166
  }
189
-
190
167
  if (!fileSystemRoutes) {
191
- _context2.next = 38;
168
+ _context2.next = 39;
192
169
  break;
193
170
  }
194
-
195
171
  initialRoutes = [];
196
172
  nestedRoute = null;
197
-
198
173
  if (entrypoint.entry) {
199
174
  initialRoutes = getRoutes({
200
175
  entrypoint: entrypoint,
@@ -204,76 +179,73 @@ export var generateCode = /*#__PURE__*/function () {
204
179
  internalDirAlias: internalDirAlias
205
180
  });
206
181
  }
207
-
208
182
  if (!entrypoint.nestedRoutesEntry) {
209
183
  _context2.next = 16;
210
184
  break;
211
185
  }
212
-
213
186
  if (islegacy) {
214
187
  _context2.next = 14;
215
188
  break;
216
189
  }
217
-
218
190
  _context2.next = 10;
219
191
  return walk(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
220
192
  name: internalSrcAlias,
221
193
  basename: srcDirectory
222
- });
223
-
194
+ }, entrypoint.entryName);
224
195
  case 10:
225
196
  nestedRoute = _context2.sent;
226
-
227
197
  if (nestedRoute) {
228
198
  initialRoutes.unshift(nestedRoute);
229
199
  }
230
-
231
200
  _context2.next = 16;
232
201
  break;
233
-
234
202
  case 14:
235
- logger.error('Nested routes is not supported in legacy mode.'); // eslint-disable-next-line no-process-exit
236
-
203
+ logger.error('Nested routes is not supported in legacy mode.');
204
+ // eslint-disable-next-line no-process-exit
237
205
  process.exit(1);
238
-
239
206
  case 16:
240
207
  _context2.next = 18;
241
208
  return hookRunners.modifyFileSystemRoutes({
242
209
  entrypoint: entrypoint,
243
210
  routes: initialRoutes
244
211
  });
245
-
246
212
  case 18:
247
213
  _yield$hookRunners$mo = _context2.sent;
248
214
  routes = _yield$hookRunners$mo.routes;
249
215
  _config = useResolvedConfigContext();
250
216
  ssr = _config === null || _config === void 0 ? void 0 : _config.server.ssr;
251
-
252
217
  if (ssr) {
253
218
  mode = _typeof(ssr) === 'object' ? ssr.mode || 'string' : 'string';
254
219
  } else {
255
220
  mode = false;
256
221
  }
257
-
258
- _context2.next = 25;
222
+ if (mode === 'stream') {
223
+ hasPageRoute = routes.some(function (route) {
224
+ return 'type' in route && route.type === 'page';
225
+ });
226
+ if (hasPageRoute) {
227
+ logger.error('streaming ssr is not supported when pages dir exists');
228
+ // eslint-disable-next-line no-process-exit
229
+ process.exit(1);
230
+ }
231
+ }
232
+ _context2.next = 26;
259
233
  return hookRunners.beforeGenerateRoutes({
260
234
  entrypoint: entrypoint,
261
235
  code: templates.fileSystemRoutes({
262
236
  routes: routes,
263
237
  ssrMode: mode,
264
- nestedRoutesEntry: entrypoint.nestedRoutesEntry
238
+ nestedRoutesEntry: entrypoint.nestedRoutesEntry,
239
+ entryName: entrypoint.entryName
265
240
  })
266
241
  });
267
-
268
- case 25:
242
+ case 26:
269
243
  _yield$hookRunners$be = _context2.sent;
270
244
  _code = _yield$hookRunners$be.code;
271
-
272
245
  if (!entrypoint.nestedRoutesEntry) {
273
- _context2.next = 37;
246
+ _context2.next = 38;
274
247
  break;
275
248
  }
276
-
277
249
  routesServerFile = path.join(internalDirectory, entryName, 'routes.server.js');
278
250
  outputRoutesServerFile = path.join(distDirectory, 'loader-routes', entryName, 'index.js');
279
251
  _code2 = templates.routesForServer({
@@ -283,22 +255,18 @@ export var generateCode = /*#__PURE__*/function () {
283
255
  basename: srcDirectory
284
256
  }
285
257
  });
286
- _context2.next = 33;
258
+ _context2.next = 34;
287
259
  return fs.ensureFile(routesServerFile);
288
-
289
- case 33:
290
- _context2.next = 35;
260
+ case 34:
261
+ _context2.next = 36;
291
262
  return fs.writeFile(routesServerFile, _code2);
292
-
293
- case 35:
294
- _context2.next = 37;
263
+ case 36:
264
+ _context2.next = 38;
295
265
  return buildLoader(routesServerFile, outputRoutesServerFile);
296
-
297
- case 37:
298
- fs.outputFileSync(path.resolve(internalDirectory, "./".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_FILE_NAME)), _code, 'utf8');
299
-
300
266
  case 38:
301
- _context2.next = 40;
267
+ fs.outputFileSync(path.resolve(internalDirectory, "./".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_FILE_NAME)), _code, 'utf8');
268
+ case 39:
269
+ _context2.next = 41;
302
270
  return hookRunners.modifyEntryImports({
303
271
  entrypoint: entrypoint,
304
272
  imports: getDefaultImports({
@@ -309,20 +277,18 @@ export var generateCode = /*#__PURE__*/function () {
309
277
  internalDirectory: internalDirectory
310
278
  })
311
279
  });
312
-
313
- case 40:
280
+ case 41:
314
281
  _yield$hookRunners$mo2 = _context2.sent;
315
282
  importStatements = _yield$hookRunners$mo2.imports;
316
- _context2.next = 44;
283
+ _context2.next = 45;
317
284
  return hookRunners.modifyEntryRuntimePlugins({
318
285
  entrypoint: entrypoint,
319
286
  plugins: []
320
287
  });
321
-
322
- case 44:
288
+ case 45:
323
289
  _yield$hookRunners$mo3 = _context2.sent;
324
290
  plugins = _yield$hookRunners$mo3.plugins;
325
- _context2.next = 48;
291
+ _context2.next = 49;
326
292
  return hookRunners.modifyEntryRenderFunction({
327
293
  entrypoint: entrypoint,
328
294
  code: templates.renderFunction({
@@ -331,17 +297,15 @@ export var generateCode = /*#__PURE__*/function () {
331
297
  fileSystemRoutes: fileSystemRoutes
332
298
  })
333
299
  });
334
-
335
- case 48:
300
+ case 49:
336
301
  _yield$hookRunners$mo4 = _context2.sent;
337
302
  renderFunction = _yield$hookRunners$mo4.code;
338
- _context2.next = 52;
303
+ _context2.next = 53;
339
304
  return hookRunners.modifyEntryExport({
340
305
  entrypoint: entrypoint,
341
306
  exportStatement: 'export default AppWrapper;'
342
307
  });
343
-
344
- case 52:
308
+ case 53:
345
309
  _yield$hookRunners$mo5 = _context2.sent;
346
310
  exportStatement = _yield$hookRunners$mo5.exportStatement;
347
311
  code = templates.index({
@@ -351,32 +315,29 @@ export var generateCode = /*#__PURE__*/function () {
351
315
  exportStatement: exportStatement
352
316
  });
353
317
  entryFile = path.resolve(internalDirectory, "./".concat(entryName, "/").concat(ENTRY_POINT_FILE_NAME));
354
- entrypoint.entry = entryFile; // generate entry file.
318
+ entrypoint.entry = entryFile;
355
319
 
320
+ // generate entry file.
356
321
  if (!config.source.enableAsyncEntry) {
357
- _context2.next = 67;
322
+ _context2.next = 68;
358
323
  break;
359
324
  }
360
-
361
- _context2.next = 60;
325
+ _context2.next = 61;
362
326
  return hookRunners.modifyAsyncEntry({
363
327
  entrypoint: entrypoint,
364
328
  code: "import('./".concat(ENTRY_BOOTSTRAP_FILE_NAME, "');")
365
329
  });
366
-
367
- case 60:
330
+ case 61:
368
331
  _yield$hookRunners$mo6 = _context2.sent;
369
332
  asyncEntryCode = _yield$hookRunners$mo6.code;
370
333
  fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
371
334
  bootstrapFile = path.resolve(internalDirectory, "./".concat(entryName, "/").concat(ENTRY_BOOTSTRAP_FILE_NAME));
372
335
  fs.outputFileSync(bootstrapFile, code, 'utf8');
373
- _context2.next = 68;
336
+ _context2.next = 69;
374
337
  break;
375
-
376
- case 67:
377
- fs.outputFileSync(entryFile, code, 'utf8');
378
-
379
338
  case 68:
339
+ fs.outputFileSync(entryFile, code, 'utf8');
340
+ case 69:
380
341
  case "end":
381
342
  return _context2.stop();
382
343
  }
@@ -385,11 +346,9 @@ export var generateCode = /*#__PURE__*/function () {
385
346
  }));
386
347
  return _generateEntryCode.apply(this, arguments);
387
348
  };
388
-
389
349
  generateEntryCode = function _generateEntryCode2(_x7) {
390
350
  return _generateEntryCode.apply(this, arguments);
391
351
  };
392
-
393
352
  internalDirectory = appContext.internalDirectory, distDirectory = appContext.distDirectory, srcDirectory = appContext.srcDirectory, internalDirAlias = appContext.internalDirAlias, internalSrcAlias = appContext.internalSrcAlias;
394
353
  hookRunners = api.useHookRunners();
395
354
  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);
@@ -397,7 +356,6 @@ export var generateCode = /*#__PURE__*/function () {
397
356
  getRoutes = islegacy ? getClientRoutesLegacy : getClientRoutes;
398
357
  _context3.next = 9;
399
358
  return Promise.all(entrypoints.map(generateEntryCode));
400
-
401
359
  case 9:
402
360
  case "end":
403
361
  return _context3.stop();
@@ -405,7 +363,6 @@ export var generateCode = /*#__PURE__*/function () {
405
363
  }
406
364
  }, _callee3);
407
365
  }));
408
-
409
366
  return function generateCode(_x3, _x4, _x5, _x6) {
410
367
  return _ref3.apply(this, arguments);
411
368
  };
@@ -2,43 +2,38 @@ import path from 'path';
2
2
  import { ensureAbsolutePath, fs, findExists, MAIN_ENTRY_NAME } from '@modern-js/utils';
3
3
  import { getFileSystemEntry } from "./getFileSystemEntry";
4
4
  import { JS_EXTENSIONS } from "./constants";
5
-
6
5
  var ensureExtensions = function ensureExtensions(file) {
7
6
  if (!path.extname(file)) {
8
7
  return findExists(JS_EXTENSIONS.map(function (ext) {
9
8
  return "".concat(file).concat(ext);
10
9
  })) || file;
11
10
  }
12
-
13
11
  return file;
14
12
  };
15
-
16
13
  var ifAlreadyExists = function ifAlreadyExists(entrypoints, checked) {
17
14
  return entrypoints.some(function (entrypoint) {
18
15
  if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
19
16
  // reset entryName
20
17
  checked.entryName = entrypoint.entryName;
21
18
  return true;
22
- } // filesystem routes entrypoint conflict with normal entrypoint.
23
-
24
-
19
+ }
20
+ // filesystem routes entrypoint conflict with normal entrypoint.
25
21
  if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
26
22
  throw new Error("Entry configuration conflicts\n Your configuration: ".concat(checked.entry, ".\n Default entrypoint: ").concat(entrypoint.entry, "\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules."));
27
23
  }
28
-
29
24
  return false;
30
25
  });
31
26
  };
32
-
33
27
  export var getBundleEntry = function getBundleEntry(appContext, config) {
34
28
  var appDirectory = appContext.appDirectory,
35
- packageName = appContext.packageName;
29
+ packageName = appContext.packageName;
36
30
  var _config$source = config.source,
37
- disableDefaultEntries = _config$source.disableDefaultEntries,
38
- entries = _config$source.entries,
39
- entriesDir = _config$source.entriesDir;
40
- var defaults = disableDefaultEntries ? [] : getFileSystemEntry(appContext, config); // merge entrypoints from user config with directory convention.
31
+ disableDefaultEntries = _config$source.disableDefaultEntries,
32
+ entries = _config$source.entries,
33
+ entriesDir = _config$source.entriesDir;
34
+ var defaults = disableDefaultEntries ? [] : getFileSystemEntry(appContext, config);
41
35
 
36
+ // merge entrypoints from user config with directory convention.
42
37
  if (entries) {
43
38
  Object.keys(entries).forEach(function (name) {
44
39
  var value = entries[name];
@@ -53,25 +48,22 @@ export var getBundleEntry = function getBundleEntry(appContext, config) {
53
48
  isAutoMount: !value.disableMount,
54
49
  fileSystemRoutes: value.enableFileSystemRoutes ? {} : undefined
55
50
  };
56
-
57
51
  if (!ifAlreadyExists(defaults, entrypoint)) {
58
52
  defaults.push(entrypoint);
59
53
  }
60
54
  });
61
55
  }
62
-
63
56
  if (!disableDefaultEntries) {
64
57
  // find main entry point which server route is '/'.
65
58
  var entriesDirAbs = ensureAbsolutePath(appDirectory, entriesDir);
66
59
  var found = defaults.find(function (_ref) {
67
60
  var entryName = _ref.entryName,
68
- entry = _ref.entry,
69
- _ref$nestedRoutesEntr = _ref.nestedRoutesEntry,
70
- nestedRoutesEntry = _ref$nestedRoutesEntr === void 0 ? '' : _ref$nestedRoutesEntr;
61
+ entry = _ref.entry,
62
+ _ref$nestedRoutesEntr = _ref.nestedRoutesEntry,
63
+ nestedRoutesEntry = _ref$nestedRoutesEntr === void 0 ? '' : _ref$nestedRoutesEntr;
71
64
  return entryName === packageName || path.dirname(entry) === entriesDirAbs || path.dirname(nestedRoutesEntry) === entriesDirAbs;
72
65
  });
73
66
  found && (found.entryName = MAIN_ENTRY_NAME);
74
67
  }
75
-
76
68
  return defaults;
77
69
  };
@@ -6,33 +6,29 @@ import { makeLegalIdentifier } from "../makeLegalIdentifier";
6
6
  import { FILE_SYSTEM_ROUTES_COMPONENTS_DIR, FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP, FILE_SYSTEM_ROUTES_INDEX, FILE_SYSTEM_ROUTES_LAYOUT } from "../constants";
7
7
  import { replaceWithAlias } from "../utils";
8
8
  import { debug, findLayout, shouldSkip, getRouteWeight } from "./utils";
9
-
10
9
  var compName = function compName(srcDirectory, filePath) {
11
10
  var legalCompName = makeLegalIdentifier(path.relative(srcDirectory, filePath));
12
11
  return "Comp_".concat(legalCompName);
13
12
  };
14
-
15
13
  var layoutNameAbbr = function layoutNameAbbr(filePath) {
16
14
  var prefix = 'L_';
17
15
  var dirName = path.dirname(filePath).split('/').pop() || '';
18
16
  return "".concat(prefix).concat(makeLegalIdentifier(dirName));
19
17
  };
20
-
21
18
  var parents = [];
22
- /* eslint-disable no-param-reassign */
23
19
 
20
+ /* eslint-disable no-param-reassign */
24
21
  var recursiveReadDir = function recursiveReadDir(_ref) {
25
22
  var dir = _ref.dir,
26
- routes = _ref.routes,
27
- _ref$basePath = _ref.basePath,
28
- basePath = _ref$basePath === void 0 ? '/' : _ref$basePath,
29
- srcDirectory = _ref.srcDirectory,
30
- srcAlias = _ref.srcAlias;
23
+ routes = _ref.routes,
24
+ _ref$basePath = _ref.basePath,
25
+ basePath = _ref$basePath === void 0 ? '/' : _ref$basePath,
26
+ srcDirectory = _ref.srcDirectory,
27
+ srcAlias = _ref.srcAlias;
31
28
  var hasDynamicRoute = false;
32
29
  var resetParent = false;
33
30
  var parent = parents[parents.length - 1];
34
31
  var layout = findLayout(dir);
35
-
36
32
  if (layout) {
37
33
  if (basePath === '/') {
38
34
  throw new Error("should use _app instead of _layout in ".concat(dir));
@@ -54,24 +50,17 @@ var recursiveReadDir = function recursiveReadDir(_ref) {
54
50
  routes = route.children;
55
51
  }
56
52
  }
57
-
58
53
  var _iterator = _createForOfIteratorHelper(fs.readdirSync(dir)),
59
- _step;
60
-
54
+ _step;
61
55
  try {
62
56
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
63
57
  var relative = _step.value;
64
58
  var filePath = path.join(dir, relative);
65
-
66
59
  if (!shouldSkip(filePath)) {
67
60
  var filename = path.basename(filePath, path.extname(filePath));
68
-
69
61
  var _alias = replaceWithAlias(srcDirectory, filePath, srcAlias);
70
-
71
62
  var _componentName = compName(srcDirectory, filePath);
72
-
73
63
  var dynamicRouteMatched = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP.exec(filename);
74
-
75
64
  if (dynamicRouteMatched) {
76
65
  if (hasDynamicRoute) {
77
66
  throw new Error("Can't set two dynamic route in one directory: ".concat(dir));
@@ -79,7 +68,6 @@ var recursiveReadDir = function recursiveReadDir(_ref) {
79
68
  hasDynamicRoute = true;
80
69
  }
81
70
  }
82
-
83
71
  var _route = {
84
72
  path: "".concat(basePath).concat(dynamicRouteMatched ? ":".concat(dynamicRouteMatched[1]).concat(dynamicRouteMatched[2]) : filename),
85
73
  _component: _alias,
@@ -87,7 +75,6 @@ var recursiveReadDir = function recursiveReadDir(_ref) {
87
75
  parent: parent,
88
76
  type: 'page'
89
77
  };
90
-
91
78
  if (fs.statSync(filePath).isDirectory()) {
92
79
  recursiveReadDir({
93
80
  dir: filePath,
@@ -98,19 +85,15 @@ var recursiveReadDir = function recursiveReadDir(_ref) {
98
85
  });
99
86
  continue;
100
87
  }
101
-
102
88
  if (filename === FILE_SYSTEM_ROUTES_LAYOUT) {
103
89
  continue;
104
90
  }
105
-
106
91
  if (filename === FILE_SYSTEM_ROUTES_INDEX) {
107
92
  _route.path = basePath === '/' ? basePath : "".concat(basePath.substring(0, basePath.length - 1));
108
93
  }
109
-
110
94
  if (filename === '404' && basePath === '/') {
111
95
  _route.path = '*';
112
96
  }
113
-
114
97
  routes.push(_route);
115
98
  }
116
99
  }
@@ -119,26 +102,24 @@ var recursiveReadDir = function recursiveReadDir(_ref) {
119
102
  } finally {
120
103
  _iterator.f();
121
104
  }
122
-
123
105
  if (resetParent) {
124
106
  parents.pop();
125
107
  }
126
108
  };
127
109
  /* eslint-enable no-param-reassign */
128
110
 
129
-
130
111
  var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalComponentsDir, internalDirectory, internalDirAlias) {
131
112
  var flat = function flat(routes) {
132
113
  return routes.reduce(function (memo, route) {
133
114
  return memo.concat(Array.isArray(route.children) ? flat(route.children) : [route]);
134
115
  }, []);
135
116
  };
136
-
137
117
  var generate = function generate(route) {
138
118
  var codes = [];
139
119
  var lastComponent = route.component;
140
- var imports = ["import React from 'react';", "import ".concat(lastComponent, " from '").concat(route._component, "'")]; // eslint-disable-next-line no-param-reassign, no-cond-assign
120
+ var imports = ["import React from 'react';", "import ".concat(lastComponent, " from '").concat(route._component, "'")];
141
121
 
122
+ // eslint-disable-next-line no-param-reassign, no-cond-assign
142
123
  while (route = route.parent) {
143
124
  var layoutComponent = route.component;
144
125
  var layoutComponentAbbr = layoutNameAbbr(route._component);
@@ -147,7 +128,6 @@ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalCompo
147
128
  codes.push("const ".concat(currentComponent, " = props => <").concat(layoutComponent, " Component={").concat(lastComponent, "} {...props} />;"));
148
129
  lastComponent = currentComponent;
149
130
  }
150
-
151
131
  var file = path.resolve(internalComponentsDir, "".concat(lastComponent, ".jsx"));
152
132
  fs.outputFileSync(file, "".concat(imports.join('\n'), "\n").concat(codes.join('\n'), "\nexport default ").concat(lastComponent));
153
133
  return {
@@ -155,7 +135,6 @@ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalCompo
155
135
  _component: replaceWithAlias(internalDirectory, file, internalDirAlias)
156
136
  };
157
137
  };
158
-
159
138
  var normalized = flat(nested).map(function (route) {
160
139
  return route.parent ? _objectSpread(_objectSpread(_objectSpread({}, route), generate(route)), {}, {
161
140
  parent: undefined
@@ -165,24 +144,20 @@ var normalizeNestedRoutes = function normalizeNestedRoutes(nested, internalCompo
165
144
  });
166
145
  return normalized;
167
146
  };
168
-
169
147
  export var getClientRoutes = function getClientRoutes(_ref2) {
170
148
  var entrypoint = _ref2.entrypoint,
171
- srcDirectory = _ref2.srcDirectory,
172
- srcAlias = _ref2.srcAlias,
173
- internalDirectory = _ref2.internalDirectory,
174
- internalDirAlias = _ref2.internalDirAlias;
149
+ srcDirectory = _ref2.srcDirectory,
150
+ srcAlias = _ref2.srcAlias,
151
+ internalDirectory = _ref2.internalDirectory,
152
+ internalDirAlias = _ref2.internalDirAlias;
175
153
  var entry = entrypoint.entry,
176
- entryName = entrypoint.entryName;
177
-
154
+ entryName = entrypoint.entryName;
178
155
  if (!fs.existsSync(entry)) {
179
156
  throw new Error("generate file system routes error, ".concat(entry, " directory not found."));
180
157
  }
181
-
182
158
  if (!(fs.existsSync(entry) && fs.statSync(entry).isDirectory())) {
183
159
  throw new Error("generate file system routes error, ".concat(entry, " should be directory."));
184
160
  }
185
-
186
161
  var routes = [];
187
162
  recursiveReadDir({
188
163
  dir: entry,
@@ -194,15 +169,14 @@ export var getClientRoutes = function getClientRoutes(_ref2) {
194
169
  var internalComponentsDir = path.resolve(internalDirectory, "".concat(entryName, "/").concat(FILE_SYSTEM_ROUTES_COMPONENTS_DIR));
195
170
  fs.emptyDirSync(internalComponentsDir);
196
171
  routes = normalizeNestedRoutes(routes, internalComponentsDir, internalDirectory, internalDirAlias);
197
- parents.length = 0; // FIXME: support more situations
172
+ parents.length = 0;
198
173
 
174
+ // FIXME: support more situations
199
175
  routes.sort(function (a, b) {
200
176
  var delta = getRouteWeight(a.path) - getRouteWeight(b.path);
201
-
202
177
  if (delta === 0) {
203
178
  return a.path.length - b.path.length;
204
179
  }
205
-
206
180
  return delta;
207
181
  });
208
182
  debug("fileSystem routes: %o", routes);