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

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 +55 -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 +19 -41
  29. package/dist/js/modern/server/modern-server-split.js +0 -11
  30. package/dist/js/modern/server/modern-server.js +83 -164
  31. package/dist/js/modern/utils.js +2 -21
  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 +19 -61
  62. package/dist/js/node/server/modern-server-split.js +0 -13
  63. package/dist/js/node/server/modern-server.js +83 -193
  64. package/dist/js/node/utils.js +3 -46
  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 +343 -0
  100. package/dist/js/treeshaking/server/modern-server-split.js +152 -0
  101. package/dist/js/treeshaking/server/modern-server.js +945 -0
  102. package/dist/js/treeshaking/type.js +1 -0
  103. package/dist/js/treeshaking/utils.js +87 -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 +0 -1
  112. package/dist/types/worker-server.d.ts +16 -0
  113. package/package.json +28 -12
package/CHANGELOG.md CHANGED
@@ -1,5 +1,60 @@
1
1
  # @modern-js/prod-server
2
2
 
3
+ ## 2.0.0-beta.1
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9: chore: v2
8
+
9
+ ### Minor Changes
10
+
11
+ - c9e800d39a: feat: support React18 streaming SSR
12
+ feat: 支持 React18 流式 SSR
13
+ - 543be9558e: feat: compile server loader and support handle loader request
14
+ feat: 编译 server loader 并支持处理 loader 的请求
15
+
16
+ ### Patch Changes
17
+
18
+ - d032d49: export createHandle
19
+ 导出 createHandle 函数
20
+ - 15bf09d9c8: feat: support completely custom server, export render() api for render single page
21
+ feat: 支持完全自定义 Server,导出 render() 方法用来渲染单个页面
22
+ - cc971eabfc: refactor: move server plugin load logic in `@modern-js/core`
23
+ refactor:移除在 `@modern-js/core` 中的 server 插件加载逻辑
24
+ - 40ed587: feat: inject css chunk into html for streaming ssr
25
+ feat: streaming ssr 返回的 html 注入 css chunk
26
+ - 102d32e4ba: feat(server): add `req` and `res` to SSR context
27
+
28
+ feat(server): 添加 `req` 和 `res` 到 SSR context 中
29
+
30
+ - 3bbea92b2a: feat: support Hook、Middleware new API
31
+ feat: 支持 Hook、Middleware 的新 API
32
+ - 73cd29dd9f: fix(server): add favicon fallback handler
33
+
34
+ fix(server): 添加 favicon 兜底处理逻辑
35
+
36
+ - 18aaf42: fix: fix server loader redirects
37
+ fix: 修复 server loader 重定向错误
38
+ - 14b712d: fix: use consistent alias type and default value across packages
39
+
40
+ fix: 在各个包中使用一致的 alias 类型定义和默认值
41
+
42
+ - Updated dependencies [15bf09d9c8]
43
+ - Updated dependencies [92f0ead]
44
+ - Updated dependencies [edd1cfb1af]
45
+ - Updated dependencies [cc971eabfc]
46
+ - Updated dependencies [5b9049f]
47
+ - Updated dependencies [92004d1]
48
+ - Updated dependencies [b8bbe036c7]
49
+ - Updated dependencies [d5a31df781]
50
+ - Updated dependencies [dda38c9]
51
+ - Updated dependencies [3bbea92b2a]
52
+ - Updated dependencies [abf3421]
53
+ - Updated dependencies [543be9558e]
54
+ - Updated dependencies [14b712d]
55
+ - @modern-js/server-core@2.0.0-beta.1
56
+ - @modern-js/utils@2.0.0-beta.1
57
+
3
58
  ## 2.0.0-beta.0
4
59
 
5
60
  ### Major Changes
@@ -6,12 +6,10 @@ export const AGGRED_DIR = {
6
6
  lambda: 'lambda'
7
7
  };
8
8
  export let ApiServerMode;
9
-
10
9
  (function (ApiServerMode) {
11
10
  ApiServerMode["func"] = "function";
12
11
  ApiServerMode["frame"] = "framework";
13
12
  })(ApiServerMode || (ApiServerMode = {}));
14
-
15
13
  export const ERROR_DIGEST = {
16
14
  INIT: 'Server init error',
17
15
  ENOTF: 'Page could not be found',
@@ -8,7 +8,6 @@ export default (options => {
8
8
  if (options == null) {
9
9
  throw new Error('can not start mserver without options');
10
10
  }
11
-
12
11
  const server = new Server(options);
13
12
  return server.init();
14
13
  });
@@ -1,5 +1,4 @@
1
1
  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; }
2
-
3
2
  import { URL } from 'url';
4
3
  import qs from 'querystring';
5
4
  import { Buffer } from 'buffer';
@@ -18,221 +17,176 @@ export class ModernServerContext {
18
17
  /**
19
18
  * url params
20
19
  */
20
+
21
21
  get logger() {
22
22
  return this.req.logger;
23
23
  }
24
-
25
24
  get metrics() {
26
25
  return this.req.metrics;
27
26
  }
28
-
29
27
  constructor(req, res, options) {
30
28
  _defineProperty(this, "req", void 0);
31
-
32
29
  _defineProperty(this, "res", void 0);
33
-
34
30
  _defineProperty(this, "params", {});
35
-
36
31
  _defineProperty(this, "serverData", void 0);
37
-
38
32
  _defineProperty(this, "options", {});
39
-
40
33
  this.req = req;
41
34
  this.res = res;
42
35
  this.options = options || {};
43
36
  this.serverData = {};
44
37
  this.bind();
45
38
  }
46
-
47
39
  bind() {
48
40
  const {
49
41
  req,
50
42
  res
51
43
  } = this;
52
-
53
44
  req.get = key => this.getReqHeader(key);
54
-
55
45
  res.set = (key, value) => this.res.setHeader(key, value);
56
-
57
46
  res.send = body => {
58
47
  this.send(body);
59
48
  };
60
- } // compat express res.send, only support etag now
61
-
49
+ }
62
50
 
51
+ // compat express res.send, only support etag now
63
52
  send(body) {
64
53
  try {
65
54
  const generateETag = !this.res.getHeader('ETag') && this.options.etag;
66
-
67
55
  if (body !== undefined && generateETag) {
68
56
  const encoding = typeof body === 'string' ? 'utf-8' : undefined;
69
57
  const buf = !Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body;
70
58
  const etag = createEtag(buf, {
71
59
  weak: true
72
60
  });
73
-
74
61
  if (etag) {
75
62
  this.res.setHeader('ETag', etag);
76
63
  }
77
64
  }
78
-
79
65
  if (this.fresh) {
80
66
  this.status = 304;
81
67
  }
82
68
  } catch (e) {
83
69
  this.logger.error(e.message);
84
70
  }
85
-
86
71
  this.res.end(body);
87
72
  }
88
-
89
73
  setParams(params) {
90
74
  this.params = params;
91
75
  }
92
-
93
76
  setServerData(key, value) {
94
77
  this.serverData[key] = value;
95
78
  }
96
-
97
79
  getReqHeader(key) {
98
80
  const {
99
81
  req
100
82
  } = this;
101
83
  const field = key.toLowerCase();
102
-
103
84
  switch (field) {
104
85
  case 'referer':
105
86
  case 'referrer':
106
87
  return req.headers.referrer || req.headers.referer || '';
107
-
108
88
  default:
109
89
  return req.headers[field] || '';
110
90
  }
111
91
  }
112
-
113
92
  get fresh() {
114
93
  const {
115
94
  status,
116
95
  res,
117
96
  method
118
- } = this; // GET or HEAD for weak freshness validation only
97
+ } = this;
119
98
 
99
+ // GET or HEAD for weak freshness validation only
120
100
  if ('GET' !== method && 'HEAD' !== method) {
121
101
  return false;
122
102
  }
123
-
124
103
  if (status >= 200 && status < 300 || 304 === status) {
125
104
  return fresh(this.headers, {
126
105
  etag: res.getHeader('ETag'),
127
106
  'last-modified': res.getHeader('Last-Modified')
128
107
  });
129
108
  }
130
-
131
109
  return false;
132
110
  }
133
- /* request property */
134
-
135
111
 
112
+ /* request property */
136
113
  get headers() {
137
114
  return this.req.headers;
138
115
  }
139
-
140
116
  get method() {
141
117
  return this.req.method;
142
118
  }
143
-
144
119
  get url() {
145
120
  return this.req.url || '';
146
121
  }
147
-
148
122
  set url(val) {
149
123
  this.req.url = val;
150
124
  }
151
-
152
125
  get host() {
153
126
  let host = this.getReqHeader('X-Forwarded-Host');
154
-
155
127
  if (!host) {
156
128
  host = this.getReqHeader('Host');
157
129
  }
158
-
159
130
  return host.split(/\s*,\s*/, 1)[0] || '';
160
131
  }
161
-
162
132
  get protocol() {
163
133
  if (this.req.socket.encrypted) {
164
134
  return 'https';
165
135
  }
166
-
167
136
  const proto = this.getReqHeader('X-Forwarded-Proto');
168
137
  return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http';
169
138
  }
170
-
171
139
  get origin() {
172
140
  return `${this.protocol}://${this.host}`;
173
141
  }
174
-
175
142
  get href() {
176
143
  return this.origin + this.url;
177
144
  }
178
-
179
145
  get parsedURL() {
180
146
  const url = new URL(this.req.url, this.origin);
181
147
  return url;
182
148
  }
183
-
184
149
  get path() {
185
150
  return this.parsedURL.pathname;
186
151
  }
187
-
188
152
  set path(p) {
189
- const url = new URL(this.req.url, this.origin); // this should never happened
190
-
153
+ const url = new URL(this.req.url, this.origin);
154
+ // this should never happened
191
155
  if (!url || !p) {
192
156
  return;
193
157
  }
194
-
195
158
  if (url.pathname === p) {
196
159
  return;
197
160
  }
198
-
199
161
  url.pathname = p;
200
162
  this.url = url.toString();
201
163
  }
202
-
203
164
  get querystring() {
204
165
  if (!this.req) {
205
166
  return '';
206
167
  }
207
-
208
168
  return this.parsedURL.search.replace(/^\?/, '') || '';
209
169
  }
210
-
211
170
  get query() {
212
171
  const str = this.querystring;
213
172
  return qs.parse(str);
214
173
  }
215
- /* response property */
216
-
217
174
 
175
+ /* response property */
218
176
  get status() {
219
177
  return this.res.statusCode;
220
178
  }
221
-
222
179
  set status(statusCode) {
223
180
  this.res.statusCode = statusCode;
224
181
  }
182
+
225
183
  /**
226
184
  * 判断链接是否已经关闭
227
185
  */
228
-
229
-
230
186
  resHasHandled() {
231
187
  return this.res.writableEnded;
232
188
  }
233
-
234
189
  error(dig, e = '') {
235
190
  this.logger.error(`Web Server Error - ${dig}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, this.path, headersWithoutCookie(this.headers));
236
191
  }
237
-
238
192
  }
@@ -1,21 +1,14 @@
1
1
  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; }
2
-
3
2
  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; }
4
-
5
3
  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; }
6
-
7
4
  import cookie from 'cookie';
8
5
  import { RouteAPI } from "./route";
9
6
  import { TemplateAPI } from "./template";
10
-
11
7
  class Response {
12
8
  constructor(res) {
13
9
  _defineProperty(this, "cookies", void 0);
14
-
15
10
  _defineProperty(this, "res", void 0);
16
-
17
11
  _defineProperty(this, "_cookie", void 0);
18
-
19
12
  this.res = res;
20
13
  this._cookie = cookie.parse(res.getHeader('set-cookie') || '');
21
14
  this.cookies = {
@@ -26,49 +19,39 @@ class Response {
26
19
  apply: this.applyCookie.bind(this)
27
20
  };
28
21
  }
29
-
30
22
  get(key) {
31
23
  return this.res.getHeader(key);
32
24
  }
33
-
34
25
  set(key, value) {
35
26
  return this.res.setHeader(key, value);
36
27
  }
37
-
38
28
  status(code) {
39
29
  this.res.statusCode = code;
40
30
  }
41
-
42
31
  getCookie(key) {
43
32
  return this._cookie[key];
44
33
  }
45
-
46
34
  setCookie(key, value) {
47
35
  this._cookie[key] = value;
48
36
  }
49
-
50
37
  deleteCookie(key) {
51
38
  if (this._cookie[key]) {
52
39
  delete this._cookie[key];
53
40
  }
54
41
  }
55
-
56
42
  clearCookie() {
57
43
  this._cookie = {};
58
44
  }
59
-
60
45
  applyCookie() {
61
46
  const str = Object.entries(this._cookie).map(([key, value]) => {
62
47
  return cookie.serialize(key, value);
63
48
  }).join('; ');
64
-
65
49
  if (str) {
66
50
  this.res.setHeader('set-cookie', str);
67
51
  } else {
68
52
  this.res.removeHeader('set-cookie');
69
53
  }
70
54
  }
71
-
72
55
  raw(body, {
73
56
  status,
74
57
  headers
@@ -76,32 +59,21 @@ class Response {
76
59
  Object.entries(headers).forEach(([key, value]) => {
77
60
  this.res.setHeader(key, value);
78
61
  });
79
-
80
62
  if (status) {
81
63
  this.res.statusCode = status;
82
64
  }
83
-
84
65
  this.res.end(body);
85
66
  }
86
-
87
67
  }
88
-
89
68
  class Request {
90
69
  constructor(ctx) {
91
70
  _defineProperty(this, "host", void 0);
92
-
93
71
  _defineProperty(this, "pathname", void 0);
94
-
95
72
  _defineProperty(this, "query", void 0);
96
-
97
73
  _defineProperty(this, "headers", void 0);
98
-
99
74
  _defineProperty(this, "cookie", void 0);
100
-
101
75
  _defineProperty(this, "cookies", void 0);
102
-
103
76
  _defineProperty(this, "_cookie", void 0);
104
-
105
77
  this.host = ctx.host;
106
78
  this.pathname = ctx.path;
107
79
  this.query = ctx.query;
@@ -112,13 +84,10 @@ class Request {
112
84
  get: this.getCookie.bind(this)
113
85
  };
114
86
  }
115
-
116
87
  getCookie(key) {
117
88
  return this._cookie[key];
118
89
  }
119
-
120
90
  }
121
-
122
91
  export const base = context => {
123
92
  const {
124
93
  res
@@ -1,29 +1,21 @@
1
1
  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; }
2
-
3
2
  export class RouteAPI {
4
3
  constructor(entryName) {
5
4
  _defineProperty(this, "current", void 0);
6
-
7
5
  _defineProperty(this, "status", void 0);
8
-
9
6
  _defineProperty(this, "url", void 0);
10
-
11
7
  this.current = entryName;
12
8
  this.status = 200;
13
9
  this.url = '';
14
10
  }
15
-
16
11
  redirect(url, status = 302) {
17
12
  this.url = url;
18
13
  this.status = status;
19
14
  }
20
-
21
15
  rewrite(entryName) {
22
16
  this.current = entryName;
23
17
  }
24
-
25
18
  use(entryName) {
26
19
  this.rewrite(entryName);
27
20
  }
28
-
29
21
  }
@@ -1,5 +1,4 @@
1
1
  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; }
2
-
3
2
  import { Transform } from 'stream';
4
3
  const RegList = {
5
4
  before: {
@@ -14,50 +13,41 @@ const RegList = {
14
13
  export class TemplateAPI {
15
14
  constructor(content) {
16
15
  _defineProperty(this, "content", void 0);
17
-
18
16
  this.content = content;
19
17
  }
20
-
21
18
  get() {
22
19
  return this.content;
23
20
  }
24
-
25
21
  set(content) {
26
22
  this.content = content;
27
23
  }
28
-
29
24
  prependHead(fragment) {
30
25
  const {
31
26
  head
32
27
  } = RegList.before;
33
28
  return this.replace(head, `${head}${fragment}`);
34
29
  }
35
-
36
30
  appendHead(fragment) {
37
31
  const {
38
32
  head
39
33
  } = RegList.after;
40
34
  return this.replace(head, `${fragment}${head}`);
41
35
  }
42
-
43
36
  prependBody(fragment) {
44
37
  const {
45
38
  body
46
39
  } = RegList.before;
47
40
  return this.replace(body, `${body}${fragment}`);
48
41
  }
49
-
50
42
  appendBody(fragment) {
51
43
  const {
52
44
  body
53
45
  } = RegList.after;
54
46
  return this.replace(body, `${fragment}${body}`);
55
47
  }
56
-
57
48
  replace(reg, text) {
58
49
  this.content = this.content.replace(reg, text);
59
50
  }
60
-
61
51
  }
62
52
  export const templateInjectableStream = ({
63
53
  prependHead,
@@ -67,37 +57,31 @@ export const templateInjectableStream = ({
67
57
  }) => new Transform({
68
58
  write(chunk, _, callback) {
69
59
  let chunk_str = chunk.toString();
70
-
71
60
  if (prependHead) {
72
61
  const {
73
62
  head
74
63
  } = RegList.before;
75
64
  chunk_str = chunk_str.replace(head, `${head}${prependHead}`);
76
65
  }
77
-
78
66
  if (appendHead) {
79
67
  const {
80
68
  head
81
69
  } = RegList.after;
82
70
  chunk_str = chunk_str.replace(head, `${appendHead}${head}`);
83
71
  }
84
-
85
72
  if (prependBody) {
86
73
  const {
87
74
  body
88
75
  } = RegList.before;
89
76
  chunk_str = chunk_str.replace(body, `${body}${prependBody}`);
90
77
  }
91
-
92
78
  if (appendBody) {
93
79
  const {
94
80
  body
95
81
  } = RegList.after;
96
82
  chunk_str = chunk_str.replace(body, `${appendBody}${body}`);
97
83
  }
98
-
99
84
  this.push(chunk_str);
100
85
  callback();
101
86
  }
102
-
103
87
  });
@@ -1,9 +1,6 @@
1
1
  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; }
2
-
3
2
  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; }
4
-
5
3
  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; }
6
-
7
4
  import * as path from 'path';
8
5
  import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
9
6
  import mergeDeep from 'merge-deep';
@@ -15,34 +12,30 @@ export const requireConfig = serverConfigPath => {
15
12
  if (fs.pathExistsSync(serverConfigPath)) {
16
13
  return compatRequire(serverConfigPath);
17
14
  }
18
-
19
15
  return {};
20
16
  };
17
+
21
18
  /**
22
19
  * 对配置进行合并,开发环境下,cliConfig 与 serverConfig 进行深合并
23
20
  * 生产环境下,resolvedConfig 与 serverConfig 进行深合并
24
21
  * resolvedConfigPath: 构建序列化后的 modern.config.js 文件路径
25
22
  */
26
-
27
23
  export const loadConfig = ({
28
24
  cliConfig,
29
25
  serverConfig,
30
26
  resolvedConfigPath
31
27
  }) => {
32
28
  let config = null;
33
-
34
29
  if (process.env.NODE_ENV === 'production') {
35
- const resolvedConfig = requireConfig(resolvedConfigPath); // cli config has a higher priority,because it's an argument passed in.
36
-
30
+ const resolvedConfig = requireConfig(resolvedConfigPath);
31
+ // cli config has a higher priority,because it's an argument passed in.
37
32
  config = mergeDeep(_objectSpread(_objectSpread({}, resolvedConfig), {}, {
38
33
  plugins: [] // filter cli plugins
39
-
40
34
  }), serverConfig, cliConfig);
41
35
  } else {
42
36
  config = mergeDeep(_objectSpread(_objectSpread({}, cliConfig), {}, {
43
37
  plugins: []
44
38
  }), serverConfig);
45
39
  }
46
-
47
40
  return config;
48
41
  };
@@ -1,12 +1,12 @@
1
1
  const metrics = {
2
- gauges() {// no impl
2
+ gauges() {
3
+ // no impl
3
4
  },
4
-
5
- emitCounter() {// no impl
5
+ emitCounter() {
6
+ // no impl
6
7
  },
7
-
8
- emitTimer() {// no impl
8
+ emitTimer() {
9
+ // no impl
9
10
  }
10
-
11
11
  };
12
12
  export { metrics };
@@ -1,14 +1,10 @@
1
1
  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; }
2
-
3
2
  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; }
4
-
5
3
  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; }
6
-
7
4
  import { createProxyMiddleware } from 'http-proxy-middleware';
8
5
  import { debug } from "../utils";
9
6
  export function formatProxyOptions(proxyOptions) {
10
7
  const formattedProxy = [];
11
-
12
8
  if (!Array.isArray(proxyOptions)) {
13
9
  if ('target' in proxyOptions) {
14
10
  formattedProxy.push(proxyOptions);
@@ -28,29 +24,29 @@ export function formatProxyOptions(proxyOptions) {
28
24
  } else {
29
25
  formattedProxy.push(...proxyOptions);
30
26
  }
31
-
32
27
  return formattedProxy;
33
28
  }
34
29
  export const createProxyHandler = proxyOptions => {
35
30
  debug('createProxyHandler', proxyOptions);
36
-
37
31
  if (!proxyOptions) {
38
32
  return null;
39
- } // If it is not an array, it may be an object that uses the context attribute
40
- // or an object in the form of { source: ProxyDetail }
41
-
33
+ }
42
34
 
35
+ // If it is not an array, it may be an object that uses the context attribute
36
+ // or an object in the form of { source: ProxyDetail }
43
37
  const formattedProxy = formatProxyOptions(proxyOptions);
44
38
  const middlewares = formattedProxy.map(option => {
45
- const middleware = createProxyMiddleware(option.context, option); // eslint-disable-next-line consistent-return
39
+ const middleware = createProxyMiddleware(option.context, option);
46
40
 
41
+ // eslint-disable-next-line consistent-return
47
42
  return async (ctx, next) => {
48
43
  const {
49
44
  req,
50
45
  res
51
46
  } = ctx;
52
- const bypassUrl = typeof option.bypass === 'function' ? option.bypass(req, res, option) : null; // only false, no true
47
+ const bypassUrl = typeof option.bypass === 'function' ? option.bypass(req, res, option) : null;
53
48
 
49
+ // only false, no true
54
50
  if (typeof bypassUrl === 'boolean') {
55
51
  ctx.status = 404;
56
52
  return next();
@@ -58,7 +54,6 @@ export const createProxyHandler = proxyOptions => {
58
54
  ctx.url = bypassUrl;
59
55
  return next();
60
56
  }
61
-
62
57
  middleware(req, res, next);
63
58
  };
64
59
  });
@@ -18,7 +18,6 @@ describe('test spr util functions', () => {
18
18
  });
19
19
  it('should return function correctly', () => {
20
20
  const urlParams = (() => new url.URLSearchParams())();
21
-
22
21
  urlParams.set('name', 'modern');
23
22
  const getParam = valueFactory(urlParams);
24
23
  expect(getParam('name')).toBe('modern');