@modern-js/prod-server 1.1.4 → 1.1.5

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 (69) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/js/modern/libs/context/context.js +10 -4
  3. package/dist/js/modern/libs/hook-api/route.js +8 -3
  4. package/dist/js/modern/libs/hook-api/template.js +4 -1
  5. package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -2
  6. package/dist/js/modern/libs/render/cache/spr.js +7 -4
  7. package/dist/js/modern/libs/render/reader.js +4 -1
  8. package/dist/js/modern/libs/route/index.js +6 -2
  9. package/dist/js/modern/libs/route/matcher.js +10 -4
  10. package/dist/js/modern/libs/route/route.js +20 -9
  11. package/dist/js/modern/server/index.js +12 -6
  12. package/dist/js/modern/server/modern-server.js +53 -25
  13. package/dist/js/node/libs/context/context.js +10 -4
  14. package/dist/js/node/libs/hook-api/route.js +8 -3
  15. package/dist/js/node/libs/hook-api/template.js +5 -1
  16. package/dist/js/node/libs/render/cache/page-caches/lru.js +6 -2
  17. package/dist/js/node/libs/render/cache/spr.js +6 -6
  18. package/dist/js/node/libs/render/reader.js +4 -1
  19. package/dist/js/node/libs/route/index.js +6 -2
  20. package/dist/js/node/libs/route/matcher.js +10 -4
  21. package/dist/js/node/libs/route/route.js +20 -9
  22. package/dist/js/node/server/index.js +12 -6
  23. package/dist/js/node/server/modern-server.js +53 -25
  24. package/dist/types/server/modern-server.d.ts +1 -1
  25. package/package.json +30 -8
  26. package/.eslintrc.js +0 -8
  27. package/dist/js/styles/tsconfig.json +0 -12
  28. package/jest.config.js +0 -8
  29. package/modern.config.js +0 -2
  30. package/src/constants.ts +0 -31
  31. package/src/index.ts +0 -19
  32. package/src/libs/context/context.ts +0 -178
  33. package/src/libs/context/index.ts +0 -7
  34. package/src/libs/hook-api/route.ts +0 -42
  35. package/src/libs/hook-api/template.ts +0 -53
  36. package/src/libs/loadConfig.ts +0 -59
  37. package/src/libs/metrics.ts +0 -15
  38. package/src/libs/proxy.ts +0 -44
  39. package/src/libs/render/cache/__tests__/cache.fun.test.ts +0 -94
  40. package/src/libs/render/cache/__tests__/cache.test.ts +0 -240
  41. package/src/libs/render/cache/__tests__/cacheable.ts +0 -44
  42. package/src/libs/render/cache/__tests__/error-configuration.ts +0 -34
  43. package/src/libs/render/cache/__tests__/matched-cache.ts +0 -88
  44. package/src/libs/render/cache/index.ts +0 -73
  45. package/src/libs/render/cache/page-caches/index.ts +0 -11
  46. package/src/libs/render/cache/page-caches/lru.ts +0 -38
  47. package/src/libs/render/cache/spr.ts +0 -300
  48. package/src/libs/render/cache/type.ts +0 -59
  49. package/src/libs/render/cache/util.ts +0 -94
  50. package/src/libs/render/index.ts +0 -79
  51. package/src/libs/render/measure.ts +0 -66
  52. package/src/libs/render/modern/browser-list.ts +0 -7
  53. package/src/libs/render/modern/index.ts +0 -41
  54. package/src/libs/render/modern/module.d.ts +0 -4
  55. package/src/libs/render/reader.ts +0 -119
  56. package/src/libs/render/ssr.ts +0 -69
  57. package/src/libs/render/static.ts +0 -52
  58. package/src/libs/render/type.ts +0 -38
  59. package/src/libs/route/index.ts +0 -76
  60. package/src/libs/route/matcher.ts +0 -108
  61. package/src/libs/route/route.ts +0 -34
  62. package/src/libs/serve-file.ts +0 -34
  63. package/src/server/index.ts +0 -232
  64. package/src/server/modern-server-split.ts +0 -93
  65. package/src/server/modern-server.ts +0 -620
  66. package/src/tsconfig.json +0 -12
  67. package/src/type.ts +0 -95
  68. package/src/utils.ts +0 -116
  69. package/tsconfig.json +0 -11
@@ -1,232 +0,0 @@
1
- import { IncomingMessage, ServerResponse, Server as httpServer } from 'http';
2
- import path from 'path';
3
- import {
4
- serverManager,
5
- AppContext,
6
- ConfigContext,
7
- loadPlugins,
8
- ServerConfig,
9
- } from '@modern-js/server-core';
10
- import {
11
- Logger,
12
- SHARED_DIR,
13
- OUTPUT_CONFIG_FILE,
14
- LoggerInterface,
15
- } from '@modern-js/utils';
16
- import type { UserConfig } from '@modern-js/core';
17
- import { ISAppContext } from '@modern-js/types';
18
- import {
19
- ModernServerOptions,
20
- ServerHookRunner,
21
- ServerConstructor,
22
- ModernServerInterface,
23
- } from '../type';
24
- import { metrics as defaultMetrics } from '../libs/metrics';
25
- import {
26
- loadConfig,
27
- getServerConfigPath,
28
- requireConfig,
29
- } from '../libs/loadConfig';
30
- import { debug } from '../utils';
31
- import { createProdServer } from './modern-server-split';
32
-
33
- export class Server {
34
- public options: ModernServerOptions;
35
-
36
- protected serverImpl: ServerConstructor = createProdServer;
37
-
38
- private server!: ModernServerInterface;
39
-
40
- private app!: httpServer;
41
-
42
- private runner!: ServerHookRunner;
43
-
44
- private serverConfig: ServerConfig;
45
-
46
- constructor(options: ModernServerOptions) {
47
- options.logger =
48
- options.logger ||
49
- (new Logger({
50
- level: 'warn',
51
- }) as Logger & LoggerInterface);
52
- options.metrics = options.metrics || defaultMetrics;
53
-
54
- this.options = options;
55
- this.serverConfig = {};
56
- }
57
-
58
- /**
59
- * 初始化顺序
60
- * - 获取 server runtime config
61
- * - 设置 context
62
- * - 创建 hooksRunner
63
- * - 合并插件,内置插件和 serverConfig 中配置的插件
64
- * - 执行 config hook
65
- * - 获取最终的配置
66
- * - 设置配置到 context
67
- * - 初始化 server
68
- * - 执行 prepare hook
69
- * - 执行 server init
70
- */
71
- public async init() {
72
- const { options } = this;
73
-
74
- this.initServerConfig(options);
75
-
76
- await this.injectContext(this.runner, options);
77
-
78
- // initialize server runner
79
- this.runner = await this.createHookRunner();
80
-
81
- // init config and execute config hook
82
- await this.initConfig(this.runner, options);
83
-
84
- await this.injectContext(this.runner, options);
85
-
86
- // initialize server
87
- this.server = this.serverImpl(options);
88
-
89
- await this.runPrepareHook(this.runner);
90
-
91
- // create http-server
92
- this.app = await this.server.createHTTPServer(this.getRequestHandler());
93
-
94
- // runner can only be used after server init
95
- await this.server.onInit(this.runner);
96
-
97
- return this;
98
- }
99
-
100
- /**
101
- * Execute config hooks
102
- * @param runner
103
- * @param options
104
- */
105
- private runConfigHook(runner: ServerHookRunner, serverConfig: ServerConfig) {
106
- const newServerConfig = runner.config(serverConfig || {});
107
- return newServerConfig;
108
- }
109
-
110
- private async runPrepareHook(runner: ServerHookRunner) {
111
- runner.prepare();
112
- }
113
-
114
- private initServerConfig(options: ModernServerOptions) {
115
- const { pwd, serverConfigFile } = options;
116
- const distDirectory = path.join(pwd, options.config.output?.path || 'dist');
117
- const serverConfigPath = getServerConfigPath(
118
- distDirectory,
119
- serverConfigFile,
120
- );
121
- const serverConfig = requireConfig(serverConfigPath);
122
- this.serverConfig = serverConfig;
123
- }
124
-
125
- /**
126
- *
127
- * merge cliConfig and serverConfig
128
- */
129
- private async initConfig(
130
- runner: ServerHookRunner,
131
- options: ModernServerOptions,
132
- ) {
133
- const { pwd, config } = options;
134
-
135
- const { serverConfig } = this;
136
-
137
- const finalServerConfig = this.runConfigHook(runner, serverConfig);
138
-
139
- const resolvedConfigPath = path.join(
140
- pwd,
141
- config?.output?.path || 'dist',
142
- OUTPUT_CONFIG_FILE,
143
- );
144
-
145
- options.config = loadConfig({
146
- cliConfig: config,
147
- serverConfig: finalServerConfig,
148
- resolvedConfigPath,
149
- });
150
- }
151
-
152
- public async close() {
153
- await this.server.onClose();
154
- await new Promise<void>(resolve =>
155
- this.app.close(() => {
156
- resolve();
157
- }),
158
- );
159
- }
160
-
161
- public listen(port = 8080, listener: any) {
162
- this.app.listen(process.env.PORT || port, () => {
163
- if (listener) {
164
- listener();
165
- }
166
-
167
- this.server.onListening(this.app);
168
- });
169
- }
170
-
171
- public getRequestHandler() {
172
- return (req: IncomingMessage, res: ServerResponse, next?: () => void) => {
173
- const requestHandler = this.server.getRequestHandler();
174
- return requestHandler(req, res, next);
175
- };
176
- }
177
-
178
- private async createHookRunner() {
179
- // clear server manager every create time
180
- serverManager.clear();
181
-
182
- const { options } = this;
183
- // TODO: 确认下这里是不是可以不从 options 中取插件,而是从 config 中取和过滤
184
- const { plugins = [], pwd, config } = options;
185
-
186
- const serverPlugins = this.serverConfig.plugins || [];
187
-
188
- // server app context for serve plugin
189
- const loadedPlugins = loadPlugins(plugins.concat(serverPlugins), pwd);
190
-
191
- debug('plugins', config.plugins, loadedPlugins);
192
- loadedPlugins.forEach(p => {
193
- serverManager.usePlugin(p);
194
- });
195
-
196
- // create runner
197
- const hooksRunner = await serverManager.init({});
198
-
199
- return hooksRunner;
200
- }
201
-
202
- private async injectContext(
203
- runner: ServerHookRunner,
204
- options: ModernServerOptions,
205
- ) {
206
- const appContext = this.initAppContext();
207
- const { config, pwd } = options;
208
-
209
- serverManager.run(() => {
210
- ConfigContext.set(config as UserConfig);
211
- AppContext.set({
212
- ...appContext,
213
- distDirectory: path.join(pwd, config.output?.path || 'dist'),
214
- });
215
- });
216
- }
217
-
218
- private initAppContext(): ISAppContext {
219
- const { options } = this;
220
- const { pwd: appDirectory, plugins = [], config } = options;
221
- const serverPlugins = plugins.map(p => ({
222
- server: p,
223
- }));
224
-
225
- return {
226
- appDirectory,
227
- distDirectory: path.join(appDirectory, config.output?.path || 'dist'),
228
- sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
229
- plugins: serverPlugins,
230
- };
231
- }
232
- }
@@ -1,93 +0,0 @@
1
- import { APIServerStartInput } from '@modern-js/server-core';
2
- import { ModernRoute, ModernRouteInterface } from '../libs/route';
3
- import { ApiServerMode, RUN_MODE } from '../constants';
4
- import { ModernServerContext } from '../libs/context';
5
- import { ModernServerOptions, ModernServerInterface, HookNames } from '../type';
6
- import { ModernServer } from './modern-server';
7
-
8
- class ModernSSRServer extends ModernServer {
9
- protected prepareAPIHandler(
10
- _m: ApiServerMode,
11
- _: APIServerStartInput['config'],
12
- ) {
13
- return null as any;
14
- }
15
-
16
- protected filterRoutes(routes: ModernRouteInterface[]) {
17
- return routes.filter(route => route.isSSR);
18
- }
19
-
20
- protected async setupBeforeProdMiddleware() {
21
- if (this.runMode === RUN_MODE.FULL) {
22
- await super.setupBeforeProdMiddleware();
23
- }
24
- }
25
-
26
- protected async emitRouteHook(_: HookNames, _input: any) {
27
- if (this.runMode === RUN_MODE.FULL) {
28
- await super.emitRouteHook(_, _input);
29
- }
30
- }
31
- }
32
-
33
- class ModernAPIServer extends ModernServer {
34
- protected prepareWebHandler(_: APIServerStartInput['config']) {
35
- return null as any;
36
- }
37
-
38
- protected filterRoutes(routes: ModernRouteInterface[]) {
39
- return routes.filter(route => route.isApi);
40
- }
41
-
42
- protected async setupBeforeProdMiddleware() {
43
- if (this.runMode === RUN_MODE.FULL) {
44
- await super.setupBeforeProdMiddleware();
45
- }
46
- }
47
-
48
- protected async emitRouteHook(_: HookNames, _input: any) {
49
- // empty
50
- }
51
- }
52
-
53
- class ModernWebServer extends ModernServer {
54
- protected async warmupSSRBundle() {
55
- return null;
56
- }
57
-
58
- protected async handleAPI(context: ModernServerContext) {
59
- const { proxyTarget } = this;
60
-
61
- if (proxyTarget?.api) {
62
- return this.proxy();
63
- } else {
64
- return this.render404(context);
65
- }
66
- }
67
-
68
- protected async handleWeb(context: ModernServerContext, route: ModernRoute) {
69
- const { proxyTarget } = this;
70
-
71
- if (route.isSSR && proxyTarget?.ssr) {
72
- return this.proxy();
73
- } else {
74
- // if no proxyTarget but access web server, degradation to csr
75
- route.isSSR = false;
76
- return super.handleWeb(context, route);
77
- }
78
- }
79
- }
80
-
81
- export const createProdServer = (
82
- options: ModernServerOptions,
83
- ): ModernServerInterface => {
84
- if (options.apiOnly) {
85
- return new ModernAPIServer(options);
86
- } else if (options.ssrOnly) {
87
- return new ModernSSRServer(options);
88
- } else if (options.webOnly) {
89
- return new ModernWebServer(options);
90
- } else {
91
- return new ModernServer(options);
92
- }
93
- };