@modern-js/server 1.4.1 → 1.4.4-beta.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.
Files changed (145) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/js/modern/dev-tools/mock/index.js +1 -1
  3. package/dist/js/modern/dev-tools/socket-server.js +63 -46
  4. package/dist/js/modern/index.js +1 -1
  5. package/dist/js/modern/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
  6. package/dist/js/modern/server/{dev-server/dev-server.js → dev-server.js} +19 -14
  7. package/dist/js/modern/server/index.js +15 -138
  8. package/dist/js/modern/{libs/render/cache/type.js → types.js} +0 -0
  9. package/dist/js/node/dev-tools/mock/index.js +2 -2
  10. package/dist/js/node/dev-tools/socket-server.js +63 -47
  11. package/dist/js/node/index.js +2 -2
  12. package/dist/js/node/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
  13. package/dist/js/node/server/{dev-server/dev-server.js → dev-server.js} +22 -24
  14. package/dist/js/node/server/index.js +17 -152
  15. package/dist/js/node/{libs/render/cache/type.js → types.js} +0 -0
  16. package/dist/types/dev-tools/babel/register.d.ts +1 -2
  17. package/dist/types/dev-tools/dev-server-plugin.d.ts +1 -1
  18. package/dist/types/dev-tools/launch-editor/index.d.ts +1 -2
  19. package/dist/types/dev-tools/mock/getMockData.d.ts +1 -2
  20. package/dist/types/dev-tools/mock/index.d.ts +1 -2
  21. package/dist/types/dev-tools/socket-server.d.ts +5 -1
  22. package/dist/types/index.d.ts +4 -5
  23. package/dist/types/server/{dev-server/dev-server-split.d.ts → dev-server-split.d.ts} +6 -5
  24. package/dist/types/server/{dev-server/dev-server.d.ts → dev-server.d.ts} +4 -3
  25. package/dist/types/server/index.d.ts +4 -19
  26. package/dist/types/types.d.ts +28 -0
  27. package/modern.config.js +5 -1
  28. package/package.json +13 -42
  29. package/tests/dev.test.ts +17 -0
  30. package/tests/server.test.ts +10 -3
  31. package/dist/js/modern/constants.js +0 -26
  32. package/dist/js/modern/libs/context/context.js +0 -180
  33. package/dist/js/modern/libs/context/index.js +0 -3
  34. package/dist/js/modern/libs/hook-api/route.js +0 -39
  35. package/dist/js/modern/libs/hook-api/template.js +0 -61
  36. package/dist/js/modern/libs/metrics.js +0 -12
  37. package/dist/js/modern/libs/proxy.js +0 -59
  38. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -70
  39. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -233
  40. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +0 -53
  41. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +0 -35
  42. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +0 -121
  43. package/dist/js/modern/libs/render/cache/index.js +0 -74
  44. package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -9
  45. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -35
  46. package/dist/js/modern/libs/render/cache/spr.js +0 -280
  47. package/dist/js/modern/libs/render/cache/util.js +0 -79
  48. package/dist/js/modern/libs/render/index.js +0 -65
  49. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  50. package/dist/js/modern/libs/render/modern/index.js +0 -42
  51. package/dist/js/modern/libs/render/reader.js +0 -112
  52. package/dist/js/modern/libs/render/ssr.js +0 -58
  53. package/dist/js/modern/libs/render/static.js +0 -46
  54. package/dist/js/modern/libs/render/type.js +0 -7
  55. package/dist/js/modern/libs/route/index.js +0 -68
  56. package/dist/js/modern/libs/route/matcher.js +0 -94
  57. package/dist/js/modern/libs/route/route.js +0 -24
  58. package/dist/js/modern/libs/serve-file.js +0 -28
  59. package/dist/js/modern/server/dev-server/index.js +0 -2
  60. package/dist/js/modern/server/modern-server-split.js +0 -81
  61. package/dist/js/modern/server/modern-server.js +0 -584
  62. package/dist/js/modern/type.js +0 -1
  63. package/dist/js/modern/utils.js +0 -112
  64. package/dist/js/node/constants.js +0 -36
  65. package/dist/js/node/libs/context/context.js +0 -194
  66. package/dist/js/node/libs/context/index.js +0 -18
  67. package/dist/js/node/libs/hook-api/route.js +0 -48
  68. package/dist/js/node/libs/hook-api/template.js +0 -69
  69. package/dist/js/node/libs/metrics.js +0 -18
  70. package/dist/js/node/libs/proxy.js +0 -69
  71. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -77
  72. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -238
  73. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +0 -60
  74. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +0 -42
  75. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +0 -128
  76. package/dist/js/node/libs/render/cache/index.js +0 -86
  77. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -17
  78. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -47
  79. package/dist/js/node/libs/render/cache/spr.js +0 -298
  80. package/dist/js/node/libs/render/cache/util.js +0 -105
  81. package/dist/js/node/libs/render/index.js +0 -91
  82. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  83. package/dist/js/node/libs/render/modern/index.js +0 -58
  84. package/dist/js/node/libs/render/reader.js +0 -139
  85. package/dist/js/node/libs/render/ssr.js +0 -76
  86. package/dist/js/node/libs/render/static.js +0 -62
  87. package/dist/js/node/libs/render/type.js +0 -14
  88. package/dist/js/node/libs/route/index.js +0 -83
  89. package/dist/js/node/libs/route/matcher.js +0 -108
  90. package/dist/js/node/libs/route/route.js +0 -33
  91. package/dist/js/node/libs/serve-file.js +0 -41
  92. package/dist/js/node/server/dev-server/index.js +0 -27
  93. package/dist/js/node/server/modern-server-split.js +0 -97
  94. package/dist/js/node/server/modern-server.js +0 -622
  95. package/dist/js/node/type.js +0 -5
  96. package/dist/js/node/utils.js +0 -143
  97. package/dist/types/constants.d.ts +0 -20
  98. package/dist/types/libs/context/context.d.ts +0 -61
  99. package/dist/types/libs/context/index.d.ts +0 -4
  100. package/dist/types/libs/hook-api/route.d.ts +0 -14
  101. package/dist/types/libs/hook-api/template.d.ts +0 -14
  102. package/dist/types/libs/metrics.d.ts +0 -3
  103. package/dist/types/libs/proxy.d.ts +0 -16
  104. package/dist/types/libs/render/cache/__tests__/cache.fun.test.d.ts +0 -1
  105. package/dist/types/libs/render/cache/__tests__/cache.test.d.ts +0 -1
  106. package/dist/types/libs/render/cache/__tests__/cacheable.d.ts +0 -62
  107. package/dist/types/libs/render/cache/__tests__/error-configuration.d.ts +0 -28
  108. package/dist/types/libs/render/cache/__tests__/matched-cache.d.ts +0 -124
  109. package/dist/types/libs/render/cache/index.d.ts +0 -6
  110. package/dist/types/libs/render/cache/page-caches/index.d.ts +0 -2
  111. package/dist/types/libs/render/cache/page-caches/lru.d.ts +0 -15
  112. package/dist/types/libs/render/cache/spr.d.ts +0 -24
  113. package/dist/types/libs/render/cache/type.d.ts +0 -48
  114. package/dist/types/libs/render/cache/util.d.ts +0 -17
  115. package/dist/types/libs/render/index.d.ts +0 -18
  116. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  117. package/dist/types/libs/render/modern/index.d.ts +0 -3
  118. package/dist/types/libs/render/reader.d.ts +0 -18
  119. package/dist/types/libs/render/ssr.d.ts +0 -10
  120. package/dist/types/libs/render/static.d.ts +0 -3
  121. package/dist/types/libs/render/type.d.ts +0 -34
  122. package/dist/types/libs/route/index.d.ts +0 -15
  123. package/dist/types/libs/route/matcher.d.ts +0 -15
  124. package/dist/types/libs/route/route.d.ts +0 -14
  125. package/dist/types/libs/serve-file.d.ts +0 -8
  126. package/dist/types/server/dev-server/index.d.ts +0 -2
  127. package/dist/types/server/modern-server-split.d.ts +0 -26
  128. package/dist/types/server/modern-server.d.ts +0 -72
  129. package/dist/types/type.d.ts +0 -80
  130. package/dist/types/utils.d.ts +0 -19
  131. package/tests/context.test.ts +0 -52
  132. package/tests/fixtures/hosting-files/static/index.js +0 -1
  133. package/tests/fixtures/reader/index.ts +0 -3
  134. package/tests/fixtures/route-spec/dynamic.json +0 -13
  135. package/tests/fixtures/route-spec/index.json +0 -29
  136. package/tests/fixtures/ssr/bundle.js +0 -5
  137. package/tests/fixtures/static-dir/bar.html +0 -11
  138. package/tests/fixtures/static-dir/baz/index.html +0 -11
  139. package/tests/fixtures/static-dir/foo/index.html +0 -11
  140. package/tests/helper.ts +0 -8
  141. package/tests/hook.test.ts +0 -44
  142. package/tests/middleware.test.ts +0 -179
  143. package/tests/render.test.ts +0 -102
  144. package/tests/route.test.ts +0 -77
  145. package/tests/utils.test.ts +0 -106
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @modern-js/server
2
2
 
3
+ ## 1.4.3
4
+
5
+ ### Patch Changes
6
+
7
+ - deeaa602: support svg/proxy/multi-version in unbundled
8
+ - fab92861: fix: @modern-js/core phantom dep
9
+ - Updated dependencies [deeaa602]
10
+ - Updated dependencies [54786e58]
11
+ - @modern-js/hmr-client@1.2.2
12
+ - @modern-js/utils@1.3.2
13
+ - @modern-js/core@1.4.3
14
+
15
+ ## 1.4.2
16
+
17
+ ### Patch Changes
18
+
19
+ - 735b2a81: prevent ssr compiler to send socket message
20
+ - Updated dependencies [b376c8d6]
21
+ - Updated dependencies [e62c4efd]
22
+ - Updated dependencies [e2a8233f]
23
+ - @modern-js/core@1.4.2
24
+
3
25
  ## 1.4.1
4
26
 
5
27
  ### Patch Changes
@@ -1,6 +1,6 @@
1
1
  import path from 'path';
2
2
  import { fs } from '@modern-js/utils';
3
- import { AGGRED_DIR } from "../../constants";
3
+ import { AGGRED_DIR } from '@modern-js/prod-server';
4
4
  import getMockData, { getMatched } from "./getMockData";
5
5
  export const createMockHandler = ({
6
6
  pwd
@@ -1,6 +1,5 @@
1
1
  import ws from 'ws';
2
2
  import { logger } from '@modern-js/utils';
3
- import { noop } from "../utils";
4
3
  export default class SocketServer {
5
4
  constructor(options) {
6
5
  this.wsServer = void 0;
@@ -8,6 +7,7 @@ export default class SocketServer {
8
7
  this.options = void 0;
9
8
  this.app = void 0;
10
9
  this.stats = void 0;
10
+ this.timer = null;
11
11
  this.options = options;
12
12
  } // create socket, install socket handler, bind socket event
13
13
 
@@ -32,7 +32,7 @@ export default class SocketServer {
32
32
  // only dev server, use default logger
33
33
  logger.error(err);
34
34
  });
35
- setInterval(() => {
35
+ this.timer = setInterval(() => {
36
36
  this.wsServer.clients.forEach(socket => {
37
37
  const extWs = socket;
38
38
 
@@ -40,54 +40,13 @@ export default class SocketServer {
40
40
  extWs.terminate();
41
41
  } else {
42
42
  extWs.isAlive = false;
43
- extWs.ping(noop);
43
+ extWs.ping(() => {// empty
44
+ });
44
45
  }
45
46
  });
46
47
  }, 30000);
47
48
  this.wsServer.on('connection', socket => {
48
- const connection = socket;
49
- connection.isAlive = true;
50
- connection.on('pong', () => {
51
- connection.isAlive = true;
52
- });
53
-
54
- if (!connection) {
55
- return;
56
- }
57
-
58
- this.sockets.push(connection);
59
- connection.on('close', () => {
60
- const idx = this.sockets.indexOf(connection);
61
-
62
- if (idx >= 0) {
63
- this.sockets.splice(idx, 1);
64
- }
65
- });
66
-
67
- if (this.options.client.logging) {
68
- this.sockWrite('logging', this.options.client.logging);
69
- }
70
-
71
- if (this.options.hot || this.options.hot === 'only') {
72
- this.sockWrite('hot');
73
- }
74
-
75
- if (this.options.liveReload) {
76
- this.sockWrite('liveReload');
77
- }
78
-
79
- if (this.options.client.progress) {
80
- this.sockWrite('progress', this.options.client.progress);
81
- }
82
-
83
- if (this.options.client.overlay) {
84
- this.sockWrite('overlay', this.options.client.overlay);
85
- } // send first stats to active client sock if stats exist
86
-
87
-
88
- if (this.stats) {
89
- this.sendStats(true);
90
- }
49
+ this.onConnect(socket);
91
50
  });
92
51
  }
93
52
 
@@ -106,10 +65,68 @@ export default class SocketServer {
106
65
  });
107
66
  }
108
67
 
68
+ singleWrite(socket, type, data) {
69
+ this.send(socket, JSON.stringify({
70
+ type,
71
+ data
72
+ }));
73
+ }
74
+
109
75
  close() {
110
76
  this.sockets.forEach(socket => {
111
77
  socket.close();
112
78
  });
79
+
80
+ if (this.timer) {
81
+ clearInterval(this.timer);
82
+ this.timer = null;
83
+ }
84
+ }
85
+
86
+ onConnect(socket) {
87
+ const connection = socket;
88
+ connection.isAlive = true;
89
+ connection.on('pong', () => {
90
+ connection.isAlive = true;
91
+ });
92
+
93
+ if (!connection) {
94
+ return;
95
+ }
96
+
97
+ this.sockets.push(connection);
98
+ connection.on('close', () => {
99
+ const idx = this.sockets.indexOf(connection);
100
+
101
+ if (idx >= 0) {
102
+ this.sockets.splice(idx, 1);
103
+ }
104
+ });
105
+
106
+ if (this.options.client.logging) {
107
+ this.singleWrite(connection, 'logging', this.options.client.logging);
108
+ }
109
+
110
+ if (this.options.hot || this.options.hot === 'only') {
111
+ this.singleWrite(connection, 'hot');
112
+ }
113
+
114
+ if (this.options.liveReload) {
115
+ this.singleWrite(connection, 'liveReload');
116
+ }
117
+
118
+ if (this.options.client.progress) {
119
+ this.singleWrite(connection, 'progress', this.options.client.progress);
120
+ }
121
+
122
+ if (this.options.client.overlay) {
123
+ this.singleWrite(connection, 'overlay', this.options.client.overlay);
124
+ } // send first stats to active client sock if stats exist
125
+
126
+
127
+ if (this.stats) {
128
+ this.sendStats(true);
129
+ }
113
130
  } // get standard stats
114
131
 
115
132
 
@@ -1,4 +1,4 @@
1
- import { Server } from "./server";
1
+ import { DevServer as Server } from "./server";
2
2
  export { Server };
3
3
  export default (options => {
4
4
  if (options == null) {
@@ -9,16 +9,13 @@ import path from 'path';
9
9
  import { createServer as createHttpsServer } from 'https';
10
10
  import { API_DIR, HMR_SOCK_PATH, SERVER_DIR, SHARED_DIR } from '@modern-js/utils';
11
11
  import webpackDevMiddleware from 'webpack-dev-middleware';
12
- import { ModernServer } from "../modern-server";
13
- import { createMockHandler } from "../../dev-tools/mock";
14
- import { createProxyHandler } from "../../libs/proxy";
15
- import SocketServer from "../../dev-tools/socket-server";
16
- import DevServerPlugin from "../../dev-tools/dev-server-plugin";
17
- import { createLaunchEditorHandler } from "../../dev-tools/launch-editor";
18
- import { enableRegister } from "../../dev-tools/babel/register";
19
- import * as reader from "../../libs/render/reader";
20
- import Watcher from "../../dev-tools/watcher";
21
- import { AGGRED_DIR } from "../../constants";
12
+ import { createProxyHandler, ModernServer, AGGRED_DIR } from '@modern-js/prod-server';
13
+ import { createMockHandler } from "../dev-tools/mock";
14
+ import SocketServer from "../dev-tools/socket-server";
15
+ import DevServerPlugin from "../dev-tools/dev-server-plugin";
16
+ import { createLaunchEditorHandler } from "../dev-tools/launch-editor";
17
+ import { enableRegister } from "../dev-tools/babel/register";
18
+ import Watcher from "../dev-tools/watcher";
22
19
  const DEFAULT_DEV_OPTIONS = {
23
20
  client: {
24
21
  port: '8080',
@@ -37,8 +34,7 @@ const DEFAULT_DEV_OPTIONS = {
37
34
  };
38
35
  export class ModernDevServer extends ModernServer {
39
36
  constructor(options) {
40
- super(options); // set webpack compiler
41
-
37
+ super(options);
42
38
  this.devProxyHandler = null;
43
39
  this.mockHandler = null;
44
40
  this.dev = void 0;
@@ -46,6 +42,8 @@ export class ModernDevServer extends ModernServer {
46
42
  this.socketServer = void 0;
47
43
  this.watcher = void 0;
48
44
  this.devMiddleware = void 0;
45
+ this.workDir = this.pwd; // set webpack compiler
46
+
49
47
  this.compiler = options.compiler; // set dev server options, like webpack-dev-server
50
48
 
51
49
  this.dev = typeof options.dev === 'boolean' ? DEFAULT_DEV_OPTIONS : _objectSpread(_objectSpread({}, DEFAULT_DEV_OPTIONS), options.dev);
@@ -107,7 +105,7 @@ export class ModernDevServer extends ModernServer {
107
105
  this.router.reset(this.filterRoutes(options.routes || this.presetRoutes || []));
108
106
  this.cleanSSRCache(); // reset static file
109
107
 
110
- reader.updateFile();
108
+ this.reader.updateFile();
111
109
  this.runner.reset();
112
110
  }
113
111
 
@@ -143,13 +141,16 @@ export class ModernDevServer extends ModernServer {
143
141
  if (devHttpsOption) {
144
142
  const {
145
143
  genHttpsOptions
146
- } = require("../../dev-tools/https");
144
+ } = require("../dev-tools/https");
147
145
 
148
146
  const httpsOptions = await genHttpsOptions(devHttpsOption);
149
147
  return createHttpsServer(httpsOptions, handler);
150
148
  } else {
151
149
  return createServer(handler);
152
150
  }
151
+ }
152
+
153
+ warmupSSRBundle() {// empty
153
154
  } // set up plugin to each compiler
154
155
  // register hooks for each compilation, update socket stats if recompiled
155
156
  // start dev middleware
@@ -183,6 +184,10 @@ export class ModernDevServer extends ModernServer {
183
184
  };
184
185
 
185
186
  const addHooks = compiler => {
187
+ if (compiler.name === 'server') {
188
+ return;
189
+ }
190
+
186
191
  const {
187
192
  compile,
188
193
  invalid,
@@ -1,147 +1,24 @@
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; }
1
+ import { Server } from '@modern-js/prod-server';
2
+ import { ModernDevServer } from "./dev-server";
3
+ import { ModernAPIDevServer, ModernSSRDevServer } from "./dev-server-split";
2
4
 
3
- 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
- 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
- import path from 'path';
8
- import { serverManager, AppContext, ConfigContext } from '@modern-js/server-core';
9
- import { compatRequire, logger as defaultLogger } from '@modern-js/utils';
10
- import { initAppContext, initAppDir, loadUserConfig } from '@modern-js/core';
11
- import { metrics as defaultMetrics } from "../libs/metrics";
12
- import { ModernServer } from "./modern-server";
13
- import { ModernAPIServer, ModernSSRServer, ModernWebServer } from "./modern-server-split";
14
- export class Server {
15
- constructor(options) {
16
- this.options = void 0;
17
- this.server = void 0;
18
- this.app = void 0;
19
- this.runner = void 0;
20
- this.options = options;
21
- }
22
-
23
- getRequestHandler() {
24
- return (req, res, next) => {
25
- const requestHandler = this.server.getRequestHandler();
26
- return requestHandler(req, res, next);
27
- };
5
+ const createDevServer = options => {
6
+ if (options.apiOnly) {
7
+ return new ModernAPIDevServer(options);
8
+ } else if (options.ssrOnly) {
9
+ return new ModernSSRDevServer(options);
10
+ } else {
11
+ return new ModernDevServer(options);
28
12
  }
13
+ };
29
14
 
30
- ready(readyOptions = {}) {
31
- this.server.ready(readyOptions);
32
- }
33
-
34
- async init() {
35
- const {
36
- options
37
- } = this;
38
- options.logger = options.logger || defaultLogger;
39
- options.metrics = options.metrics || defaultMetrics; // initialize server
15
+ export class DevServer extends Server {
16
+ constructor(options) {
17
+ super(options);
40
18
 
41
19
  if (options.dev) {
42
- this.server = this.createDevServer();
43
- } else {
44
- this.server = this.createProdServer();
45
- } // check if https is configured when start dev server
46
-
47
-
48
- this.app = await this.server.createHTTPServer(this.getRequestHandler());
49
- this.runner = await this.createHookRunner(); // runner can only be used after server init
50
-
51
- await this.server.init(this.runner);
52
- return this;
53
- }
54
-
55
- listen(port = 8080, listener) {
56
- this.app.listen(process.env.PORT || port, () => {
57
- if (listener) {
58
- listener();
59
- }
60
-
61
- this.listener(this.app);
62
- });
63
- }
64
-
65
- listener(app) {
66
- this.server.onListening(app);
67
- }
68
-
69
- async close() {
70
- await this.server.close();
71
- await new Promise(resolve => this.app.close(() => {
72
- resolve();
73
- }));
74
- }
75
-
76
- createProdServer() {
77
- const {
78
- options
79
- } = this;
80
-
81
- if (options.apiOnly) {
82
- return new ModernAPIServer(options);
83
- } else if (options.ssrOnly) {
84
- return new ModernSSRServer(options);
85
- } else if (options.webOnly) {
86
- return new ModernWebServer(options);
87
- } else {
88
- return new ModernServer(options);
89
- }
90
- }
91
-
92
- createDevServer() {
93
- const {
94
- options
95
- } = this;
96
-
97
- const {
98
- ModernAPIDevServer,
99
- ModernSSRDevServer,
100
- ModernDevServer
101
- } = require("./dev-server");
102
-
103
- if (options.apiOnly) {
104
- return new ModernAPIDevServer(options);
105
- } else if (options.ssrOnly) {
106
- return new ModernSSRDevServer(options);
107
- } else {
108
- return new ModernDevServer(options);
20
+ this.serverImpl = createDevServer;
109
21
  }
110
22
  }
111
23
 
112
- async createHookRunner() {
113
- var _options$plugins;
114
-
115
- const {
116
- options
117
- } = this;
118
- serverManager.clear();
119
- (_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
120
- serverManager.usePlugin(compatRequire(p.pluginPath));
121
- });
122
- const appContext = await this.initAppContext();
123
- serverManager.run(() => {
124
- var _options$config$outpu;
125
-
126
- ConfigContext.set(this.options.config);
127
- AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
128
- distDirectory: path.join(options.pwd, ((_options$config$outpu = options.config.output) === null || _options$config$outpu === void 0 ? void 0 : _options$config$outpu.path) || 'dist')
129
- }));
130
- });
131
- return serverManager.init({});
132
- }
133
-
134
- async initAppContext() {
135
- var _this$options$plugins;
136
-
137
- const appDirectory = await initAppDir();
138
- const loaded = await loadUserConfig(appDirectory);
139
- const plugins = (_this$options$plugins = this.options.plugins) === null || _this$options$plugins === void 0 ? void 0 : _this$options$plugins.map(p => ({
140
- server: p,
141
- cli: undefined
142
- }));
143
- const appContext = initAppContext(appDirectory, plugins || [], loaded.filePath);
144
- return appContext;
145
- }
146
-
147
24
  }
@@ -9,7 +9,7 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- var _constants = require("../../constants");
12
+ var _prodServer = require("@modern-js/prod-server");
13
13
 
14
14
  var _getMockData = _interopRequireWildcard(require("./getMockData"));
15
15
 
@@ -26,7 +26,7 @@ const createMockHandler = ({
26
26
  let filepath = '';
27
27
 
28
28
  for (const ext of exts) {
29
- const maybeMatch = _path.default.join(pwd, `${_constants.AGGRED_DIR.mock}/index${ext}`);
29
+ const maybeMatch = _path.default.join(pwd, `${_prodServer.AGGRED_DIR.mock}/index${ext}`);
30
30
 
31
31
  if (_utils.fs.existsSync(maybeMatch)) {
32
32
  filepath = maybeMatch;
@@ -9,8 +9,6 @@ var _ws = _interopRequireDefault(require("ws"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- var _utils2 = require("../utils");
13
-
14
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
13
 
16
14
  class SocketServer {
@@ -20,6 +18,7 @@ class SocketServer {
20
18
  this.options = void 0;
21
19
  this.app = void 0;
22
20
  this.stats = void 0;
21
+ this.timer = null;
23
22
  this.options = options;
24
23
  } // create socket, install socket handler, bind socket event
25
24
 
@@ -44,7 +43,7 @@ class SocketServer {
44
43
  // only dev server, use default logger
45
44
  _utils.logger.error(err);
46
45
  });
47
- setInterval(() => {
46
+ this.timer = setInterval(() => {
48
47
  this.wsServer.clients.forEach(socket => {
49
48
  const extWs = socket;
50
49
 
@@ -52,54 +51,13 @@ class SocketServer {
52
51
  extWs.terminate();
53
52
  } else {
54
53
  extWs.isAlive = false;
55
- extWs.ping(_utils2.noop);
54
+ extWs.ping(() => {// empty
55
+ });
56
56
  }
57
57
  });
58
58
  }, 30000);
59
59
  this.wsServer.on('connection', socket => {
60
- const connection = socket;
61
- connection.isAlive = true;
62
- connection.on('pong', () => {
63
- connection.isAlive = true;
64
- });
65
-
66
- if (!connection) {
67
- return;
68
- }
69
-
70
- this.sockets.push(connection);
71
- connection.on('close', () => {
72
- const idx = this.sockets.indexOf(connection);
73
-
74
- if (idx >= 0) {
75
- this.sockets.splice(idx, 1);
76
- }
77
- });
78
-
79
- if (this.options.client.logging) {
80
- this.sockWrite('logging', this.options.client.logging);
81
- }
82
-
83
- if (this.options.hot || this.options.hot === 'only') {
84
- this.sockWrite('hot');
85
- }
86
-
87
- if (this.options.liveReload) {
88
- this.sockWrite('liveReload');
89
- }
90
-
91
- if (this.options.client.progress) {
92
- this.sockWrite('progress', this.options.client.progress);
93
- }
94
-
95
- if (this.options.client.overlay) {
96
- this.sockWrite('overlay', this.options.client.overlay);
97
- } // send first stats to active client sock if stats exist
98
-
99
-
100
- if (this.stats) {
101
- this.sendStats(true);
102
- }
60
+ this.onConnect(socket);
103
61
  });
104
62
  }
105
63
 
@@ -118,10 +76,68 @@ class SocketServer {
118
76
  });
119
77
  }
120
78
 
79
+ singleWrite(socket, type, data) {
80
+ this.send(socket, JSON.stringify({
81
+ type,
82
+ data
83
+ }));
84
+ }
85
+
121
86
  close() {
122
87
  this.sockets.forEach(socket => {
123
88
  socket.close();
124
89
  });
90
+
91
+ if (this.timer) {
92
+ clearInterval(this.timer);
93
+ this.timer = null;
94
+ }
95
+ }
96
+
97
+ onConnect(socket) {
98
+ const connection = socket;
99
+ connection.isAlive = true;
100
+ connection.on('pong', () => {
101
+ connection.isAlive = true;
102
+ });
103
+
104
+ if (!connection) {
105
+ return;
106
+ }
107
+
108
+ this.sockets.push(connection);
109
+ connection.on('close', () => {
110
+ const idx = this.sockets.indexOf(connection);
111
+
112
+ if (idx >= 0) {
113
+ this.sockets.splice(idx, 1);
114
+ }
115
+ });
116
+
117
+ if (this.options.client.logging) {
118
+ this.singleWrite(connection, 'logging', this.options.client.logging);
119
+ }
120
+
121
+ if (this.options.hot || this.options.hot === 'only') {
122
+ this.singleWrite(connection, 'hot');
123
+ }
124
+
125
+ if (this.options.liveReload) {
126
+ this.singleWrite(connection, 'liveReload');
127
+ }
128
+
129
+ if (this.options.client.progress) {
130
+ this.singleWrite(connection, 'progress', this.options.client.progress);
131
+ }
132
+
133
+ if (this.options.client.overlay) {
134
+ this.singleWrite(connection, 'overlay', this.options.client.overlay);
135
+ } // send first stats to active client sock if stats exist
136
+
137
+
138
+ if (this.stats) {
139
+ this.sendStats(true);
140
+ }
125
141
  } // get standard stats
126
142
 
127
143
 
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  Object.defineProperty(exports, "Server", {
7
7
  enumerable: true,
8
8
  get: function () {
9
- return _server.Server;
9
+ return _server.DevServer;
10
10
  }
11
11
  });
12
12
  exports.default = void 0;
@@ -18,7 +18,7 @@ var _default = options => {
18
18
  throw new Error('can not start mserver without options');
19
19
  }
20
20
 
21
- const server = new _server.Server(options);
21
+ const server = new _server.DevServer(options);
22
22
  return server.init();
23
23
  };
24
24