@modern-js/app-tools 2.21.1 → 2.22.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 (85) hide show
  1. package/CHANGELOG.md +91 -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 +17 -12
  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 +38 -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 +23 -15
  71. package/dist/esm/analyze/utils.js +12 -1
  72. package/dist/esm/builder/generator/createBuilderOptions.js +4 -3
  73. package/dist/esm/builder/generator/createBuilderProviderConfig.js +1 -1
  74. package/dist/esm/builder/shared/bundlerPlugins/RouterPlugin.js +126 -118
  75. package/dist/esm-node/analyze/generateCode.js +39 -22
  76. package/dist/esm-node/analyze/index.js +16 -13
  77. package/dist/esm-node/analyze/utils.js +12 -1
  78. package/dist/esm-node/builder/generator/createBuilderOptions.js +4 -3
  79. package/dist/esm-node/builder/generator/createBuilderProviderConfig.js +2 -2
  80. package/dist/esm-node/builder/shared/bundlerPlugins/RouterPlugin.js +27 -6
  81. package/dist/types/analyze/generateCode.d.ts +19 -2
  82. package/dist/types/analyze/utils.d.ts +2 -1
  83. package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +7 -0
  84. package/dist/types/types/hooks.d.ts +1 -0
  85. package/package.json +24 -24
@@ -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
  });
@@ -53,11 +53,14 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
53
53
  const { internalDirectory, srcDirectory, internalDirAlias, internalSrcAlias, packageName } = appContext;
54
54
  const hookRunners = api.useHookRunners();
55
55
  const isV5 = isRouterV5(config);
56
- const { mountId } = config.html;
57
56
  const getRoutes = isV5 ? getClientRoutesLegacy : getClientRoutes;
57
+ const importsStatemets = /* @__PURE__ */ new Map();
58
58
  await Promise.all(entrypoints.map(generateEntryCode));
59
+ return {
60
+ importsStatemets
61
+ };
59
62
  async function generateEntryCode(entrypoint) {
60
- const { entryName, isAutoMount, customBootstrap, fileSystemRoutes } = entrypoint;
63
+ const { entryName, isAutoMount, fileSystemRoutes } = entrypoint;
61
64
  if (isAutoMount) {
62
65
  if (fileSystemRoutes) {
63
66
  var _config_output;
@@ -99,7 +102,7 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
99
102
  process.exit(1);
100
103
  }
101
104
  }
102
- const { code: code2 } = await hookRunners.beforeGenerateRoutes({
105
+ const { code } = await hookRunners.beforeGenerateRoutes({
103
106
  entrypoint,
104
107
  code: await templates.fileSystemRoutes({
105
108
  routes,
@@ -112,20 +115,20 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
112
115
  });
113
116
  if (entrypoint.nestedRoutesEntry && isUseSSRBundle(config2)) {
114
117
  const routesServerFile = getServerLoadersFile(internalDirectory, entryName);
115
- const code3 = templates.routesForServer({
118
+ const code2 = templates.routesForServer({
116
119
  routes
117
120
  });
118
121
  await fs.ensureFile(routesServerFile);
119
- await fs.writeFile(routesServerFile, code3);
122
+ await fs.writeFile(routesServerFile, code2);
120
123
  }
121
124
  const serverLoaderCombined = templates.ssrLoaderCombinedModule(entrypoints, entrypoint, config2, appContext);
122
125
  if (serverLoaderCombined) {
123
126
  const serverLoaderFile = getServerCombinedModueFile(internalDirectory, entryName);
124
127
  await fs.outputFile(serverLoaderFile, serverLoaderCombined);
125
128
  }
126
- fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code2, "utf8");
129
+ fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code, "utf8");
127
130
  }
128
- const { imports: importStatements } = await hookRunners.modifyEntryImports({
131
+ const { imports } = await hookRunners.modifyEntryImports({
129
132
  entrypoint,
130
133
  imports: getDefaultImports({
131
134
  entrypoint,
@@ -135,9 +138,23 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
135
138
  internalDirectory
136
139
  })
137
140
  });
141
+ importsStatemets.set(entryName, imports);
142
+ const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
143
+ entrypoint.internalEntry = entryFile;
144
+ }
145
+ }
146
+ };
147
+ export const generateIndexCode = async ({ appContext, api, entrypoints, config, importsStatemets, bundlerConfigs }) => {
148
+ const hookRunners = api.useHookRunners();
149
+ const { mountId } = config.html;
150
+ const { internalDirectory, packageName } = appContext;
151
+ await Promise.all(entrypoints.map(async (entrypoint) => {
152
+ const { entryName, isAutoMount, customBootstrap, fileSystemRoutes } = entrypoint;
153
+ if (isAutoMount) {
138
154
  const { plugins } = await hookRunners.modifyEntryRuntimePlugins({
139
155
  entrypoint,
140
- plugins: []
156
+ plugins: [],
157
+ bundlerConfigs
141
158
  });
142
159
  const { code: renderFunction } = await hookRunners.modifyEntryRenderFunction({
143
160
  entrypoint,
@@ -151,31 +168,31 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
151
168
  entrypoint,
152
169
  exportStatement: "export default AppWrapper;"
153
170
  });
171
+ const imports = importsStatemets.get(entryName);
154
172
  const code = templates.index({
155
173
  mountId,
156
- imports: createImportStatements(importStatements),
174
+ imports: createImportStatements(imports),
157
175
  renderFunction,
158
176
  exportStatement
159
177
  });
160
178
  const entryFile = path.resolve(internalDirectory, `./${entryName}/${ENTRY_POINT_FILE_NAME}`);
161
- entrypoint.entry = entryFile;
162
179
  if (config.source.enableAsyncEntry) {
163
180
  let rawAsyncEntryCode = `import('./${ENTRY_BOOTSTRAP_FILE_NAME}');`;
164
181
  const ssr = getEntryOptions(entryName, config.server.ssr, config.server.ssrByEntries, packageName);
165
182
  if (ssr) {
166
183
  rawAsyncEntryCode = `
167
- export const ${SERVER_RENDER_FUNCTION_NAME} = async (...args) => {
168
- let entry = await ${rawAsyncEntryCode};
169
- if (entry.default instanceof Promise){
170
- entry = await entry.default;
171
- return entry.default.${SERVER_RENDER_FUNCTION_NAME}.apply(null, args);
172
- }
173
- return entry.${SERVER_RENDER_FUNCTION_NAME}.apply(null, args);
174
- };
175
- if(typeof window!=='undefined'){
176
- ${rawAsyncEntryCode}
184
+ export const ${SERVER_RENDER_FUNCTION_NAME} = async (...args) => {
185
+ let entry = await ${rawAsyncEntryCode};
186
+ if (entry.default instanceof Promise){
187
+ entry = await entry.default;
188
+ return entry.default.${SERVER_RENDER_FUNCTION_NAME}.apply(null, args);
177
189
  }
178
- `;
190
+ return entry.${SERVER_RENDER_FUNCTION_NAME}.apply(null, args);
191
+ };
192
+ if(typeof window!=='undefined'){
193
+ ${rawAsyncEntryCode}
194
+ }
195
+ `;
179
196
  }
180
197
  const { code: asyncEntryCode } = await hookRunners.modifyAsyncEntry({
181
198
  entrypoint,
@@ -188,5 +205,5 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
188
205
  fs.outputFileSync(entryFile, code, "utf8");
189
206
  }
190
207
  }
191
- }
208
+ }));
192
209
  };
@@ -1,5 +1,5 @@
1
1
  import * as path from "path";
2
- import { createDebugger, findExists, fs, isApiOnly, minimist, getCommand, isDevCommand, getArgv } from "@modern-js/utils";
2
+ import { createDebugger, findExists, fs, isApiOnly, minimist, isDevCommand, getArgv } from "@modern-js/utils";
3
3
  import { cloneDeep } from "@modern-js/utils/lodash";
4
4
  import { printInstructions } from "../utils/printInstructions";
5
5
  import { generateRoutes } from "../utils/routes";
@@ -7,8 +7,9 @@ import { emitResolvedConfig } from "../utils/config";
7
7
  import { getSelectedEntries } from "../utils/getSelectedEntries";
8
8
  import { initialNormalizedConfig } from "../config";
9
9
  import { createBuilderGenerator } from "../builder";
10
- import { isPageComponentFile, parseModule, replaceWithAlias } from "./utils";
10
+ import { checkIsBuildCommands, isPageComponentFile, parseModule, replaceWithAlias } from "./utils";
11
11
  import { APP_CONFIG_NAME, APP_INIT_EXPORTED, APP_INIT_IMPORTED } from "./constants";
12
+ import { generateIndexCode } from "./generateCode";
12
13
  const debug = createDebugger("plugin-analyze");
13
14
  export default ({ bundler }) => {
14
15
  return {
@@ -24,7 +25,9 @@ export default ({ bundler }) => {
24
25
  const resolvedConfig = api.useResolvedConfigContext();
25
26
  const hookRunners = api.useHookRunners();
26
27
  try {
27
- fs.emptydirSync(appContext.internalDirectory);
28
+ if (checkIsBuildCommands()) {
29
+ fs.emptydirSync(appContext.internalDirectory);
30
+ }
28
31
  } catch {
29
32
  }
30
33
  const apiOnly = await isApiOnly(appContext.appDirectory, (_resolvedConfig_source = resolvedConfig.source) === null || _resolvedConfig_source === void 0 ? void 0 : _resolvedConfig_source.entriesDir);
@@ -67,7 +70,7 @@ export default ({ bundler }) => {
67
70
  nestedRouteEntries = entrypoints.map((point) => point.nestedRoutesEntry).filter(Boolean);
68
71
  pagesDir = entrypoints.map((point) => point.entry).filter((entry) => entry && !path.extname(entry)).concat(nestedRouteEntries);
69
72
  originEntrypoints = cloneDeep(entrypoints);
70
- await generateCode(appContext, resolvedConfig, entrypoints, api);
73
+ const { importsStatemets } = await generateCode(appContext, resolvedConfig, entrypoints, api);
71
74
  const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
72
75
  appContext,
73
76
  config: resolvedConfig
@@ -89,15 +92,7 @@ export default ({ bundler }) => {
89
92
  htmlTemplates
90
93
  };
91
94
  api.setAppContext(appContext);
92
- const command = getCommand();
93
- const buildCommands = [
94
- "dev",
95
- "start",
96
- "build",
97
- "inspect",
98
- "deploy"
99
- ];
100
- if (buildCommands.includes(command)) {
95
+ if (checkIsBuildCommands()) {
101
96
  const normalizedConfig = api.useResolvedConfigContext();
102
97
  const createBuilderForModern = await createBuilderGenerator(bundler);
103
98
  const builder = await createBuilderForModern({
@@ -130,6 +125,14 @@ export default ({ bundler }) => {
130
125
  },
131
126
  async onBeforeCreateCompiler({ bundlerConfigs }) {
132
127
  const hookRunners2 = api.useHookRunners();
128
+ await generateIndexCode({
129
+ appContext,
130
+ config: resolvedConfig,
131
+ entrypoints,
132
+ api,
133
+ importsStatemets,
134
+ bundlerConfigs
135
+ });
133
136
  await hookRunners2.beforeCreateCompiler({
134
137
  bundlerConfigs
135
138
  });
@@ -1,6 +1,6 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import { isReact18, normalizeToPosixPath, fs as fse } from "@modern-js/utils";
3
+ import { isReact18, normalizeToPosixPath, fs as fse, getCommand } from "@modern-js/utils";
4
4
  import { transform } from "esbuild";
5
5
  import { parse } from "es-module-lexer";
6
6
  import { FILE_SYSTEM_ROUTES_FILE_NAME, JS_EXTENSIONS, LOADER_EXPORT_NAME } from "./constants";
@@ -137,3 +137,14 @@ export const getServerLoadersFile = (internalDirectory, entryName) => {
137
137
  export const getServerCombinedModueFile = (internalDirectory, entryName) => {
138
138
  return path.join(internalDirectory, entryName, "server-loader-combined.js");
139
139
  };
140
+ export const checkIsBuildCommands = () => {
141
+ const buildCommands = [
142
+ "dev",
143
+ "start",
144
+ "build",
145
+ "inspect",
146
+ "deploy"
147
+ ];
148
+ const command = getCommand();
149
+ return buildCommands.includes(command);
150
+ };
@@ -1,15 +1,16 @@
1
1
  export function createBuilderOptions(target, appContext) {
2
2
  const entries = {};
3
3
  const { entrypoints = [], checkedEntries } = appContext;
4
- for (const { entryName, entry } of entrypoints) {
4
+ for (const { entryName, internalEntry, entry } of entrypoints) {
5
5
  if (checkedEntries && !checkedEntries.includes(entryName)) {
6
6
  continue;
7
7
  }
8
+ const finalEntry = internalEntry || entry;
8
9
  if (entryName in entries) {
9
- entries[entryName].push(entry);
10
+ entries[entryName].push(finalEntry);
10
11
  } else {
11
12
  entries[entryName] = [
12
- entry
13
+ finalEntry
13
14
  ];
14
15
  }
15
16
  }
@@ -4,8 +4,8 @@ export function createBuilderProviderConfig(resolveConfig, appContext, modifyBui
4
4
  };
5
5
  if (!htmlConfig.template) {
6
6
  htmlConfig.templateByEntries = {
7
- ...htmlConfig.templateByEntries,
8
- ...appContext.htmlTemplates
7
+ ...appContext.htmlTemplates,
8
+ ...htmlConfig.templateByEntries
9
9
  };
10
10
  }
11
11
  const config = {
@@ -1,5 +1,6 @@
1
1
  import path from "path";
2
- import { fs, ROUTE_MANIFEST_FILE } from "@modern-js/utils";
2
+ import { mergeWith } from "@modern-js/utils/lodash";
3
+ import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
3
4
  import { ROUTE_MANIFEST } from "@modern-js/utils/universal/constants";
4
5
  const PLUGIN_NAME = "ModernjsRoutePlugin";
5
6
  export class RouterPlugin {
@@ -41,10 +42,15 @@ export class RouterPlugin {
41
42
  if (!namedChunkGroups) {
42
43
  return;
43
44
  }
45
+ const prevManifestAsset = compilation.getAsset(ROUTE_MANIFEST_FILE);
46
+ const prevManifestStr = prevManifestAsset ? prevManifestAsset.source.source().toString() : JSON.stringify({
47
+ routeAssets: {}
48
+ });
49
+ const prevManifest = JSON.parse(prevManifestStr);
44
50
  for (const [name, chunkGroup] of Object.entries(namedChunkGroups)) {
45
51
  const assets = chunkGroup.assets.map((asset) => {
46
- const filename2 = asset.name;
47
- return publicPath ? normalizePath(publicPath) + filename2 : filename2;
52
+ const filename = asset.name;
53
+ return publicPath ? normalizePath(publicPath) + filename : filename;
48
54
  });
49
55
  const referenceCssAssets = assets.filter((asset) => /\.css$/.test(asset));
50
56
  routeAssets[name] = {
@@ -52,6 +58,14 @@ export class RouterPlugin {
52
58
  assets,
53
59
  referenceCssAssets
54
60
  };
61
+ if (prevManifest.routeAssets[name]) {
62
+ mergeWith(routeAssets[name], prevManifest.routeAssets[name], (obj, source) => {
63
+ if (Array.isArray(obj)) {
64
+ return obj.concat(source);
65
+ }
66
+ return Object.assign(source, obj);
67
+ });
68
+ }
55
69
  }
56
70
  const manifest = {
57
71
  routeAssets
@@ -93,9 +107,16 @@ export class RouterPlugin {
93
107
  void 0
94
108
  );
95
109
  }
96
- const filename = path.join(outputPath, ROUTE_MANIFEST_FILE);
97
- await fs.ensureFile(filename);
98
- await fs.writeFile(filename, JSON.stringify(manifest, null, 2));
110
+ if (prevManifestAsset) {
111
+ compilation.updateAsset(
112
+ ROUTE_MANIFEST_FILE,
113
+ new RawSource(JSON.stringify(manifest, null, 2)),
114
+ // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
115
+ void 0
116
+ );
117
+ } else {
118
+ compilation.emitAsset(ROUTE_MANIFEST_FILE, new RawSource(JSON.stringify(manifest, null, 2)));
119
+ }
99
120
  });
100
121
  });
101
122
  }
@@ -1,5 +1,22 @@
1
1
  import { IAppContext, PluginAPI } from '@modern-js/core';
2
2
  import type { Entrypoint } from '@modern-js/types';
3
- import { AppNormalizedConfig, AppTools, ImportStatement } from '../types';
3
+ import { AppNormalizedConfig, AppTools, ImportStatement, Rspack, webpack } from '../types';
4
4
  export declare const createImportStatements: (statements: ImportStatement[]) => string;
5
- export declare const generateCode: (appContext: IAppContext, config: AppNormalizedConfig<'shared'>, entrypoints: Entrypoint[], api: PluginAPI<AppTools<'shared'>>) => Promise<void>;
5
+ export declare const generateCode: (appContext: IAppContext, config: AppNormalizedConfig<'shared'>, entrypoints: Entrypoint[], api: PluginAPI<AppTools<'shared'>>) => Promise<{
6
+ importsStatemets: Map<string, ImportStatement[]>;
7
+ }>;
8
+ export declare const generateIndexCode: ({
9
+ appContext,
10
+ api,
11
+ entrypoints,
12
+ config,
13
+ importsStatemets,
14
+ bundlerConfigs
15
+ }: {
16
+ appContext: IAppContext;
17
+ api: PluginAPI<AppTools<'shared'>>;
18
+ entrypoints: Entrypoint[];
19
+ config: AppNormalizedConfig<'shared'>;
20
+ importsStatemets: Map<string, ImportStatement[]>;
21
+ bundlerConfigs?: webpack.Configuration[] | Rspack.RspackOptions[] | undefined;
22
+ }) => Promise<void>;
@@ -25,4 +25,5 @@ export declare const parseModule: ({
25
25
  }) => Promise<readonly [imports: readonly import("es-module-lexer").ImportSpecifier[], exports: readonly import("es-module-lexer").ExportSpecifier[], facade: boolean]>;
26
26
  export declare const hasLoader: (filename: string) => Promise<boolean>;
27
27
  export declare const getServerLoadersFile: (internalDirectory: string, entryName: string) => string;
28
- export declare const getServerCombinedModueFile: (internalDirectory: string, entryName: string) => string;
28
+ export declare const getServerCombinedModueFile: (internalDirectory: string, entryName: string) => string;
29
+ export declare const checkIsBuildCommands: () => boolean;
@@ -1,5 +1,12 @@
1
1
  import type { webpack } from '@modern-js/builder-webpack-provider';
2
2
  import type { Rspack } from '@modern-js/builder-rspack-provider';
3
+ export interface RouteAssets {
4
+ [routeId: string]: {
5
+ chunkIds?: (string | number)[];
6
+ assets?: string[];
7
+ referenceCssAssets?: string[];
8
+ };
9
+ }
3
10
  export declare class RouterPlugin {
4
11
  apply(compiler: Rspack.Compiler | webpack.Compiler): void;
5
12
  }
@@ -31,6 +31,7 @@ export type AppToolsHooks<B extends Bundler = 'webpack'> = {
31
31
  modifyEntryRuntimePlugins: AsyncWaterfall<{
32
32
  entrypoint: Entrypoint;
33
33
  plugins: RuntimePlugin[];
34
+ bundlerConfigs?: B extends 'rspack' ? Rspack.Configuration[] : webpack.Configuration[];
34
35
  }>;
35
36
  modifyEntryRenderFunction: AsyncWaterfall<{
36
37
  entrypoint: Entrypoint;