@modern-js/app-tools 2.21.1 → 2.22.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 (82) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/dist/cjs/analyze/constants.js +66 -22
  3. package/dist/cjs/analyze/generateCode.js +48 -24
  4. package/dist/cjs/analyze/getBundleEntry.js +3 -1
  5. package/dist/cjs/analyze/getClientRoutes/getRoutes.js +3 -1
  6. package/dist/cjs/analyze/getClientRoutes/getRoutesLegacy.js +3 -1
  7. package/dist/cjs/analyze/getClientRoutes/index.js +6 -2
  8. package/dist/cjs/analyze/getClientRoutes/utils.js +12 -4
  9. package/dist/cjs/analyze/getFileSystemEntry.js +3 -1
  10. package/dist/cjs/analyze/getHtmlTemplate.js +3 -1
  11. package/dist/cjs/analyze/getServerRoutes.js +3 -1
  12. package/dist/cjs/analyze/index.js +13 -2
  13. package/dist/cjs/analyze/isDefaultExportFunction.js +3 -1
  14. package/dist/cjs/analyze/makeLegalIdentifier.js +3 -1
  15. package/dist/cjs/analyze/nestedRoutes.js +3 -1
  16. package/dist/cjs/analyze/templates.js +18 -6
  17. package/dist/cjs/analyze/utils.js +24 -8
  18. package/dist/cjs/builder/builder-rspack/adapterCopy.js +3 -1
  19. package/dist/cjs/builder/builder-rspack/index.js +3 -1
  20. package/dist/cjs/builder/builder-webpack/adapterModern.js +3 -1
  21. package/dist/cjs/builder/builder-webpack/createCopyPattern.js +6 -2
  22. package/dist/cjs/builder/builder-webpack/index.js +3 -1
  23. package/dist/cjs/builder/generator/createBuilderOptions.js +7 -4
  24. package/dist/cjs/builder/generator/createBuilderProviderConfig.js +5 -3
  25. package/dist/cjs/builder/generator/getBuilderTargets.js +3 -1
  26. package/dist/cjs/builder/generator/index.js +3 -1
  27. package/dist/cjs/builder/index.js +3 -1
  28. package/dist/cjs/builder/shared/builderPlugins/adapterBasic.js +6 -2
  29. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +3 -1
  30. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +3 -1
  31. package/dist/cjs/builder/shared/bundlerPlugins/HtmlAsyncChunkPlugin.js +3 -1
  32. package/dist/cjs/builder/shared/bundlerPlugins/HtmlBottomTemplate.js +3 -1
  33. package/dist/cjs/builder/shared/bundlerPlugins/RouterPlugin.js +29 -6
  34. package/dist/cjs/builder/shared/createCopyInfo.js +3 -1
  35. package/dist/cjs/builder/shared/loaders/serverModuleLoader.js +3 -1
  36. package/dist/cjs/commands/build.js +3 -1
  37. package/dist/cjs/commands/deploy.js +3 -1
  38. package/dist/cjs/commands/dev.js +3 -1
  39. package/dist/cjs/commands/inspect.js +3 -1
  40. package/dist/cjs/commands/serve.js +3 -1
  41. package/dist/cjs/config/default.js +6 -2
  42. package/dist/cjs/config/initialize/index.js +3 -1
  43. package/dist/cjs/config/initialize/inits.js +9 -3
  44. package/dist/cjs/config/legacy/createHtmlConfig.js +3 -1
  45. package/dist/cjs/config/legacy/createOutputConfig.js +3 -1
  46. package/dist/cjs/config/legacy/createSourceConfig.js +3 -1
  47. package/dist/cjs/config/legacy/createToolsConfig.js +3 -1
  48. package/dist/cjs/config/legacy/index.js +6 -2
  49. package/dist/cjs/defineConfig.js +6 -2
  50. package/dist/cjs/exports/server.js +3 -1
  51. package/dist/cjs/hooks.js +3 -1
  52. package/dist/cjs/index.js +12 -4
  53. package/dist/cjs/initialize/index.js +3 -1
  54. package/dist/cjs/locale/en.js +3 -1
  55. package/dist/cjs/locale/index.js +6 -2
  56. package/dist/cjs/locale/zh.js +3 -1
  57. package/dist/cjs/schema/Schema.js +3 -1
  58. package/dist/cjs/schema/index.js +6 -2
  59. package/dist/cjs/schema/legacy.js +3 -1
  60. package/dist/cjs/utils/config.js +12 -4
  61. package/dist/cjs/utils/createServer.js +15 -5
  62. package/dist/cjs/utils/env.js +3 -1
  63. package/dist/cjs/utils/generateWatchFiles.js +6 -2
  64. package/dist/cjs/utils/getSelectedEntries.js +3 -1
  65. package/dist/cjs/utils/getServerInternalPlugins.js +3 -1
  66. package/dist/cjs/utils/printInstructions.js +3 -1
  67. package/dist/cjs/utils/restart.js +3 -1
  68. package/dist/cjs/utils/routes.js +3 -1
  69. package/dist/esm/analyze/generateCode.js +131 -75
  70. package/dist/esm/analyze/index.js +17 -3
  71. package/dist/esm/builder/generator/createBuilderOptions.js +4 -3
  72. package/dist/esm/builder/generator/createBuilderProviderConfig.js +1 -1
  73. package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +126 -118
  74. package/dist/esm-node/analyze/generateCode.js +39 -22
  75. package/dist/esm-node/analyze/index.js +10 -1
  76. package/dist/esm-node/builder/generator/createBuilderOptions.js +4 -3
  77. package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +2 -2
  78. package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +27 -6
  79. package/dist/types/analyze/generateCode.d.ts +19 -2
  80. package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +7 -0
  81. package/dist/types/types/hooks.d.ts +1 -0
  82. package/package.json +24 -24
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  });
5
5
  Object.defineProperty(exports, "printInstructions", {
6
6
  enumerable: true,
7
- get: () => printInstructions
7
+ get: function() {
8
+ return printInstructions;
9
+ }
8
10
  });
9
11
  const _utils = require("@modern-js/utils");
10
12
  const printInstructions = async (hookRunners, appContext, config) => {
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  });
5
5
  Object.defineProperty(exports, "restart", {
6
6
  enumerable: true,
7
- get: () => restart
7
+ get: function() {
8
+ return restart;
9
+ }
8
10
  });
9
11
  const _core = require("@modern-js/core");
10
12
  const _utils = require("@modern-js/utils");
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  });
5
5
  Object.defineProperty(exports, "generateRoutes", {
6
6
  enumerable: true,
7
- get: () => generateRoutes
7
+ get: function() {
8
+ return generateRoutes;
9
+ }
8
10
  });
9
11
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
10
12
  const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
@@ -94,21 +94,21 @@ export var createImportStatements = function(statements) {
94
94
  };
95
95
  export var generateCode = function() {
96
96
  var _ref = _async_to_generator(function(appContext, config, entrypoints, api) {
97
- var internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName, hookRunners, isV5, mountId, getRoutes;
97
+ var internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName, hookRunners, isV5, getRoutes, importsStatemets;
98
98
  function generateEntryCode(entrypoint) {
99
99
  return _generateEntryCode.apply(this, arguments);
100
100
  }
101
101
  function _generateEntryCode() {
102
102
  _generateEntryCode = _async_to_generator(function(entrypoint) {
103
- var entryName, isAutoMount, customBootstrap, fileSystemRoutes, _config_output, initialRoutes, nestedRoute, routes, _$config, ssr, useSSG, mode, hasPageRoute, code, _, _tmp, routesServerFile, code1, serverLoaderCombined, serverLoaderFile, _ref2, importStatements, plugins, _ref1, renderFunction, exportStatement, code2, entryFile, rawAsyncEntryCode, ssr1, _ref22, asyncEntryCode, bootstrapFile;
103
+ var entryName, isAutoMount, fileSystemRoutes, _config_output, initialRoutes, nestedRoute, routes, _$config, ssr, useSSG, mode, hasPageRoute, code, _, _tmp, routesServerFile, code1, serverLoaderCombined, serverLoaderFile, imports, entryFile;
104
104
  return _ts_generator(this, function(_state) {
105
105
  switch (_state.label) {
106
106
  case 0:
107
- entryName = entrypoint.entryName, isAutoMount = entrypoint.isAutoMount, customBootstrap = entrypoint.customBootstrap, fileSystemRoutes = entrypoint.fileSystemRoutes;
107
+ entryName = entrypoint.entryName, isAutoMount = entrypoint.isAutoMount, fileSystemRoutes = entrypoint.fileSystemRoutes;
108
108
  if (!isAutoMount)
109
109
  return [
110
110
  3,
111
- 18
111
+ 13
112
112
  ];
113
113
  if (!fileSystemRoutes)
114
114
  return [
@@ -248,76 +248,12 @@ export var generateCode = function() {
248
248
  })
249
249
  ];
250
250
  case 12:
251
- _ref2 = _state.sent(), importStatements = _ref2.imports;
252
- return [
253
- 4,
254
- hookRunners.modifyEntryRuntimePlugins({
255
- entrypoint: entrypoint,
256
- plugins: []
257
- })
258
- ];
259
- case 13:
260
- plugins = _state.sent().plugins;
261
- return [
262
- 4,
263
- hookRunners.modifyEntryRenderFunction({
264
- entrypoint: entrypoint,
265
- code: templates.renderFunction({
266
- plugins: plugins,
267
- customBootstrap: customBootstrap,
268
- fileSystemRoutes: fileSystemRoutes
269
- })
270
- })
271
- ];
272
- case 14:
273
- _ref1 = _state.sent(), renderFunction = _ref1.code;
274
- return [
275
- 4,
276
- hookRunners.modifyEntryExport({
277
- entrypoint: entrypoint,
278
- exportStatement: "export default AppWrapper;"
279
- })
280
- ];
281
- case 15:
282
- exportStatement = _state.sent().exportStatement;
283
- code2 = templates.index({
284
- mountId: mountId,
285
- imports: createImportStatements(importStatements),
286
- renderFunction: renderFunction,
287
- exportStatement: exportStatement
288
- });
251
+ imports = _state.sent().imports;
252
+ importsStatemets.set(entryName, imports);
289
253
  entryFile = path.resolve(internalDirectory, "./".concat(entryName, "/").concat(ENTRY_POINT_FILE_NAME));
290
- entrypoint.entry = entryFile;
291
- if (!config.source.enableAsyncEntry)
292
- return [
293
- 3,
294
- 17
295
- ];
296
- rawAsyncEntryCode = "import('./".concat(ENTRY_BOOTSTRAP_FILE_NAME, "');");
297
- ssr1 = getEntryOptions(entryName, config.server.ssr, config.server.ssrByEntries, packageName);
298
- if (ssr1) {
299
- rawAsyncEntryCode = "\n export const ".concat(SERVER_RENDER_FUNCTION_NAME, " = async (...args) => {\n let entry = await ").concat(rawAsyncEntryCode, ";\n if (entry.default instanceof Promise){\n entry = await entry.default;\n return entry.default.").concat(SERVER_RENDER_FUNCTION_NAME, ".apply(null, args);\n }\n return entry.").concat(SERVER_RENDER_FUNCTION_NAME, ".apply(null, args);\n };\n if(typeof window!=='undefined'){\n ").concat(rawAsyncEntryCode, "\n }\n ");
300
- }
301
- return [
302
- 4,
303
- hookRunners.modifyAsyncEntry({
304
- entrypoint: entrypoint,
305
- code: rawAsyncEntryCode
306
- })
307
- ];
308
- case 16:
309
- _ref22 = _state.sent(), asyncEntryCode = _ref22.code;
310
- fs.outputFileSync(entryFile, asyncEntryCode, "utf8");
311
- bootstrapFile = path.resolve(internalDirectory, "./".concat(entryName, "/").concat(ENTRY_BOOTSTRAP_FILE_NAME));
312
- fs.outputFileSync(bootstrapFile, code2, "utf8");
313
- return [
314
- 3,
315
- 18
316
- ];
317
- case 17:
318
- fs.outputFileSync(entryFile, code2, "utf8");
319
- _state.label = 18;
320
- case 18:
254
+ entrypoint.internalEntry = entryFile;
255
+ _state.label = 13;
256
+ case 13:
321
257
  return [
322
258
  2
323
259
  ];
@@ -332,8 +268,8 @@ export var generateCode = function() {
332
268
  internalDirectory = appContext.internalDirectory, srcDirectory = appContext.srcDirectory, internalDirAlias = appContext.internalDirAlias, internalSrcAlias = appContext.internalSrcAlias, packageName = appContext.packageName;
333
269
  hookRunners = api.useHookRunners();
334
270
  isV5 = isRouterV5(config);
335
- mountId = config.html.mountId;
336
271
  getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
272
+ importsStatemets = /* @__PURE__ */ new Map();
337
273
  return [
338
274
  4,
339
275
  Promise.all(entrypoints.map(generateEntryCode))
@@ -341,7 +277,10 @@ export var generateCode = function() {
341
277
  case 1:
342
278
  _state.sent();
343
279
  return [
344
- 2
280
+ 2,
281
+ {
282
+ importsStatemets: importsStatemets
283
+ }
345
284
  ];
346
285
  }
347
286
  });
@@ -350,3 +289,120 @@ export var generateCode = function() {
350
289
  return _ref.apply(this, arguments);
351
290
  };
352
291
  }();
292
+ export var generateIndexCode = function() {
293
+ var _ref = _async_to_generator(function(param) {
294
+ var appContext, api, entrypoints, config, importsStatemets, bundlerConfigs, hookRunners, mountId, internalDirectory, packageName;
295
+ return _ts_generator(this, function(_state) {
296
+ switch (_state.label) {
297
+ case 0:
298
+ appContext = param.appContext, api = param.api, entrypoints = param.entrypoints, config = param.config, importsStatemets = param.importsStatemets, bundlerConfigs = param.bundlerConfigs;
299
+ hookRunners = api.useHookRunners();
300
+ mountId = config.html.mountId;
301
+ internalDirectory = appContext.internalDirectory, packageName = appContext.packageName;
302
+ return [
303
+ 4,
304
+ Promise.all(entrypoints.map(function() {
305
+ var _ref2 = _async_to_generator(function(entrypoint) {
306
+ var entryName, isAutoMount, customBootstrap, fileSystemRoutes, plugins, _ref3, renderFunction, exportStatement, imports, code, entryFile, rawAsyncEntryCode, ssr, _ref1, asyncEntryCode, bootstrapFile;
307
+ return _ts_generator(this, function(_state2) {
308
+ switch (_state2.label) {
309
+ case 0:
310
+ entryName = entrypoint.entryName, isAutoMount = entrypoint.isAutoMount, customBootstrap = entrypoint.customBootstrap, fileSystemRoutes = entrypoint.fileSystemRoutes;
311
+ if (!isAutoMount)
312
+ return [
313
+ 3,
314
+ 6
315
+ ];
316
+ return [
317
+ 4,
318
+ hookRunners.modifyEntryRuntimePlugins({
319
+ entrypoint: entrypoint,
320
+ plugins: [],
321
+ bundlerConfigs: bundlerConfigs
322
+ })
323
+ ];
324
+ case 1:
325
+ plugins = _state2.sent().plugins;
326
+ return [
327
+ 4,
328
+ hookRunners.modifyEntryRenderFunction({
329
+ entrypoint: entrypoint,
330
+ code: templates.renderFunction({
331
+ plugins: plugins,
332
+ customBootstrap: customBootstrap,
333
+ fileSystemRoutes: fileSystemRoutes
334
+ })
335
+ })
336
+ ];
337
+ case 2:
338
+ _ref3 = _state2.sent(), renderFunction = _ref3.code;
339
+ return [
340
+ 4,
341
+ hookRunners.modifyEntryExport({
342
+ entrypoint: entrypoint,
343
+ exportStatement: "export default AppWrapper;"
344
+ })
345
+ ];
346
+ case 3:
347
+ exportStatement = _state2.sent().exportStatement;
348
+ imports = importsStatemets.get(entryName);
349
+ code = templates.index({
350
+ mountId: mountId,
351
+ imports: createImportStatements(imports),
352
+ renderFunction: renderFunction,
353
+ exportStatement: exportStatement
354
+ });
355
+ entryFile = path.resolve(internalDirectory, "./".concat(entryName, "/").concat(ENTRY_POINT_FILE_NAME));
356
+ if (!config.source.enableAsyncEntry)
357
+ return [
358
+ 3,
359
+ 5
360
+ ];
361
+ rawAsyncEntryCode = "import('./".concat(ENTRY_BOOTSTRAP_FILE_NAME, "');");
362
+ ssr = getEntryOptions(entryName, config.server.ssr, config.server.ssrByEntries, packageName);
363
+ if (ssr) {
364
+ rawAsyncEntryCode = "\n export const ".concat(SERVER_RENDER_FUNCTION_NAME, " = async (...args) => {\n let entry = await ").concat(rawAsyncEntryCode, ";\n if (entry.default instanceof Promise){\n entry = await entry.default;\n return entry.default.").concat(SERVER_RENDER_FUNCTION_NAME, ".apply(null, args);\n }\n return entry.").concat(SERVER_RENDER_FUNCTION_NAME, ".apply(null, args);\n };\n if(typeof window!=='undefined'){\n ").concat(rawAsyncEntryCode, "\n }\n ");
365
+ }
366
+ return [
367
+ 4,
368
+ hookRunners.modifyAsyncEntry({
369
+ entrypoint: entrypoint,
370
+ code: rawAsyncEntryCode
371
+ })
372
+ ];
373
+ case 4:
374
+ _ref1 = _state2.sent(), asyncEntryCode = _ref1.code;
375
+ fs.outputFileSync(entryFile, asyncEntryCode, "utf8");
376
+ bootstrapFile = path.resolve(internalDirectory, "./".concat(entryName, "/").concat(ENTRY_BOOTSTRAP_FILE_NAME));
377
+ fs.outputFileSync(bootstrapFile, code, "utf8");
378
+ return [
379
+ 3,
380
+ 6
381
+ ];
382
+ case 5:
383
+ fs.outputFileSync(entryFile, code, "utf8");
384
+ _state2.label = 6;
385
+ case 6:
386
+ return [
387
+ 2
388
+ ];
389
+ }
390
+ });
391
+ });
392
+ return function(entrypoint) {
393
+ return _ref2.apply(this, arguments);
394
+ };
395
+ }()))
396
+ ];
397
+ case 1:
398
+ _state.sent();
399
+ return [
400
+ 2
401
+ ];
402
+ }
403
+ });
404
+ });
405
+ return function generateIndexCode2(_) {
406
+ return _ref.apply(this, arguments);
407
+ };
408
+ }();
@@ -14,6 +14,7 @@ import { initialNormalizedConfig } from "../config";
14
14
  import { createBuilderGenerator } from "../builder";
15
15
  import { isPageComponentFile, parseModule, replaceWithAlias } from "./utils";
16
16
  import { APP_CONFIG_NAME, APP_INIT_EXPORTED, APP_INIT_IMPORTED } from "./constants";
17
+ import { generateIndexCode } from "./generateCode";
17
18
  var debug = createDebugger("plugin-analyze");
18
19
  export default function(param) {
19
20
  var bundler = param.bundler;
@@ -26,7 +27,7 @@ export default function(param) {
26
27
  return {
27
28
  prepare: function prepare() {
28
29
  return _async_to_generator(function() {
29
- var _resolvedConfig_source, appContext, resolvedConfig, hookRunners, apiOnly, routes, _ref, getBundleEntry, getServerRoutes, generateCode, getHtmlTemplate, entrypoints, initialRoutes, routes1, htmlTemplates, checkedEntries, entry, command, buildCommands, normalizedConfig, createBuilderForModern, builder;
30
+ var _resolvedConfig_source, appContext, resolvedConfig, hookRunners, apiOnly, routes, _ref, getBundleEntry, getServerRoutes, generateCode, getHtmlTemplate, entrypoints, initialRoutes, routes1, importsStatemets, htmlTemplates, checkedEntries, entry, command, buildCommands, normalizedConfig, createBuilderForModern, builder;
30
31
  return _ts_generator(this, function(_state) {
31
32
  switch (_state.label) {
32
33
  case 0:
@@ -120,7 +121,7 @@ export default function(param) {
120
121
  generateCode(appContext, resolvedConfig, entrypoints, api)
121
122
  ];
122
123
  case 7:
123
- _state.sent();
124
+ importsStatemets = _state.sent().importsStatemets;
124
125
  return [
125
126
  4,
126
127
  getHtmlTemplate(entrypoints, api, {
@@ -276,11 +277,24 @@ export default function(param) {
276
277
  hookRunners2 = api.useHookRunners();
277
278
  return [
278
279
  4,
279
- hookRunners2.beforeCreateCompiler({
280
+ generateIndexCode({
281
+ appContext: appContext,
282
+ config: resolvedConfig,
283
+ entrypoints: entrypoints,
284
+ api: api,
285
+ importsStatemets: importsStatemets,
280
286
  bundlerConfigs: bundlerConfigs
281
287
  })
282
288
  ];
283
289
  case 1:
290
+ _state2.sent();
291
+ return [
292
+ 4,
293
+ hookRunners2.beforeCreateCompiler({
294
+ bundlerConfigs: bundlerConfigs
295
+ })
296
+ ];
297
+ case 2:
284
298
  _state2.sent();
285
299
  return [
286
300
  2
@@ -4,15 +4,16 @@ export function createBuilderOptions(target, appContext) {
4
4
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
5
5
  try {
6
6
  for (var _iterator = entrypoints[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
7
- var _step_value = _step.value, entryName = _step_value.entryName, entry = _step_value.entry;
7
+ var _step_value = _step.value, entryName = _step_value.entryName, internalEntry = _step_value.internalEntry, entry = _step_value.entry;
8
8
  if (checkedEntries && !checkedEntries.includes(entryName)) {
9
9
  continue;
10
10
  }
11
+ var finalEntry = internalEntry || entry;
11
12
  if (entryName in entries) {
12
- entries[entryName].push(entry);
13
+ entries[entryName].push(finalEntry);
13
14
  } else {
14
15
  entries[entryName] = [
15
- entry
16
+ finalEntry
16
17
  ];
17
18
  }
18
19
  }
@@ -3,7 +3,7 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
3
  export function createBuilderProviderConfig(resolveConfig, appContext, modifyBuilderConfig) {
4
4
  var htmlConfig = _object_spread({}, resolveConfig.html);
5
5
  if (!htmlConfig.template) {
6
- htmlConfig.templateByEntries = _object_spread({}, htmlConfig.templateByEntries, appContext.htmlTemplates);
6
+ htmlConfig.templateByEntries = _object_spread({}, appContext.htmlTemplates, htmlConfig.templateByEntries);
7
7
  }
8
8
  var config = _object_spread_props(_object_spread({}, resolveConfig), {
9
9
  dev: _object_spread_props(_object_spread({}, resolveConfig.dev), {
@@ -5,7 +5,8 @@ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
5
5
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
6
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
7
  import path from "path";
8
- import { fs, ROUTE_MANIFEST_FILE } from "@modern-js/utils";
8
+ import { mergeWith } from "@modern-js/utils/lodash";
9
+ import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
9
10
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
10
11
  var PLUGIN_NAME = "ModernjsRoutePlugin";
11
12
  export var RouterPlugin = /* @__PURE__ */ function() {
@@ -41,130 +42,137 @@ export var RouterPlugin = /* @__PURE__ */ function() {
41
42
  name: PLUGIN_NAME,
42
43
  stage: PROCESS_ASSETS_STAGE_REPORT
43
44
  }, /* @__PURE__ */ _async_to_generator(function() {
44
- var stats, publicPath, _stats_chunks, chunks, namedChunkGroups, routeAssets, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, assets, referenceCssAssets, manifest, injectedContent, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, file, asset, newContent, filename;
45
+ var stats, publicPath, _stats_chunks, chunks, namedChunkGroups, routeAssets, prevManifestAsset, prevManifestStr, prevManifest, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, assets, referenceCssAssets, manifest, injectedContent, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, file, asset, newContent;
45
46
  return _ts_generator(this, function(_state) {
46
- switch (_state.label) {
47
- case 0:
48
- stats = compilation.getStats().toJson({
49
- all: false,
50
- publicPath: true,
51
- assets: true,
52
- chunkGroups: true,
53
- chunks: true,
54
- ids: true
55
- });
56
- publicPath = stats.publicPath, _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
57
- routeAssets = {};
58
- if (!namedChunkGroups) {
59
- return [
60
- 2
61
- ];
62
- }
63
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
64
- try {
65
- for (_iterator = Object.entries(namedChunkGroups)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
66
- _step_value = _sliced_to_array(_step.value, 2), name = _step_value[0], chunkGroup = _step_value[1];
67
- assets = chunkGroup.assets.map(function(asset2) {
68
- var filename2 = asset2.name;
69
- return publicPath ? normalizePath(publicPath) + filename2 : filename2;
70
- });
71
- referenceCssAssets = assets.filter(function(asset2) {
72
- return /\.css$/.test(asset2);
73
- });
74
- routeAssets[name] = {
75
- chunkIds: chunkGroup.chunks,
76
- assets: assets,
77
- referenceCssAssets: referenceCssAssets
78
- };
79
- }
80
- } catch (err) {
81
- _didIteratorError = true;
82
- _iteratorError = err;
83
- } finally {
84
- try {
85
- if (!_iteratorNormalCompletion && _iterator.return != null) {
86
- _iterator.return();
87
- }
88
- } finally {
89
- if (_didIteratorError) {
90
- throw _iteratorError;
91
- }
92
- }
93
- }
94
- manifest = {
95
- routeAssets: routeAssets
96
- };
97
- injectedContent = "\n ;(function(){\n window.".concat(ROUTE_MANIFEST, " = ").concat(JSON.stringify(manifest, function(k, v) {
98
- if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
99
- return v.map(function(item) {
100
- return item.replace(publicPath, "");
101
- });
102
- }
103
- return v;
104
- }), ";\n })();\n ");
105
- entrypointsArray = Array.from(compilation.entrypoints.entries());
106
- entryChunkIds = entrypointsArray.map(function(entrypoint) {
107
- return entrypoint[0];
47
+ stats = compilation.getStats().toJson({
48
+ all: false,
49
+ publicPath: true,
50
+ assets: true,
51
+ chunkGroups: true,
52
+ chunks: true,
53
+ ids: true
54
+ });
55
+ publicPath = stats.publicPath, _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks, namedChunkGroups = stats.namedChunkGroups;
56
+ routeAssets = {};
57
+ if (!namedChunkGroups) {
58
+ return [
59
+ 2
60
+ ];
61
+ }
62
+ prevManifestAsset = compilation.getAsset(ROUTE_MANIFEST_FILE);
63
+ prevManifestStr = prevManifestAsset ? prevManifestAsset.source.source().toString() : JSON.stringify({
64
+ routeAssets: {}
65
+ });
66
+ prevManifest = JSON.parse(prevManifestStr);
67
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
68
+ try {
69
+ for (_iterator = Object.entries(namedChunkGroups)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
70
+ _step_value = _sliced_to_array(_step.value, 2), name = _step_value[0], chunkGroup = _step_value[1];
71
+ assets = chunkGroup.assets.map(function(asset2) {
72
+ var filename = asset2.name;
73
+ return publicPath ? normalizePath(publicPath) + filename : filename;
108
74
  });
109
- entryChunks = _to_consumable_array(chunks).filter(function(chunk) {
110
- var _chunk_names;
111
- return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some(function(name2) {
112
- return entryChunkIds.includes(name2);
113
- });
75
+ referenceCssAssets = assets.filter(function(asset2) {
76
+ return /\.css$/.test(asset2);
114
77
  });
115
- entryChunkFiles = entryChunks.map(function(chunk) {
116
- return _to_consumable_array(chunk.files || []).find(function(fname) {
117
- return fname.includes(".js");
78
+ routeAssets[name] = {
79
+ chunkIds: chunkGroup.chunks,
80
+ assets: assets,
81
+ referenceCssAssets: referenceCssAssets
82
+ };
83
+ if (prevManifest.routeAssets[name]) {
84
+ mergeWith(routeAssets[name], prevManifest.routeAssets[name], function(obj, source) {
85
+ if (Array.isArray(obj)) {
86
+ return obj.concat(source);
87
+ }
88
+ return Object.assign(source, obj);
118
89
  });
90
+ }
91
+ }
92
+ } catch (err) {
93
+ _didIteratorError = true;
94
+ _iteratorError = err;
95
+ } finally {
96
+ try {
97
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
98
+ _iterator.return();
99
+ }
100
+ } finally {
101
+ if (_didIteratorError) {
102
+ throw _iteratorError;
103
+ }
104
+ }
105
+ }
106
+ manifest = {
107
+ routeAssets: routeAssets
108
+ };
109
+ injectedContent = "\n ;(function(){\n window.".concat(ROUTE_MANIFEST, " = ").concat(JSON.stringify(manifest, function(k, v) {
110
+ if ((k === "assets" || k === "referenceCssAssets") && Array.isArray(v)) {
111
+ return v.map(function(item) {
112
+ return item.replace(publicPath, "");
119
113
  });
120
- _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
121
- try {
122
- for (_iterator1 = entryChunkFiles[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
123
- file = _step1.value;
124
- asset = compilation.assets[file];
125
- if (!asset) {
126
- continue;
127
- }
128
- newContent = "".concat(injectedContent).concat(asset.source().toString());
129
- newAssetsMap.set(path.join(outputPath, file), newContent);
130
- compilation.updateAsset(
131
- file,
132
- new RawSource(newContent),
133
- // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
134
- void 0
135
- );
136
- }
137
- } catch (err) {
138
- _didIteratorError1 = true;
139
- _iteratorError1 = err;
140
- } finally {
141
- try {
142
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
143
- _iterator1.return();
144
- }
145
- } finally {
146
- if (_didIteratorError1) {
147
- throw _iteratorError1;
148
- }
149
- }
114
+ }
115
+ return v;
116
+ }), ";\n })();\n ");
117
+ entrypointsArray = Array.from(compilation.entrypoints.entries());
118
+ entryChunkIds = entrypointsArray.map(function(entrypoint) {
119
+ return entrypoint[0];
120
+ });
121
+ entryChunks = _to_consumable_array(chunks).filter(function(chunk) {
122
+ var _chunk_names;
123
+ return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some(function(name2) {
124
+ return entryChunkIds.includes(name2);
125
+ });
126
+ });
127
+ entryChunkFiles = entryChunks.map(function(chunk) {
128
+ return _to_consumable_array(chunk.files || []).find(function(fname) {
129
+ return fname.includes(".js");
130
+ });
131
+ });
132
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
133
+ try {
134
+ for (_iterator1 = entryChunkFiles[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
135
+ file = _step1.value;
136
+ asset = compilation.assets[file];
137
+ if (!asset) {
138
+ continue;
139
+ }
140
+ newContent = "".concat(injectedContent).concat(asset.source().toString());
141
+ newAssetsMap.set(path.join(outputPath, file), newContent);
142
+ compilation.updateAsset(
143
+ file,
144
+ new RawSource(newContent),
145
+ // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
146
+ void 0
147
+ );
148
+ }
149
+ } catch (err) {
150
+ _didIteratorError1 = true;
151
+ _iteratorError1 = err;
152
+ } finally {
153
+ try {
154
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
155
+ _iterator1.return();
150
156
  }
151
- filename = path.join(outputPath, ROUTE_MANIFEST_FILE);
152
- return [
153
- 4,
154
- fs.ensureFile(filename)
155
- ];
156
- case 1:
157
- _state.sent();
158
- return [
159
- 4,
160
- fs.writeFile(filename, JSON.stringify(manifest, null, 2))
161
- ];
162
- case 2:
163
- _state.sent();
164
- return [
165
- 2
166
- ];
157
+ } finally {
158
+ if (_didIteratorError1) {
159
+ throw _iteratorError1;
160
+ }
161
+ }
162
+ }
163
+ if (prevManifestAsset) {
164
+ compilation.updateAsset(
165
+ ROUTE_MANIFEST_FILE,
166
+ new RawSource(JSON.stringify(manifest, null, 2)),
167
+ // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
168
+ void 0
169
+ );
170
+ } else {
171
+ compilation.emitAsset(ROUTE_MANIFEST_FILE, new RawSource(JSON.stringify(manifest, null, 2)));
167
172
  }
173
+ return [
174
+ 2
175
+ ];
168
176
  });
169
177
  }));
170
178
  });