@modern-js/server 1.1.3-beta.0 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/dist/js/modern/dev-tools/babel/register.js +2 -2
  3. package/dist/js/modern/dev-tools/mock/getMockData.js +2 -2
  4. package/dist/js/modern/libs/proxy.js +2 -2
  5. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +2 -2
  6. package/dist/js/modern/libs/render/cache/index.js +2 -2
  7. package/dist/js/modern/libs/render/cache/type.js +0 -1
  8. package/dist/js/modern/server/dev-server/dev-server-split.js +2 -6
  9. package/dist/js/modern/server/dev-server/dev-server.js +1 -1
  10. package/dist/js/modern/server/dev-server/index.js +1 -1
  11. package/dist/js/modern/server/index.js +17 -14
  12. package/dist/js/modern/server/modern-server-split.js +55 -6
  13. package/dist/js/modern/server/modern-server.js +85 -55
  14. package/dist/js/modern/type.js +0 -1
  15. package/dist/js/modern/utils.js +9 -1
  16. package/dist/js/node/dev-tools/babel/register.js +2 -2
  17. package/dist/js/node/dev-tools/mock/getMockData.js +2 -2
  18. package/dist/js/node/libs/proxy.js +2 -2
  19. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +2 -2
  20. package/dist/js/node/libs/render/cache/index.js +2 -2
  21. package/dist/js/node/server/dev-server/dev-server-split.js +5 -9
  22. package/dist/js/node/server/dev-server/dev-server.js +1 -1
  23. package/dist/js/node/server/dev-server/index.js +4 -4
  24. package/dist/js/node/server/index.js +16 -12
  25. package/dist/js/node/server/modern-server-split.js +61 -9
  26. package/dist/js/node/server/modern-server.js +86 -56
  27. package/dist/js/node/utils.js +12 -2
  28. package/dist/types/server/dev-server/dev-server-split.d.ts +3 -4
  29. package/dist/types/server/dev-server/index.d.ts +1 -1
  30. package/dist/types/server/modern-server-split.d.ts +15 -5
  31. package/dist/types/server/modern-server.d.ts +13 -6
  32. package/dist/types/type.d.ts +5 -0
  33. package/dist/types/utils.d.ts +2 -1
  34. package/package.json +17 -18
  35. package/src/server/dev-server/dev-server-split.ts +3 -7
  36. package/src/server/dev-server/dev-server.ts +7 -11
  37. package/src/server/dev-server/index.ts +1 -1
  38. package/src/server/index.ts +22 -12
  39. package/src/server/modern-server-split.ts +59 -7
  40. package/src/server/modern-server.ts +96 -62
  41. package/src/type.ts +5 -0
  42. package/src/utils.ts +14 -0
@@ -11,9 +11,9 @@ var _util = require("./util");
11
11
 
12
12
  var _constants = require("../../../constants");
13
13
 
14
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
14
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
15
15
 
16
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
16
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
17
17
 
18
18
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
19
 
@@ -3,11 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.WebModernDevServer = exports.APIModernDevServer = void 0;
6
+ exports.ModernSSRDevServer = exports.ModernAPIDevServer = void 0;
7
7
 
8
8
  var _devServer = require("./dev-server");
9
9
 
10
- class WebModernDevServer extends _devServer.ModernDevServer {
10
+ class ModernSSRDevServer extends _devServer.ModernDevServer {
11
11
  prepareAPIHandler(_m, _) {
12
12
  return null;
13
13
  }
@@ -22,13 +22,9 @@ class WebModernDevServer extends _devServer.ModernDevServer {
22
22
 
23
23
  }
24
24
 
25
- exports.WebModernDevServer = WebModernDevServer;
26
-
27
- class APIModernDevServer extends _devServer.ModernDevServer {
28
- prepareWebHandler(_) {
29
- return null;
30
- }
25
+ exports.ModernSSRDevServer = ModernSSRDevServer;
31
26
 
27
+ class ModernAPIDevServer extends _devServer.ModernDevServer {
32
28
  async prepareAPIHandler(mode, extension) {
33
29
  return super.prepareAPIHandler(mode, extension);
34
30
  }
@@ -42,4 +38,4 @@ class APIModernDevServer extends _devServer.ModernDevServer {
42
38
 
43
39
  }
44
40
 
45
- exports.APIModernDevServer = APIModernDevServer;
41
+ exports.ModernAPIDevServer = ModernAPIDevServer;
@@ -259,7 +259,7 @@ class ModernDevServer extends _modernServer.ModernServer {
259
259
  const {
260
260
  mock
261
261
  } = _constants.AGGRED_DIR;
262
- const defaultWatched = [`${pwd}/${mock}/**/*`, `${pwd}/${_utils.SERVER_DIR}/**/*`, `${pwd}/${_utils.API_DIR}/**/*`, `${pwd}/${_utils.SHARED_DIR}/**/*`];
262
+ const defaultWatched = [`${pwd}/${mock}/**/*`, `${pwd}/${_utils.SERVER_DIR}/**/*`, `${pwd}/${_utils.API_DIR}/!(typings)/**`, `${pwd}/${_utils.SHARED_DIR}/**/*`];
263
263
  const watcher = new _watcher.default();
264
264
  watcher.createDepTree(); // 监听文件变动,如果有变动则给 client,也就是 start 启动的插件发消息
265
265
 
@@ -3,10 +3,10 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "APIModernDevServer", {
6
+ Object.defineProperty(exports, "ModernAPIDevServer", {
7
7
  enumerable: true,
8
8
  get: function () {
9
- return _devServerSplit.APIModernDevServer;
9
+ return _devServerSplit.ModernAPIDevServer;
10
10
  }
11
11
  });
12
12
  Object.defineProperty(exports, "ModernDevServer", {
@@ -15,10 +15,10 @@ Object.defineProperty(exports, "ModernDevServer", {
15
15
  return _devServer.ModernDevServer;
16
16
  }
17
17
  });
18
- Object.defineProperty(exports, "WebModernDevServer", {
18
+ Object.defineProperty(exports, "ModernSSRDevServer", {
19
19
  enumerable: true,
20
20
  get: function () {
21
- return _devServerSplit.WebModernDevServer;
21
+ return _devServerSplit.ModernSSRDevServer;
22
22
  }
23
23
  });
24
24
 
@@ -21,9 +21,9 @@ var _measure = require("../libs/measure");
21
21
 
22
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
23
 
24
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
24
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
25
25
 
26
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
26
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
27
27
 
28
28
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
29
29
 
@@ -95,9 +95,11 @@ class Server {
95
95
  } = this;
96
96
 
97
97
  if (options.apiOnly) {
98
- return new _modernServerSplit.APIModernServer(options);
98
+ return new _modernServerSplit.ModernAPIServer(options);
99
+ } else if (options.ssrOnly) {
100
+ return new _modernServerSplit.ModernSSRServer(options);
99
101
  } else if (options.webOnly) {
100
- return new _modernServerSplit.WebModernServer(options);
102
+ return new _modernServerSplit.ModernWebServer(options);
101
103
  } else {
102
104
  return new _modernServer.ModernServer(options);
103
105
  }
@@ -109,15 +111,15 @@ class Server {
109
111
  } = this;
110
112
 
111
113
  const {
112
- APIModernDevServer,
113
- WebModernDevServer,
114
+ ModernAPIDevServer,
115
+ ModernSSRDevServer,
114
116
  ModernDevServer
115
117
  } = require("./dev-server");
116
118
 
117
119
  if (options.apiOnly) {
118
- return new APIModernDevServer(options);
119
- } else if (options.webOnly) {
120
- return new WebModernDevServer(options);
120
+ return new ModernAPIDevServer(options);
121
+ } else if (options.ssrOnly) {
122
+ return new ModernSSRDevServer(options);
121
123
  } else {
122
124
  return new ModernDevServer(options);
123
125
  }
@@ -129,17 +131,19 @@ class Server {
129
131
  const {
130
132
  options
131
133
  } = this;
134
+ (_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
135
+ _serverPlugin.serverManager.usePlugin(p);
136
+ });
132
137
  const appContext = await this.initAppContext();
133
138
 
134
139
  _serverPlugin.serverManager.run(() => {
140
+ _core.ConfigContext.set(this.options.config);
141
+
135
142
  _core.AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
136
143
  distDirectory: _path.default.join(options.pwd, options.config.output.path || 'dist')
137
144
  }));
138
145
  });
139
146
 
140
- (_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
141
- _serverPlugin.serverManager.usePlugin(p);
142
- });
143
147
  return _serverPlugin.serverManager.init({});
144
148
  }
145
149
 
@@ -3,28 +3,46 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.WebModernServer = exports.APIModernServer = void 0;
6
+ exports.ModernWebServer = exports.ModernSSRServer = exports.ModernAPIServer = void 0;
7
7
 
8
8
  var _modernServer = require("./modern-server");
9
9
 
10
- class WebModernServer extends _modernServer.ModernServer {
10
+ class ModernSSRServer extends _modernServer.ModernServer {
11
+ // Todo should not invoke any route hook in modernSSRServer
12
+ async warmupSSRBundle() {// empty
13
+ }
14
+
15
+ verifyMatch(context, matched) {
16
+ if (matched.generate().isApi) {
17
+ this.render404(context);
18
+ }
19
+ }
20
+
11
21
  prepareAPIHandler(_m, _) {
12
22
  return null;
13
23
  }
14
24
 
15
25
  async prepareWebHandler(extension) {
16
26
  return super.prepareWebHandler(extension);
17
- }
27
+ } // protected filterRoutes(routes: ModernRouteInterface[]) {
28
+ // return routes.filter(route => route.entryName);
29
+ // }
18
30
 
19
- filterRoutes(routes) {
20
- return routes.filter(route => route.entryName);
31
+
32
+ async preServerInit() {// empty
21
33
  }
22
34
 
23
35
  }
24
36
 
25
- exports.WebModernServer = WebModernServer;
37
+ exports.ModernSSRServer = ModernSSRServer;
38
+
39
+ class ModernAPIServer extends _modernServer.ModernServer {
40
+ async emitRouteHook(_, _input) {// empty
41
+ }
42
+
43
+ async warmupSSRBundle() {// empty
44
+ }
26
45
 
27
- class APIModernServer extends _modernServer.ModernServer {
28
46
  prepareWebHandler(_) {
29
47
  return null;
30
48
  }
@@ -37,9 +55,43 @@ class APIModernServer extends _modernServer.ModernServer {
37
55
  return routes.filter(route => route.isApi);
38
56
  }
39
57
 
40
- async preServerInit() {// noop
58
+ async preServerInit() {// empty
59
+ }
60
+
61
+ }
62
+
63
+ exports.ModernAPIServer = ModernAPIServer;
64
+
65
+ class ModernWebServer extends _modernServer.ModernServer {
66
+ async warmupSSRBundle() {// empty
67
+ }
68
+
69
+ async handleAPI(context) {
70
+ const {
71
+ proxyTarget
72
+ } = this;
73
+
74
+ if (!(proxyTarget !== null && proxyTarget !== void 0 && proxyTarget.api)) {
75
+ this.proxy();
76
+ } else {
77
+ this.render404(context);
78
+ }
79
+ }
80
+
81
+ async handleWeb(context, route) {
82
+ const {
83
+ proxyTarget
84
+ } = this;
85
+
86
+ if (route.isSSR && proxyTarget !== null && proxyTarget !== void 0 && proxyTarget.ssr) {
87
+ return this.proxy();
88
+ } else {
89
+ // if no proxyTarget but access web server, degradation to csr
90
+ route.isSSR = false;
91
+ return super.handleWeb(context, route);
92
+ }
41
93
  }
42
94
 
43
95
  }
44
96
 
45
- exports.APIModernServer = APIModernServer;
97
+ exports.ModernWebServer = ModernWebServer;
@@ -47,9 +47,9 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
47
47
 
48
48
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
49
49
 
50
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
50
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
51
51
 
52
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
52
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
53
53
 
54
54
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
55
55
 
@@ -71,7 +71,8 @@ class ModernServer {
71
71
  routes,
72
72
  staticGenerate,
73
73
  logger,
74
- measure
74
+ measure,
75
+ proxyTarget
75
76
  }) {
76
77
  this.pwd = void 0;
77
78
  this.distDir = void 0;
@@ -83,6 +84,7 @@ class ModernServer {
83
84
  this.runner = void 0;
84
85
  this.logger = void 0;
85
86
  this.measure = void 0;
87
+ this.proxyTarget = void 0;
86
88
  this.isDev = false;
87
89
  this.staticFileHandler = void 0;
88
90
  this.routeRenderHandler = void 0;
@@ -96,13 +98,14 @@ class ModernServer {
96
98
 
97
99
  this.isDev = Boolean(dev);
98
100
  this.pwd = pwd;
99
- this.distDir = _path.default.join(pwd, config.output.path || '');
101
+ this.distDir = _path.default.join(pwd, config.output.path || 'dist');
100
102
  this.workDir = this.isDev ? pwd : this.distDir;
101
103
  this.conf = config;
102
104
  this.logger = logger;
103
105
  this.measure = measure;
104
106
  this.router = new _route.RouteMatchManager();
105
107
  this.presetRoutes = routes;
108
+ this.proxyTarget = proxyTarget;
106
109
 
107
110
  if (staticGenerate) {
108
111
  this.staticGenerate = staticGenerate;
@@ -189,20 +192,6 @@ class ModernServer {
189
192
 
190
193
  async createHTTPServer(handler) {
191
194
  return (0, _http.createServer)(handler);
192
- } // warmup ssr function
193
-
194
-
195
- warmupSSRBundle() {
196
- const {
197
- distDir
198
- } = this;
199
- const bundles = this.router.getBundles();
200
- bundles.forEach(bundle => {
201
- const filepath = _path.default.join(distDir, bundle); // if error, just throw and let process die
202
-
203
-
204
- require(filepath);
205
- });
206
195
  } // read route spec from route.json
207
196
 
208
197
 
@@ -266,10 +255,13 @@ class ModernServer {
266
255
  const mode = _utils.fs.existsSync(_path.default.join(apiDir, _constants.AGGRED_DIR.lambda)) ? _constants.ApiServerMode.frame : _constants.ApiServerMode.func; // if use lambda/, mean framework style of writing, then discard user extension
267
256
 
268
257
  const apiExtension = (0, _utils2.mergeExtension)(pluginAPIExt);
269
- this.frameAPIHandler = await this.prepareAPIHandler(mode, _objectSpread(_objectSpread({}, apiExtension), {}, {
270
- modernJsConfig: this.conf
271
- }));
258
+ this.frameAPIHandler = await this.prepareAPIHandler(mode, apiExtension);
272
259
  }
260
+ } // Todo
261
+
262
+
263
+ async proxy() {
264
+ return null;
273
265
  }
274
266
  /* —————————————————————— function will be overwrite —————————————————————— */
275
267
 
@@ -311,34 +303,55 @@ class ModernServer {
311
303
  return routes;
312
304
  }
313
305
 
306
+ async emitRouteHook(eventName, input) {
307
+ return this.runner[eventName](input, {
308
+ onLast: _utils2.noop
309
+ });
310
+ } // warmup ssr function
311
+
312
+
313
+ warmupSSRBundle() {
314
+ const {
315
+ distDir
316
+ } = this;
317
+ const bundles = this.router.getBundles();
318
+ bundles.forEach(bundle => {
319
+ const filepath = _path.default.join(distDir, bundle); // if error, just throw and let process die
320
+
321
+
322
+ require(filepath);
323
+ });
324
+ }
325
+
314
326
  async preServerInit() {
315
327
  const {
316
- conf
328
+ conf,
329
+ runner
317
330
  } = this;
318
- const preMiddleware = await this.runner.preServerInit(conf);
331
+ const preMiddleware = await runner.preServerInit(conf);
319
332
  preMiddleware.flat().forEach(mid => {
320
333
  this.addHandler(mid);
321
334
  });
322
335
  }
323
336
 
324
- prepareFavicons(favicon, faviconByEntries) {
325
- const faviconNames = [];
337
+ async handleAPI(context) {
338
+ const {
339
+ req,
340
+ res
341
+ } = context;
326
342
 
327
- if (favicon) {
328
- faviconNames.push(favicon.substring(favicon.lastIndexOf('/') + 1));
343
+ if (!this.frameAPIHandler) {
344
+ throw new Error('can not found api hanlder');
329
345
  }
330
346
 
331
- if (faviconByEntries) {
332
- Object.keys(faviconByEntries).forEach(f => {
333
- const curFavicon = faviconByEntries[f];
347
+ await this.frameAPIHandler(req, res);
348
+ }
334
349
 
335
- if (curFavicon) {
336
- faviconNames.push(curFavicon.substring(curFavicon.lastIndexOf('/') + 1));
337
- }
338
- });
339
- }
350
+ async handleWeb(context, route) {
351
+ return this.routeRenderHandler(context, route);
352
+ }
340
353
 
341
- return faviconNames;
354
+ verifyMatch(_c, _m) {// empty
342
355
  }
343
356
  /* —————————————————————— private function —————————————————————— */
344
357
  // handler route.json, include api / csr / ssr
@@ -350,10 +363,8 @@ class ModernServer {
350
363
  req,
351
364
  res
352
365
  } = context;
353
- await this.runner.beforeMatch({
366
+ await this.emitRouteHook('beforeMatch', {
354
367
  context
355
- }, {
356
- onLast: _utils2.noop
357
368
  }); // match routes in the route spec
358
369
 
359
370
  const matched = this.router.match(context.url);
@@ -361,14 +372,18 @@ class ModernServer {
361
372
  if (!matched) {
362
373
  this.render404(context);
363
374
  return;
375
+ } else {
376
+ this.verifyMatch(context, matched);
377
+ }
378
+
379
+ if (res.headersSent) {
380
+ return;
364
381
  }
365
382
 
366
383
  const routeAPI = (0, _route2.createRouteAPI)(matched, this.router);
367
- await this.runner.afterMatch({
384
+ await this.emitRouteHook('afterMatch', {
368
385
  context,
369
386
  routeAPI
370
- }, {
371
- onLast: _utils2.noop
372
387
  });
373
388
 
374
389
  if (res.headersSent) {
@@ -383,11 +398,7 @@ class ModernServer {
383
398
  context.setParams(params); // route is api service
384
399
 
385
400
  if (route.isApi) {
386
- if (!this.frameAPIHandler) {
387
- throw new Error('can not found api hanlder');
388
- }
389
-
390
- await this.frameAPIHandler(req, res);
401
+ this.handleAPI(context);
391
402
  return;
392
403
  }
393
404
 
@@ -400,12 +411,13 @@ class ModernServer {
400
411
  return;
401
412
  }
402
413
 
403
- await this.runner.beforeRender({
404
- context
405
- }, {
406
- onLast: _utils2.noop
407
- });
408
- const file = await this.routeRenderHandler(context, route);
414
+ if (route.entryName) {
415
+ await this.emitRouteHook('beforeRender', {
416
+ context
417
+ });
418
+ }
419
+
420
+ const file = await this.handleWeb(context, route);
409
421
 
410
422
  if (!file) {
411
423
  this.render404(context);
@@ -423,11 +435,9 @@ class ModernServer {
423
435
 
424
436
  if (route.entryName) {
425
437
  const templateAPI = (0, _template.createTemplateAPI)(file.content.toString());
426
- await this.runner.afterRender({
438
+ await this.emitRouteHook('afterRender', {
427
439
  context,
428
440
  templateAPI
429
- }, {
430
- onLast: _utils2.noop
431
441
  });
432
442
  await this.injectMicroFE(context, templateAPI);
433
443
  response = templateAPI.get();
@@ -595,6 +605,26 @@ class ModernServer {
595
605
  res.end((0, _utils2.createErrorDocument)(status, text));
596
606
  }
597
607
 
608
+ prepareFavicons(favicon, faviconByEntries) {
609
+ const faviconNames = [];
610
+
611
+ if (favicon) {
612
+ faviconNames.push(favicon.substring(favicon.lastIndexOf('/') + 1));
613
+ }
614
+
615
+ if (faviconByEntries) {
616
+ Object.keys(faviconByEntries).forEach(f => {
617
+ const curFavicon = faviconByEntries[f];
618
+
619
+ if (curFavicon) {
620
+ faviconNames.push(curFavicon.substring(curFavicon.lastIndexOf('/') + 1));
621
+ }
622
+ });
623
+ }
624
+
625
+ return faviconNames;
626
+ }
627
+
598
628
  }
599
629
  /* eslint-enable max-lines */
600
630
 
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.applyMixins = applyMixins;
6
7
  exports.toMessage = exports.noop = exports.mergeExtension = exports.createErrorDocument = void 0;
7
8
 
8
9
  const mergeExtension = users => {
@@ -63,6 +64,15 @@ const createErrorDocument = (status, text) => {
63
64
  </body>
64
65
  </html>
65
66
  `;
66
- };
67
+ }; // This can live anywhere in your codebase:
68
+
69
+
70
+ exports.createErrorDocument = createErrorDocument;
67
71
 
68
- exports.createErrorDocument = createErrorDocument;
72
+ function applyMixins(derivedCtor, constructors) {
73
+ constructors.forEach(baseCtor => {
74
+ Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
75
+ Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null));
76
+ });
77
+ });
78
+ }
@@ -3,14 +3,13 @@ import { ModernDevServer } from './dev-server';
3
3
  import { mergeExtension } from "../../utils.d";
4
4
  import { ModernRouteInterface } from "../../libs/route";
5
5
  import { ApiServerMode } from "../../constants.d";
6
- export declare class WebModernDevServer extends ModernDevServer {
6
+ export declare class ModernSSRDevServer extends ModernDevServer {
7
7
  protected prepareAPIHandler(_m: ApiServerMode, _: APIServerStartInput['config']): any;
8
8
  protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
9
9
  protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
10
10
  }
11
- export declare class APIModernDevServer extends ModernDevServer {
12
- protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
13
- protected prepareAPIHandler(mode: ApiServerMode, extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
11
+ export declare class ModernAPIDevServer extends ModernDevServer {
12
+ protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<import("@modern-js/server-plugin").Adapter>;
14
13
  protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
15
14
  protected preServerInit(): Promise<void>;
16
15
  }
@@ -1,2 +1,2 @@
1
- export { APIModernDevServer, WebModernDevServer } from './dev-server-split';
1
+ export { ModernAPIDevServer, ModernSSRDevServer } from './dev-server-split';
2
2
  export { ModernDevServer } from './dev-server';
@@ -1,16 +1,26 @@
1
1
  import { APIServerStartInput } from '@modern-js/server-plugin';
2
2
  import { ModernServer } from './modern-server';
3
3
  import { mergeExtension } from "../utils.d";
4
- import { ModernRouteInterface } from "../libs/route";
4
+ import { ModernRoute, ModernRouteInterface, RouteMatcher } from "../libs/route";
5
5
  import { ApiServerMode } from "../constants.d";
6
- export declare class WebModernServer extends ModernServer {
7
- protected prepareAPIHandler(_m: ApiServerMode, _: ReturnType<typeof mergeExtension>): any;
6
+ import { ModernServerContext } from "../libs/context";
7
+ export declare class ModernSSRServer extends ModernServer {
8
+ protected warmupSSRBundle(): Promise<void>;
9
+ protected verifyMatch(context: ModernServerContext, matched: RouteMatcher): void;
10
+ protected prepareAPIHandler(_m: ApiServerMode, _: APIServerStartInput['config']): any;
8
11
  protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
9
- protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
12
+ protected preServerInit(): Promise<void>;
10
13
  }
11
- export declare class APIModernServer extends ModernServer {
14
+ export declare class ModernAPIServer extends ModernServer {
15
+ protected emitRouteHook(_: string, _input: any): Promise<void>;
16
+ protected warmupSSRBundle(): Promise<void>;
12
17
  protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
13
18
  protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<import("@modern-js/server-plugin").Adapter>;
14
19
  protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
15
20
  protected preServerInit(): Promise<void>;
21
+ }
22
+ export declare class ModernWebServer extends ModernServer {
23
+ protected warmupSSRBundle(): Promise<void>;
24
+ protected handleAPI(context: ModernServerContext): Promise<void>;
25
+ protected handleWeb(context: ModernServerContext, route: ModernRoute): Promise<any>;
16
26
  }
@@ -2,9 +2,9 @@
2
2
  import { IncomingMessage, ServerResponse, Server } from 'http';
3
3
  import { Adapter, APIServerStartInput } from '@modern-js/server-plugin';
4
4
  import type { NormalizedConfig } from '@modern-js/core';
5
- import { ModernServerOptions, NextFunction, ServerHookRunner, Measure, Logger, ReadyOptions } from '../type';
6
- import { RouteMatchManager, ModernRouteInterface } from '../libs/route';
7
- import { mergeExtension } from '../utils';
5
+ import { ModernServerOptions, NextFunction, ServerHookRunner, Measure, Logger, ReadyOptions } from "../type.d";
6
+ import { RouteMatchManager, ModernRouteInterface, ModernRoute, RouteMatcher } from "../libs/route";
7
+ import { mergeExtension } from "../utils.d";
8
8
  import { ModernServerContext } from "../libs/context";
9
9
  import { ApiServerMode } from "../constants.d";
10
10
  declare type ModernServerHandler = (context: ModernServerContext, next: NextFunction) => Promise<void> | void;
@@ -20,6 +20,7 @@ export declare class ModernServer {
20
20
  protected runner: ServerHookRunner;
21
21
  protected readonly logger: Logger;
22
22
  protected readonly measure: Measure;
23
+ protected readonly proxyTarget: ModernServerOptions['proxyTarget'];
23
24
  private readonly isDev;
24
25
  private staticFileHandler;
25
26
  private routeRenderHandler;
@@ -35,7 +36,8 @@ export declare class ModernServer {
35
36
  routes,
36
37
  staticGenerate,
37
38
  logger,
38
- measure
39
+ measure,
40
+ proxyTarget
39
41
  }: ModernServerOptions);
40
42
  getRequestHandler(): (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
41
43
  init(runner: ServerHookRunner): Promise<void>;
@@ -43,21 +45,26 @@ export declare class ModernServer {
43
45
  onListening(_: Server): void;
44
46
  close(): void;
45
47
  createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<Server>;
46
- protected warmupSSRBundle(): void;
47
48
  protected readRouteSpec(): ModernRouteInterface[];
48
49
  protected addHandler(handler: ModernServerHandler): void;
49
50
  protected render404(context: ModernServerContext): void;
50
51
  protected prepareFrameHandler(): Promise<void>;
52
+ protected proxy(): Promise<any>;
51
53
  protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<Adapter>;
52
54
  protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<Adapter>;
53
55
  protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
56
+ protected emitRouteHook(eventName: 'beforeMatch' | 'afterMatch' | 'beforeRender' | 'afterRender', input: any): Promise<any>;
57
+ protected warmupSSRBundle(): void;
54
58
  protected preServerInit(): Promise<void>;
55
- private prepareFavicons;
59
+ protected handleAPI(context: ModernServerContext): Promise<void>;
60
+ protected handleWeb(context: ModernServerContext, route: ModernRoute): Promise<import("@/type").RenderResult | null>;
61
+ protected verifyMatch(_c: ModernServerContext, _m: RouteMatcher): void;
56
62
  private routeHandler;
57
63
  private injectMicroFE;
58
64
  private compose;
59
65
  private requestHandler;
60
66
  private onError;
61
67
  private renderErrorPage;
68
+ private prepareFavicons;
62
69
  }
63
70
  export {};