@modern-js/prod-server 1.1.9 → 1.2.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.
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 1.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 2ed8f7d: fix: the \_SERVER_DATA injection twice causes the prod-server route error.
|
|
8
|
+
fix: \_SERVER_DATA 二次注入,导致服务器路由错误
|
|
9
|
+
- Updated dependencies [4fc801f]
|
|
10
|
+
- Updated dependencies [c8614b8]
|
|
11
|
+
- @modern-js/utils@1.8.0
|
|
12
|
+
- @modern-js/server-core@1.4.1
|
|
13
|
+
|
|
14
|
+
## 1.2.0
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- 3d64b2f: feat: prod-server supports that load server env from .env.\*
|
|
19
|
+
|
|
20
|
+
feat: prod-server 支持从 .env.\* 文件加载服务器环境变量
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- 7b902b3: feat: support ListenOptions for prod-server
|
|
25
|
+
|
|
26
|
+
feat: server 支持传入 listernOptions 参数
|
|
27
|
+
|
|
28
|
+
- Updated dependencies [a27ab8d]
|
|
29
|
+
- @modern-js/server-core@1.4.1
|
|
30
|
+
- @modern-js/utils@1.7.12
|
|
31
|
+
|
|
3
32
|
## 1.1.9
|
|
4
33
|
|
|
5
34
|
### Patch Changes
|
|
@@ -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
|
|
|
@@ -127,14 +130,20 @@ export class Server {
|
|
|
127
130
|
}));
|
|
128
131
|
}
|
|
129
132
|
|
|
130
|
-
listen(
|
|
131
|
-
|
|
133
|
+
listen(options, listener) {
|
|
134
|
+
const callback = () => {
|
|
132
135
|
if (listener) {
|
|
133
136
|
listener();
|
|
134
137
|
}
|
|
135
138
|
|
|
136
139
|
this.server.onListening(this.app);
|
|
137
|
-
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
if (typeof options === 'object') {
|
|
143
|
+
this.app.listen(options, callback);
|
|
144
|
+
} else {
|
|
145
|
+
this.app.listen(process.env.PORT || options || 8080, callback);
|
|
146
|
+
}
|
|
138
147
|
}
|
|
139
148
|
|
|
140
149
|
getRequestHandler() {
|
|
@@ -204,4 +213,19 @@ export class Server {
|
|
|
204
213
|
};
|
|
205
214
|
}
|
|
206
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
|
+
|
|
207
231
|
}
|
|
@@ -443,8 +443,12 @@ export class ModernServer {
|
|
|
443
443
|
context,
|
|
444
444
|
templateAPI
|
|
445
445
|
});
|
|
446
|
-
await this.injectMicroFE(context, templateAPI);
|
|
447
|
-
|
|
446
|
+
await this.injectMicroFE(context, templateAPI); // It will inject _SERVER_DATA twice, when SSG mode.
|
|
447
|
+
// The first time was in ssg html created, the seoncd time was in prod-server start.
|
|
448
|
+
// but the second wound causes route error.
|
|
449
|
+
// To ensure that the second injection fails, the _SERVER_DATA inject at the front of head,
|
|
450
|
+
|
|
451
|
+
templateAPI.prependHead(`<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>`);
|
|
448
452
|
response = templateAPI.get();
|
|
449
453
|
}
|
|
450
454
|
|
|
@@ -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
|
|
|
@@ -147,14 +151,20 @@ class Server {
|
|
|
147
151
|
}));
|
|
148
152
|
}
|
|
149
153
|
|
|
150
|
-
listen(
|
|
151
|
-
|
|
154
|
+
listen(options, listener) {
|
|
155
|
+
const callback = () => {
|
|
152
156
|
if (listener) {
|
|
153
157
|
listener();
|
|
154
158
|
}
|
|
155
159
|
|
|
156
160
|
this.server.onListening(this.app);
|
|
157
|
-
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
if (typeof options === 'object') {
|
|
164
|
+
this.app.listen(options, callback);
|
|
165
|
+
} else {
|
|
166
|
+
this.app.listen(process.env.PORT || options || 8080, callback);
|
|
167
|
+
}
|
|
158
168
|
}
|
|
159
169
|
|
|
160
170
|
getRequestHandler() {
|
|
@@ -227,6 +237,23 @@ class Server {
|
|
|
227
237
|
};
|
|
228
238
|
}
|
|
229
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
|
+
|
|
230
257
|
}
|
|
231
258
|
|
|
232
259
|
exports.Server = Server;
|
|
@@ -480,8 +480,12 @@ class ModernServer {
|
|
|
480
480
|
context,
|
|
481
481
|
templateAPI
|
|
482
482
|
});
|
|
483
|
-
await this.injectMicroFE(context, templateAPI);
|
|
484
|
-
|
|
483
|
+
await this.injectMicroFE(context, templateAPI); // It will inject _SERVER_DATA twice, when SSG mode.
|
|
484
|
+
// The first time was in ssg html created, the seoncd time was in prod-server start.
|
|
485
|
+
// but the second wound causes route error.
|
|
486
|
+
// To ensure that the second injection fails, the _SERVER_DATA inject at the front of head,
|
|
487
|
+
|
|
488
|
+
templateAPI.prependHead(`<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>`);
|
|
485
489
|
response = templateAPI.get();
|
|
486
490
|
}
|
|
487
491
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
+
import type { ListenOptions } from 'net';
|
|
2
4
|
import { ModernServerOptions, ServerConstructor } from '../type';
|
|
3
5
|
export declare class Server {
|
|
4
6
|
options: ModernServerOptions;
|
|
@@ -10,6 +12,7 @@ export declare class Server {
|
|
|
10
12
|
constructor(options: ModernServerOptions);
|
|
11
13
|
/**
|
|
12
14
|
* 初始化顺序
|
|
15
|
+
* - 读取 .env.{process.env.MODERN_ENV} 文件,加载环境变量
|
|
13
16
|
* - 获取 server runtime config
|
|
14
17
|
* - 设置 context
|
|
15
18
|
* - 创建 hooksRunner
|
|
@@ -39,9 +42,10 @@ export declare class Server {
|
|
|
39
42
|
|
|
40
43
|
private initConfig;
|
|
41
44
|
close(): Promise<void>;
|
|
42
|
-
listen
|
|
45
|
+
listen<T extends number | ListenOptions | undefined>(options: T, listener: any): void;
|
|
43
46
|
getRequestHandler(): (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
|
|
44
47
|
private createHookRunner;
|
|
45
48
|
private injectContext;
|
|
46
49
|
private initAppContext;
|
|
50
|
+
private loadServerEnv;
|
|
47
51
|
}
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.1
|
|
14
|
+
"version": "1.2.1",
|
|
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.0",
|
|
47
|
+
"@modern-js/core": "1.13.1",
|
|
48
48
|
"@scripts/jest-config": "0.0.0",
|
|
49
49
|
"@scripts/build": "0.0.0",
|
|
50
50
|
"@types/cookie": "^0.4.1",
|