@modern-js/server 2.15.1-alpha.0 → 2.16.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 (98) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/constants.js +12 -28
  3. package/dist/cjs/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +14 -32
  4. package/dist/cjs/dev-tools/dev-middleware/hmr-client/index.js +24 -42
  5. package/dist/cjs/dev-tools/dev-middleware/index.js +45 -50
  6. package/dist/cjs/dev-tools/dev-middleware/socketServer.js +48 -46
  7. package/dist/cjs/dev-tools/https/index.js +21 -38
  8. package/dist/cjs/dev-tools/mock/getMockData.js +33 -33
  9. package/dist/cjs/dev-tools/mock/index.js +67 -47
  10. package/dist/cjs/dev-tools/register/index.js +59 -70
  11. package/dist/cjs/dev-tools/watcher/dependencyTree.js +52 -55
  12. package/dist/cjs/dev-tools/watcher/index.js +52 -53
  13. package/dist/cjs/dev-tools/watcher/statsCache.js +37 -47
  14. package/dist/cjs/index.js +16 -28
  15. package/dist/cjs/server/devServer.js +163 -109
  16. package/dist/cjs/server/index.js +11 -29
  17. package/dist/cjs/types.js +4 -15
  18. package/dist/esm/constants.js +19 -20
  19. package/dist/esm/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +43 -44
  20. package/dist/esm/dev-tools/dev-middleware/hmr-client/index.js +148 -157
  21. package/dist/esm/dev-tools/dev-middleware/index.js +307 -291
  22. package/dist/esm/dev-tools/dev-middleware/socketServer.js +216 -202
  23. package/dist/esm/dev-tools/https/index.js +189 -182
  24. package/dist/esm/dev-tools/mock/getMockData.js +297 -278
  25. package/dist/esm/dev-tools/mock/index.js +198 -192
  26. package/dist/esm/dev-tools/register/index.js +141 -141
  27. package/dist/esm/dev-tools/watcher/dependencyTree.js +153 -138
  28. package/dist/esm/dev-tools/watcher/index.js +187 -175
  29. package/dist/esm/dev-tools/watcher/statsCache.js +118 -116
  30. package/dist/esm/index.js +9 -8
  31. package/dist/esm/server/devServer.js +803 -766
  32. package/dist/esm/server/index.js +80 -76
  33. package/dist/esm/types.js +1 -1
  34. package/dist/esm-node/constants.js +4 -5
  35. package/dist/esm-node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +2 -11
  36. package/dist/esm-node/dev-tools/dev-middleware/hmr-client/index.js +125 -139
  37. package/dist/esm-node/dev-tools/dev-middleware/index.js +31 -17
  38. package/dist/esm-node/dev-tools/dev-middleware/socketServer.js +33 -12
  39. package/dist/esm-node/dev-tools/https/index.js +13 -15
  40. package/dist/esm-node/dev-tools/mock/getMockData.js +20 -12
  41. package/dist/esm-node/dev-tools/mock/index.js +7 -7
  42. package/dist/esm-node/dev-tools/register/index.js +38 -29
  43. package/dist/esm-node/dev-tools/watcher/dependencyTree.js +32 -21
  44. package/dist/esm-node/dev-tools/watcher/index.js +31 -18
  45. package/dist/esm-node/dev-tools/watcher/statsCache.js +18 -8
  46. package/dist/esm-node/index.js +2 -5
  47. package/dist/esm-node/server/devServer.js +87 -62
  48. package/dist/esm-node/server/index.js +1 -4
  49. package/dist/esm-node/types.js +1 -0
  50. package/package.json +13 -9
  51. package/dist/js/modern/constants.js +0 -19
  52. package/dist/js/modern/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -41
  53. package/dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js +0 -143
  54. package/dist/js/modern/dev-tools/dev-middleware/index.js +0 -94
  55. package/dist/js/modern/dev-tools/dev-middleware/socket-server.js +0 -132
  56. package/dist/js/modern/dev-tools/https/index.js +0 -49
  57. package/dist/js/modern/dev-tools/mock/getMockData.js +0 -114
  58. package/dist/js/modern/dev-tools/mock/index.js +0 -54
  59. package/dist/js/modern/dev-tools/register/index.js +0 -111
  60. package/dist/js/modern/dev-tools/watcher/dependency-tree.js +0 -59
  61. package/dist/js/modern/dev-tools/watcher/index.js +0 -106
  62. package/dist/js/modern/dev-tools/watcher/stats-cache.js +0 -58
  63. package/dist/js/modern/index.js +0 -12
  64. package/dist/js/modern/server/dev-server.js +0 -293
  65. package/dist/js/modern/server/index.js +0 -16
  66. package/dist/js/modern/types.js +0 -0
  67. package/dist/js/node/constants.js +0 -42
  68. package/dist/js/node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -65
  69. package/dist/js/node/dev-tools/dev-middleware/hmr-client/index.js +0 -152
  70. package/dist/js/node/dev-tools/dev-middleware/index.js +0 -119
  71. package/dist/js/node/dev-tools/dev-middleware/socket-server.js +0 -159
  72. package/dist/js/node/dev-tools/https/index.js +0 -72
  73. package/dist/js/node/dev-tools/mock/getMockData.js +0 -135
  74. package/dist/js/node/dev-tools/mock/index.js +0 -83
  75. package/dist/js/node/dev-tools/register/index.js +0 -138
  76. package/dist/js/node/dev-tools/watcher/dependency-tree.js +0 -89
  77. package/dist/js/node/dev-tools/watcher/index.js +0 -135
  78. package/dist/js/node/dev-tools/watcher/stats-cache.js +0 -87
  79. package/dist/js/node/index.js +0 -35
  80. package/dist/js/node/server/dev-server.js +0 -310
  81. package/dist/js/node/server/index.js +0 -39
  82. package/dist/js/node/types.js +0 -15
  83. package/dist/js/treeshaking/constants.js +0 -21
  84. package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +0 -51
  85. package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/index.js +0 -157
  86. package/dist/js/treeshaking/dev-tools/dev-middleware/index.js +0 -322
  87. package/dist/js/treeshaking/dev-tools/dev-middleware/socket-server.js +0 -209
  88. package/dist/js/treeshaking/dev-tools/https/index.js +0 -193
  89. package/dist/js/treeshaking/dev-tools/mock/getMockData.js +0 -327
  90. package/dist/js/treeshaking/dev-tools/mock/index.js +0 -191
  91. package/dist/js/treeshaking/dev-tools/register/index.js +0 -153
  92. package/dist/js/treeshaking/dev-tools/watcher/dependency-tree.js +0 -150
  93. package/dist/js/treeshaking/dev-tools/watcher/index.js +0 -200
  94. package/dist/js/treeshaking/dev-tools/watcher/stats-cache.js +0 -128
  95. package/dist/js/treeshaking/index.js +0 -9
  96. package/dist/js/treeshaking/server/dev-server.js +0 -799
  97. package/dist/js/treeshaking/server/index.js +0 -92
  98. package/dist/js/treeshaking/types.js +0 -1
@@ -1,214 +1,228 @@
1
- function _classCallCheck(instance, Constructor) {
2
- if (!(instance instanceof Constructor)) {
3
- throw new TypeError("Cannot call a class as a function");
4
- }
1
+ function _class_call_check(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
4
+ }
5
5
  }
6
6
  function _defineProperties(target, props) {
7
- for(var i = 0; i < props.length; i++){
8
- var descriptor = props[i];
9
- descriptor.enumerable = descriptor.enumerable || false;
10
- descriptor.configurable = true;
11
- if ("value" in descriptor) descriptor.writable = true;
12
- Object.defineProperty(target, descriptor.key, descriptor);
13
- }
7
+ for (var i = 0; i < props.length; i++) {
8
+ var descriptor = props[i];
9
+ descriptor.enumerable = descriptor.enumerable || false;
10
+ descriptor.configurable = true;
11
+ if ("value" in descriptor)
12
+ descriptor.writable = true;
13
+ Object.defineProperty(target, descriptor.key, descriptor);
14
+ }
14
15
  }
15
- function _createClass(Constructor, protoProps, staticProps) {
16
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
- if (staticProps) _defineProperties(Constructor, staticProps);
18
- return Constructor;
16
+ function _create_class(Constructor, protoProps, staticProps) {
17
+ if (protoProps)
18
+ _defineProperties(Constructor.prototype, protoProps);
19
+ if (staticProps)
20
+ _defineProperties(Constructor, staticProps);
21
+ return Constructor;
19
22
  }
20
- function _defineProperty(obj, key, value) {
21
- if (key in obj) {
22
- Object.defineProperty(obj, key, {
23
- value: value,
24
- enumerable: true,
25
- configurable: true,
26
- writable: true
27
- });
28
- } else {
29
- obj[key] = value;
30
- }
31
- return obj;
23
+ function _define_property(obj, key, value) {
24
+ if (key in obj) {
25
+ Object.defineProperty(obj, key, {
26
+ value,
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true
30
+ });
31
+ } else {
32
+ obj[key] = value;
33
+ }
34
+ return obj;
32
35
  }
33
36
  import ws from "ws";
34
37
  import { logger } from "@modern-js/utils";
35
- var SocketServer = /*#__PURE__*/ function() {
36
- "use strict";
37
- function SocketServer(options) {
38
- _classCallCheck(this, SocketServer);
39
- _defineProperty(this, "wsServer", void 0);
40
- _defineProperty(this, "sockets", []);
41
- _defineProperty(this, "options", void 0);
42
- _defineProperty(this, "app", void 0);
43
- _defineProperty(this, "stats", void 0);
44
- _defineProperty(this, "timer", null);
45
- this.options = options;
46
- }
47
- _createClass(SocketServer, [
48
- {
49
- // create socket, install socket handler, bind socket event
50
- key: "prepare",
51
- value: function prepare(app) {
52
- var _this = this;
53
- var _this_options_client;
54
- this.app = app;
55
- this.wsServer = new ws.Server({
56
- noServer: true,
57
- path: (_this_options_client = this.options.client) === null || _this_options_client === void 0 ? void 0 : _this_options_client.path
58
- });
59
- this.app.on("upgrade", function(req, sock, head) {
60
- if (!_this.wsServer.shouldHandle(req)) {
61
- return;
62
- }
63
- _this.wsServer.handleUpgrade(req, sock, head, function(connection) {
64
- _this.wsServer.emit("connection", connection, req);
65
- });
66
- });
67
- this.wsServer.on("error", function(err) {
68
- logger.error(err);
69
- });
70
- this.timer = setInterval(function() {
71
- _this.wsServer.clients.forEach(function(socket) {
72
- var extWs = socket;
73
- if (!extWs.isAlive) {
74
- extWs.terminate();
75
- } else {
76
- extWs.isAlive = false;
77
- extWs.ping(function() {});
78
- }
79
- });
80
- }, 3e4);
81
- this.wsServer.on("connection", function(socket) {
82
- _this.onConnect(socket);
83
- });
84
- }
85
- },
86
- {
87
- key: "updateStats",
88
- value: function updateStats(stats) {
89
- this.stats = stats;
90
- this.sendStats();
91
- }
92
- },
93
- {
94
- // write message to each socket
95
- key: "sockWrite",
96
- value: function sockWrite(type, data) {
97
- var _this = this;
98
- this.sockets.forEach(function(socket) {
99
- _this.send(socket, JSON.stringify({
100
- type: type,
101
- data: data
102
- }));
103
- });
104
- }
105
- },
106
- {
107
- key: "singleWrite",
108
- value: function singleWrite(socket, type, data) {
109
- this.send(socket, JSON.stringify({
110
- type: type,
111
- data: data
112
- }));
113
- }
114
- },
115
- {
116
- key: "close",
117
- value: function close() {
118
- this.sockets.forEach(function(socket) {
119
- socket.close();
120
- });
121
- if (this.timer) {
122
- clearInterval(this.timer);
123
- this.timer = null;
124
- }
125
- }
126
- },
127
- {
128
- key: "onConnect",
129
- value: function onConnect(socket) {
130
- var _this = this;
131
- var connection = socket;
132
- connection.isAlive = true;
133
- connection.on("pong", function() {
134
- connection.isAlive = true;
135
- });
136
- if (!connection) {
137
- return;
138
- }
139
- this.sockets.push(connection);
140
- connection.on("close", function() {
141
- var idx = _this.sockets.indexOf(connection);
142
- if (idx >= 0) {
143
- _this.sockets.splice(idx, 1);
144
- }
145
- });
146
- if (this.options.hot || this.options.hot === "only") {
147
- this.singleWrite(connection, "hot");
148
- }
149
- if (this.options.liveReload) {
150
- this.singleWrite(connection, "liveReload");
151
- }
152
- if (this.stats) {
153
- this.sendStats(true);
154
- }
155
- }
156
- },
157
- {
158
- // get standard stats
159
- key: "getStats",
160
- value: function getStats() {
161
- var curStats = this.stats;
162
- if (!curStats) {
163
- return null;
164
- }
165
- var defaultStats = {
166
- all: false,
167
- hash: true,
168
- assets: true,
169
- warnings: true,
170
- errors: true,
171
- errorDetails: false
172
- };
173
- return curStats.toJson(defaultStats);
38
+ var SocketServer = /* @__PURE__ */ function() {
39
+ "use strict";
40
+ function SocketServer2(options) {
41
+ _class_call_check(this, SocketServer2);
42
+ _define_property(this, "wsServer", void 0);
43
+ _define_property(this, "sockets", []);
44
+ _define_property(this, "options", void 0);
45
+ _define_property(this, "app", void 0);
46
+ _define_property(this, "stats", void 0);
47
+ _define_property(this, "timer", null);
48
+ this.options = options;
49
+ }
50
+ _create_class(SocketServer2, [
51
+ {
52
+ key: "prepare",
53
+ value: (
54
+ // create socket, install socket handler, bind socket event
55
+ function prepare(app) {
56
+ var _this = this;
57
+ var _this_options_client;
58
+ this.app = app;
59
+ this.wsServer = new ws.Server({
60
+ noServer: true,
61
+ path: (_this_options_client = this.options.client) === null || _this_options_client === void 0 ? void 0 : _this_options_client.path
62
+ });
63
+ this.app.on("upgrade", function(req, sock, head) {
64
+ if (!_this.wsServer.shouldHandle(req)) {
65
+ return;
174
66
  }
175
- },
176
- {
177
- // determine what message should send by stats
178
- key: "sendStats",
179
- value: function sendStats() {
180
- var force = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
181
- var stats = this.getStats();
182
- if (!stats) {
183
- return null;
184
- }
185
- var shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(function(asset) {
186
- return !asset.emitted;
67
+ _this.wsServer.handleUpgrade(req, sock, head, function(connection) {
68
+ _this.wsServer.emit("connection", connection, req);
69
+ });
70
+ });
71
+ this.wsServer.on("error", function(err) {
72
+ logger.error(err);
73
+ });
74
+ this.timer = setInterval(function() {
75
+ _this.wsServer.clients.forEach(function(socket) {
76
+ var extWs = socket;
77
+ if (!extWs.isAlive) {
78
+ extWs.terminate();
79
+ } else {
80
+ extWs.isAlive = false;
81
+ extWs.ping(function() {
187
82
  });
188
- if (shouldEmit) {
189
- return this.sockWrite("still-ok");
190
- }
191
- this.sockWrite("hash", stats.hash);
192
- if (stats.errors && stats.errors.length > 0) {
193
- return this.sockWrite("errors", stats.errors);
194
- } else if (stats.warnings && stats.warnings.length > 0) {
195
- return this.sockWrite("warnings", stats.warnings);
196
- } else {
197
- return this.sockWrite("ok");
198
- }
199
- }
200
- },
201
- {
202
- // send message to connecting socket
203
- key: "send",
204
- value: function send(connection, message) {
205
- if (connection.readyState !== 1) {
206
- return;
207
- }
208
- connection.send(message);
209
- }
83
+ }
84
+ });
85
+ }, 3e4);
86
+ this.wsServer.on("connection", function(socket) {
87
+ _this.onConnect(socket);
88
+ });
89
+ }
90
+ )
91
+ },
92
+ {
93
+ key: "updateStats",
94
+ value: function updateStats(stats) {
95
+ this.stats = stats;
96
+ this.sendStats();
97
+ }
98
+ },
99
+ {
100
+ key: "sockWrite",
101
+ value: (
102
+ // write message to each socket
103
+ function sockWrite(type, data) {
104
+ var _this = this;
105
+ this.sockets.forEach(function(socket) {
106
+ _this.send(socket, JSON.stringify({
107
+ type,
108
+ data
109
+ }));
110
+ });
111
+ }
112
+ )
113
+ },
114
+ {
115
+ key: "singleWrite",
116
+ value: function singleWrite(socket, type, data) {
117
+ this.send(socket, JSON.stringify({
118
+ type,
119
+ data
120
+ }));
121
+ }
122
+ },
123
+ {
124
+ key: "close",
125
+ value: function close() {
126
+ this.sockets.forEach(function(socket) {
127
+ socket.close();
128
+ });
129
+ if (this.timer) {
130
+ clearInterval(this.timer);
131
+ this.timer = null;
132
+ }
133
+ }
134
+ },
135
+ {
136
+ key: "onConnect",
137
+ value: function onConnect(socket) {
138
+ var _this = this;
139
+ var connection = socket;
140
+ connection.isAlive = true;
141
+ connection.on("pong", function() {
142
+ connection.isAlive = true;
143
+ });
144
+ if (!connection) {
145
+ return;
210
146
  }
211
- ]);
212
- return SocketServer;
147
+ this.sockets.push(connection);
148
+ connection.on("close", function() {
149
+ var idx = _this.sockets.indexOf(connection);
150
+ if (idx >= 0) {
151
+ _this.sockets.splice(idx, 1);
152
+ }
153
+ });
154
+ if (this.options.hot || this.options.hot === "only") {
155
+ this.singleWrite(connection, "hot");
156
+ }
157
+ if (this.options.liveReload) {
158
+ this.singleWrite(connection, "liveReload");
159
+ }
160
+ if (this.stats) {
161
+ this.sendStats(true);
162
+ }
163
+ }
164
+ },
165
+ {
166
+ key: "getStats",
167
+ value: (
168
+ // get standard stats
169
+ function getStats() {
170
+ var curStats = this.stats;
171
+ if (!curStats) {
172
+ return null;
173
+ }
174
+ var defaultStats = {
175
+ all: false,
176
+ hash: true,
177
+ assets: true,
178
+ warnings: true,
179
+ errors: true,
180
+ errorDetails: false
181
+ };
182
+ return curStats.toJson(defaultStats);
183
+ }
184
+ )
185
+ },
186
+ {
187
+ key: "sendStats",
188
+ value: (
189
+ // determine what message should send by stats
190
+ function sendStats() {
191
+ var force = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
192
+ var stats = this.getStats();
193
+ if (!stats) {
194
+ return null;
195
+ }
196
+ var shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(function(asset) {
197
+ return !asset.emitted;
198
+ });
199
+ if (shouldEmit) {
200
+ return this.sockWrite("still-ok");
201
+ }
202
+ this.sockWrite("hash", stats.hash);
203
+ if (stats.errors && stats.errors.length > 0) {
204
+ return this.sockWrite("errors", stats.errors);
205
+ } else if (stats.warnings && stats.warnings.length > 0) {
206
+ return this.sockWrite("warnings", stats.warnings);
207
+ } else {
208
+ return this.sockWrite("ok");
209
+ }
210
+ }
211
+ )
212
+ },
213
+ {
214
+ key: "send",
215
+ value: (
216
+ // send message to connecting socket
217
+ function send(connection, message) {
218
+ if (connection.readyState !== 1) {
219
+ return;
220
+ }
221
+ connection.send(message);
222
+ }
223
+ )
224
+ }
225
+ ]);
226
+ return SocketServer2;
213
227
  }();
214
228
  export { SocketServer as default };