@modern-js/app-tools 2.7.0 → 2.8.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 (76) hide show
  1. package/CHANGELOG.md +53 -2
  2. package/README.md +2 -2
  3. package/dist/cjs/analyze/generateCode.js +16 -2
  4. package/dist/cjs/analyze/index.js +0 -32
  5. package/dist/cjs/analyze/templates.js +26 -2
  6. package/dist/cjs/analyze/utils.js +5 -0
  7. package/dist/cjs/builder/builder-webpack/builderPlugins/compatModern.js +2 -13
  8. package/dist/cjs/builder/generator/index.js +10 -2
  9. package/dist/cjs/builder/index.js +1 -1
  10. package/dist/cjs/builder/shared/builderPlugins/adapterBasic.js +101 -0
  11. package/dist/cjs/builder/shared/builderPlugins/adapterHtml.js +91 -0
  12. package/dist/cjs/builder/shared/builderPlugins/adapterSSR.js +164 -0
  13. package/dist/cjs/builder/{builder-webpack/webpackPlugins → shared/builderPlugins}/index.js +5 -3
  14. package/dist/cjs/builder/{builder-webpack/webpackPlugins → shared/bundlerPlugins}/RouterPlugin.js +11 -5
  15. package/dist/cjs/builder/shared/bundlerPlugins/index.js +19 -0
  16. package/dist/cjs/builder/shared/index.js +1 -1
  17. package/dist/cjs/index.js +1 -1
  18. package/dist/cjs/locale/en.js +1 -0
  19. package/dist/cjs/locale/zh.js +1 -0
  20. package/dist/esm/analyze/generateCode.js +34 -19
  21. package/dist/esm/analyze/index.js +2 -25
  22. package/dist/esm/analyze/templates.js +16 -2
  23. package/dist/esm/analyze/utils.js +4 -1
  24. package/dist/esm/builder/builder-webpack/builderPlugins/compatModern.js +0 -11
  25. package/dist/esm/builder/generator/index.js +6 -4
  26. package/dist/esm/builder/index.js +1 -1
  27. package/dist/esm/builder/shared/builderPlugins/adapterBasic.js +101 -0
  28. package/dist/esm/builder/shared/builderPlugins/adapterHtml.js +120 -0
  29. package/dist/esm/builder/shared/builderPlugins/adapterSSR.js +350 -0
  30. package/dist/esm/builder/shared/builderPlugins/index.js +3 -0
  31. package/dist/esm/builder/{builder-webpack/webpackPlugins → shared/bundlerPlugins}/RouterPlugin.js +10 -6
  32. package/dist/esm/builder/shared/bundlerPlugins/index.js +3 -0
  33. package/dist/esm/builder/shared/index.js +1 -1
  34. package/dist/esm/index.js +1 -1
  35. package/dist/esm/locale/en.js +1 -0
  36. package/dist/esm/locale/zh.js +1 -0
  37. package/dist/esm-node/analyze/generateCode.js +29 -4
  38. package/dist/esm-node/analyze/index.js +1 -39
  39. package/dist/esm-node/analyze/templates.js +25 -2
  40. package/dist/esm-node/analyze/utils.js +4 -0
  41. package/dist/esm-node/builder/builder-webpack/builderPlugins/compatModern.js +2 -13
  42. package/dist/esm-node/builder/generator/index.js +10 -2
  43. package/dist/esm-node/builder/index.js +1 -1
  44. package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.js +67 -0
  45. package/dist/esm-node/builder/shared/builderPlugins/adapterHtml.js +67 -0
  46. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.js +143 -0
  47. package/dist/esm-node/builder/shared/builderPlugins/index.js +3 -0
  48. package/dist/esm-node/builder/{builder-webpack/webpackPlugins → shared/bundlerPlugins}/RouterPlugin.js +11 -5
  49. package/dist/esm-node/builder/shared/bundlerPlugins/index.js +3 -0
  50. package/dist/esm-node/builder/shared/index.js +1 -1
  51. package/dist/esm-node/index.js +1 -1
  52. package/dist/esm-node/locale/en.js +1 -0
  53. package/dist/esm-node/locale/zh.js +1 -0
  54. package/dist/types/analyze/templates.d.ts +3 -2
  55. package/dist/types/analyze/utils.d.ts +2 -1
  56. package/dist/types/builder/shared/builderPlugins/adapterBasic.d.ts +7 -0
  57. package/dist/types/builder/shared/builderPlugins/adapterHtml.d.ts +5 -0
  58. package/dist/types/builder/shared/builderPlugins/adapterSSR.d.ts +4 -0
  59. package/dist/types/builder/shared/builderPlugins/index.d.ts +3 -0
  60. package/dist/types/builder/shared/bundlerPlugins/RouterPlugin.d.ts +5 -0
  61. package/dist/types/builder/shared/bundlerPlugins/index.d.ts +3 -0
  62. package/dist/types/builder/shared/index.d.ts +1 -1
  63. package/dist/types/builder/shared/types.d.ts +7 -4
  64. package/dist/types/locale/en.d.ts +1 -0
  65. package/dist/types/locale/index.d.ts +2 -0
  66. package/dist/types/locale/zh.d.ts +1 -0
  67. package/dist/types/types/config/output.d.ts +2 -15
  68. package/package.json +26 -26
  69. package/dist/cjs/builder/shared/builderPlugins/adapterModern.js +0 -234
  70. package/dist/esm/builder/builder-webpack/webpackPlugins/index.js +0 -1
  71. package/dist/esm/builder/shared/builderPlugins/adapterModern.js +0 -300
  72. package/dist/esm-node/builder/builder-webpack/webpackPlugins/index.js +0 -1
  73. package/dist/esm-node/builder/shared/builderPlugins/adapterModern.js +0 -202
  74. package/dist/types/builder/builder-webpack/webpackPlugins/RouterPlugin.d.ts +0 -4
  75. package/dist/types/builder/builder-webpack/webpackPlugins/index.d.ts +0 -1
  76. package/dist/types/builder/shared/builderPlugins/adapterModern.d.ts +0 -13
@@ -0,0 +1,350 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _asyncToGenerator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ var __generator = this && this.__generator || function(thisArg, body) {
31
+ var f, y, t, g, _ = {
32
+ label: 0,
33
+ sent: function() {
34
+ if (t[0] & 1) throw t[1];
35
+ return t[1];
36
+ },
37
+ trys: [],
38
+ ops: []
39
+ };
40
+ return(g = {
41
+ next: verb(0),
42
+ "throw": verb(1),
43
+ "return": verb(2)
44
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
+ return this;
46
+ }), g);
47
+ function verb(n) {
48
+ return function(v) {
49
+ return step([
50
+ n,
51
+ v
52
+ ]);
53
+ };
54
+ }
55
+ function step(op) {
56
+ if (f) throw new TypeError("Generator is already executing.");
57
+ while(_)try {
58
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
+ if (y = 0, t) op = [
60
+ op[0] & 2,
61
+ t.value
62
+ ];
63
+ switch(op[0]){
64
+ case 0:
65
+ case 1:
66
+ t = op;
67
+ break;
68
+ case 4:
69
+ _.label++;
70
+ return {
71
+ value: op[1],
72
+ done: false
73
+ };
74
+ case 5:
75
+ _.label++;
76
+ y = op[1];
77
+ op = [
78
+ 0
79
+ ];
80
+ continue;
81
+ case 7:
82
+ op = _.ops.pop();
83
+ _.trys.pop();
84
+ continue;
85
+ default:
86
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
+ _ = 0;
88
+ continue;
89
+ }
90
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
+ _.label = op[1];
92
+ break;
93
+ }
94
+ if (op[0] === 6 && _.label < t[1]) {
95
+ _.label = t[1];
96
+ t = op;
97
+ break;
98
+ }
99
+ if (t && _.label < t[2]) {
100
+ _.label = t[2];
101
+ _.ops.push(op);
102
+ break;
103
+ }
104
+ if (t[2]) _.ops.pop();
105
+ _.trys.pop();
106
+ continue;
107
+ }
108
+ op = body.call(thisArg, _);
109
+ } catch (e) {
110
+ op = [
111
+ 6,
112
+ e
113
+ ];
114
+ y = 0;
115
+ } finally{
116
+ f = t = 0;
117
+ }
118
+ if (op[0] & 5) throw op[1];
119
+ return {
120
+ value: op[0] ? op[1] : void 0,
121
+ done: true
122
+ };
123
+ }
124
+ };
125
+ import { mergeBuilderConfig } from "@modern-js/builder-shared";
126
+ import { fs } from "@modern-js/utils";
127
+ import { HtmlAsyncChunkPlugin, RouterPlugin } from "../bundlerPlugins";
128
+ import { getServerCombinedModueFile } from "../../../analyze/utils";
129
+ import { isHtmlEnabled } from "./adapterHtml";
130
+ var builderPluginAdapterSSR = function(options) {
131
+ return {
132
+ name: "builder-plugin-adapter-modern-ssr",
133
+ setup: function setup(api) {
134
+ var normalizedConfig = options.normalizedConfig;
135
+ api.modifyBuilderConfig(function(config) {
136
+ if (isStreamingSSR(normalizedConfig)) {
137
+ return mergeBuilderConfig(config, {
138
+ html: {
139
+ inject: "body"
140
+ }
141
+ });
142
+ }
143
+ return config;
144
+ });
145
+ api.modifyBundlerChain(function() {
146
+ var _ref = _asyncToGenerator(function(chain, param) {
147
+ var target, CHAIN_ID, isProd, HtmlBundlerPlugin, isServer, builderConfig;
148
+ return __generator(this, function(_state) {
149
+ switch(_state.label){
150
+ case 0:
151
+ target = param.target, CHAIN_ID = param.CHAIN_ID, isProd = param.isProd, HtmlBundlerPlugin = param.HtmlPlugin, isServer = param.isServer;
152
+ builderConfig = api.getNormalizedConfig();
153
+ applyRouterPlugin(chain, options);
154
+ return [
155
+ 4,
156
+ applySSRLoaderEntry(chain, options, isServer)
157
+ ];
158
+ case 1:
159
+ _state.sent();
160
+ if ([
161
+ "node",
162
+ "service-worker"
163
+ ].includes(target)) {
164
+ applyFilterEntriesBySSRConfig({
165
+ isProd: isProd,
166
+ chain: chain,
167
+ appNormalizedConfig: options.normalizedConfig
168
+ });
169
+ }
170
+ if (isHtmlEnabled(builderConfig, target)) {
171
+ applyAsyncChunkHtmlPlugin({
172
+ chain: chain,
173
+ modernConfig: options.normalizedConfig,
174
+ CHAIN_ID: CHAIN_ID,
175
+ HtmlBundlerPlugin: HtmlBundlerPlugin
176
+ });
177
+ }
178
+ return [
179
+ 2
180
+ ];
181
+ }
182
+ });
183
+ });
184
+ return function(chain, _) {
185
+ return _ref.apply(this, arguments);
186
+ };
187
+ }());
188
+ }
189
+ };
190
+ };
191
+ var isStreamingSSR = function(userConfig) {
192
+ var isStreaming = function(ssr) {
193
+ return ssr && typeof ssr === "object" && ssr.mode === "stream";
194
+ };
195
+ var server = userConfig.server;
196
+ if (isStreaming(server.ssr)) {
197
+ return true;
198
+ }
199
+ if ((server === null || server === void 0 ? void 0 : server.ssrByEntries) && typeof server.ssrByEntries === "object") {
200
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
201
+ try {
202
+ for(var _iterator = Object.keys(server.ssrByEntries)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
203
+ var name = _step.value;
204
+ if (isStreaming(server.ssrByEntries[name])) {
205
+ return true;
206
+ }
207
+ }
208
+ } catch (err) {
209
+ _didIteratorError = true;
210
+ _iteratorError = err;
211
+ } finally{
212
+ try {
213
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
214
+ _iterator.return();
215
+ }
216
+ } finally{
217
+ if (_didIteratorError) {
218
+ throw _iteratorError;
219
+ }
220
+ }
221
+ }
222
+ }
223
+ return false;
224
+ };
225
+ function applyAsyncChunkHtmlPlugin(param) {
226
+ var chain = param.chain, modernConfig = param.modernConfig, CHAIN_ID = param.CHAIN_ID, HtmlBundlerPlugin = param.HtmlBundlerPlugin;
227
+ if (isStreamingSSR(modernConfig)) {
228
+ chain.plugin(CHAIN_ID.PLUGIN.HTML_ASYNC_CHUNK).use(HtmlAsyncChunkPlugin, [
229
+ HtmlBundlerPlugin
230
+ ]);
231
+ }
232
+ }
233
+ function applyRouterPlugin(chain, options) {
234
+ var _normalizedConfig_runtime;
235
+ var appContext = options.appContext, normalizedConfig = options.normalizedConfig;
236
+ var entrypoints = appContext.entrypoints;
237
+ var existNestedRoutes = entrypoints.some(function(entrypoint) {
238
+ return entrypoint.nestedRoutesEntry;
239
+ });
240
+ var routerConfig = normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig_runtime = normalizedConfig.runtime) === null || _normalizedConfig_runtime === void 0 ? void 0 : _normalizedConfig_runtime.router;
241
+ var routerManifest = Boolean(routerConfig === null || routerConfig === void 0 ? void 0 : routerConfig.manifest);
242
+ if (existNestedRoutes || routerManifest) {
243
+ chain.plugin("route-plugin").use(RouterPlugin);
244
+ }
245
+ }
246
+ function applyFilterEntriesBySSRConfig(param) {
247
+ var isProd = param.isProd, chain = param.chain, appNormalizedConfig = param.appNormalizedConfig;
248
+ var _outputConfig_ssg;
249
+ var serverConfig = appNormalizedConfig.server, outputConfig = appNormalizedConfig.output;
250
+ var entries = chain.entryPoints.entries();
251
+ if (isProd && ((outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg) === true || typeof (outputConfig === null || outputConfig === void 0 ? void 0 : (_outputConfig_ssg = outputConfig.ssg) === null || _outputConfig_ssg === void 0 ? void 0 : _outputConfig_ssg[0]) === "function")) {
252
+ return;
253
+ }
254
+ if (typeof entries === "undefined") {
255
+ throw new Error("No entry found, one of src/routes/layout.tsx, src/App.tsx, src/index.tsx is required");
256
+ }
257
+ var entryNames = Object.keys(entries);
258
+ if (isProd && entryNames.length === 1 && (outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg)) {
259
+ return;
260
+ }
261
+ var ssgEntries = [];
262
+ if (isProd && (outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg)) {
263
+ var ssg = outputConfig.ssg;
264
+ entryNames.forEach(function(name) {
265
+ if (ssg[name]) {
266
+ ssgEntries.push(name);
267
+ }
268
+ });
269
+ }
270
+ var _ref = serverConfig || {}, ssr = _ref.ssr, ssrByEntries = _ref.ssrByEntries;
271
+ entryNames.forEach(function(name) {
272
+ if (!ssgEntries.includes(name) && (ssr && (ssrByEntries === null || ssrByEntries === void 0 ? void 0 : ssrByEntries[name]) === false || !ssr && !(ssrByEntries === null || ssrByEntries === void 0 ? void 0 : ssrByEntries[name]))) {
273
+ chain.entryPoints.delete(name);
274
+ }
275
+ });
276
+ }
277
+ function applySSRLoaderEntry(chain, optinos, isServer) {
278
+ return _applySSRLoaderEntry.apply(this, arguments);
279
+ }
280
+ function _applySSRLoaderEntry() {
281
+ _applySSRLoaderEntry = _asyncToGenerator(function(chain, optinos, isServer) {
282
+ var appContext, internalDirectory, entrypoints;
283
+ return __generator(this, function(_state) {
284
+ switch(_state.label){
285
+ case 0:
286
+ appContext = optinos.appContext;
287
+ internalDirectory = appContext.internalDirectory;
288
+ entrypoints = appContext.entrypoints;
289
+ return [
290
+ 4,
291
+ Promise.all(entrypoints.map(function() {
292
+ var _ref = _asyncToGenerator(function(entrypoint) {
293
+ var entryName, serverLoadersFile, err;
294
+ return __generator(this, function(_state) {
295
+ switch(_state.label){
296
+ case 0:
297
+ entryName = entrypoint.entryName;
298
+ serverLoadersFile = getServerCombinedModueFile(internalDirectory, entryName);
299
+ if (!isServer) return [
300
+ 3,
301
+ 4
302
+ ];
303
+ _state.label = 1;
304
+ case 1:
305
+ _state.trys.push([
306
+ 1,
307
+ 3,
308
+ ,
309
+ 4
310
+ ]);
311
+ return [
312
+ 4,
313
+ fs.access(serverLoadersFile, fs.constants.F_OK)
314
+ ];
315
+ case 2:
316
+ _state.sent();
317
+ chain.entry("".concat(entryName, "-server-loaders")).add(serverLoadersFile);
318
+ return [
319
+ 3,
320
+ 4
321
+ ];
322
+ case 3:
323
+ err = _state.sent();
324
+ return [
325
+ 3,
326
+ 4
327
+ ];
328
+ case 4:
329
+ return [
330
+ 2
331
+ ];
332
+ }
333
+ });
334
+ });
335
+ return function(entrypoint) {
336
+ return _ref.apply(this, arguments);
337
+ };
338
+ }()))
339
+ ];
340
+ case 1:
341
+ _state.sent();
342
+ return [
343
+ 2
344
+ ];
345
+ }
346
+ });
347
+ });
348
+ return _applySSRLoaderEntry.apply(this, arguments);
349
+ }
350
+ export { builderPluginAdapterSSR };
@@ -0,0 +1,3 @@
1
+ export * from "./adapterBasic";
2
+ export * from "./adapterHtml";
3
+ export * from "./adapterSSR";
@@ -235,7 +235,7 @@ var RouterPlugin = /*#__PURE__*/ function() {
235
235
  name: PLUGIN_NAME,
236
236
  stage: PROCESS_ASSETS_STAGE_REPORT
237
237
  }, /*#__PURE__*/ _asyncToGenerator(function() {
238
- var stats, publicPath, routeAssets, namedChunkGroups, assetsByChunkName, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, manifest, injectedContent, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, file, asset, newContent, filename;
238
+ var stats, publicPath, _stats_chunks, chunks, routeAssets, namedChunkGroups, assetsByChunkName, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, name, chunkGroup, manifest, injectedContent, entrypointsArray, entryChunkIds, entryChunks, entryChunkFiles, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, file, asset, newContent, filename;
239
239
  return __generator(this, function(_state) {
240
240
  switch(_state.label){
241
241
  case 0:
@@ -243,7 +243,7 @@ var RouterPlugin = /*#__PURE__*/ function() {
243
243
  chunkGroups: true,
244
244
  chunks: true
245
245
  });
246
- publicPath = stats.publicPath;
246
+ publicPath = stats.publicPath, _stats_chunks = stats.chunks, chunks = _stats_chunks === void 0 ? [] : _stats_chunks;
247
247
  routeAssets = {};
248
248
  namedChunkGroups = stats.namedChunkGroups, assetsByChunkName = stats.assetsByChunkName;
249
249
  if (!namedChunkGroups || !assetsByChunkName) {
@@ -287,11 +287,14 @@ var RouterPlugin = /*#__PURE__*/ function() {
287
287
  entryChunkIds = entrypointsArray.map(function(entrypoint) {
288
288
  return entrypoint[0];
289
289
  });
290
- entryChunks = _toConsumableArray(compilation.chunks).filter(function(chunk) {
291
- return entryChunkIds.includes(chunk.name);
290
+ entryChunks = _toConsumableArray(chunks).filter(function(chunk) {
291
+ var _chunk_names;
292
+ return (_chunk_names = chunk.names) === null || _chunk_names === void 0 ? void 0 : _chunk_names.some(function(name) {
293
+ return entryChunkIds.includes(name);
294
+ });
292
295
  });
293
296
  entryChunkFiles = entryChunks.map(function(chunk) {
294
- return _toConsumableArray(chunk.files).find(function(fname) {
297
+ return _toConsumableArray(chunk.files || []).find(function(fname) {
295
298
  return fname.includes(".js");
296
299
  });
297
300
  });
@@ -302,7 +305,8 @@ var RouterPlugin = /*#__PURE__*/ function() {
302
305
  asset = compilation.assets[file];
303
306
  newContent = "".concat(injectedContent).concat(asset.source().toString());
304
307
  newAssetsMap.set(path.join(outputPath, file), newContent);
305
- compilation.updateAsset(file, new RawSource(newContent));
308
+ compilation.updateAsset(file, new RawSource(newContent), // FIXME: The arguments third of updatgeAsset is a optional function in webpack.
309
+ void 0);
306
310
  }
307
311
  } catch (err) {
308
312
  _didIteratorError1 = true;
@@ -0,0 +1,3 @@
1
+ export * from "./HtmlAsyncChunkPlugin";
2
+ export * from "./HtmlBottomTemplate";
3
+ export * from "./RouterPlugin";
@@ -1,3 +1,3 @@
1
1
  export * from "./createCopyPattern";
2
2
  export * from "./types";
3
- export * from "./builderPlugins/adapterModern";
3
+ export * from "./builderPlugins";
package/dist/esm/index.js CHANGED
@@ -201,7 +201,7 @@ var devCommand = function() {
201
201
  ];
202
202
  case 1:
203
203
  devToolMetas = _state.sent();
204
- devProgram = program.command("dev").alias("start").usage("[options]").description(i18n.t(localeKeys.command.dev.describe)).option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("-e --entry [entry...]", i18n.t(localeKeys.command.dev.entry)).option("--analyze", i18n.t(localeKeys.command.shared.analyze)).option("--api-only", i18n.t(localeKeys.command.dev.apiOnly)).action(function() {
204
+ devProgram = program.command("dev").alias("start").usage("[options]").description(i18n.t(localeKeys.command.dev.describe)).option("-c --config <config>", i18n.t(localeKeys.command.shared.config)).option("-e --entry [entry...]", i18n.t(localeKeys.command.dev.entry)).option("--analyze", i18n.t(localeKeys.command.shared.analyze)).option("--api-only", i18n.t(localeKeys.command.dev.apiOnly)).option("--web-only", i18n.t(localeKeys.command.dev.webOnly)).action(function() {
205
205
  var _ref = _asyncToGenerator(function(options) {
206
206
  var dev;
207
207
  return __generator(this, function(_state) {
@@ -8,6 +8,7 @@ var EN_LOCALE = {
8
8
  describe: "start dev server",
9
9
  entry: "compiler by entry",
10
10
  apiOnly: "start api server only",
11
+ webOnly: "start web server only",
11
12
  selectEntry: "Please select the entry that needs to be built",
12
13
  requireEntry: "You must choose at least one entry"
13
14
  },
@@ -8,6 +8,7 @@ var ZH_LOCALE = {
8
8
  describe: "本地开发命令",
9
9
  entry: "指定入口,编译特定的页面",
10
10
  apiOnly: "仅启动 API 接口服务",
11
+ webOnly: "仅启动 Web 服务",
11
12
  selectEntry: "请选择需要构建的入口",
12
13
  requireEntry: "请至少选择一个入口"
13
14
  },
@@ -1,5 +1,12 @@
1
1
  import path from "path";
2
- import { fs, getEntryOptions, isRouterV5, logger } from "@modern-js/utils";
2
+ import {
3
+ fs,
4
+ getEntryOptions,
5
+ isRouterV5,
6
+ isSSGEntry,
7
+ isUseSSRBundle,
8
+ logger
9
+ } from "@modern-js/utils";
3
10
  import * as templates from "./templates";
4
11
  import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
5
12
  import {
@@ -7,7 +14,11 @@ import {
7
14
  ENTRY_POINT_FILE_NAME,
8
15
  ENTRY_BOOTSTRAP_FILE_NAME
9
16
  } from "./constants";
10
- import { getDefaultImports, getServerLoadersFile } from "./utils";
17
+ import {
18
+ getDefaultImports,
19
+ getServerLoadersFile,
20
+ getServerCombinedModueFile
21
+ } from "./utils";
11
22
  import { walk } from "./nestedRoutes";
12
23
  const createImportSpecifier = (specifiers) => {
13
24
  let defaults = "";
@@ -109,6 +120,7 @@ const generateCode = async (appContext, config, entrypoints, api) => {
109
120
  config2.server.ssrByEntries,
110
121
  packageName
111
122
  );
123
+ const useSSG = isSSGEntry(config2, entryName, entrypoints);
112
124
  let mode;
113
125
  if (ssr) {
114
126
  mode = typeof ssr === "object" ? ssr.mode || "string" : "string";
@@ -128,13 +140,13 @@ const generateCode = async (appContext, config, entrypoints, api) => {
128
140
  entrypoint,
129
141
  code: await templates.fileSystemRoutes({
130
142
  routes,
131
- ssrMode: mode,
143
+ ssrMode: useSSG ? "string" : mode,
132
144
  nestedRoutesEntry: entrypoint.nestedRoutesEntry,
133
145
  entryName: entrypoint.entryName,
134
146
  internalDirectory
135
147
  })
136
148
  });
137
- if (entrypoint.nestedRoutesEntry && mode) {
149
+ if (entrypoint.nestedRoutesEntry && isUseSSRBundle(config2)) {
138
150
  const routesServerFile = getServerLoadersFile(
139
151
  internalDirectory,
140
152
  entryName
@@ -145,6 +157,19 @@ const generateCode = async (appContext, config, entrypoints, api) => {
145
157
  await fs.ensureFile(routesServerFile);
146
158
  await fs.writeFile(routesServerFile, code3);
147
159
  }
160
+ const serverLoaderCombined = templates.ssrLoaderCombinedModule(
161
+ entrypoints,
162
+ entrypoint,
163
+ config2,
164
+ appContext
165
+ );
166
+ if (serverLoaderCombined) {
167
+ const serverLoaderFile = getServerCombinedModueFile(
168
+ internalDirectory,
169
+ entryName
170
+ );
171
+ await fs.outputFile(serverLoaderFile, serverLoaderCombined);
172
+ }
148
173
  fs.outputFileSync(
149
174
  path.resolve(
150
175
  internalDirectory,
@@ -3,26 +3,19 @@ import {
3
3
  createDebugger,
4
4
  findExists,
5
5
  fs,
6
- getEntryOptions,
7
6
  isApiOnly,
8
7
  minimist,
9
8
  getCommand,
10
9
  isDevCommand
11
10
  } from "@modern-js/utils";
12
11
  import { cloneDeep } from "@modern-js/utils/lodash";
13
- import { createVirtualModule } from "@modern-js/builder-shared";
14
12
  import { printInstructions } from "../utils/printInstructions";
15
13
  import { generateRoutes } from "../utils/routes";
16
14
  import { emitResolvedConfig } from "../utils/config";
17
15
  import { getSelectedEntries } from "../utils/getSelectedEntries";
18
16
  import { initialNormalizedConfig } from "../config";
19
17
  import { createBuilderGenerator } from "../builder";
20
- import {
21
- getServerLoadersFile,
22
- isPageComponentFile,
23
- parseModule,
24
- replaceWithAlias
25
- } from "./utils";
18
+ import { isPageComponentFile, parseModule, replaceWithAlias } from "./utils";
26
19
  import {
27
20
  APP_CONFIG_NAME,
28
21
  APP_INIT_EXPORTED,
@@ -176,37 +169,6 @@ var analyze_default = ({
176
169
  watchFiles() {
177
170
  return pagesDir;
178
171
  },
179
- config() {
180
- return {
181
- tools: {
182
- webpackChain: (chain, { name }) => {
183
- const appContext = api.useAppContext();
184
- const resolvedConfig = api.useResolvedConfigContext();
185
- const { entrypoints, internalDirectory, packageName } = appContext;
186
- entrypoints.forEach((entrypoint) => {
187
- const { entryName } = entrypoint;
188
- const ssr = getEntryOptions(
189
- entryName,
190
- resolvedConfig.server.ssr,
191
- resolvedConfig.server.ssrByEntries,
192
- packageName
193
- );
194
- if (entrypoint.nestedRoutesEntry && ssr && name === "server") {
195
- const serverLoaderRuntime = require.resolve("@modern-js/plugin-data-loader/runtime");
196
- const serverLoadersFile = getServerLoadersFile(
197
- internalDirectory,
198
- entryName
199
- );
200
- const combinedModule = createVirtualModule(
201
- `export * from "${serverLoaderRuntime}"; export * from "${serverLoadersFile}"`
202
- );
203
- chain.entry(`${entryName}-server-loaders`).add(combinedModule);
204
- }
205
- });
206
- }
207
- }
208
- };
209
- },
210
172
  resolvedConfig({ resolved }) {
211
173
  const appContext = api.useAppContext();
212
174
  const config = initialNormalizedConfig(resolved, appContext, bundler);
@@ -1,6 +1,7 @@
1
1
  import path from "path";
2
- import { fs, slash } from "@modern-js/utils";
2
+ import { fs, getEntryOptions, isSSGEntry, slash } from "@modern-js/utils";
3
3
  import { APP_CONFIG_NAME, TEMP_LOADERS_DIR } from "./constants";
4
+ import { getServerLoadersFile } from "./utils";
4
5
  const index = ({
5
6
  mountId,
6
7
  imports,
@@ -283,10 +284,32 @@ const fileSystemRoutes = async ({
283
284
  ${routeComponentsCode}
284
285
  `;
285
286
  };
287
+ function ssrLoaderCombinedModule(entrypoints, entrypoint, config, appContext) {
288
+ const { entryName } = entrypoint;
289
+ const { packageName, internalDirectory } = appContext;
290
+ const ssr = getEntryOptions(
291
+ entryName,
292
+ config.server.ssr,
293
+ config.server.ssrByEntries,
294
+ packageName
295
+ );
296
+ const ssg = isSSGEntry(config, entryName, entrypoints);
297
+ if (entrypoint.nestedRoutesEntry && (ssr || ssg)) {
298
+ const serverLoaderRuntime = require.resolve("@modern-js/plugin-data-loader/runtime");
299
+ const serverLoadersFile = getServerLoadersFile(
300
+ internalDirectory,
301
+ entryName
302
+ );
303
+ const combinedModule = `export * from "${serverLoaderRuntime}"; export * from "${serverLoadersFile}"`;
304
+ return combinedModule;
305
+ }
306
+ return null;
307
+ }
286
308
  export {
287
309
  fileSystemRoutes,
288
310
  html,
289
311
  index,
290
312
  renderFunction,
291
- routesForServer
313
+ routesForServer,
314
+ ssrLoaderCombinedModule
292
315
  };
@@ -110,8 +110,12 @@ const hasLoader = async (filename) => {
110
110
  const getServerLoadersFile = (internalDirectory, entryName) => {
111
111
  return path.join(internalDirectory, entryName, "route-server-loaders.js");
112
112
  };
113
+ const getServerCombinedModueFile = (internalDirectory, entryName) => {
114
+ return path.join(internalDirectory, entryName, "server-loader-combined.js");
115
+ };
113
116
  export {
114
117
  getDefaultImports,
118
+ getServerCombinedModueFile,
115
119
  getServerLoadersFile,
116
120
  hasLoader,
117
121
  isPageComponentFile,
@@ -1,12 +1,10 @@
1
1
  import { join } from "path";
2
2
  import { createCopyPattern } from "../../shared";
3
- import { RouterPlugin } from "../webpackPlugins";
4
3
  const builderPluginCompatModern = (options) => ({
5
4
  name: "builder-plugin-compat-modern",
6
5
  setup(api) {
7
6
  const { normalizedConfig: modernConfig, appContext } = options;
8
7
  api.modifyWebpackChain((chain, { CHAIN_ID }) => {
9
- var _a;
10
8
  chain.resolve.modules.add("node_modules").add(join(api.context.rootPath, "node_modules"));
11
9
  if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) {
12
10
  const defaultCopyPattern = createCopyPattern(
@@ -16,23 +14,14 @@ const builderPluginCompatModern = (options) => ({
16
14
  chain
17
15
  );
18
16
  chain.plugin(CHAIN_ID.PLUGIN.COPY).tap((args) => {
19
- var _a2;
17
+ var _a;
20
18
  return [
21
19
  {
22
- patterns: [...((_a2 = args[0]) == null ? void 0 : _a2.patterns) || [], defaultCopyPattern]
20
+ patterns: [...((_a = args[0]) == null ? void 0 : _a.patterns) || [], defaultCopyPattern]
23
21
  }
24
22
  ];
25
23
  });
26
24
  }
27
- const { entrypoints } = appContext;
28
- const existNestedRoutes = entrypoints.some(
29
- (entrypoint) => entrypoint.nestedRoutesEntry
30
- );
31
- const routerConfig = (_a = modernConfig == null ? void 0 : modernConfig.runtime) == null ? void 0 : _a.router;
32
- const routerManifest = Boolean(routerConfig == null ? void 0 : routerConfig.manifest);
33
- if (existNestedRoutes || routerManifest) {
34
- chain.plugin("route-plugin").use(RouterPlugin);
35
- }
36
25
  });
37
26
  }
38
27
  });