@modern-js/server-core 2.65.5 → 2.65.6-alpha.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 (67) hide show
  1. package/dist/cjs/index.js +2 -5
  2. package/dist/cjs/plugins/compat/hooks.js +92 -0
  3. package/dist/cjs/plugins/compat/index.js +65 -0
  4. package/dist/cjs/plugins/customServer/index.js +11 -17
  5. package/dist/cjs/plugins/default.js +2 -0
  6. package/dist/cjs/plugins/index.js +5 -0
  7. package/dist/cjs/plugins/render/index.js +2 -2
  8. package/dist/cjs/serverBase.js +31 -40
  9. package/dist/cjs/types/index.js +2 -2
  10. package/dist/cjs/types/{plugin.js → plugins/base.js} +2 -2
  11. package/dist/cjs/types/plugins/index.js +24 -0
  12. package/dist/cjs/types/plugins/new.js +16 -0
  13. package/dist/cjs/types/plugins/old.js +16 -0
  14. package/dist/esm/index.js +1 -3
  15. package/dist/esm/plugins/compat/hooks.js +70 -0
  16. package/dist/esm/plugins/compat/index.js +45 -0
  17. package/dist/esm/plugins/customServer/index.js +30 -22
  18. package/dist/esm/plugins/default.js +2 -0
  19. package/dist/esm/plugins/index.js +3 -0
  20. package/dist/esm/plugins/render/index.js +3 -3
  21. package/dist/esm/serverBase.js +34 -38
  22. package/dist/esm/types/index.js +1 -1
  23. package/dist/esm/types/plugins/base.js +0 -0
  24. package/dist/esm/types/plugins/index.js +2 -0
  25. package/dist/esm/types/plugins/new.js +0 -0
  26. package/dist/esm/types/plugins/old.js +0 -0
  27. package/dist/esm-node/index.js +1 -3
  28. package/dist/esm-node/plugins/compat/hooks.js +66 -0
  29. package/dist/esm-node/plugins/compat/index.js +40 -0
  30. package/dist/esm-node/plugins/customServer/index.js +10 -16
  31. package/dist/esm-node/plugins/default.js +2 -0
  32. package/dist/esm-node/plugins/index.js +3 -0
  33. package/dist/esm-node/plugins/render/index.js +2 -2
  34. package/dist/esm-node/serverBase.js +31 -40
  35. package/dist/esm-node/types/index.js +1 -1
  36. package/dist/esm-node/types/plugins/index.js +2 -0
  37. package/dist/esm-node/types/plugins/old.js +0 -0
  38. package/dist/types/adapters/node/plugins/nodeServer.d.ts +4 -2
  39. package/dist/types/adapters/node/plugins/resource.d.ts +3 -3
  40. package/dist/types/adapters/node/plugins/static.d.ts +2 -2
  41. package/dist/types/index.d.ts +1 -2
  42. package/dist/types/plugins/compat/hooks.d.ts +8 -0
  43. package/dist/types/plugins/compat/index.d.ts +3 -0
  44. package/dist/types/plugins/customServer/index.d.ts +3 -3
  45. package/dist/types/plugins/default.d.ts +2 -2
  46. package/dist/types/plugins/favicon.d.ts +2 -2
  47. package/dist/types/plugins/index.d.ts +1 -0
  48. package/dist/types/plugins/log.d.ts +2 -2
  49. package/dist/types/plugins/monitors.d.ts +4 -4
  50. package/dist/types/plugins/processedBy.d.ts +2 -2
  51. package/dist/types/plugins/render/index.d.ts +2 -2
  52. package/dist/types/plugins/render/inject.d.ts +2 -2
  53. package/dist/types/plugins/route.d.ts +2 -2
  54. package/dist/types/serverBase.d.ts +11 -22
  55. package/dist/types/types/index.d.ts +1 -1
  56. package/dist/types/types/plugins/base.d.ts +80 -0
  57. package/dist/types/types/plugins/index.d.ts +3 -0
  58. package/dist/types/types/plugins/new.d.ts +34 -0
  59. package/dist/types/types/plugins/old.d.ts +56 -0
  60. package/package.json +4 -3
  61. package/dist/cjs/pluginManager.js +0 -106
  62. package/dist/esm/pluginManager.js +0 -140
  63. package/dist/esm-node/pluginManager.js +0 -82
  64. package/dist/types/pluginManager.d.ts +0 -31
  65. package/dist/types/types/plugin.d.ts +0 -150
  66. /package/dist/esm-node/types/{plugin.js → plugins/base.js} +0 -0
  67. /package/dist/{esm/types/plugin.js → esm-node/types/plugins/new.js} +0 -0
@@ -3,6 +3,7 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
3
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
5
  import { time } from "@modern-js/runtime-utils/time";
6
+ import { isArray, isFunction } from "@modern-js/utils";
6
7
  import { ServerTimings } from "../../constants";
7
8
  import { getLoaderCtx } from "../../helper";
8
9
  import { transformResponse } from "../../utils";
@@ -16,20 +17,16 @@ var isHtmlResponse = function(response) {
16
17
  };
17
18
  var CustomServer = /* @__PURE__ */ function() {
18
19
  "use strict";
19
- function CustomServer2(runner, serverBase, pwd) {
20
+ function CustomServer2(hooks, serverBase, pwd) {
20
21
  _class_call_check(this, CustomServer2);
21
- this.runner = runner;
22
+ this.hooks = hooks;
22
23
  this.serverBase = serverBase;
23
24
  var webExtension = [];
24
- this.serverMiddlewarePromise = runner.prepareWebServer({
25
+ this.serverMiddlewarePromise = hooks.prepareWebServer.call({
25
26
  pwd,
26
27
  config: {
27
28
  middleware: webExtension
28
29
  }
29
- }, {
30
- onLast: function() {
31
- return [];
32
- }
33
30
  });
34
31
  }
35
32
  var _proto = CustomServer2.prototype;
@@ -50,9 +47,7 @@ var CustomServer = /* @__PURE__ */ function() {
50
47
  getCost = time();
51
48
  return [
52
49
  4,
53
- _this.runner.afterMatch(afterMatchCtx, {
54
- onLast: noop
55
- })
50
+ _this.hooks.afterMatch.call(afterMatchCtx)
56
51
  ];
57
52
  case 1:
58
53
  _state.sent();
@@ -105,15 +100,30 @@ var CustomServer = /* @__PURE__ */ function() {
105
100
  3
106
101
  ];
107
102
  afterStreamingRenderContext = createAfterStreamingRenderContext(baseHookCtx, routeInfo);
108
- c.res = transformResponse(c.res, function(chunk) {
109
- var context = afterStreamingRenderContext(chunk);
110
- return _this.runner.afterStreamingRender(context, {
111
- onLast: function(param) {
112
- var _$chunk = param.chunk;
113
- return _$chunk;
114
- }
103
+ c.res = transformResponse(c.res, function() {
104
+ var _ref2 = _async_to_generator(function(chunk) {
105
+ var context, _ref3, newChunk;
106
+ return _ts_generator(this, function(_state2) {
107
+ switch (_state2.label) {
108
+ case 0:
109
+ context = afterStreamingRenderContext(chunk);
110
+ return [
111
+ 4,
112
+ _this.hooks.afterStreamingRender.call(context)
113
+ ];
114
+ case 1:
115
+ _ref3 = _state2.sent(), newChunk = _ref3.chunk;
116
+ return [
117
+ 2,
118
+ newChunk
119
+ ];
120
+ }
121
+ });
115
122
  });
116
- });
123
+ return function(chunk) {
124
+ return _ref2.apply(this, arguments);
125
+ };
126
+ }());
117
127
  return [
118
128
  3,
119
129
  6
@@ -128,9 +138,7 @@ var CustomServer = /* @__PURE__ */ function() {
128
138
  getCost1 = time();
129
139
  return [
130
140
  4,
131
- _this.runner.afterRender(afterRenderCtx, {
132
- onLast: noop
133
- })
141
+ _this.hooks.afterRender.call(afterRenderCtx)
134
142
  ];
135
143
  case 5:
136
144
  _state.sent();
@@ -170,7 +178,7 @@ var CustomServer = /* @__PURE__ */ function() {
170
178
  ];
171
179
  case 1:
172
180
  serverMiddleware = _state.sent();
173
- if (!serverMiddleware) {
181
+ if (!serverMiddleware || !isFunction(serverMiddleware) && !isArray(serverMiddleware)) {
174
182
  return [
175
183
  2,
176
184
  renderMiddlewares
@@ -1,3 +1,4 @@
1
+ import { compatPlugin } from "./compat";
1
2
  import { logPlugin } from "./log";
2
3
  import { initMonitorsPlugin, injectServerTiming, injectloggerPlugin } from "./monitors";
3
4
  import { processedByPlugin } from "./processedBy";
@@ -14,6 +15,7 @@ function createSilenceLogger() {
14
15
  function createDefaultPlugins() {
15
16
  var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
16
17
  var plugins = [
18
+ compatPlugin(),
17
19
  logPlugin(),
18
20
  initMonitorsPlugin(),
19
21
  injectRenderHandlerPlugin(options),
@@ -4,10 +4,13 @@ import { injectServerTiming, injectloggerPlugin } from "./monitors";
4
4
  import { processedByPlugin } from "./processedBy";
5
5
  import { logPlugin } from "./log";
6
6
  import { createDefaultPlugins } from "./default";
7
+ import { compatPlugin, handleSetupResult } from "./compat";
7
8
  export {
9
+ compatPlugin,
8
10
  createDefaultPlugins,
9
11
  faviconPlugin,
10
12
  getRenderHandler,
13
+ handleSetupResult,
11
14
  injectRenderHandlerPlugin,
12
15
  injectServerTiming,
13
16
  injectloggerPlugin,
@@ -13,19 +13,19 @@ var renderPlugin = function() {
13
13
  return {
14
14
  prepare: function prepare() {
15
15
  return _async_to_generator(function() {
16
- var _config_render, _api_useAppContext, middlewares, routes, render, pwd, serverBase, runner, config, customServer, serverMiddleware, pageRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, originUrlPath, _route_entryName, entryName, urlPath, customServerHookMiddleware, customServerMiddleware, err;
16
+ var _config_render, _api_useAppContext, middlewares, routes, render, pwd, serverBase, hooks, config, customServer, serverMiddleware, pageRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, originUrlPath, _route_entryName, entryName, urlPath, customServerHookMiddleware, customServerMiddleware, err;
17
17
  return _ts_generator(this, function(_state) {
18
18
  switch (_state.label) {
19
19
  case 0:
20
20
  _api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, routes = _api_useAppContext.routes, render = _api_useAppContext.render, pwd = _api_useAppContext.distDirectory, serverBase = _api_useAppContext.serverBase;
21
- runner = api.useHookRunners();
21
+ hooks = api.getHooks();
22
22
  config = api.useConfigContext();
23
23
  if (!routes) {
24
24
  return [
25
25
  2
26
26
  ];
27
27
  }
28
- customServer = new CustomServer(runner, serverBase, pwd);
28
+ customServer = new CustomServer(hooks, serverBase, pwd);
29
29
  serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
30
30
  pageRoutes = getPageRoutes(routes);
31
31
  middlewares.push({
@@ -5,48 +5,54 @@ import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_m
5
5
  import { _ as _create_class } from "@swc/helpers/_/_create_class";
6
6
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
7
7
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
8
- import { createContext } from "@modern-js/plugin";
8
+ import { server } from "@modern-js/plugin-v2/server";
9
9
  import { Hono } from "hono";
10
- import { PluginManager } from "./pluginManager";
11
- var _getAppContext = /* @__PURE__ */ new WeakSet(), _applyMiddlewares = /* @__PURE__ */ new WeakSet();
10
+ import { handleSetupResult } from "./plugins/compat/hooks";
11
+ import { loadConfig } from "./utils";
12
+ var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
12
13
  var ServerBase = /* @__PURE__ */ function() {
13
14
  "use strict";
14
15
  function ServerBase2(options) {
15
16
  _class_call_check(this, ServerBase2);
16
- _class_private_method_init(this, _getAppContext);
17
17
  _class_private_method_init(this, _applyMiddlewares);
18
+ this.plugins = [];
19
+ this.serverContext = null;
18
20
  this.options = options;
19
- var config = options.config, serverConfig = options.serverConfig;
20
- var appContext = _class_private_method_get(this, _getAppContext, getAppContext).call(this);
21
- this.appContext = appContext;
22
- this.pluginManager = new PluginManager({
23
- cliConfig: config,
24
- appContext,
25
- serverConfig
26
- });
27
21
  this.app = new Hono();
28
22
  }
29
23
  var _proto = ServerBase2.prototype;
30
24
  _proto.init = function init() {
31
25
  var _this = this;
32
26
  return _async_to_generator(function() {
33
- var runner;
27
+ var _this_options, serverConfig, cliConfig, mergedConfig, serverContext;
34
28
  return _ts_generator(this, function(_state) {
35
29
  switch (_state.label) {
36
30
  case 0:
31
+ _this_options = _this.options, serverConfig = _this_options.serverConfig, cliConfig = _this_options.config;
32
+ mergedConfig = loadConfig({
33
+ cliConfig,
34
+ serverConfig: serverConfig || {}
35
+ });
36
+ _this.addPlugins(mergedConfig.plugins || []);
37
37
  return [
38
38
  4,
39
- _this.pluginManager.init()
39
+ server.run({
40
+ plugins: _this.plugins,
41
+ options: _this.options,
42
+ config: mergedConfig,
43
+ handleSetupResult
44
+ })
40
45
  ];
41
46
  case 1:
42
- runner = _state.sent();
43
- _this.runner = runner;
47
+ serverContext = _state.sent().serverContext;
48
+ serverContext.serverBase = _this;
44
49
  return [
45
50
  4,
46
- runner.prepare()
51
+ serverContext.hooks.onPrepare.call()
47
52
  ];
48
53
  case 2:
49
54
  _state.sent();
55
+ _this.serverContext = serverContext;
50
56
  _class_private_method_get(_this, _applyMiddlewares, applyMiddlewares).call(_this);
51
57
  return [
52
58
  2,
@@ -57,9 +63,16 @@ var ServerBase = /* @__PURE__ */ function() {
57
63
  })();
58
64
  };
59
65
  _proto.addPlugins = function addPlugins(plugins) {
60
- this.pluginManager.addPlugins(plugins);
66
+ var _this_plugins;
67
+ (_this_plugins = this.plugins).push.apply(_this_plugins, _to_consumable_array(plugins));
61
68
  };
62
69
  _create_class(ServerBase2, [
70
+ {
71
+ key: "hooks",
72
+ get: function get() {
73
+ return this.serverContext.hooks;
74
+ }
75
+ },
63
76
  {
64
77
  key: "all",
65
78
  get: function get() {
@@ -129,25 +142,8 @@ var ServerBase = /* @__PURE__ */ function() {
129
142
  ]);
130
143
  return ServerBase2;
131
144
  }();
132
- function getAppContext() {
133
- var _this_options = this.options, context = _this_options.appContext, pwd = _this_options.pwd, routes = _this_options.routes, metaName = _this_options.metaName;
134
- var appContext = {
135
- routes,
136
- middlewares: [],
137
- appDirectory: (context === null || context === void 0 ? void 0 : context.appDirectory) || "",
138
- apiDirectory: context === null || context === void 0 ? void 0 : context.apiDirectory,
139
- internalDirectory: (context === null || context === void 0 ? void 0 : context.internalDirectory) || "",
140
- lambdaDirectory: context === null || context === void 0 ? void 0 : context.lambdaDirectory,
141
- sharedDirectory: (context === null || context === void 0 ? void 0 : context.sharedDirectory) || "",
142
- distDirectory: pwd,
143
- plugins: [],
144
- metaName: metaName || "modern-js",
145
- serverBase: this
146
- };
147
- return createContext(appContext);
148
- }
149
145
  function applyMiddlewares() {
150
- var middlewares = this.appContext.get().middlewares;
146
+ var middlewares = this.serverContext.middlewares;
151
147
  var preMiddlewares = [];
152
148
  var defaultMiddlewares = [];
153
149
  var postMiddlewares = [];
@@ -242,8 +238,8 @@ function createServerBase(options) {
242
238
  if (options == null) {
243
239
  throw new Error("can not start server without options");
244
240
  }
245
- var server = new ServerBase(options);
246
- return server;
241
+ var server2 = new ServerBase(options);
242
+ return server2;
247
243
  }
248
244
  export {
249
245
  ServerBase,
@@ -1,4 +1,4 @@
1
1
  export * from "./config";
2
- export * from "./plugin";
2
+ export * from "./plugins";
3
3
  export * from "./render";
4
4
  export * from "./server";
File without changes
@@ -0,0 +1,2 @@
1
+ export * from "./new";
2
+ export * from "./old";
File without changes
File without changes
@@ -1,10 +1,9 @@
1
1
  import { createErrorHtml, onError, ErrorDigest } from "./utils";
2
2
  import { AGGRED_DIR } from "./constants";
3
3
  import { createServerBase } from "./serverBase";
4
- import { PluginManager } from "./pluginManager";
5
4
  import { getLoaderCtx } from "./helper";
6
5
  export * from "./plugins";
7
- export * from "./types/plugin";
6
+ export * from "./types/plugins";
8
7
  export * from "./types/render";
9
8
  export * from "@modern-js/plugin";
10
9
  export * from "./types/config";
@@ -12,7 +11,6 @@ export * from "./types/requestHandler";
12
11
  export {
13
12
  AGGRED_DIR,
14
13
  ErrorDigest,
15
- PluginManager,
16
14
  createErrorHtml,
17
15
  createServerBase,
18
16
  getLoaderCtx,
@@ -0,0 +1,66 @@
1
+ function getHookRunners(context) {
2
+ const { hooks } = context;
3
+ return {
4
+ config: (params) => {
5
+ return hooks.modifyConfig.call(params);
6
+ },
7
+ prepare: () => {
8
+ return hooks.onPrepare.call();
9
+ },
10
+ reset: (params) => {
11
+ return hooks.onReset.call(params);
12
+ },
13
+ fallback: (input) => {
14
+ return hooks.fallback.call(input);
15
+ },
16
+ prepareWebServer: (input) => {
17
+ return hooks.prepareWebServer.call(input);
18
+ },
19
+ prepareApiServer: (input) => {
20
+ return hooks.prepareApiServer.call(input);
21
+ },
22
+ afterMatch: (ctx) => {
23
+ return hooks.afterMatch.call(ctx);
24
+ },
25
+ afterRender: (ctx) => {
26
+ return hooks.afterRender.call(ctx);
27
+ },
28
+ afterStreamingRender: (ctx) => {
29
+ return hooks.afterStreamingRender.call(ctx);
30
+ }
31
+ };
32
+ }
33
+ function transformHookRunner(hookRunnerName) {
34
+ switch (hookRunnerName) {
35
+ case "config":
36
+ return "modifyConfig";
37
+ case "prepare":
38
+ return "onPrepare";
39
+ case "reset":
40
+ return "onReset";
41
+ default:
42
+ return hookRunnerName;
43
+ }
44
+ }
45
+ function handleSetupResult(setupResult, api) {
46
+ if (!setupResult) {
47
+ return;
48
+ }
49
+ Object.keys(setupResult).forEach((key) => {
50
+ const fn = setupResult[key];
51
+ if (typeof fn === "function") {
52
+ const newAPI = transformHookRunner(key);
53
+ if (api[newAPI]) {
54
+ api[newAPI]((...params) => {
55
+ const res = fn(...params);
56
+ return res;
57
+ });
58
+ }
59
+ }
60
+ });
61
+ }
62
+ export {
63
+ getHookRunners,
64
+ handleSetupResult,
65
+ transformHookRunner
66
+ };
@@ -0,0 +1,40 @@
1
+ import { createAsyncHook, createAsyncPipelineHook } from "@modern-js/plugin-v2";
2
+ import { getHookRunners } from "./hooks";
3
+ import { handleSetupResult } from "./hooks";
4
+ const compatPlugin = () => ({
5
+ name: "@modern-js/server-compat",
6
+ registryHooks: {
7
+ fallback: createAsyncHook(),
8
+ prepareWebServer: createAsyncPipelineHook(),
9
+ prepareApiServer: createAsyncPipelineHook(),
10
+ afterMatch: createAsyncPipelineHook(),
11
+ afterRender: createAsyncPipelineHook(),
12
+ afterStreamingRender: createAsyncPipelineHook()
13
+ },
14
+ _registryApi: (getServerContext, updateServerContext) => {
15
+ const getInternalContext = () => {
16
+ return getServerContext()._internalContext;
17
+ };
18
+ return {
19
+ useConfigContext: () => {
20
+ return getInternalContext().config;
21
+ },
22
+ useAppContext: () => {
23
+ const { _internalContext, ...serverContext } = getServerContext();
24
+ return serverContext;
25
+ },
26
+ setAppContext: (context) => {
27
+ return updateServerContext(context);
28
+ },
29
+ useHookRunners: () => {
30
+ return getHookRunners(getInternalContext());
31
+ }
32
+ };
33
+ },
34
+ setup: () => {
35
+ }
36
+ });
37
+ export {
38
+ compatPlugin,
39
+ handleSetupResult
40
+ };
@@ -1,4 +1,5 @@
1
1
  import { time } from "@modern-js/runtime-utils/time";
2
+ import { isArray, isFunction } from "@modern-js/utils";
2
3
  import { ServerTimings } from "../../constants";
3
4
  import { getLoaderCtx } from "../../helper";
4
5
  import { transformResponse } from "../../utils";
@@ -18,9 +19,7 @@ class CustomServer {
18
19
  const baseHookCtx = createBaseHookContext(c);
19
20
  const afterMatchCtx = getAfterMatchCtx(entryName, baseHookCtx);
20
21
  const getCost = time();
21
- await this.runner.afterMatch(afterMatchCtx, {
22
- onLast: noop
23
- });
22
+ await this.hooks.afterMatch.call(afterMatchCtx);
24
23
  const cost = getCost();
25
24
  cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_MATCH, cost));
26
25
  const { url, status } = afterMatchCtx.router;
@@ -50,18 +49,15 @@ class CustomServer {
50
49
  }
51
50
  if (routeInfo.isStream) {
52
51
  const afterStreamingRenderContext = createAfterStreamingRenderContext(baseHookCtx, routeInfo);
53
- c.res = transformResponse(c.res, (chunk) => {
52
+ c.res = transformResponse(c.res, async (chunk) => {
54
53
  const context = afterStreamingRenderContext(chunk);
55
- return this.runner.afterStreamingRender(context, {
56
- onLast: ({ chunk: chunk2 }) => chunk2
57
- });
54
+ const { chunk: newChunk } = await this.hooks.afterStreamingRender.call(context);
55
+ return newChunk;
58
56
  });
59
57
  } else {
60
58
  const afterRenderCtx = await getAfterRenderCtx(c, baseHookCtx, routeInfo);
61
59
  const getCost2 = time();
62
- await this.runner.afterRender(afterRenderCtx, {
63
- onLast: noop
64
- });
60
+ await this.hooks.afterRender.call(afterRenderCtx);
65
61
  const cost2 = getCost2();
66
62
  cost2 && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_RENDER, cost2));
67
63
  if (afterRenderCtx.response.private_overrided) {
@@ -74,7 +70,7 @@ class CustomServer {
74
70
  }
75
71
  async getServerMiddleware(renderMiddlewares) {
76
72
  const serverMiddleware = await this.serverMiddlewarePromise;
77
- if (!serverMiddleware) {
73
+ if (!serverMiddleware || !isFunction(serverMiddleware) && !isArray(serverMiddleware)) {
78
74
  return renderMiddlewares;
79
75
  }
80
76
  if (Array.isArray(serverMiddleware)) {
@@ -110,17 +106,15 @@ class CustomServer {
110
106
  }
111
107
  };
112
108
  }
113
- constructor(runner, serverBase, pwd) {
114
- this.runner = runner;
109
+ constructor(hooks, serverBase, pwd) {
110
+ this.hooks = hooks;
115
111
  this.serverBase = serverBase;
116
112
  const webExtension = [];
117
- this.serverMiddlewarePromise = runner.prepareWebServer({
113
+ this.serverMiddlewarePromise = hooks.prepareWebServer.call({
118
114
  pwd,
119
115
  config: {
120
116
  middleware: webExtension
121
117
  }
122
- }, {
123
- onLast: () => []
124
118
  });
125
119
  }
126
120
  }
@@ -1,3 +1,4 @@
1
+ import { compatPlugin } from "./compat";
1
2
  import { logPlugin } from "./log";
2
3
  import { initMonitorsPlugin, injectServerTiming, injectloggerPlugin } from "./monitors";
3
4
  import { processedByPlugin } from "./processedBy";
@@ -13,6 +14,7 @@ function createSilenceLogger() {
13
14
  }
14
15
  function createDefaultPlugins(options = {}) {
15
16
  const plugins = [
17
+ compatPlugin(),
16
18
  logPlugin(),
17
19
  initMonitorsPlugin(),
18
20
  injectRenderHandlerPlugin(options),
@@ -4,10 +4,13 @@ import { injectServerTiming, injectloggerPlugin } from "./monitors";
4
4
  import { processedByPlugin } from "./processedBy";
5
5
  import { logPlugin } from "./log";
6
6
  import { createDefaultPlugins } from "./default";
7
+ import { compatPlugin, handleSetupResult } from "./compat";
7
8
  export {
9
+ compatPlugin,
8
10
  createDefaultPlugins,
9
11
  faviconPlugin,
10
12
  getRenderHandler,
13
+ handleSetupResult,
11
14
  injectRenderHandlerPlugin,
12
15
  injectServerTiming,
13
16
  injectloggerPlugin,
@@ -11,12 +11,12 @@ const renderPlugin = () => ({
11
11
  async prepare() {
12
12
  var _config_render;
13
13
  const { middlewares, routes, render, distDirectory: pwd, serverBase } = api.useAppContext();
14
- const runner = api.useHookRunners();
14
+ const hooks = api.getHooks();
15
15
  const config = api.useConfigContext();
16
16
  if (!routes) {
17
17
  return;
18
18
  }
19
- const customServer = new CustomServer(runner, serverBase, pwd);
19
+ const customServer = new CustomServer(hooks, serverBase, pwd);
20
20
  const serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
21
21
  const pageRoutes = getPageRoutes(routes);
22
22
  middlewares.push({
@@ -1,25 +1,40 @@
1
1
  import { _ as _class_private_method_get } from "@swc/helpers/_/_class_private_method_get";
2
2
  import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_method_init";
3
- import { createContext } from "@modern-js/plugin";
3
+ import { server } from "@modern-js/plugin-v2/server";
4
4
  import { Hono } from "hono";
5
- import { PluginManager } from "./pluginManager";
6
- var _getAppContext = /* @__PURE__ */ new WeakSet(), _applyMiddlewares = /* @__PURE__ */ new WeakSet();
5
+ import { handleSetupResult } from "./plugins/compat/hooks";
6
+ import { loadConfig } from "./utils";
7
+ var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
7
8
  class ServerBase {
8
9
  /**
9
- * 初始化顺序
10
- * - 初始化 pluginManager;
11
- * - 执行 runner.prepare;
12
- * - 应用 middlewares
10
+ * Order
11
+ * - server runner
12
+ * - apply middlewares
13
13
  */
14
14
  async init() {
15
- const runner = await this.pluginManager.init();
16
- this.runner = runner;
17
- await runner.prepare();
15
+ const { serverConfig, config: cliConfig } = this.options;
16
+ const mergedConfig = loadConfig({
17
+ cliConfig,
18
+ serverConfig: serverConfig || {}
19
+ });
20
+ this.addPlugins(mergedConfig.plugins || []);
21
+ const { serverContext } = await server.run({
22
+ plugins: this.plugins,
23
+ options: this.options,
24
+ config: mergedConfig,
25
+ handleSetupResult
26
+ });
27
+ serverContext.serverBase = this;
28
+ await serverContext.hooks.onPrepare.call();
29
+ this.serverContext = serverContext;
18
30
  _class_private_method_get(this, _applyMiddlewares, applyMiddlewares).call(this);
19
31
  return this;
20
32
  }
21
33
  addPlugins(plugins) {
22
- this.pluginManager.addPlugins(plugins);
34
+ this.plugins.push(...plugins);
35
+ }
36
+ get hooks() {
37
+ return this.serverContext.hooks;
23
38
  }
24
39
  get all() {
25
40
  return this.app.all.bind(this.app);
@@ -55,39 +70,15 @@ class ServerBase {
55
70
  return this.app.onError.bind(this.app);
56
71
  }
57
72
  constructor(options) {
58
- _class_private_method_init(this, _getAppContext);
59
73
  _class_private_method_init(this, _applyMiddlewares);
74
+ this.plugins = [];
75
+ this.serverContext = null;
60
76
  this.options = options;
61
- const { config, serverConfig } = options;
62
- const appContext = _class_private_method_get(this, _getAppContext, getAppContext).call(this);
63
- this.appContext = appContext;
64
- this.pluginManager = new PluginManager({
65
- cliConfig: config,
66
- appContext,
67
- serverConfig
68
- });
69
77
  this.app = new Hono();
70
78
  }
71
79
  }
72
- function getAppContext() {
73
- const { appContext: context, pwd, routes, metaName } = this.options;
74
- const appContext = {
75
- routes,
76
- middlewares: [],
77
- appDirectory: (context === null || context === void 0 ? void 0 : context.appDirectory) || "",
78
- apiDirectory: context === null || context === void 0 ? void 0 : context.apiDirectory,
79
- internalDirectory: (context === null || context === void 0 ? void 0 : context.internalDirectory) || "",
80
- lambdaDirectory: context === null || context === void 0 ? void 0 : context.lambdaDirectory,
81
- sharedDirectory: (context === null || context === void 0 ? void 0 : context.sharedDirectory) || "",
82
- distDirectory: pwd,
83
- plugins: [],
84
- metaName: metaName || "modern-js",
85
- serverBase: this
86
- };
87
- return createContext(appContext);
88
- }
89
80
  function applyMiddlewares() {
90
- const { middlewares } = this.appContext.get();
81
+ const { middlewares } = this.serverContext;
91
82
  const preMiddlewares = [];
92
83
  const defaultMiddlewares = [];
93
84
  const postMiddlewares = [];
@@ -145,8 +136,8 @@ function createServerBase(options) {
145
136
  if (options == null) {
146
137
  throw new Error("can not start server without options");
147
138
  }
148
- const server = new ServerBase(options);
149
- return server;
139
+ const server2 = new ServerBase(options);
140
+ return server2;
150
141
  }
151
142
  export {
152
143
  ServerBase,
@@ -1,4 +1,4 @@
1
1
  export * from "./config";
2
- export * from "./plugin";
2
+ export * from "./plugins";
3
3
  export * from "./render";
4
4
  export * from "./server";
@@ -0,0 +1,2 @@
1
+ export * from "./new";
2
+ export * from "./old";
File without changes