@modern-js/prod-server 1.1.10-beta.0 → 1.2.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.
- package/CHANGELOG.md +39 -0
- package/dist/js/modern/libs/route/route.js +3 -0
- package/dist/js/modern/libs/serve-file.js +20 -1
- package/dist/js/modern/server/index.js +29 -15
- package/dist/js/modern/server/modern-server.js +22 -4
- package/dist/js/modern/utils.js +11 -3
- package/dist/js/node/libs/route/route.js +3 -0
- package/dist/js/node/libs/serve-file.js +20 -1
- package/dist/js/node/server/index.js +32 -15
- package/dist/js/node/server/modern-server.js +22 -4
- package/dist/js/node/utils.js +14 -3
- package/dist/types/libs/route/route.d.ts +1 -0
- package/dist/types/libs/serve-file.d.ts +2 -1
- package/dist/types/server/index.d.ts +2 -0
- package/dist/types/utils.d.ts +1 -0
- package/package.json +6 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 1.2.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0e28456: fix assets prefix bug in prod env
|
|
8
|
+
- 44e3bb1: feat: support response headers
|
|
9
|
+
feat: 支持设置响应头
|
|
10
|
+
- @modern-js/server-core@1.4.1
|
|
11
|
+
- @modern-js/utils@1.8.0
|
|
12
|
+
|
|
13
|
+
## 1.2.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 2ed8f7d: fix: the \_SERVER_DATA injection twice causes the prod-server route error.
|
|
18
|
+
fix: \_SERVER_DATA 二次注入,导致服务器路由错误
|
|
19
|
+
- Updated dependencies [4fc801f]
|
|
20
|
+
- Updated dependencies [c8614b8]
|
|
21
|
+
- @modern-js/utils@1.8.0
|
|
22
|
+
- @modern-js/server-core@1.4.1
|
|
23
|
+
|
|
24
|
+
## 1.2.0
|
|
25
|
+
|
|
26
|
+
### Minor Changes
|
|
27
|
+
|
|
28
|
+
- 3d64b2f: feat: prod-server supports that load server env from .env.\*
|
|
29
|
+
|
|
30
|
+
feat: prod-server 支持从 .env.\* 文件加载服务器环境变量
|
|
31
|
+
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- 7b902b3: feat: support ListenOptions for prod-server
|
|
35
|
+
|
|
36
|
+
feat: server 支持传入 listernOptions 参数
|
|
37
|
+
|
|
38
|
+
- Updated dependencies [a27ab8d]
|
|
39
|
+
- @modern-js/server-core@1.4.1
|
|
40
|
+
- @modern-js/utils@1.7.12
|
|
41
|
+
|
|
3
42
|
## 1.1.9
|
|
4
43
|
|
|
5
44
|
### Patch Changes
|
|
@@ -22,6 +22,8 @@ export class ModernRoute {
|
|
|
22
22
|
|
|
23
23
|
_defineProperty(this, "params", {});
|
|
24
24
|
|
|
25
|
+
_defineProperty(this, "responseHeaders", void 0);
|
|
26
|
+
|
|
25
27
|
this.entryName = routeSpec.entryName || '';
|
|
26
28
|
this.urlPath = routeSpec.urlPath;
|
|
27
29
|
this.entryPath = routeSpec.entryPath || '';
|
|
@@ -30,6 +32,7 @@ export class ModernRoute {
|
|
|
30
32
|
this.isApi = routeSpec.isApi || false;
|
|
31
33
|
this.bundle = routeSpec.bundle || '';
|
|
32
34
|
this.enableModernMode = (_routeSpec$enableMode = routeSpec.enableModernMode) !== null && _routeSpec$enableMode !== void 0 ? _routeSpec$enableMode : false;
|
|
35
|
+
this.responseHeaders = routeSpec.responseHeaders;
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
}
|
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
// Todo 看看是不是能 fork 一份,即使命中也返回
|
|
2
2
|
import serve from 'serve-static';
|
|
3
3
|
import { isString, isRegExp } from '@modern-js/utils';
|
|
4
|
-
|
|
4
|
+
import { useLocalPrefix } from "../utils";
|
|
5
|
+
|
|
6
|
+
const removedPrefix = (req, prefix) => {
|
|
7
|
+
if (useLocalPrefix(prefix)) {
|
|
8
|
+
req.url = req.url.slice(prefix.length);
|
|
9
|
+
return () => {
|
|
10
|
+
req.url = prefix + req.url;
|
|
11
|
+
};
|
|
12
|
+
} else {
|
|
13
|
+
return () => {// emptyy
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const createStaticFileHandler = (rules, output = {}) => // eslint-disable-next-line consistent-return
|
|
5
19
|
async (context, next) => {
|
|
6
20
|
const {
|
|
7
21
|
url: requestUrl,
|
|
8
22
|
req,
|
|
9
23
|
res
|
|
10
24
|
} = context;
|
|
25
|
+
const {
|
|
26
|
+
assetPrefix = '/'
|
|
27
|
+
} = output;
|
|
11
28
|
const hitRule = rules.find(item => {
|
|
12
29
|
if (isString(item.path) && requestUrl.startsWith(item.path)) {
|
|
13
30
|
return true;
|
|
@@ -19,7 +36,9 @@ async (context, next) => {
|
|
|
19
36
|
});
|
|
20
37
|
|
|
21
38
|
if (hitRule) {
|
|
39
|
+
const resume = removedPrefix(req, assetPrefix);
|
|
22
40
|
serve(hitRule.target)(req, res, () => {
|
|
41
|
+
resume();
|
|
23
42
|
next();
|
|
24
43
|
});
|
|
25
44
|
} else {
|
|
@@ -5,8 +5,9 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
5
5
|
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
6
|
|
|
7
7
|
import path from 'path';
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
import { Logger, SHARED_DIR, OUTPUT_CONFIG_FILE, dotenv, dotenvExpand } from '@modern-js/utils';
|
|
8
10
|
import { serverManager, AppContext, ConfigContext, loadPlugins } from '@modern-js/server-core';
|
|
9
|
-
import { Logger, SHARED_DIR, OUTPUT_CONFIG_FILE } from '@modern-js/utils';
|
|
10
11
|
import { metrics as defaultMetrics } from "../libs/metrics";
|
|
11
12
|
import { loadConfig, getServerConfigPath, requireConfig } from "../libs/loadConfig";
|
|
12
13
|
import { debug } from "../utils";
|
|
@@ -34,6 +35,7 @@ export class Server {
|
|
|
34
35
|
}
|
|
35
36
|
/**
|
|
36
37
|
* 初始化顺序
|
|
38
|
+
* - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
|
|
37
39
|
* - 获取 server runtime config
|
|
38
40
|
* - 设置 context
|
|
39
41
|
* - 创建 hooksRunner
|
|
@@ -51,6 +53,7 @@ export class Server {
|
|
|
51
53
|
const {
|
|
52
54
|
options
|
|
53
55
|
} = this;
|
|
56
|
+
this.loadServerEnv(options);
|
|
54
57
|
this.initServerConfig(options);
|
|
55
58
|
await this.injectContext(this.runner, options); // initialize server runner
|
|
56
59
|
|
|
@@ -128,22 +131,18 @@ export class Server {
|
|
|
128
131
|
}
|
|
129
132
|
|
|
130
133
|
listen(options, listener) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
134
|
+
const callback = () => {
|
|
135
|
+
if (listener) {
|
|
136
|
+
listener();
|
|
137
|
+
}
|
|
136
138
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
} else {
|
|
140
|
-
this.app.listen(process.env.PORT || options || 8080, () => {
|
|
141
|
-
if (listener) {
|
|
142
|
-
listener();
|
|
143
|
-
}
|
|
139
|
+
this.server.onListening(this.app);
|
|
140
|
+
};
|
|
144
141
|
|
|
145
|
-
|
|
146
|
-
|
|
142
|
+
if (typeof options === 'object') {
|
|
143
|
+
this.app.listen(options, callback);
|
|
144
|
+
} else {
|
|
145
|
+
this.app.listen(process.env.PORT || options || 8080, callback);
|
|
147
146
|
}
|
|
148
147
|
}
|
|
149
148
|
|
|
@@ -214,4 +213,19 @@ export class Server {
|
|
|
214
213
|
};
|
|
215
214
|
}
|
|
216
215
|
|
|
216
|
+
loadServerEnv(options) {
|
|
217
|
+
const {
|
|
218
|
+
pwd: appDirectory
|
|
219
|
+
} = options;
|
|
220
|
+
const serverEnv = process.env.MODERN_ENV;
|
|
221
|
+
const serverEnvPath = path.resolve(appDirectory, `.env.${serverEnv}`);
|
|
222
|
+
|
|
223
|
+
if (serverEnv && fs.existsSync(serverEnvPath) && !fs.statSync(serverEnvPath).isDirectory()) {
|
|
224
|
+
const envConfig = dotenv.config({
|
|
225
|
+
path: serverEnvPath
|
|
226
|
+
});
|
|
227
|
+
dotenvExpand(envConfig);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
217
231
|
}
|
|
@@ -136,7 +136,7 @@ export class ModernServer {
|
|
|
136
136
|
this.staticFileHandler = createStaticFileHandler([{
|
|
137
137
|
path: staticPathRegExp,
|
|
138
138
|
target: distDir
|
|
139
|
-
}]);
|
|
139
|
+
}], this.conf.output);
|
|
140
140
|
this.routeRenderHandler = createRenderHandler({
|
|
141
141
|
distDir,
|
|
142
142
|
staticGenerate
|
|
@@ -415,6 +415,16 @@ export class ModernServer {
|
|
|
415
415
|
return;
|
|
416
416
|
}
|
|
417
417
|
|
|
418
|
+
if (route.responseHeaders) {
|
|
419
|
+
Object.keys(route.responseHeaders).forEach(key => {
|
|
420
|
+
const value = route.responseHeaders[key];
|
|
421
|
+
|
|
422
|
+
if (value) {
|
|
423
|
+
context.res.setHeader(key, value);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
|
|
418
428
|
if (route.entryName) {
|
|
419
429
|
await this.emitRouteHook('beforeRender', {
|
|
420
430
|
context
|
|
@@ -443,8 +453,12 @@ export class ModernServer {
|
|
|
443
453
|
context,
|
|
444
454
|
templateAPI
|
|
445
455
|
});
|
|
446
|
-
await this.injectMicroFE(context, templateAPI);
|
|
447
|
-
|
|
456
|
+
await this.injectMicroFE(context, templateAPI); // It will inject _SERVER_DATA twice, when SSG mode.
|
|
457
|
+
// The first time was in ssg html created, the seoncd time was in prod-server start.
|
|
458
|
+
// but the second wound causes route error.
|
|
459
|
+
// To ensure that the second injection fails, the _SERVER_DATA inject at the front of head,
|
|
460
|
+
|
|
461
|
+
templateAPI.prependHead(`<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>`);
|
|
448
462
|
response = templateAPI.get();
|
|
449
463
|
}
|
|
450
464
|
|
|
@@ -537,7 +551,11 @@ export class ModernServer {
|
|
|
537
551
|
this._handler = (context, next) => {
|
|
538
552
|
let i = 0;
|
|
539
553
|
|
|
540
|
-
const dispatch =
|
|
554
|
+
const dispatch = error => {
|
|
555
|
+
if (error) {
|
|
556
|
+
return this.onError(context, error);
|
|
557
|
+
}
|
|
558
|
+
|
|
541
559
|
const handler = handlers[i++];
|
|
542
560
|
|
|
543
561
|
if (!handler) {
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { compile } from 'path-to-regexp';
|
|
2
|
-
import { createDebugger } from '@modern-js/utils';
|
|
2
|
+
import { createDebugger, isProd } from '@modern-js/utils';
|
|
3
3
|
export const debug = createDebugger('prod-server');
|
|
4
4
|
export const mergeExtension = users => {
|
|
5
5
|
const output = [];
|
|
@@ -71,17 +71,25 @@ export const toPath = (reg, params) => {
|
|
|
71
71
|
});
|
|
72
72
|
return fn(params);
|
|
73
73
|
};
|
|
74
|
+
export const useLocalPrefix = url => {
|
|
75
|
+
return isProd() && !url.includes('.');
|
|
76
|
+
};
|
|
74
77
|
export const getStaticReg = (output = {}) => {
|
|
75
78
|
const {
|
|
76
79
|
favicon,
|
|
77
80
|
faviconByEntries,
|
|
78
81
|
cssPath,
|
|
79
82
|
jsPath,
|
|
80
|
-
mediaPath
|
|
83
|
+
mediaPath,
|
|
84
|
+
assetPrefix = '/'
|
|
81
85
|
} = output;
|
|
86
|
+
const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : '';
|
|
82
87
|
const favicons = prepareFavicons(favicon, faviconByEntries);
|
|
83
88
|
const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
|
|
84
|
-
const
|
|
89
|
+
const staticReg = ['static/', 'upload/', ...staticFiles];
|
|
90
|
+
const iconReg = ['favicon.ico', 'icon.png', ...favicons];
|
|
91
|
+
const regPrefix = prefix === '/' ? '' : prefix;
|
|
92
|
+
const staticPathRegExp = new RegExp(`^${regPrefix}/(${[...staticReg, ...iconReg].join('|')})`);
|
|
85
93
|
return staticPathRegExp;
|
|
86
94
|
};
|
|
87
95
|
export const prepareFavicons = (favicon, faviconByEntries) => {
|
|
@@ -29,6 +29,8 @@ class ModernRoute {
|
|
|
29
29
|
|
|
30
30
|
_defineProperty(this, "params", {});
|
|
31
31
|
|
|
32
|
+
_defineProperty(this, "responseHeaders", void 0);
|
|
33
|
+
|
|
32
34
|
this.entryName = routeSpec.entryName || '';
|
|
33
35
|
this.urlPath = routeSpec.urlPath;
|
|
34
36
|
this.entryPath = routeSpec.entryPath || '';
|
|
@@ -37,6 +39,7 @@ class ModernRoute {
|
|
|
37
39
|
this.isApi = routeSpec.isApi || false;
|
|
38
40
|
this.bundle = routeSpec.bundle || '';
|
|
39
41
|
this.enableModernMode = (_routeSpec$enableMode = routeSpec.enableModernMode) !== null && _routeSpec$enableMode !== void 0 ? _routeSpec$enableMode : false;
|
|
42
|
+
this.responseHeaders = routeSpec.responseHeaders;
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
}
|
|
@@ -9,16 +9,33 @@ var _serveStatic = _interopRequireDefault(require("serve-static"));
|
|
|
9
9
|
|
|
10
10
|
var _utils = require("@modern-js/utils");
|
|
11
11
|
|
|
12
|
+
var _utils2 = require("../utils");
|
|
13
|
+
|
|
12
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
15
|
|
|
14
16
|
// Todo 看看是不是能 fork 一份,即使命中也返回
|
|
15
|
-
const
|
|
17
|
+
const removedPrefix = (req, prefix) => {
|
|
18
|
+
if ((0, _utils2.useLocalPrefix)(prefix)) {
|
|
19
|
+
req.url = req.url.slice(prefix.length);
|
|
20
|
+
return () => {
|
|
21
|
+
req.url = prefix + req.url;
|
|
22
|
+
};
|
|
23
|
+
} else {
|
|
24
|
+
return () => {// emptyy
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const createStaticFileHandler = (rules, output = {}) => // eslint-disable-next-line consistent-return
|
|
16
30
|
async (context, next) => {
|
|
17
31
|
const {
|
|
18
32
|
url: requestUrl,
|
|
19
33
|
req,
|
|
20
34
|
res
|
|
21
35
|
} = context;
|
|
36
|
+
const {
|
|
37
|
+
assetPrefix = '/'
|
|
38
|
+
} = output;
|
|
22
39
|
const hitRule = rules.find(item => {
|
|
23
40
|
if ((0, _utils.isString)(item.path) && requestUrl.startsWith(item.path)) {
|
|
24
41
|
return true;
|
|
@@ -30,7 +47,9 @@ async (context, next) => {
|
|
|
30
47
|
});
|
|
31
48
|
|
|
32
49
|
if (hitRule) {
|
|
50
|
+
const resume = removedPrefix(req, assetPrefix);
|
|
33
51
|
(0, _serveStatic.default)(hitRule.target)(req, res, () => {
|
|
52
|
+
resume();
|
|
34
53
|
next();
|
|
35
54
|
});
|
|
36
55
|
} else {
|
|
@@ -7,10 +7,12 @@ exports.Server = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _path = _interopRequireDefault(require("path"));
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
11
11
|
|
|
12
12
|
var _utils = require("@modern-js/utils");
|
|
13
13
|
|
|
14
|
+
var _serverCore = require("@modern-js/server-core");
|
|
15
|
+
|
|
14
16
|
var _metrics = require("../libs/metrics");
|
|
15
17
|
|
|
16
18
|
var _loadConfig = require("../libs/loadConfig");
|
|
@@ -50,6 +52,7 @@ class Server {
|
|
|
50
52
|
}
|
|
51
53
|
/**
|
|
52
54
|
* 初始化顺序
|
|
55
|
+
* - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
|
|
53
56
|
* - 获取 server runtime config
|
|
54
57
|
* - 设置 context
|
|
55
58
|
* - 创建 hooksRunner
|
|
@@ -67,6 +70,7 @@ class Server {
|
|
|
67
70
|
const {
|
|
68
71
|
options
|
|
69
72
|
} = this;
|
|
73
|
+
this.loadServerEnv(options);
|
|
70
74
|
this.initServerConfig(options);
|
|
71
75
|
await this.injectContext(this.runner, options); // initialize server runner
|
|
72
76
|
|
|
@@ -148,22 +152,18 @@ class Server {
|
|
|
148
152
|
}
|
|
149
153
|
|
|
150
154
|
listen(options, listener) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
155
|
+
const callback = () => {
|
|
156
|
+
if (listener) {
|
|
157
|
+
listener();
|
|
158
|
+
}
|
|
156
159
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
} else {
|
|
160
|
-
this.app.listen(process.env.PORT || options || 8080, () => {
|
|
161
|
-
if (listener) {
|
|
162
|
-
listener();
|
|
163
|
-
}
|
|
160
|
+
this.server.onListening(this.app);
|
|
161
|
+
};
|
|
164
162
|
|
|
165
|
-
|
|
166
|
-
|
|
163
|
+
if (typeof options === 'object') {
|
|
164
|
+
this.app.listen(options, callback);
|
|
165
|
+
} else {
|
|
166
|
+
this.app.listen(process.env.PORT || options || 8080, callback);
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
|
|
@@ -237,6 +237,23 @@ class Server {
|
|
|
237
237
|
};
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
+
loadServerEnv(options) {
|
|
241
|
+
const {
|
|
242
|
+
pwd: appDirectory
|
|
243
|
+
} = options;
|
|
244
|
+
const serverEnv = process.env.MODERN_ENV;
|
|
245
|
+
|
|
246
|
+
const serverEnvPath = _path.default.resolve(appDirectory, `.env.${serverEnv}`);
|
|
247
|
+
|
|
248
|
+
if (serverEnv && _fs.default.existsSync(serverEnvPath) && !_fs.default.statSync(serverEnvPath).isDirectory()) {
|
|
249
|
+
const envConfig = _utils.dotenv.config({
|
|
250
|
+
path: serverEnvPath
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
(0, _utils.dotenvExpand)(envConfig);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
240
257
|
}
|
|
241
258
|
|
|
242
259
|
exports.Server = Server;
|
|
@@ -165,7 +165,7 @@ class ModernServer {
|
|
|
165
165
|
this.staticFileHandler = (0, _serveFile.createStaticFileHandler)([{
|
|
166
166
|
path: staticPathRegExp,
|
|
167
167
|
target: distDir
|
|
168
|
-
}]);
|
|
168
|
+
}], this.conf.output);
|
|
169
169
|
this.routeRenderHandler = (0, _render.createRenderHandler)({
|
|
170
170
|
distDir,
|
|
171
171
|
staticGenerate
|
|
@@ -452,6 +452,16 @@ class ModernServer {
|
|
|
452
452
|
return;
|
|
453
453
|
}
|
|
454
454
|
|
|
455
|
+
if (route.responseHeaders) {
|
|
456
|
+
Object.keys(route.responseHeaders).forEach(key => {
|
|
457
|
+
const value = route.responseHeaders[key];
|
|
458
|
+
|
|
459
|
+
if (value) {
|
|
460
|
+
context.res.setHeader(key, value);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
|
|
455
465
|
if (route.entryName) {
|
|
456
466
|
await this.emitRouteHook('beforeRender', {
|
|
457
467
|
context
|
|
@@ -480,8 +490,12 @@ class ModernServer {
|
|
|
480
490
|
context,
|
|
481
491
|
templateAPI
|
|
482
492
|
});
|
|
483
|
-
await this.injectMicroFE(context, templateAPI);
|
|
484
|
-
|
|
493
|
+
await this.injectMicroFE(context, templateAPI); // It will inject _SERVER_DATA twice, when SSG mode.
|
|
494
|
+
// The first time was in ssg html created, the seoncd time was in prod-server start.
|
|
495
|
+
// but the second wound causes route error.
|
|
496
|
+
// To ensure that the second injection fails, the _SERVER_DATA inject at the front of head,
|
|
497
|
+
|
|
498
|
+
templateAPI.prependHead(`<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>`);
|
|
485
499
|
response = templateAPI.get();
|
|
486
500
|
}
|
|
487
501
|
|
|
@@ -574,7 +588,11 @@ class ModernServer {
|
|
|
574
588
|
this._handler = (context, next) => {
|
|
575
589
|
let i = 0;
|
|
576
590
|
|
|
577
|
-
const dispatch =
|
|
591
|
+
const dispatch = error => {
|
|
592
|
+
if (error) {
|
|
593
|
+
return this.onError(context, error);
|
|
594
|
+
}
|
|
595
|
+
|
|
578
596
|
const handler = handlers[i++];
|
|
579
597
|
|
|
580
598
|
if (!handler) {
|
package/dist/js/node/utils.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.toPath = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
|
|
6
|
+
exports.useLocalPrefix = exports.toPath = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
|
|
7
7
|
|
|
8
8
|
var _pathToRegexp = require("path-to-regexp");
|
|
9
9
|
|
|
@@ -97,17 +97,28 @@ const toPath = (reg, params) => {
|
|
|
97
97
|
|
|
98
98
|
exports.toPath = toPath;
|
|
99
99
|
|
|
100
|
+
const useLocalPrefix = url => {
|
|
101
|
+
return (0, _utils.isProd)() && !url.includes('.');
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
exports.useLocalPrefix = useLocalPrefix;
|
|
105
|
+
|
|
100
106
|
const getStaticReg = (output = {}) => {
|
|
101
107
|
const {
|
|
102
108
|
favicon,
|
|
103
109
|
faviconByEntries,
|
|
104
110
|
cssPath,
|
|
105
111
|
jsPath,
|
|
106
|
-
mediaPath
|
|
112
|
+
mediaPath,
|
|
113
|
+
assetPrefix = '/'
|
|
107
114
|
} = output;
|
|
115
|
+
const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : '';
|
|
108
116
|
const favicons = prepareFavicons(favicon, faviconByEntries);
|
|
109
117
|
const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
|
|
110
|
-
const
|
|
118
|
+
const staticReg = ['static/', 'upload/', ...staticFiles];
|
|
119
|
+
const iconReg = ['favicon.ico', 'icon.png', ...favicons];
|
|
120
|
+
const regPrefix = prefix === '/' ? '' : prefix;
|
|
121
|
+
const staticPathRegExp = new RegExp(`^${regPrefix}/(${[...staticReg, ...iconReg].join('|')})`);
|
|
111
122
|
return staticPathRegExp;
|
|
112
123
|
};
|
|
113
124
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { NormalizedConfig } from '@modern-js/core';
|
|
1
2
|
import { NextFunction } from '../type';
|
|
2
3
|
import { ModernServerContext } from './context';
|
|
3
4
|
declare type Rule = {
|
|
4
5
|
path: string | RegExp;
|
|
5
6
|
target: string;
|
|
6
7
|
};
|
|
7
|
-
export declare const createStaticFileHandler: (rules: Rule[]) => (context: ModernServerContext, next: NextFunction) => Promise<void>;
|
|
8
|
+
export declare const createStaticFileHandler: (rules: Rule[], output?: NormalizedConfig['output']) => (context: ModernServerContext, next: NextFunction) => Promise<void>;
|
|
8
9
|
export {};
|
|
@@ -12,6 +12,7 @@ export declare class Server {
|
|
|
12
12
|
constructor(options: ModernServerOptions);
|
|
13
13
|
/**
|
|
14
14
|
* 初始化顺序
|
|
15
|
+
* - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
|
|
15
16
|
* - 获取 server runtime config
|
|
16
17
|
* - 设置 context
|
|
17
18
|
* - 创建 hooksRunner
|
|
@@ -46,4 +47,5 @@ export declare class Server {
|
|
|
46
47
|
private createHookRunner;
|
|
47
48
|
private injectContext;
|
|
48
49
|
private initAppContext;
|
|
50
|
+
private loadServerEnv;
|
|
49
51
|
}
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -15,5 +15,6 @@ export declare const createMiddlewareCollecter: () => {
|
|
|
15
15
|
addAPIMiddleware: (input: any) => void;
|
|
16
16
|
};
|
|
17
17
|
export declare const toPath: (reg: string, params: Record<string, any>) => string;
|
|
18
|
+
export declare const useLocalPrefix: (url: string) => boolean;
|
|
18
19
|
export declare const getStaticReg: (output?: NormalizedConfig['output']) => RegExp;
|
|
19
20
|
export declare const prepareFavicons: (favicon: string | undefined, faviconByEntries?: Record<string, string | undefined>) => string[];
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.2.2",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@modern-js/utils": "^1.
|
|
31
|
+
"@modern-js/utils": "^1.8.0",
|
|
32
32
|
"@babel/compat-data": "^7.17.10",
|
|
33
|
-
"@modern-js/server-core": "^1.4.
|
|
33
|
+
"@modern-js/server-core": "^1.4.1",
|
|
34
34
|
"axios": "^0.24.0",
|
|
35
35
|
"compare-versions": "^3.6.0",
|
|
36
36
|
"cookie": "^0.4.2",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"ua-parser-js": "^0.7.28"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@modern-js/types": "1.
|
|
47
|
-
"@modern-js/core": "1.
|
|
46
|
+
"@modern-js/types": "1.6.1",
|
|
47
|
+
"@modern-js/core": "1.13.2",
|
|
48
48
|
"@scripts/jest-config": "0.0.0",
|
|
49
49
|
"@scripts/build": "0.0.0",
|
|
50
50
|
"@types/cookie": "^0.4.1",
|
|
@@ -67,8 +67,7 @@
|
|
|
67
67
|
},
|
|
68
68
|
"publishConfig": {
|
|
69
69
|
"access": "public",
|
|
70
|
-
"registry": "https://registry.npmjs.org/"
|
|
71
|
-
"types": "./dist/types/index.d.ts"
|
|
70
|
+
"registry": "https://registry.npmjs.org/"
|
|
72
71
|
},
|
|
73
72
|
"wireit": {
|
|
74
73
|
"build": {
|