@modern-js/prod-server 2.0.0-beta.3 → 2.0.0-beta.6

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 (130) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/dist/js/modern/constants.js +32 -25
  3. package/dist/js/modern/index.js +11 -6
  4. package/dist/js/modern/libs/context/context.js +52 -84
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +44 -35
  7. package/dist/js/modern/libs/hook-api/route.js +6 -7
  8. package/dist/js/modern/libs/hook-api/template.js +20 -34
  9. package/dist/js/modern/libs/loadConfig.js +45 -24
  10. package/dist/js/modern/libs/metrics.js +3 -4
  11. package/dist/js/modern/libs/proxy.js +68 -37
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
  14. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
  15. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
  16. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
  17. package/dist/js/modern/libs/render/cache/index.js +88 -54
  18. package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
  19. package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -6
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -117
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -39
  23. package/dist/js/modern/libs/render/index.js +76 -56
  24. package/dist/js/modern/libs/render/measure.js +38 -27
  25. package/dist/js/modern/libs/render/reader.js +65 -62
  26. package/dist/js/modern/libs/render/ssr.js +50 -32
  27. package/dist/js/modern/libs/render/static.js +50 -33
  28. package/dist/js/modern/libs/render/type.js +9 -6
  29. package/dist/js/modern/libs/route/index.js +8 -15
  30. package/dist/js/modern/libs/route/matcher.js +20 -34
  31. package/dist/js/modern/libs/route/route.js +9 -18
  32. package/dist/js/modern/libs/serve-file.js +33 -20
  33. package/dist/js/modern/server/index.js +150 -145
  34. package/dist/js/modern/server/modern-server-split.js +46 -12
  35. package/dist/js/modern/server/modern-server.js +384 -422
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -43
  38. package/dist/js/modern/worker-server.js +34 -14
  39. package/dist/js/node/constants.js +53 -30
  40. package/dist/js/node/index.js +37 -57
  41. package/dist/js/node/libs/context/context.js +83 -94
  42. package/dist/js/node/libs/context/index.js +28 -13
  43. package/dist/js/node/libs/hook-api/index.js +76 -48
  44. package/dist/js/node/libs/hook-api/route.js +26 -11
  45. package/dist/js/node/libs/hook-api/template.js +41 -39
  46. package/dist/js/node/libs/loadConfig.js +73 -35
  47. package/dist/js/node/libs/metrics.js +25 -9
  48. package/dist/js/node/libs/proxy.js +89 -44
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -56
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -128
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
  54. package/dist/js/node/libs/render/cache/index.js +110 -64
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +54 -14
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -12
  57. package/dist/js/node/libs/render/cache/spr.js +161 -129
  58. package/dist/js/node/libs/render/cache/type.js +15 -5
  59. package/dist/js/node/libs/render/cache/util.js +99 -45
  60. package/dist/js/node/libs/render/index.js +106 -67
  61. package/dist/js/node/libs/render/measure.js +58 -31
  62. package/dist/js/node/libs/render/reader.js +95 -70
  63. package/dist/js/node/libs/render/ssr.js +80 -47
  64. package/dist/js/node/libs/render/static.js +79 -40
  65. package/dist/js/node/libs/render/type.js +31 -12
  66. package/dist/js/node/libs/route/index.js +31 -26
  67. package/dist/js/node/libs/route/matcher.js +40 -41
  68. package/dist/js/node/libs/route/route.js +29 -22
  69. package/dist/js/node/libs/serve-file.js +66 -32
  70. package/dist/js/node/server/index.js +168 -160
  71. package/dist/js/node/server/modern-server-split.js +72 -22
  72. package/dist/js/node/server/modern-server.js +403 -445
  73. package/dist/js/node/type.js +15 -3
  74. package/dist/js/node/utils.js +85 -52
  75. package/dist/js/node/worker-server.js +57 -21
  76. package/dist/js/treeshaking/constants.js +26 -25
  77. package/dist/js/treeshaking/index.js +10 -10
  78. package/dist/js/treeshaking/libs/context/context.js +268 -237
  79. package/dist/js/treeshaking/libs/context/index.js +3 -3
  80. package/dist/js/treeshaking/libs/hook-api/index.js +267 -143
  81. package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
  82. package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
  83. package/dist/js/treeshaking/libs/loadConfig.js +80 -37
  84. package/dist/js/treeshaking/libs/metrics.js +4 -10
  85. package/dist/js/treeshaking/libs/proxy.js +240 -76
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
  91. package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
  96. package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
  97. package/dist/js/treeshaking/libs/render/index.js +228 -95
  98. package/dist/js/treeshaking/libs/render/measure.js +142 -57
  99. package/dist/js/treeshaking/libs/render/reader.js +325 -177
  100. package/dist/js/treeshaking/libs/render/ssr.js +220 -95
  101. package/dist/js/treeshaking/libs/render/static.js +210 -78
  102. package/dist/js/treeshaking/libs/render/type.js +7 -6
  103. package/dist/js/treeshaking/libs/route/index.js +125 -89
  104. package/dist/js/treeshaking/libs/route/matcher.js +132 -107
  105. package/dist/js/treeshaking/libs/route/route.js +40 -26
  106. package/dist/js/treeshaking/libs/serve-file.js +177 -68
  107. package/dist/js/treeshaking/server/index.js +493 -327
  108. package/dist/js/treeshaking/server/modern-server-split.js +352 -144
  109. package/dist/js/treeshaking/server/modern-server.js +1048 -909
  110. package/dist/js/treeshaking/type.js +1 -1
  111. package/dist/js/treeshaking/utils.js +138 -81
  112. package/dist/js/treeshaking/worker-server.js +176 -55
  113. package/dist/types/index.d.ts +2 -0
  114. package/dist/types/libs/context/context.d.ts +4 -1
  115. package/dist/types/libs/loadConfig.d.ts +1 -0
  116. package/dist/types/libs/render/cache/index.d.ts +2 -0
  117. package/dist/types/libs/render/cache/spr.d.ts +2 -0
  118. package/dist/types/libs/route/route.d.ts +0 -1
  119. package/dist/types/server/index.d.ts +3 -0
  120. package/dist/types/utils.d.ts +1 -1
  121. package/dist/types/worker-server.d.ts +1 -2
  122. package/package.json +7 -14
  123. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  124. package/dist/js/modern/libs/render/modern/index.js +0 -37
  125. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  126. package/dist/js/node/libs/render/modern/index.js +0 -46
  127. package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
  129. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  130. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,339 +1,505 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
- import _typeof from "@babel/runtime/helpers/esm/typeof";
3
- import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
4
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
6
- import _createClass from "@babel/runtime/helpers/esm/createClass";
7
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
8
- import path from 'path';
9
- import fs from 'fs';
10
- import { Logger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand, INTERNAL_SERVER_PLUGINS } from '@modern-js/utils';
11
- import { serverManager, AppContext, ConfigContext, loadPlugins } from '@modern-js/server-core';
12
- import { metrics as defaultMetrics } from "../libs/metrics";
13
- import { loadConfig, getServerConfigPath, requireConfig } from "../libs/loadConfig";
14
- import { debug } from "../utils";
15
- import { createProdServer } from "./modern-server-split";
16
- export var Server = /*#__PURE__*/function () {
17
- function Server(options) {
18
- _classCallCheck(this, Server);
19
- _defineProperty(this, "options", void 0);
20
- _defineProperty(this, "serverImpl", createProdServer);
21
- _defineProperty(this, "server", void 0);
22
- _defineProperty(this, "app", void 0);
23
- _defineProperty(this, "runner", void 0);
24
- _defineProperty(this, "serverConfig", void 0);
25
- options.logger = options.logger || new Logger({
26
- level: 'warn'
27
- });
28
- options.metrics = options.metrics || defaultMetrics;
29
- this.options = options;
30
- this.serverConfig = {};
31
- }
32
-
33
- /**
34
- * 初始化顺序
35
- * - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
36
- * - 获取 server runtime config
37
- * - 设置 context
38
- * - 创建 hooksRunner
39
- * - 合并插件,内置插件和 serverConfig 中配置的插件
40
- * - 执行 config hook
41
- * - 获取最终的配置
42
- * - 设置配置到 context
43
- * - 初始化 server
44
- * - 执行 prepare hook
45
- * - 执行 server init
46
- */
47
- _createClass(Server, [{
48
- key: "init",
49
- value: function () {
50
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
51
- var options;
52
- return _regeneratorRuntime().wrap(function _callee$(_context) {
53
- while (1) {
54
- switch (_context.prev = _context.next) {
55
- case 0:
56
- options = this.options;
57
- this.loadServerEnv(options);
58
- this.initServerConfig(options);
59
- _context.next = 5;
60
- return this.injectContext(this.runner, options);
61
- case 5:
62
- _context.next = 7;
63
- return this.createHookRunner();
64
- case 7:
65
- this.runner = _context.sent;
66
- _context.next = 10;
67
- return this.initConfig(this.runner, options);
68
- case 10:
69
- _context.next = 12;
70
- return this.injectContext(this.runner, options);
71
- case 12:
72
- // initialize server
73
- this.server = this.serverImpl(options);
74
- _context.next = 15;
75
- return this.runPrepareHook(this.runner);
76
- case 15:
77
- _context.next = 17;
78
- return this.server.createHTTPServer(this.getRequestHandler());
79
- case 17:
80
- this.app = _context.sent;
81
- _context.next = 20;
82
- return this.server.onInit(this.runner, this.app);
83
- case 20:
84
- return _context.abrupt("return", this);
85
- case 21:
86
- case "end":
87
- return _context.stop();
88
- }
89
- }
90
- }, _callee, this);
91
- }));
92
- function init() {
93
- return _init.apply(this, arguments);
94
- }
95
- return init;
96
- }()
97
- /**
98
- * Execute config hooks
99
- * @param runner
100
- * @param options
101
- */
102
- }, {
103
- key: "runConfigHook",
104
- value: function runConfigHook(runner, serverConfig) {
105
- var newServerConfig = runner.config(serverConfig || {});
106
- return newServerConfig;
1
+ function _arrayLikeToArray(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _arrayWithoutHoles(arr) {
7
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
8
+ }
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
+ try {
11
+ var info = gen[key](arg);
12
+ var value = info.value;
13
+ } catch (error) {
14
+ reject(error);
15
+ return;
107
16
  }
108
- }, {
109
- key: "runPrepareHook",
110
- value: function () {
111
- var _runPrepareHook = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(runner) {
112
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
113
- while (1) {
114
- switch (_context2.prev = _context2.next) {
115
- case 0:
116
- runner.prepare();
117
- case 1:
118
- case "end":
119
- return _context2.stop();
120
- }
121
- }
122
- }, _callee2);
123
- }));
124
- function runPrepareHook(_x) {
125
- return _runPrepareHook.apply(this, arguments);
126
- }
127
- return runPrepareHook;
128
- }()
129
- }, {
130
- key: "initServerConfig",
131
- value: function initServerConfig(options) {
132
- var pwd = options.pwd,
133
- serverConfigFile = options.serverConfigFile;
134
- var distDirectory = path.join(pwd, options.config.output.path || 'dist');
135
- var serverConfigPath = getServerConfigPath(distDirectory, serverConfigFile);
136
- var serverConfig = requireConfig(serverConfigPath);
137
- this.serverConfig = serverConfig;
17
+ if (info.done) {
18
+ resolve(value);
19
+ } else {
20
+ Promise.resolve(value).then(_next, _throw);
138
21
  }
139
-
140
- /**
141
- *
142
- * merge cliConfig and serverConfig
143
- */
144
- }, {
145
- key: "initConfig",
146
- value: function () {
147
- var _initConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(runner, options) {
148
- var pwd, config, serverConfig, finalServerConfig, resolvedConfigPath;
149
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
150
- while (1) {
151
- switch (_context3.prev = _context3.next) {
152
- case 0:
153
- pwd = options.pwd, config = options.config;
154
- serverConfig = this.serverConfig;
155
- finalServerConfig = this.runConfigHook(runner, serverConfig);
156
- resolvedConfigPath = path.join(pwd, config.output.path || 'dist', OUTPUT_CONFIG_FILE);
157
- options.config = loadConfig({
158
- cliConfig: config,
159
- serverConfig: finalServerConfig,
160
- resolvedConfigPath: resolvedConfigPath
161
- });
162
- case 5:
163
- case "end":
164
- return _context3.stop();
22
+ }
23
+ function _asyncToGenerator(fn) {
24
+ return function() {
25
+ var self = this, args = arguments;
26
+ return new Promise(function(resolve, reject) {
27
+ var gen = fn.apply(self, args);
28
+ function _next(value) {
29
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
165
30
  }
166
- }
167
- }, _callee3, this);
168
- }));
169
- function initConfig(_x2, _x3) {
170
- return _initConfig.apply(this, arguments);
171
- }
172
- return initConfig;
173
- }()
174
- }, {
175
- key: "close",
176
- value: function () {
177
- var _close = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
178
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
179
- while (1) {
180
- switch (_context4.prev = _context4.next) {
181
- case 0:
182
- this.app.close();
183
- case 1:
184
- case "end":
185
- return _context4.stop();
31
+ function _throw(err) {
32
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
186
33
  }
187
- }
188
- }, _callee4, this);
189
- }));
190
- function close() {
191
- return _close.apply(this, arguments);
192
- }
193
- return close;
194
- }()
195
- }, {
196
- key: "listen",
197
- value: function listen(options, listener) {
198
- var callback = function callback() {
199
- listener === null || listener === void 0 ? void 0 : listener();
200
- };
201
- if (_typeof(options) === 'object') {
202
- this.app.listen(options, callback);
203
- } else {
204
- this.app.listen(process.env.PORT || options || 8080, callback);
205
- }
34
+ _next(undefined);
35
+ });
36
+ };
37
+ }
38
+ function _classCallCheck(instance, Constructor) {
39
+ if (!(instance instanceof Constructor)) {
40
+ throw new TypeError("Cannot call a class as a function");
206
41
  }
207
- }, {
208
- key: "getRequestHandler",
209
- value: function getRequestHandler() {
210
- var _this = this;
211
- return function (req, res, next) {
212
- var requestHandler = _this.server.getRequestHandler();
213
- return requestHandler(req, res, next);
214
- };
42
+ }
43
+ function _defineProperties(target, props) {
44
+ for(var i = 0; i < props.length; i++){
45
+ var descriptor = props[i];
46
+ descriptor.enumerable = descriptor.enumerable || false;
47
+ descriptor.configurable = true;
48
+ if ("value" in descriptor) descriptor.writable = true;
49
+ Object.defineProperty(target, descriptor.key, descriptor);
215
50
  }
216
- }, {
217
- key: "render",
218
- value: function () {
219
- var _render = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, res, url) {
220
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
221
- while (1) {
222
- switch (_context5.prev = _context5.next) {
223
- case 0:
224
- return _context5.abrupt("return", this.server.render(req, res, url));
225
- case 1:
226
- case "end":
227
- return _context5.stop();
228
- }
229
- }
230
- }, _callee5, this);
231
- }));
232
- function render(_x4, _x5, _x6) {
233
- return _render.apply(this, arguments);
234
- }
235
- return render;
236
- }()
237
- }, {
238
- key: "createHookRunner",
239
- value: function () {
240
- var _createHookRunner = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
241
- var options, _options$internalPlug, internalPlugins, pwd, serverPlugins, loadedPlugins, hooksRunner;
242
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
243
- while (1) {
244
- switch (_context6.prev = _context6.next) {
245
- case 0:
246
- // clear server manager every create time
247
- serverManager.clear();
248
- options = this.options; // TODO: 确认下这里是不是可以不从 options 中取插件,而是从 config 中取和过滤
249
- _options$internalPlug = options.internalPlugins, internalPlugins = _options$internalPlug === void 0 ? INTERNAL_SERVER_PLUGINS : _options$internalPlug, pwd = options.pwd;
250
- serverPlugins = this.serverConfig.plugins || []; // server app context for serve plugin
251
- loadedPlugins = loadPlugins(pwd, serverPlugins, {
252
- internalPlugins: internalPlugins
253
- });
254
- debug('plugins', loadedPlugins);
255
- loadedPlugins.forEach(function (p) {
256
- serverManager.usePlugin(p);
257
- });
258
-
259
- // create runner
260
- _context6.next = 9;
261
- return serverManager.init();
262
- case 9:
263
- hooksRunner = _context6.sent;
264
- return _context6.abrupt("return", hooksRunner);
265
- case 11:
266
- case "end":
267
- return _context6.stop();
268
- }
269
- }
270
- }, _callee6, this);
271
- }));
272
- function createHookRunner() {
273
- return _createHookRunner.apply(this, arguments);
274
- }
275
- return createHookRunner;
276
- }()
277
- }, {
278
- key: "injectContext",
279
- value: function () {
280
- var _injectContext = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(runner, options) {
281
- var appContext, config, pwd;
282
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
283
- while (1) {
284
- switch (_context7.prev = _context7.next) {
285
- case 0:
286
- appContext = this.initAppContext();
287
- config = options.config, pwd = options.pwd;
288
- ConfigContext.set(config);
289
- AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
290
- distDirectory: path.join(pwd, config.output.path || 'dist')
291
- }));
292
- case 4:
293
- case "end":
294
- return _context7.stop();
51
+ }
52
+ function _createClass(Constructor, protoProps, staticProps) {
53
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
54
+ if (staticProps) _defineProperties(Constructor, staticProps);
55
+ return Constructor;
56
+ }
57
+ function _defineProperty(obj, key, value) {
58
+ if (key in obj) {
59
+ Object.defineProperty(obj, key, {
60
+ value: value,
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true
64
+ });
65
+ } else {
66
+ obj[key] = value;
67
+ }
68
+ return obj;
69
+ }
70
+ function _iterableToArray(iter) {
71
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
72
+ }
73
+ function _nonIterableSpread() {
74
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
75
+ }
76
+ function _objectSpread(target) {
77
+ for(var i = 1; i < arguments.length; i++){
78
+ var source = arguments[i] != null ? arguments[i] : {};
79
+ var ownKeys = Object.keys(source);
80
+ if (typeof Object.getOwnPropertySymbols === "function") {
81
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
82
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
83
+ }));
84
+ }
85
+ ownKeys.forEach(function(key) {
86
+ _defineProperty(target, key, source[key]);
87
+ });
88
+ }
89
+ return target;
90
+ }
91
+ function ownKeys(object, enumerableOnly) {
92
+ var keys = Object.keys(object);
93
+ if (Object.getOwnPropertySymbols) {
94
+ var symbols = Object.getOwnPropertySymbols(object);
95
+ if (enumerableOnly) {
96
+ symbols = symbols.filter(function(sym) {
97
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
98
+ });
99
+ }
100
+ keys.push.apply(keys, symbols);
101
+ }
102
+ return keys;
103
+ }
104
+ function _objectSpreadProps(target, source) {
105
+ source = source != null ? source : {};
106
+ if (Object.getOwnPropertyDescriptors) {
107
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
108
+ } else {
109
+ ownKeys(Object(source)).forEach(function(key) {
110
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
111
+ });
112
+ }
113
+ return target;
114
+ }
115
+ function _toConsumableArray(arr) {
116
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
117
+ }
118
+ function _unsupportedIterableToArray(o, minLen) {
119
+ if (!o) return;
120
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
121
+ var n = Object.prototype.toString.call(o).slice(8, -1);
122
+ if (n === "Object" && o.constructor) n = o.constructor.name;
123
+ if (n === "Map" || n === "Set") return Array.from(n);
124
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
125
+ }
126
+ var __generator = this && this.__generator || function(thisArg, body) {
127
+ var f, y, t, g, _ = {
128
+ label: 0,
129
+ sent: function() {
130
+ if (t[0] & 1) throw t[1];
131
+ return t[1];
132
+ },
133
+ trys: [],
134
+ ops: []
135
+ };
136
+ return g = {
137
+ next: verb(0),
138
+ "throw": verb(1),
139
+ "return": verb(2)
140
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
141
+ return this;
142
+ }), g;
143
+ function verb(n) {
144
+ return function(v) {
145
+ return step([
146
+ n,
147
+ v
148
+ ]);
149
+ };
150
+ }
151
+ function step(op) {
152
+ if (f) throw new TypeError("Generator is already executing.");
153
+ while(_)try {
154
+ 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;
155
+ if (y = 0, t) op = [
156
+ op[0] & 2,
157
+ t.value
158
+ ];
159
+ switch(op[0]){
160
+ case 0:
161
+ case 1:
162
+ t = op;
163
+ break;
164
+ case 4:
165
+ _.label++;
166
+ return {
167
+ value: op[1],
168
+ done: false
169
+ };
170
+ case 5:
171
+ _.label++;
172
+ y = op[1];
173
+ op = [
174
+ 0
175
+ ];
176
+ continue;
177
+ case 7:
178
+ op = _.ops.pop();
179
+ _.trys.pop();
180
+ continue;
181
+ default:
182
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
183
+ _ = 0;
184
+ continue;
185
+ }
186
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
187
+ _.label = op[1];
188
+ break;
189
+ }
190
+ if (op[0] === 6 && _.label < t[1]) {
191
+ _.label = t[1];
192
+ t = op;
193
+ break;
194
+ }
195
+ if (t && _.label < t[2]) {
196
+ _.label = t[2];
197
+ _.ops.push(op);
198
+ break;
199
+ }
200
+ if (t[2]) _.ops.pop();
201
+ _.trys.pop();
202
+ continue;
295
203
  }
296
- }
297
- }, _callee7, this);
298
- }));
299
- function injectContext(_x7, _x8) {
300
- return _injectContext.apply(this, arguments);
301
- }
302
- return injectContext;
303
- }()
304
- }, {
305
- key: "initAppContext",
306
- value: function initAppContext() {
307
- var options = this.options;
308
- var appDirectory = options.pwd,
309
- _options$plugins = options.plugins,
310
- plugins = _options$plugins === void 0 ? [] : _options$plugins,
311
- config = options.config;
312
- var serverPlugins = plugins.map(function (p) {
204
+ op = body.call(thisArg, _);
205
+ } catch (e) {
206
+ op = [
207
+ 6,
208
+ e
209
+ ];
210
+ y = 0;
211
+ } finally{
212
+ f = t = 0;
213
+ }
214
+ if (op[0] & 5) throw op[1];
313
215
  return {
314
- server: p
216
+ value: op[0] ? op[1] : void 0,
217
+ done: true
315
218
  };
316
- });
317
- return {
318
- appDirectory: appDirectory,
319
- distDirectory: path.join(appDirectory, config.output.path || 'dist'),
320
- sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
321
- plugins: serverPlugins
322
- };
323
219
  }
324
- }, {
325
- key: "loadServerEnv",
326
- value: function loadServerEnv(options) {
327
- var appDirectory = options.pwd;
328
- var serverEnv = process.env.MODERN_ENV;
329
- var serverEnvPath = path.resolve(appDirectory, ".env.".concat(serverEnv));
330
- if (serverEnv && fs.existsSync(serverEnvPath) && !fs.statSync(serverEnvPath).isDirectory()) {
331
- var envConfig = dotenv.config({
332
- path: serverEnvPath
220
+ };
221
+ import path from "path";
222
+ import fs from "fs";
223
+ import { Logger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand, INTERNAL_SERVER_PLUGINS } from "@modern-js/utils";
224
+ import { serverManager, AppContext, ConfigContext, loadPlugins } from "@modern-js/server-core";
225
+ import { metrics as defaultMetrics } from "../libs/metrics";
226
+ import { loadConfig, getServerConfigPath, requireConfig } from "../libs/loadConfig";
227
+ import { debug } from "../utils";
228
+ import { createProdServer } from "./modern-server-split";
229
+ var Server = /*#__PURE__*/ function() {
230
+ "use strict";
231
+ function Server(options) {
232
+ _classCallCheck(this, Server);
233
+ _defineProperty(this, "options", void 0);
234
+ _defineProperty(this, "serverImpl", createProdServer);
235
+ _defineProperty(this, "server", void 0);
236
+ _defineProperty(this, "app", void 0);
237
+ _defineProperty(this, "runner", void 0);
238
+ _defineProperty(this, "serverConfig", void 0);
239
+ options.logger = options.logger || new Logger({
240
+ level: "warn"
333
241
  });
334
- dotenvExpand(envConfig);
335
- }
242
+ options.metrics = options.metrics || defaultMetrics;
243
+ this.options = options;
244
+ this.serverConfig = {};
336
245
  }
337
- }]);
338
- return Server;
339
- }();
246
+ _createClass(Server, [
247
+ {
248
+ key: "init",
249
+ value: function init() {
250
+ var _this = this;
251
+ return _asyncToGenerator(function() {
252
+ var options;
253
+ return __generator(this, function(_state) {
254
+ switch(_state.label){
255
+ case 0:
256
+ options = _this.options;
257
+ _this.loadServerEnv(options);
258
+ _this.initServerConfig(options);
259
+ return [
260
+ 4,
261
+ _this.injectContext(_this.runner, options)
262
+ ];
263
+ case 1:
264
+ _state.sent();
265
+ return [
266
+ 4,
267
+ _this.createHookRunner()
268
+ ];
269
+ case 2:
270
+ _this.runner = _state.sent();
271
+ return [
272
+ 4,
273
+ _this.initConfig(_this.runner, options)
274
+ ];
275
+ case 3:
276
+ _state.sent();
277
+ return [
278
+ 4,
279
+ _this.injectContext(_this.runner, options)
280
+ ];
281
+ case 4:
282
+ _state.sent();
283
+ _this.server = _this.serverImpl(options);
284
+ return [
285
+ 4,
286
+ _this.runPrepareHook(_this.runner)
287
+ ];
288
+ case 5:
289
+ _state.sent();
290
+ return [
291
+ 4,
292
+ _this.server.createHTTPServer(_this.getRequestHandler())
293
+ ];
294
+ case 6:
295
+ _this.app = _state.sent();
296
+ return [
297
+ 4,
298
+ _this.server.onInit(_this.runner, _this.app)
299
+ ];
300
+ case 7:
301
+ _state.sent();
302
+ return [
303
+ 2,
304
+ _this
305
+ ];
306
+ }
307
+ });
308
+ })();
309
+ }
310
+ },
311
+ {
312
+ key: "runConfigHook",
313
+ value: function runConfigHook(runner, serverConfig) {
314
+ var newServerConfig = runner.config(serverConfig || {});
315
+ return newServerConfig;
316
+ }
317
+ },
318
+ {
319
+ key: "runPrepareHook",
320
+ value: function runPrepareHook(runner) {
321
+ return _asyncToGenerator(function() {
322
+ return __generator(this, function(_state) {
323
+ runner.prepare();
324
+ return [
325
+ 2
326
+ ];
327
+ });
328
+ })();
329
+ }
330
+ },
331
+ {
332
+ key: "initServerConfig",
333
+ value: function initServerConfig(options) {
334
+ var pwd = options.pwd, serverConfigFile = options.serverConfigFile;
335
+ var distDirectory = path.join(pwd, options.config.output.path || "dist");
336
+ var serverConfigPath = getServerConfigPath(distDirectory, serverConfigFile);
337
+ var serverConfig = requireConfig(serverConfigPath);
338
+ this.serverConfig = serverConfig;
339
+ }
340
+ },
341
+ {
342
+ key: "initConfig",
343
+ value: function initConfig(runner, options) {
344
+ var _this = this;
345
+ return _asyncToGenerator(function() {
346
+ var pwd, config, serverConfig, finalServerConfig, resolvedConfigPath;
347
+ return __generator(this, function(_state) {
348
+ pwd = options.pwd, config = options.config;
349
+ serverConfig = _this.serverConfig;
350
+ finalServerConfig = _this.runConfigHook(runner, serverConfig);
351
+ resolvedConfigPath = path.join(pwd, config.output.path || "dist", OUTPUT_CONFIG_FILE);
352
+ options.config = loadConfig({
353
+ cliConfig: config,
354
+ serverConfig: finalServerConfig,
355
+ resolvedConfigPath: resolvedConfigPath
356
+ });
357
+ return [
358
+ 2
359
+ ];
360
+ });
361
+ })();
362
+ }
363
+ },
364
+ {
365
+ key: "close",
366
+ value: function close() {
367
+ var _this = this;
368
+ return _asyncToGenerator(function() {
369
+ return __generator(this, function(_state) {
370
+ _this.app.close();
371
+ return [
372
+ 2
373
+ ];
374
+ });
375
+ })();
376
+ }
377
+ },
378
+ {
379
+ key: "listen",
380
+ value: function listen(options, listener) {
381
+ var callback = function() {
382
+ listener === null || listener === void 0 ? void 0 : listener();
383
+ };
384
+ if (typeof options === "object") {
385
+ this.app.listen(options, callback);
386
+ } else {
387
+ this.app.listen(process.env.PORT || options || 8080, callback);
388
+ }
389
+ }
390
+ },
391
+ {
392
+ key: "getRequestHandler",
393
+ value: function getRequestHandler() {
394
+ var _this = this;
395
+ return function(req, res, next) {
396
+ var requestHandler = _this.server.getRequestHandler();
397
+ return requestHandler(req, res, next);
398
+ };
399
+ }
400
+ },
401
+ {
402
+ key: "render",
403
+ value: function render(req, res, url) {
404
+ var _this = this;
405
+ return _asyncToGenerator(function() {
406
+ return __generator(this, function(_state) {
407
+ return [
408
+ 2,
409
+ _this.server.render(req, res, url)
410
+ ];
411
+ });
412
+ })();
413
+ }
414
+ },
415
+ {
416
+ key: "createHookRunner",
417
+ value: function createHookRunner() {
418
+ var _this = this;
419
+ return _asyncToGenerator(function() {
420
+ var options, _internalPlugins, internalPlugins, pwd, _plugins, plugins, serverPlugins, loadedPlugins, hooksRunner;
421
+ return __generator(this, function(_state) {
422
+ switch(_state.label){
423
+ case 0:
424
+ serverManager.clear();
425
+ options = _this.options;
426
+ _internalPlugins = options.internalPlugins, internalPlugins = _internalPlugins === void 0 ? INTERNAL_SERVER_PLUGINS : _internalPlugins, pwd = options.pwd, _plugins = options.plugins, plugins = _plugins === void 0 ? [] : _plugins;
427
+ serverPlugins = _this.serverConfig.plugins || [];
428
+ loadedPlugins = loadPlugins(pwd, _toConsumableArray(serverPlugins).concat(_toConsumableArray(plugins)), {
429
+ internalPlugins: internalPlugins
430
+ });
431
+ debug("plugins", loadedPlugins);
432
+ loadedPlugins.forEach(function(p) {
433
+ serverManager.usePlugin(p);
434
+ });
435
+ return [
436
+ 4,
437
+ serverManager.init()
438
+ ];
439
+ case 1:
440
+ hooksRunner = _state.sent();
441
+ return [
442
+ 2,
443
+ hooksRunner
444
+ ];
445
+ }
446
+ });
447
+ })();
448
+ }
449
+ },
450
+ {
451
+ key: "injectContext",
452
+ value: function injectContext(runner, options) {
453
+ var _this = this;
454
+ return _asyncToGenerator(function() {
455
+ var appContext, config, pwd;
456
+ return __generator(this, function(_state) {
457
+ appContext = _this.initAppContext();
458
+ config = options.config, pwd = options.pwd;
459
+ ConfigContext.set(config);
460
+ AppContext.set(_objectSpreadProps(_objectSpread({}, appContext), {
461
+ distDirectory: path.join(pwd, config.output.path || "dist")
462
+ }));
463
+ return [
464
+ 2
465
+ ];
466
+ });
467
+ })();
468
+ }
469
+ },
470
+ {
471
+ key: "initAppContext",
472
+ value: function initAppContext() {
473
+ var options = this.options;
474
+ var appDirectory = options.pwd, _plugins = options.plugins, plugins = _plugins === void 0 ? [] : _plugins, config = options.config;
475
+ var serverPlugins = plugins.map(function(p) {
476
+ return {
477
+ server: p
478
+ };
479
+ });
480
+ return {
481
+ appDirectory: appDirectory,
482
+ distDirectory: path.join(appDirectory, config.output.path || "dist"),
483
+ sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
484
+ plugins: serverPlugins
485
+ };
486
+ }
487
+ },
488
+ {
489
+ key: "loadServerEnv",
490
+ value: function loadServerEnv(options) {
491
+ var appDirectory = options.pwd;
492
+ var serverEnv = process.env.MODERN_ENV;
493
+ var serverEnvPath = path.resolve(appDirectory, ".env.".concat(serverEnv));
494
+ if (serverEnv && fs.existsSync(serverEnvPath) && !fs.statSync(serverEnvPath).isDirectory()) {
495
+ var envConfig = dotenv.config({
496
+ path: serverEnvPath
497
+ });
498
+ dotenvExpand(envConfig);
499
+ }
500
+ }
501
+ }
502
+ ]);
503
+ return Server;
504
+ }();
505
+ export { Server };