@modern-js/prod-server 2.0.0-beta.0 → 2.0.0-beta.2

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 (113) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/dist/js/modern/constants.js +0 -2
  3. package/dist/js/modern/index.js +0 -1
  4. package/dist/js/modern/libs/context/context.js +10 -56
  5. package/dist/js/modern/libs/hook-api/index.js +0 -31
  6. package/dist/js/modern/libs/hook-api/route.js +0 -8
  7. package/dist/js/modern/libs/hook-api/template.js +0 -16
  8. package/dist/js/modern/libs/loadConfig.js +3 -10
  9. package/dist/js/modern/libs/metrics.js +6 -6
  10. package/dist/js/modern/libs/proxy.js +7 -12
  11. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -1
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -9
  13. package/dist/js/modern/libs/render/cache/index.js +8 -16
  14. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -10
  15. package/dist/js/modern/libs/render/cache/spr.js +12 -62
  16. package/dist/js/modern/libs/render/cache/util.js +0 -6
  17. package/dist/js/modern/libs/render/index.js +6 -11
  18. package/dist/js/modern/libs/render/measure.js +1 -11
  19. package/dist/js/modern/libs/render/modern/index.js +2 -13
  20. package/dist/js/modern/libs/render/reader.js +13 -24
  21. package/dist/js/modern/libs/render/ssr.js +5 -6
  22. package/dist/js/modern/libs/render/static.js +6 -9
  23. package/dist/js/modern/libs/render/type.js +0 -1
  24. package/dist/js/modern/libs/route/index.js +8 -19
  25. package/dist/js/modern/libs/route/matcher.js +21 -29
  26. package/dist/js/modern/libs/route/route.js +0 -13
  27. package/dist/js/modern/libs/serve-file.js +5 -6
  28. package/dist/js/modern/server/index.js +25 -52
  29. package/dist/js/modern/server/modern-server-split.js +0 -11
  30. package/dist/js/modern/server/modern-server.js +87 -167
  31. package/dist/js/modern/utils.js +12 -27
  32. package/dist/js/modern/worker-server.js +34 -0
  33. package/dist/js/node/constants.js +0 -2
  34. package/dist/js/node/index.js +0 -10
  35. package/dist/js/node/libs/context/context.js +10 -65
  36. package/dist/js/node/libs/context/index.js +0 -3
  37. package/dist/js/node/libs/hook-api/index.js +0 -42
  38. package/dist/js/node/libs/hook-api/route.js +0 -10
  39. package/dist/js/node/libs/hook-api/template.js +0 -22
  40. package/dist/js/node/libs/loadConfig.js +3 -22
  41. package/dist/js/node/libs/metrics.js +6 -6
  42. package/dist/js/node/libs/proxy.js +7 -17
  43. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -5
  44. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -12
  45. package/dist/js/node/libs/render/cache/index.js +8 -22
  46. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -2
  47. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -14
  48. package/dist/js/node/libs/render/cache/spr.js +12 -71
  49. package/dist/js/node/libs/render/cache/util.js +0 -18
  50. package/dist/js/node/libs/render/index.js +6 -26
  51. package/dist/js/node/libs/render/measure.js +0 -17
  52. package/dist/js/node/libs/render/modern/index.js +2 -20
  53. package/dist/js/node/libs/render/reader.js +12 -39
  54. package/dist/js/node/libs/render/ssr.js +4 -16
  55. package/dist/js/node/libs/render/static.js +6 -18
  56. package/dist/js/node/libs/render/type.js +0 -1
  57. package/dist/js/node/libs/route/index.js +8 -22
  58. package/dist/js/node/libs/route/matcher.js +18 -34
  59. package/dist/js/node/libs/route/route.js +0 -15
  60. package/dist/js/node/libs/serve-file.js +5 -13
  61. package/dist/js/node/server/index.js +25 -72
  62. package/dist/js/node/server/modern-server-split.js +0 -13
  63. package/dist/js/node/server/modern-server.js +87 -196
  64. package/dist/js/node/utils.js +13 -52
  65. package/dist/js/node/worker-server.js +41 -0
  66. package/dist/js/treeshaking/constants.js +28 -0
  67. package/dist/js/treeshaking/index.js +13 -0
  68. package/dist/js/treeshaking/libs/context/context.js +243 -0
  69. package/dist/js/treeshaking/libs/context/index.js +5 -0
  70. package/dist/js/treeshaking/libs/hook-api/index.js +157 -0
  71. package/dist/js/treeshaking/libs/hook-api/route.js +33 -0
  72. package/dist/js/treeshaking/libs/hook-api/template.js +91 -0
  73. package/dist/js/treeshaking/libs/loadConfig.js +39 -0
  74. package/dist/js/treeshaking/libs/metrics.js +12 -0
  75. package/dist/js/treeshaking/libs/proxy.js +80 -0
  76. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +124 -0
  77. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +464 -0
  78. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +53 -0
  79. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +35 -0
  80. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +121 -0
  81. package/dist/js/treeshaking/libs/render/cache/index.js +184 -0
  82. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +30 -0
  83. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +46 -0
  84. package/dist/js/treeshaking/libs/render/cache/spr.js +362 -0
  85. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  86. package/dist/js/treeshaking/libs/render/cache/util.js +101 -0
  87. package/dist/js/treeshaking/libs/render/index.js +100 -0
  88. package/dist/js/treeshaking/libs/render/measure.js +61 -0
  89. package/dist/js/treeshaking/libs/render/modern/browser-list.js +7 -0
  90. package/dist/js/treeshaking/libs/render/modern/index.js +39 -0
  91. package/dist/js/treeshaking/libs/render/reader.js +191 -0
  92. package/dist/js/treeshaking/libs/render/ssr.js +98 -0
  93. package/dist/js/treeshaking/libs/render/static.js +84 -0
  94. package/dist/js/treeshaking/libs/render/type.js +6 -0
  95. package/dist/js/treeshaking/libs/route/index.js +94 -0
  96. package/dist/js/treeshaking/libs/route/matcher.js +113 -0
  97. package/dist/js/treeshaking/libs/route/route.js +26 -0
  98. package/dist/js/treeshaking/libs/serve-file.js +75 -0
  99. package/dist/js/treeshaking/server/index.js +339 -0
  100. package/dist/js/treeshaking/server/modern-server-split.js +152 -0
  101. package/dist/js/treeshaking/server/modern-server.js +946 -0
  102. package/dist/js/treeshaking/type.js +1 -0
  103. package/dist/js/treeshaking/utils.js +90 -0
  104. package/dist/js/treeshaking/worker-server.js +56 -0
  105. package/dist/types/index.d.ts +0 -2
  106. package/dist/types/libs/context/context.d.ts +0 -3
  107. package/dist/types/libs/loadConfig.d.ts +0 -1
  108. package/dist/types/libs/render/cache/index.d.ts +0 -2
  109. package/dist/types/libs/render/cache/spr.d.ts +0 -2
  110. package/dist/types/server/index.d.ts +0 -3
  111. package/dist/types/utils.d.ts +2 -3
  112. package/dist/types/worker-server.d.ts +16 -0
  113. package/package.json +31 -15
@@ -0,0 +1,94 @@
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
4
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
+ import { RouteMatcher } from "./matcher";
6
+ export var RouteMatchManager = /*#__PURE__*/function () {
7
+ function RouteMatchManager() {
8
+ _classCallCheck(this, RouteMatchManager);
9
+ _defineProperty(this, "matchers", void 0);
10
+ _defineProperty(this, "specs", []);
11
+ this.matchers = [];
12
+ }
13
+
14
+ // get all routes matches pathname
15
+ _createClass(RouteMatchManager, [{
16
+ key: "filter",
17
+ value: function filter(pathname) {
18
+ return this.matchers.reduce(function (matches, matcher) {
19
+ if (matcher.matchUrlPath(pathname)) {
20
+ matches.push(matcher);
21
+ }
22
+ return matches;
23
+ }, []);
24
+ }
25
+
26
+ // get best match from a set of matches
27
+ }, {
28
+ key: "best",
29
+ value: function best(pathname, matches) {
30
+ var best;
31
+ var matchedLen = 0;
32
+ var _iterator = _createForOfIteratorHelper(matches),
33
+ _step;
34
+ try {
35
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
36
+ var match = _step.value;
37
+ var len = match.matchLength(pathname);
38
+ if (len === null) {
39
+ continue;
40
+ }
41
+ if (len > matchedLen) {
42
+ best = match;
43
+ matchedLen = len;
44
+ }
45
+ }
46
+ } catch (err) {
47
+ _iterator.e(err);
48
+ } finally {
49
+ _iterator.f();
50
+ }
51
+ return best;
52
+ }
53
+
54
+ // reset routes matcher
55
+ }, {
56
+ key: "reset",
57
+ value: function reset(specs) {
58
+ this.specs = specs;
59
+ var matchers = specs.reduce(function (ms, spec) {
60
+ ms.push(new RouteMatcher(spec));
61
+ return ms;
62
+ }, []);
63
+ this.matchers = matchers;
64
+ }
65
+
66
+ // get best match from all matcher in manager
67
+ }, {
68
+ key: "match",
69
+ value: function match(pathname) {
70
+ var matches = this.filter(pathname);
71
+ var best = this.best(pathname, matches);
72
+ return best;
73
+ }
74
+ }, {
75
+ key: "matchEntry",
76
+ value: function matchEntry(entryname) {
77
+ return this.matchers.find(function (matcher) {
78
+ return matcher.matchEntry(entryname);
79
+ });
80
+ }
81
+ }, {
82
+ key: "getBundles",
83
+ value: function getBundles() {
84
+ var bundles = this.specs.filter(function (route) {
85
+ return route.isSSR;
86
+ }).map(function (route) {
87
+ return route.bundle;
88
+ });
89
+ return bundles;
90
+ }
91
+ }]);
92
+ return RouteMatchManager;
93
+ }();
94
+ export { RouteMatcher };
@@ -0,0 +1,113 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ import { match, pathToRegexp, compile } from 'path-to-regexp';
5
+ import { ModernRoute } from "./route";
6
+
7
+ // avoid import @modern-js/utils
8
+ var removeTailSlash = function removeTailSlash(s) {
9
+ return s.replace(/\/+$/, '');
10
+ };
11
+ var toPath = function toPath(reg, params) {
12
+ var fn = compile(reg, {
13
+ encode: encodeURIComponent
14
+ });
15
+ return fn(params);
16
+ };
17
+
18
+ // eslint-disable-next-line no-useless-escape
19
+ var regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
20
+ export var RouteMatcher = /*#__PURE__*/function () {
21
+ function RouteMatcher(spec) {
22
+ _classCallCheck(this, RouteMatcher);
23
+ _defineProperty(this, "spec", void 0);
24
+ _defineProperty(this, "urlPath", '');
25
+ _defineProperty(this, "urlMatcher", void 0);
26
+ _defineProperty(this, "urlReg", void 0);
27
+ this.spec = spec;
28
+ this.setupUrlPath();
29
+ }
30
+
31
+ // generate modern route object
32
+ _createClass(RouteMatcher, [{
33
+ key: "generate",
34
+ value: function generate(url) {
35
+ var route = new ModernRoute(this.spec);
36
+ if (this.urlPath) {
37
+ var params = this.parseURLParams(url);
38
+ route.urlPath = toPath(route.urlPath, params);
39
+ route.params = params;
40
+ }
41
+ return route;
42
+ }
43
+ }, {
44
+ key: "parseURLParams",
45
+ value: function parseURLParams(pathname) {
46
+ if (!this.urlMatcher) {
47
+ return {};
48
+ } else {
49
+ var matchResult = this.urlMatcher(pathname);
50
+ return matchResult.params;
51
+ }
52
+ }
53
+
54
+ // get match url length
55
+ }, {
56
+ key: "matchLength",
57
+ value: function matchLength(pathname) {
58
+ if (!this.urlReg) {
59
+ return this.urlPath.length;
60
+ } else {
61
+ var _result$;
62
+ var result = this.urlReg.exec(pathname);
63
+ return (result === null || result === void 0 ? void 0 : (_result$ = result[0]) === null || _result$ === void 0 ? void 0 : _result$.length) || null;
64
+ }
65
+ }
66
+
67
+ // if match url path
68
+ }, {
69
+ key: "matchUrlPath",
70
+ value: function matchUrlPath(requestUrl) {
71
+ var urlWithoutSlash = requestUrl.endsWith('/') && requestUrl !== '/' ? requestUrl.slice(0, -1) : requestUrl;
72
+ if (urlWithoutSlash.endsWith('.html')) {
73
+ urlWithoutSlash = urlWithoutSlash.slice(0, -5);
74
+ }
75
+ if (this.urlMatcher) {
76
+ return Boolean(this.urlMatcher(urlWithoutSlash));
77
+ } else {
78
+ if (urlWithoutSlash.startsWith(this.urlPath)) {
79
+ // avoid /abcd match /a
80
+ if (this.urlPath !== '/' && urlWithoutSlash.length > this.urlPath.length && !urlWithoutSlash.startsWith("".concat(this.urlPath, "/"))) {
81
+ return false;
82
+ }
83
+ return true;
84
+ }
85
+ return false;
86
+ }
87
+ }
88
+ }, {
89
+ key: "matchEntry",
90
+ value: function matchEntry(entryName) {
91
+ return this.spec.entryName === entryName;
92
+ }
93
+
94
+ // compiler urlPath to regexp if necessary
95
+ }, {
96
+ key: "setupUrlPath",
97
+ value: function setupUrlPath() {
98
+ var urlPath = this.spec.urlPath;
99
+ this.urlPath = urlPath === '/' ? urlPath : removeTailSlash(urlPath);
100
+ var useReg = regCharsDetector.test(urlPath);
101
+ if (useReg) {
102
+ this.urlMatcher = match(urlPath, {
103
+ end: false,
104
+ decode: decodeURIComponent
105
+ });
106
+ this.urlReg = pathToRegexp(urlPath, [], {
107
+ end: false
108
+ });
109
+ }
110
+ }
111
+ }]);
112
+ return RouteMatcher;
113
+ }();
@@ -0,0 +1,26 @@
1
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ export var ModernRoute = /*#__PURE__*/_createClass(function ModernRoute(routeSpec) {
5
+ var _routeSpec$enableMode;
6
+ _classCallCheck(this, ModernRoute);
7
+ _defineProperty(this, "entryName", void 0);
8
+ _defineProperty(this, "urlPath", void 0);
9
+ _defineProperty(this, "entryPath", void 0);
10
+ _defineProperty(this, "bundle", void 0);
11
+ _defineProperty(this, "isApi", void 0);
12
+ _defineProperty(this, "isSSR", void 0);
13
+ _defineProperty(this, "isSPA", void 0);
14
+ _defineProperty(this, "enableModernMode", void 0);
15
+ _defineProperty(this, "params", {});
16
+ _defineProperty(this, "responseHeaders", void 0);
17
+ this.entryName = routeSpec.entryName || '';
18
+ this.urlPath = routeSpec.urlPath;
19
+ this.entryPath = routeSpec.entryPath || '';
20
+ this.isSSR = routeSpec.isSSR || false;
21
+ this.isSPA = routeSpec.isSPA || false;
22
+ this.isApi = routeSpec.isApi || false;
23
+ this.bundle = routeSpec.bundle || '';
24
+ this.enableModernMode = (_routeSpec$enableMode = routeSpec.enableModernMode) !== null && _routeSpec$enableMode !== void 0 ? _routeSpec$enableMode : false;
25
+ this.responseHeaders = routeSpec.responseHeaders;
26
+ });
@@ -0,0 +1,75 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ // Todo 看看是不是能 fork 一份,即使命中也返回
4
+
5
+ import serve from 'serve-static';
6
+ import { isString, isRegExp } from '@modern-js/utils';
7
+ import { useLocalPrefix } from "../utils";
8
+ var removedPrefix = function removedPrefix(req, prefix) {
9
+ if (useLocalPrefix(prefix)) {
10
+ req.url = req.url.slice(prefix.length);
11
+ return function () {
12
+ req.url = prefix + req.url;
13
+ };
14
+ } else {
15
+ return function () {
16
+ // emptyy
17
+ };
18
+ }
19
+ };
20
+ export var faviconFallbackHandler = function faviconFallbackHandler(context, next) {
21
+ if (context.url === '/favicon.ico') {
22
+ context.res.statusCode = 204;
23
+ context.res.end();
24
+ } else {
25
+ next();
26
+ }
27
+ };
28
+ export var createStaticFileHandler = function createStaticFileHandler(rules) {
29
+ var output = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
30
+ return (
31
+ /*#__PURE__*/
32
+ // eslint-disable-next-line consistent-return
33
+ function () {
34
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(context, next) {
35
+ var requestUrl, req, res, _output$assetPrefix, assetPrefix, hitRule, resume;
36
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
37
+ while (1) {
38
+ switch (_context.prev = _context.next) {
39
+ case 0:
40
+ requestUrl = context.url, req = context.req, res = context.res;
41
+ _output$assetPrefix = output.assetPrefix, assetPrefix = _output$assetPrefix === void 0 ? '/' : _output$assetPrefix;
42
+ hitRule = rules.find(function (item) {
43
+ if (isString(item.path) && requestUrl.startsWith(item.path)) {
44
+ return true;
45
+ } else if (isRegExp(item.path) && item.path.test(requestUrl)) {
46
+ return true;
47
+ }
48
+ return false;
49
+ });
50
+ if (!hitRule) {
51
+ _context.next = 8;
52
+ break;
53
+ }
54
+ resume = removedPrefix(req, assetPrefix);
55
+ serve(hitRule.target)(req, res, function () {
56
+ resume();
57
+ next();
58
+ });
59
+ _context.next = 9;
60
+ break;
61
+ case 8:
62
+ return _context.abrupt("return", next());
63
+ case 9:
64
+ case "end":
65
+ return _context.stop();
66
+ }
67
+ }
68
+ }, _callee);
69
+ }));
70
+ return function (_x, _x2) {
71
+ return _ref.apply(this, arguments);
72
+ };
73
+ }()
74
+ );
75
+ };
@@ -0,0 +1,339 @@
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;
107
+ }
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;
138
+ }
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();
165
+ }
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();
186
+ }
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
+ }
206
+ }
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
+ };
215
+ }
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();
295
+ }
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) {
313
+ return {
314
+ server: p
315
+ };
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
+ }
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
333
+ });
334
+ dotenvExpand(envConfig);
335
+ }
336
+ }
337
+ }]);
338
+ return Server;
339
+ }();