@modern-js/prod-server 1.22.1 → 2.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +271 -17
- package/dist/js/modern/constants.js +32 -27
- package/dist/js/modern/index.js +11 -7
- package/dist/js/modern/libs/context/context.js +54 -132
- package/dist/js/modern/libs/context/index.js +5 -2
- package/dist/js/modern/libs/hook-api/index.js +134 -0
- package/dist/js/modern/libs/hook-api/route.js +13 -37
- package/dist/js/modern/libs/hook-api/template.js +41 -32
- package/dist/js/modern/libs/loadConfig.js +46 -32
- package/dist/js/modern/libs/metrics.js +6 -7
- package/dist/js/modern/libs/proxy.js +70 -44
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -68
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -225
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
- package/dist/js/modern/libs/render/cache/index.js +93 -50
- package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -16
- package/dist/js/modern/libs/render/cache/spr.js +133 -167
- package/dist/js/modern/libs/render/cache/type.js +0 -1
- package/dist/js/modern/libs/render/cache/util.js +71 -45
- package/dist/js/modern/libs/render/index.js +76 -61
- package/dist/js/modern/libs/render/measure.js +42 -34
- package/dist/js/modern/libs/render/reader.js +68 -76
- package/dist/js/modern/libs/render/ssr.js +63 -33
- package/dist/js/modern/libs/render/static.js +51 -37
- package/dist/js/modern/libs/render/type.js +9 -7
- package/dist/js/modern/libs/route/index.js +12 -30
- package/dist/js/modern/libs/route/matcher.js +28 -50
- package/dist/js/modern/libs/route/route.js +9 -31
- package/dist/js/modern/libs/serve-file.js +40 -20
- package/dist/js/modern/server/index.js +152 -168
- package/dist/js/modern/server/modern-server-split.js +44 -60
- package/dist/js/modern/server/modern-server.js +416 -526
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +62 -58
- package/dist/js/modern/worker-server.js +54 -0
- package/dist/js/node/constants.js +53 -32
- package/dist/js/node/index.js +37 -67
- package/dist/js/node/libs/context/context.js +84 -150
- package/dist/js/node/libs/context/index.js +28 -16
- package/dist/js/node/libs/hook-api/index.js +164 -0
- package/dist/js/node/libs/hook-api/route.js +35 -45
- package/dist/js/node/libs/hook-api/template.js +64 -40
- package/dist/js/node/libs/loadConfig.js +74 -55
- package/dist/js/node/libs/metrics.js +28 -12
- package/dist/js/node/libs/proxy.js +90 -55
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -61
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -140
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
- package/dist/js/node/libs/render/cache/index.js +115 -65
- package/dist/js/node/libs/render/cache/page-caches/index.js +54 -16
- package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -26
- package/dist/js/node/libs/render/cache/spr.js +161 -188
- package/dist/js/node/libs/render/cache/type.js +15 -5
- package/dist/js/node/libs/render/cache/util.js +99 -63
- package/dist/js/node/libs/render/index.js +106 -87
- package/dist/js/node/libs/render/measure.js +61 -44
- package/dist/js/node/libs/render/reader.js +98 -100
- package/dist/js/node/libs/render/ssr.js +92 -58
- package/dist/js/node/libs/render/static.js +80 -53
- package/dist/js/node/libs/render/type.js +31 -13
- package/dist/js/node/libs/route/index.js +35 -44
- package/dist/js/node/libs/route/matcher.js +48 -65
- package/dist/js/node/libs/route/route.js +29 -37
- package/dist/js/node/libs/serve-file.js +74 -37
- package/dist/js/node/server/index.js +170 -203
- package/dist/js/node/server/modern-server-split.js +70 -73
- package/dist/js/node/server/modern-server.js +432 -577
- package/dist/js/node/type.js +15 -3
- package/dist/js/node/utils.js +87 -93
- package/dist/js/node/worker-server.js +77 -0
- package/dist/js/treeshaking/constants.js +29 -0
- package/dist/js/treeshaking/index.js +13 -0
- package/dist/js/treeshaking/libs/context/context.js +274 -0
- package/dist/js/treeshaking/libs/context/index.js +5 -0
- package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
- package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
- package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
- package/dist/js/treeshaking/libs/loadConfig.js +82 -0
- package/dist/js/treeshaking/libs/metrics.js +6 -0
- package/dist/js/treeshaking/libs/proxy.js +244 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
- package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
- package/dist/js/treeshaking/libs/render/index.js +233 -0
- package/dist/js/treeshaking/libs/render/measure.js +146 -0
- package/dist/js/treeshaking/libs/render/reader.js +339 -0
- package/dist/js/treeshaking/libs/render/ssr.js +223 -0
- package/dist/js/treeshaking/libs/render/static.js +216 -0
- package/dist/js/treeshaking/libs/render/type.js +7 -0
- package/dist/js/treeshaking/libs/route/index.js +130 -0
- package/dist/js/treeshaking/libs/route/matcher.js +138 -0
- package/dist/js/treeshaking/libs/route/route.js +40 -0
- package/dist/js/treeshaking/libs/serve-file.js +184 -0
- package/dist/js/treeshaking/server/index.js +505 -0
- package/dist/js/treeshaking/server/modern-server-split.js +360 -0
- package/dist/js/treeshaking/server/modern-server.js +1083 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +147 -0
- package/dist/js/treeshaking/worker-server.js +177 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/hook-api/index.d.ts +5 -0
- package/dist/types/libs/hook-api/route.d.ts +9 -14
- package/dist/types/libs/hook-api/template.d.ts +19 -9
- package/dist/types/libs/render/cache/index.d.ts +4 -2
- package/dist/types/libs/render/type.d.ts +3 -1
- package/dist/types/libs/route/route.d.ts +0 -1
- package/dist/types/libs/serve-file.d.ts +2 -1
- package/dist/types/server/index.d.ts +2 -0
- package/dist/types/server/modern-server.d.ts +11 -11
- package/dist/types/type.d.ts +8 -10
- package/dist/types/utils.d.ts +3 -4
- package/dist/types/worker-server.d.ts +15 -0
- package/package.json +31 -45
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -48
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -64
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import { ModernServerContext } from "./context";
|
|
2
|
-
|
|
3
|
-
export {
|
|
2
|
+
const createContext = (req, res, options) => new ModernServerContext(req, res, options);
|
|
3
|
+
export {
|
|
4
|
+
ModernServerContext,
|
|
5
|
+
createContext
|
|
6
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import cookie from "cookie";
|
|
21
|
+
import { RouteAPI } from "./route";
|
|
22
|
+
import { TemplateAPI } from "./template";
|
|
23
|
+
class Response {
|
|
24
|
+
constructor(res) {
|
|
25
|
+
this.res = res;
|
|
26
|
+
this._cookie = cookie.parse(res.getHeader("set-cookie") || "");
|
|
27
|
+
this.cookies = {
|
|
28
|
+
get: this.getCookie.bind(this),
|
|
29
|
+
set: this.setCookie.bind(this),
|
|
30
|
+
delete: this.deleteCookie.bind(this),
|
|
31
|
+
clear: this.clearCookie.bind(this),
|
|
32
|
+
apply: this.applyCookie.bind(this)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
get(key) {
|
|
36
|
+
return this.res.getHeader(key);
|
|
37
|
+
}
|
|
38
|
+
set(key, value) {
|
|
39
|
+
return this.res.setHeader(key, value);
|
|
40
|
+
}
|
|
41
|
+
status(code) {
|
|
42
|
+
this.res.statusCode = code;
|
|
43
|
+
}
|
|
44
|
+
getCookie(key) {
|
|
45
|
+
return this._cookie[key];
|
|
46
|
+
}
|
|
47
|
+
setCookie(key, value) {
|
|
48
|
+
this._cookie[key] = value;
|
|
49
|
+
}
|
|
50
|
+
deleteCookie(key) {
|
|
51
|
+
if (this._cookie[key]) {
|
|
52
|
+
delete this._cookie[key];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
clearCookie() {
|
|
56
|
+
this._cookie = {};
|
|
57
|
+
}
|
|
58
|
+
applyCookie() {
|
|
59
|
+
const str = Object.entries(this._cookie).map(([key, value]) => {
|
|
60
|
+
return cookie.serialize(key, value);
|
|
61
|
+
}).join("; ");
|
|
62
|
+
if (str) {
|
|
63
|
+
this.res.setHeader("set-cookie", str);
|
|
64
|
+
} else {
|
|
65
|
+
this.res.removeHeader("set-cookie");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
raw(body, options) {
|
|
69
|
+
const { status, headers = {} } = options || {};
|
|
70
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
71
|
+
this.res.setHeader(key, value);
|
|
72
|
+
});
|
|
73
|
+
if (status) {
|
|
74
|
+
this.res.statusCode = status;
|
|
75
|
+
}
|
|
76
|
+
this.res.end(body);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
class Request {
|
|
80
|
+
constructor(ctx) {
|
|
81
|
+
this.url = ctx.url;
|
|
82
|
+
this.host = ctx.host;
|
|
83
|
+
this.pathname = ctx.path;
|
|
84
|
+
this.query = ctx.query;
|
|
85
|
+
this.headers = ctx.headers;
|
|
86
|
+
this.cookie = ctx.headers.cookie || "";
|
|
87
|
+
this._cookie = cookie.parse(this.cookie);
|
|
88
|
+
this.cookies = {
|
|
89
|
+
get: this.getCookie.bind(this)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
getCookie(key) {
|
|
93
|
+
return this._cookie[key];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const base = (context) => {
|
|
97
|
+
const { res } = context;
|
|
98
|
+
return {
|
|
99
|
+
response: new Response(res),
|
|
100
|
+
request: new Request(context),
|
|
101
|
+
logger: context.logger,
|
|
102
|
+
metrics: context.metrics
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const createAfterMatchContext = (context, entryName) => {
|
|
106
|
+
const baseContext = base(context);
|
|
107
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
108
|
+
router: new RouteAPI(entryName)
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
const createAfterRenderContext = (context, content) => {
|
|
112
|
+
const baseContext = base(context);
|
|
113
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
114
|
+
template: new TemplateAPI(content)
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
const createMiddlewareContext = (context) => {
|
|
118
|
+
const baseContext = base(context);
|
|
119
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
120
|
+
response: __spreadProps(__spreadValues({}, baseContext.response), {
|
|
121
|
+
locals: context.res.locals || {}
|
|
122
|
+
}),
|
|
123
|
+
source: {
|
|
124
|
+
req: context.req,
|
|
125
|
+
res: context.res
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
export {
|
|
130
|
+
base,
|
|
131
|
+
createAfterMatchContext,
|
|
132
|
+
createAfterRenderContext,
|
|
133
|
+
createMiddlewareContext
|
|
134
|
+
};
|
|
@@ -1,44 +1,20 @@
|
|
|
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
1
|
class RouteAPI {
|
|
4
|
-
constructor(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
_defineProperty(this, "url", void 0);
|
|
10
|
-
|
|
11
|
-
this.current = matched;
|
|
12
|
-
this.router = router;
|
|
13
|
-
this.url = url;
|
|
2
|
+
constructor(entryName) {
|
|
3
|
+
this.current = entryName;
|
|
4
|
+
this.status = 200;
|
|
5
|
+
this.url = "";
|
|
14
6
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
redirect(url, status = 302) {
|
|
8
|
+
this.url = url;
|
|
9
|
+
this.status = status;
|
|
18
10
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const {
|
|
22
|
-
router
|
|
23
|
-
} = this;
|
|
24
|
-
const matched = router.matchEntry(entryName);
|
|
25
|
-
return matched ? matched.generate(this.url) : null;
|
|
11
|
+
rewrite(entryName) {
|
|
12
|
+
this.current = entryName;
|
|
26
13
|
}
|
|
27
|
-
|
|
28
14
|
use(entryName) {
|
|
29
|
-
|
|
30
|
-
router
|
|
31
|
-
} = this;
|
|
32
|
-
const matched = router.matchEntry(entryName);
|
|
33
|
-
|
|
34
|
-
if (matched) {
|
|
35
|
-
this.current = matched;
|
|
36
|
-
return true;
|
|
37
|
-
} else {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
15
|
+
this.rewrite(entryName);
|
|
40
16
|
}
|
|
41
|
-
|
|
42
17
|
}
|
|
43
|
-
|
|
44
|
-
|
|
18
|
+
export {
|
|
19
|
+
RouteAPI
|
|
20
|
+
};
|
|
@@ -1,64 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Transform } from "stream";
|
|
3
2
|
const RegList = {
|
|
4
3
|
before: {
|
|
5
|
-
head:
|
|
6
|
-
body:
|
|
4
|
+
head: "<head>",
|
|
5
|
+
body: "<body>"
|
|
7
6
|
},
|
|
8
7
|
after: {
|
|
9
|
-
head:
|
|
10
|
-
body:
|
|
8
|
+
head: "</head>",
|
|
9
|
+
body: "</body>"
|
|
11
10
|
}
|
|
12
11
|
};
|
|
13
|
-
|
|
14
12
|
class TemplateAPI {
|
|
15
13
|
constructor(content) {
|
|
16
|
-
_defineProperty(this, "content", void 0);
|
|
17
|
-
|
|
18
14
|
this.content = content;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
get() {
|
|
22
17
|
return this.content;
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
set(content) {
|
|
26
20
|
this.content = content;
|
|
27
21
|
}
|
|
28
|
-
|
|
29
22
|
prependHead(fragment) {
|
|
30
|
-
const {
|
|
31
|
-
head
|
|
32
|
-
} = RegList.before;
|
|
23
|
+
const { head } = RegList.before;
|
|
33
24
|
return this.replace(head, `${head}${fragment}`);
|
|
34
25
|
}
|
|
35
|
-
|
|
36
26
|
appendHead(fragment) {
|
|
37
|
-
const {
|
|
38
|
-
head
|
|
39
|
-
} = RegList.after;
|
|
27
|
+
const { head } = RegList.after;
|
|
40
28
|
return this.replace(head, `${fragment}${head}`);
|
|
41
29
|
}
|
|
42
|
-
|
|
43
30
|
prependBody(fragment) {
|
|
44
|
-
const {
|
|
45
|
-
body
|
|
46
|
-
} = RegList.before;
|
|
31
|
+
const { body } = RegList.before;
|
|
47
32
|
return this.replace(body, `${body}${fragment}`);
|
|
48
33
|
}
|
|
49
|
-
|
|
50
34
|
appendBody(fragment) {
|
|
51
|
-
const {
|
|
52
|
-
body
|
|
53
|
-
} = RegList.after;
|
|
35
|
+
const { body } = RegList.after;
|
|
54
36
|
return this.replace(body, `${fragment}${body}`);
|
|
55
37
|
}
|
|
56
|
-
|
|
57
38
|
replace(reg, text) {
|
|
58
39
|
this.content = this.content.replace(reg, text);
|
|
59
|
-
return this;
|
|
60
40
|
}
|
|
61
|
-
|
|
62
41
|
}
|
|
63
|
-
|
|
64
|
-
|
|
42
|
+
const templateInjectableStream = ({
|
|
43
|
+
prependHead,
|
|
44
|
+
appendHead,
|
|
45
|
+
prependBody,
|
|
46
|
+
appendBody
|
|
47
|
+
}) => new Transform({
|
|
48
|
+
write(chunk, _, callback) {
|
|
49
|
+
let chunk_str = chunk.toString();
|
|
50
|
+
if (prependHead) {
|
|
51
|
+
const { head } = RegList.before;
|
|
52
|
+
chunk_str = chunk_str.replace(head, `${head}${prependHead}`);
|
|
53
|
+
}
|
|
54
|
+
if (appendHead) {
|
|
55
|
+
const { head } = RegList.after;
|
|
56
|
+
chunk_str = chunk_str.replace(head, `${appendHead}${head}`);
|
|
57
|
+
}
|
|
58
|
+
if (prependBody) {
|
|
59
|
+
const { body } = RegList.before;
|
|
60
|
+
chunk_str = chunk_str.replace(body, `${body}${prependBody}`);
|
|
61
|
+
}
|
|
62
|
+
if (appendBody) {
|
|
63
|
+
const { body } = RegList.after;
|
|
64
|
+
chunk_str = chunk_str.replace(body, `${appendBody}${body}`);
|
|
65
|
+
}
|
|
66
|
+
this.push(chunk_str);
|
|
67
|
+
callback();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
export {
|
|
71
|
+
TemplateAPI,
|
|
72
|
+
templateInjectableStream
|
|
73
|
+
};
|
|
@@ -1,48 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import * as path from "path";
|
|
21
|
+
import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
|
|
22
|
+
import mergeDeep from "merge-deep";
|
|
23
|
+
const getServerConfigPath = (distDirectory, serverConfigFile = DEFAULT_SERVER_CONFIG) => {
|
|
11
24
|
const serverConfigPath = path.join(distDirectory, serverConfigFile);
|
|
12
25
|
return `${serverConfigPath}.js`;
|
|
13
26
|
};
|
|
14
|
-
|
|
27
|
+
const requireConfig = (serverConfigPath) => {
|
|
15
28
|
if (fs.pathExistsSync(serverConfigPath)) {
|
|
16
29
|
return compatRequire(serverConfigPath);
|
|
17
30
|
}
|
|
18
|
-
|
|
19
31
|
return {};
|
|
20
32
|
};
|
|
21
|
-
|
|
22
|
-
* 对配置进行合并,开发环境下,cliConfig 与 serverConfig 进行深合并
|
|
23
|
-
* 生产环境下,resolvedConfig 与 serverConfig 进行深合并
|
|
24
|
-
* resolvedConfigPath: 构建序列化后的 modern.config.js 文件路径
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
export const loadConfig = ({
|
|
33
|
+
const loadConfig = ({
|
|
28
34
|
cliConfig,
|
|
29
35
|
serverConfig,
|
|
30
36
|
resolvedConfigPath
|
|
31
37
|
}) => {
|
|
32
38
|
let config = null;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
if (process.env.NODE_ENV === "production") {
|
|
40
|
+
const resolvedConfig = requireConfig(resolvedConfigPath);
|
|
41
|
+
config = mergeDeep(
|
|
42
|
+
__spreadProps(__spreadValues({}, resolvedConfig), {
|
|
43
|
+
plugins: []
|
|
44
|
+
}),
|
|
45
|
+
serverConfig,
|
|
46
|
+
cliConfig
|
|
47
|
+
);
|
|
41
48
|
} else {
|
|
42
|
-
config = mergeDeep(
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
config = mergeDeep(
|
|
50
|
+
__spreadProps(__spreadValues({}, cliConfig), {
|
|
51
|
+
plugins: []
|
|
52
|
+
}),
|
|
53
|
+
serverConfig
|
|
54
|
+
);
|
|
45
55
|
}
|
|
46
|
-
|
|
47
56
|
return config;
|
|
48
|
-
};
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
getServerConfigPath,
|
|
60
|
+
loadConfig,
|
|
61
|
+
requireConfig
|
|
62
|
+
};
|
|
@@ -1,66 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
var fulfilled = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.next(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var rejected = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.throw(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
import { createProxyMiddleware } from "http-proxy-middleware";
|
|
8
38
|
import { debug } from "../utils";
|
|
9
|
-
|
|
39
|
+
function formatProxyOptions(proxyOptions) {
|
|
10
40
|
const formattedProxy = [];
|
|
11
|
-
|
|
12
41
|
if (!Array.isArray(proxyOptions)) {
|
|
13
|
-
if (
|
|
42
|
+
if ("target" in proxyOptions) {
|
|
14
43
|
formattedProxy.push(proxyOptions);
|
|
15
44
|
} else {
|
|
16
|
-
Array.prototype.push.apply(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
45
|
+
Array.prototype.push.apply(
|
|
46
|
+
formattedProxy,
|
|
47
|
+
Object.keys(proxyOptions).reduce(
|
|
48
|
+
(total, source) => {
|
|
49
|
+
const option = proxyOptions[source];
|
|
50
|
+
total.push(__spreadValues({
|
|
51
|
+
context: source,
|
|
52
|
+
changeOrigin: true,
|
|
53
|
+
logLevel: "warn"
|
|
54
|
+
}, typeof option === "string" ? { target: option } : option));
|
|
55
|
+
return total;
|
|
56
|
+
},
|
|
57
|
+
[]
|
|
58
|
+
)
|
|
59
|
+
);
|
|
27
60
|
}
|
|
28
61
|
} else {
|
|
29
62
|
formattedProxy.push(...proxyOptions);
|
|
30
63
|
}
|
|
31
|
-
|
|
32
64
|
return formattedProxy;
|
|
33
65
|
}
|
|
34
|
-
|
|
35
|
-
debug(
|
|
36
|
-
|
|
66
|
+
const createProxyHandler = (proxyOptions) => {
|
|
67
|
+
debug("createProxyHandler", proxyOptions);
|
|
37
68
|
if (!proxyOptions) {
|
|
38
69
|
return null;
|
|
39
|
-
}
|
|
40
|
-
// or an object in the form of { source: ProxyDetail }
|
|
41
|
-
|
|
42
|
-
|
|
70
|
+
}
|
|
43
71
|
const formattedProxy = formatProxyOptions(proxyOptions);
|
|
44
|
-
const middlewares = formattedProxy.map(option => {
|
|
45
|
-
const middleware = createProxyMiddleware(option.context, option);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
res
|
|
51
|
-
} = ctx;
|
|
52
|
-
const bypassUrl = typeof option.bypass === 'function' ? option.bypass(req, res, option) : null; // only false, no true
|
|
53
|
-
|
|
54
|
-
if (typeof bypassUrl === 'boolean') {
|
|
72
|
+
const middlewares = formattedProxy.map((option) => {
|
|
73
|
+
const middleware = createProxyMiddleware(option.context, option);
|
|
74
|
+
return (ctx, next) => __async(void 0, null, function* () {
|
|
75
|
+
const { req, res } = ctx;
|
|
76
|
+
const bypassUrl = typeof option.bypass === "function" ? option.bypass(req, res, option) : null;
|
|
77
|
+
if (typeof bypassUrl === "boolean") {
|
|
55
78
|
ctx.status = 404;
|
|
56
79
|
return next();
|
|
57
|
-
} else if (typeof bypassUrl ===
|
|
80
|
+
} else if (typeof bypassUrl === "string") {
|
|
58
81
|
ctx.url = bypassUrl;
|
|
59
82
|
return next();
|
|
60
83
|
}
|
|
61
|
-
|
|
62
84
|
middleware(req, res, next);
|
|
63
|
-
};
|
|
85
|
+
});
|
|
64
86
|
});
|
|
65
87
|
return middlewares;
|
|
66
|
-
};
|
|
88
|
+
};
|
|
89
|
+
export {
|
|
90
|
+
createProxyHandler,
|
|
91
|
+
formatProxyOptions
|
|
92
|
+
};
|