@modern-js/server 2.0.0-beta.2 → 2.0.0-beta.4

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 (62) hide show
  1. package/CHANGELOG.md +148 -0
  2. package/dist/js/modern/constants.js +10 -9
  3. package/dist/js/modern/dev-tools/dev-middleware/dev-server-plugin.js +14 -20
  4. package/dist/js/modern/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +14 -19
  5. package/dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js +134 -184
  6. package/dist/js/modern/dev-tools/dev-middleware/hmr-client/types.d.js +0 -0
  7. package/dist/js/modern/dev-tools/dev-middleware/index.js +65 -60
  8. package/dist/js/modern/dev-tools/dev-middleware/socket-server.js +33 -59
  9. package/dist/js/modern/dev-tools/https/global.d.js +0 -0
  10. package/dist/js/modern/dev-tools/https/index.js +28 -5
  11. package/dist/js/modern/dev-tools/mock/getMockData.js +71 -45
  12. package/dist/js/modern/dev-tools/mock/index.js +34 -15
  13. package/dist/js/modern/dev-tools/register/index.js +71 -55
  14. package/dist/js/modern/dev-tools/watcher/dependency-tree.js +25 -34
  15. package/dist/js/modern/dev-tools/watcher/index.js +51 -33
  16. package/dist/js/modern/dev-tools/watcher/stats-cache.js +13 -20
  17. package/dist/js/modern/index.js +7 -4
  18. package/dist/js/modern/server/dev-server.js +194 -216
  19. package/dist/js/modern/server/index.js +7 -4
  20. package/dist/js/node/constants.js +27 -14
  21. package/dist/js/node/dev-tools/dev-middleware/dev-server-plugin.js +36 -24
  22. package/dist/js/node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +31 -24
  23. package/dist/js/node/dev-tools/dev-middleware/hmr-client/index.js +56 -98
  24. package/dist/js/node/dev-tools/dev-middleware/hmr-client/types.d.js +0 -0
  25. package/dist/js/node/dev-tools/dev-middleware/index.js +94 -72
  26. package/dist/js/node/dev-tools/dev-middleware/socket-server.js +57 -66
  27. package/dist/js/node/dev-tools/https/global.d.js +0 -0
  28. package/dist/js/node/dev-tools/https/index.js +52 -13
  29. package/dist/js/node/dev-tools/mock/getMockData.js +88 -51
  30. package/dist/js/node/dev-tools/mock/index.js +62 -28
  31. package/dist/js/node/dev-tools/register/index.js +98 -65
  32. package/dist/js/node/dev-tools/watcher/dependency-tree.js +50 -43
  33. package/dist/js/node/dev-tools/watcher/index.js +79 -47
  34. package/dist/js/node/dev-tools/watcher/stats-cache.js +40 -30
  35. package/dist/js/node/index.js +25 -15
  36. package/dist/js/node/server/dev-server.js +235 -236
  37. package/dist/js/node/server/index.js +26 -11
  38. package/dist/js/treeshaking/constants.js +21 -0
  39. package/dist/js/treeshaking/dev-tools/dev-middleware/dev-server-plugin.js +74 -0
  40. package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +51 -0
  41. package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/index.js +157 -0
  42. package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/types.d.js +1 -0
  43. package/dist/js/treeshaking/dev-tools/dev-middleware/index.js +363 -0
  44. package/dist/js/treeshaking/dev-tools/dev-middleware/socket-server.js +209 -0
  45. package/dist/js/treeshaking/dev-tools/https/global.d.js +1 -0
  46. package/dist/js/treeshaking/dev-tools/https/index.js +161 -0
  47. package/dist/js/treeshaking/dev-tools/mock/getMockData.js +327 -0
  48. package/dist/js/treeshaking/dev-tools/mock/index.js +191 -0
  49. package/dist/js/treeshaking/dev-tools/register/index.js +153 -0
  50. package/dist/js/treeshaking/dev-tools/watcher/dependency-tree.js +150 -0
  51. package/dist/js/treeshaking/dev-tools/watcher/index.js +200 -0
  52. package/dist/js/treeshaking/dev-tools/watcher/stats-cache.js +128 -0
  53. package/dist/js/treeshaking/index.js +9 -0
  54. package/dist/js/treeshaking/server/dev-server.js +800 -0
  55. package/dist/js/treeshaking/server/index.js +92 -0
  56. package/dist/js/treeshaking/types.js +1 -0
  57. package/dist/types/dev-tools/mock/getMockData.d.ts +2 -0
  58. package/dist/types/dev-tools/watcher/dependency-tree.d.ts +2 -0
  59. package/dist/types/dev-tools/watcher/index.d.ts +1 -17
  60. package/dist/types/index.d.ts +2 -0
  61. package/package.json +19 -16
  62. package/temp-fix-hmr.js +483 -0
@@ -0,0 +1,74 @@
1
+ function _classCallCheck(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
4
+ }
5
+ }
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
+ }
14
+ }
15
+ function _createClass(Constructor, protoProps, staticProps) {
16
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
+ if (staticProps) _defineProperties(Constructor, staticProps);
18
+ return Constructor;
19
+ }
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;
32
+ }
33
+ var DevServerPlugin = /*#__PURE__*/ function() {
34
+ "use strict";
35
+ function DevServerPlugin(options) {
36
+ _classCallCheck(this, DevServerPlugin);
37
+ _defineProperty(this, "options", void 0);
38
+ this.options = options;
39
+ }
40
+ _createClass(DevServerPlugin, [
41
+ {
42
+ key: "injectHMRClient",
43
+ value: function injectHMRClient(compiler) {
44
+ var client = this.options.client;
45
+ var host = (client === null || client === void 0 ? void 0 : client.host) ? "&host=".concat(client.host) : "";
46
+ var path = (client === null || client === void 0 ? void 0 : client.path) ? "&path=".concat(client.path) : "";
47
+ var port = (client === null || client === void 0 ? void 0 : client.port) ? "&port=".concat(client.port) : "";
48
+ var clientEntry = "".concat(require.resolve("./hmr-client"), "?").concat(host).concat(path).concat(port);
49
+ new compiler.webpack.EntryPlugin(compiler.context, clientEntry, {
50
+ name: void 0
51
+ }).apply(compiler);
52
+ }
53
+ },
54
+ {
55
+ key: "apply",
56
+ value: function apply(compiler) {
57
+ if (this.options.hot || this.options.liveReload) {
58
+ this.injectHMRClient(compiler);
59
+ }
60
+ var compilerOptions = compiler.options;
61
+ var HotModuleReplacementPlugin = compiler.webpack.HotModuleReplacementPlugin;
62
+ compilerOptions.plugins = compilerOptions.plugins || [];
63
+ if (!compilerOptions.plugins.find(function(p) {
64
+ return p.constructor === HotModuleReplacementPlugin;
65
+ })) {
66
+ var plugin = new HotModuleReplacementPlugin();
67
+ plugin.apply(compiler);
68
+ }
69
+ }
70
+ }
71
+ ]);
72
+ return DevServerPlugin;
73
+ }();
74
+ export { DevServerPlugin as default };
@@ -0,0 +1,51 @@
1
+ import { HMR_SOCK_PATH } from "@modern-js/utils/constants";
2
+ function createSocketUrl(resourceQuery) {
3
+ var searchParams = resourceQuery.substr(1).split("&");
4
+ var options = {};
5
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
6
+ try {
7
+ for(var _iterator = searchParams[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
8
+ var pair = _step.value;
9
+ var ary = pair.split("=");
10
+ options[ary[0]] = decodeURIComponent(ary[1]);
11
+ }
12
+ } catch (err) {
13
+ _didIteratorError = true;
14
+ _iteratorError = err;
15
+ } finally{
16
+ try {
17
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
18
+ _iterator.return();
19
+ }
20
+ } finally{
21
+ if (_didIteratorError) {
22
+ throw _iteratorError;
23
+ }
24
+ }
25
+ }
26
+ var currentLocation = self.location;
27
+ return getSocketUrl(options, currentLocation);
28
+ }
29
+ function formatURL(param) {
30
+ var port = param.port, protocol = param.protocol, hostname = param.hostname, pathname = param.pathname;
31
+ if (window.URL) {
32
+ var url = new URL("http://localhost");
33
+ url.port = port;
34
+ url.hostname = hostname;
35
+ url.protocol = protocol;
36
+ url.pathname = pathname;
37
+ return url.toString();
38
+ }
39
+ var colon = protocol.indexOf(":") === -1 ? ":" : "";
40
+ return "".concat(protocol).concat(colon, "//").concat(hostname, ":").concat(port).concat(pathname);
41
+ }
42
+ function getSocketUrl(urlParts, location) {
43
+ var host = urlParts.host, port = urlParts.port, path = urlParts.path, protocol = urlParts.protocol;
44
+ return formatURL({
45
+ protocol: protocol || location.protocol === "https:" ? "wss" : "ws",
46
+ hostname: host || location.hostname,
47
+ port: port || location.port,
48
+ pathname: path || HMR_SOCK_PATH
49
+ });
50
+ }
51
+ export { createSocketUrl, formatURL };
@@ -0,0 +1,157 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = function(cb, mod) {
3
+ return function __require() {
4
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = {
5
+ exports: {}
6
+ }).exports, mod), mod.exports;
7
+ };
8
+ };
9
+ import stripAnsi from "@modern-js/utils/strip-ansi";
10
+ import { formatWebpackMessages } from "@modern-js/utils/format";
11
+ import { createSocketUrl } from "./createSocketUrl";
12
+ var require_hmr_client = __commonJS({
13
+ "src/dev-tools/dev-middleware/hmr-client/index.ts": function(exports, module) {
14
+ var clearOutdatedErrors = function clearOutdatedErrors() {
15
+ if (typeof console !== "undefined" && typeof console.clear === "function") {
16
+ if (hasCompileErrors) {
17
+ console.clear();
18
+ }
19
+ }
20
+ };
21
+ var handleSuccess = function handleSuccess() {
22
+ clearOutdatedErrors();
23
+ var isHotUpdate = !isFirstCompilation;
24
+ isFirstCompilation = false;
25
+ hasCompileErrors = false;
26
+ if (isHotUpdate) {
27
+ tryApplyUpdates();
28
+ }
29
+ };
30
+ var handleWarnings = function handleWarnings(warnings) {
31
+ clearOutdatedErrors();
32
+ var isHotUpdate = !isFirstCompilation;
33
+ isFirstCompilation = false;
34
+ hasCompileErrors = false;
35
+ function printWarnings() {
36
+ var formatted = formatWebpackMessages({
37
+ warnings: warnings,
38
+ errors: []
39
+ });
40
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
41
+ for(var i = 0; i < formatted.warnings.length; i++){
42
+ if (i === 5) {
43
+ console.warn("There were more warnings in other files. You can find a complete log in the terminal.");
44
+ break;
45
+ }
46
+ console.warn(stripAnsi(formatted.warnings[i]));
47
+ }
48
+ }
49
+ }
50
+ printWarnings();
51
+ if (isHotUpdate) {
52
+ tryApplyUpdates();
53
+ }
54
+ };
55
+ var handleErrors = function handleErrors(errors) {
56
+ clearOutdatedErrors();
57
+ isFirstCompilation = false;
58
+ hasCompileErrors = true;
59
+ var formatted = formatWebpackMessages({
60
+ errors: errors,
61
+ warnings: []
62
+ });
63
+ if (typeof console !== "undefined" && typeof console.error === "function") {
64
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
65
+ try {
66
+ for(var _iterator = formatted.errors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
67
+ var error = _step.value;
68
+ console.error(stripAnsi(error));
69
+ }
70
+ } catch (err) {
71
+ _didIteratorError = true;
72
+ _iteratorError = err;
73
+ } finally{
74
+ try {
75
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
76
+ _iterator.return();
77
+ }
78
+ } finally{
79
+ if (_didIteratorError) {
80
+ throw _iteratorError;
81
+ }
82
+ }
83
+ }
84
+ }
85
+ };
86
+ var handleAvailableHash = function handleAvailableHash(hash) {
87
+ mostRecentCompilationHash = hash;
88
+ };
89
+ var isUpdateAvailable = function isUpdateAvailable() {
90
+ return mostRecentCompilationHash !== __webpack_hash__;
91
+ };
92
+ var canApplyUpdates = function canApplyUpdates() {
93
+ return module.hot.status() === "idle";
94
+ };
95
+ var hadRuntimeError = false;
96
+ var socketUrl = createSocketUrl(__resourceQuery);
97
+ var connection = new WebSocket(socketUrl);
98
+ connection.onclose = function() {
99
+ if (typeof console !== "undefined" && typeof console.info === "function") {
100
+ console.info("The development server has disconnected. Refresh the page if necessary.");
101
+ }
102
+ };
103
+ var isFirstCompilation = true;
104
+ var mostRecentCompilationHash = null;
105
+ var hasCompileErrors = false;
106
+ connection.onmessage = function(e) {
107
+ var message = JSON.parse(e.data);
108
+ switch(message.type){
109
+ case "hash":
110
+ handleAvailableHash(message.data);
111
+ break;
112
+ case "still-ok":
113
+ case "ok":
114
+ handleSuccess();
115
+ break;
116
+ case "content-changed":
117
+ window.location.reload();
118
+ break;
119
+ case "warnings":
120
+ handleWarnings(message.data);
121
+ break;
122
+ case "errors":
123
+ handleErrors(message.data);
124
+ break;
125
+ default:
126
+ }
127
+ };
128
+ function tryApplyUpdates() {
129
+ if (!module.hot) {
130
+ window.location.reload();
131
+ return;
132
+ }
133
+ if (!isUpdateAvailable() || !canApplyUpdates()) {
134
+ return;
135
+ }
136
+ function handleApplyUpdates(err, updatedModules) {
137
+ var wantsForcedReload = err || !updatedModules || hadRuntimeError;
138
+ if (wantsForcedReload) {
139
+ window.location.reload();
140
+ return;
141
+ }
142
+ if (isUpdateAvailable()) {
143
+ tryApplyUpdates();
144
+ }
145
+ }
146
+ var result = module.hot.check(true, handleApplyUpdates);
147
+ if (result === null || result === void 0 ? void 0 : result.then) {
148
+ result.then(function(updatedModules) {
149
+ handleApplyUpdates(null, updatedModules);
150
+ }, function(err) {
151
+ handleApplyUpdates(err, null);
152
+ });
153
+ }
154
+ }
155
+ }
156
+ });
157
+ export default require_hmr_client();
@@ -0,0 +1,363 @@
1
+ function _assertThisInitialized(self) {
2
+ if (self === void 0) {
3
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
4
+ }
5
+ return self;
6
+ }
7
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
8
+ try {
9
+ var info = gen[key](arg);
10
+ var value = info.value;
11
+ } catch (error) {
12
+ reject(error);
13
+ return;
14
+ }
15
+ if (info.done) {
16
+ resolve(value);
17
+ } else {
18
+ Promise.resolve(value).then(_next, _throw);
19
+ }
20
+ }
21
+ function _asyncToGenerator(fn) {
22
+ return function() {
23
+ var self = this, args = arguments;
24
+ return new Promise(function(resolve, reject) {
25
+ var gen = fn.apply(self, args);
26
+ function _next(value) {
27
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
28
+ }
29
+ function _throw(err) {
30
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
31
+ }
32
+ _next(undefined);
33
+ });
34
+ };
35
+ }
36
+ function _classCallCheck(instance, Constructor) {
37
+ if (!(instance instanceof Constructor)) {
38
+ throw new TypeError("Cannot call a class as a function");
39
+ }
40
+ }
41
+ function _defineProperties(target, props) {
42
+ for(var i = 0; i < props.length; i++){
43
+ var descriptor = props[i];
44
+ descriptor.enumerable = descriptor.enumerable || false;
45
+ descriptor.configurable = true;
46
+ if ("value" in descriptor) descriptor.writable = true;
47
+ Object.defineProperty(target, descriptor.key, descriptor);
48
+ }
49
+ }
50
+ function _createClass(Constructor, protoProps, staticProps) {
51
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
52
+ if (staticProps) _defineProperties(Constructor, staticProps);
53
+ return Constructor;
54
+ }
55
+ function _defineProperty(obj, key, value) {
56
+ if (key in obj) {
57
+ Object.defineProperty(obj, key, {
58
+ value: value,
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true
62
+ });
63
+ } else {
64
+ obj[key] = value;
65
+ }
66
+ return obj;
67
+ }
68
+ function _getPrototypeOf(o) {
69
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
70
+ return o.__proto__ || Object.getPrototypeOf(o);
71
+ };
72
+ return _getPrototypeOf(o);
73
+ }
74
+ function _inherits(subClass, superClass) {
75
+ if (typeof superClass !== "function" && superClass !== null) {
76
+ throw new TypeError("Super expression must either be null or a function");
77
+ }
78
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
79
+ constructor: {
80
+ value: subClass,
81
+ writable: true,
82
+ configurable: true
83
+ }
84
+ });
85
+ if (superClass) _setPrototypeOf(subClass, superClass);
86
+ }
87
+ function _objectSpread(target) {
88
+ for(var i = 1; i < arguments.length; i++){
89
+ var source = arguments[i] != null ? arguments[i] : {};
90
+ var ownKeys = Object.keys(source);
91
+ if (typeof Object.getOwnPropertySymbols === "function") {
92
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
93
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
94
+ }));
95
+ }
96
+ ownKeys.forEach(function(key) {
97
+ _defineProperty(target, key, source[key]);
98
+ });
99
+ }
100
+ return target;
101
+ }
102
+ function _possibleConstructorReturn(self, call) {
103
+ if (call && (_typeof(call) === "object" || typeof call === "function")) {
104
+ return call;
105
+ }
106
+ return _assertThisInitialized(self);
107
+ }
108
+ function _setPrototypeOf(o, p) {
109
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
110
+ o.__proto__ = p;
111
+ return o;
112
+ };
113
+ return _setPrototypeOf(o, p);
114
+ }
115
+ var _typeof = function(obj) {
116
+ "@swc/helpers - typeof";
117
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
118
+ };
119
+ function _isNativeReflectConstruct() {
120
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
121
+ if (Reflect.construct.sham) return false;
122
+ if (typeof Proxy === "function") return true;
123
+ try {
124
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
125
+ return true;
126
+ } catch (e) {
127
+ return false;
128
+ }
129
+ }
130
+ function _createSuper(Derived) {
131
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
132
+ return function _createSuperInternal() {
133
+ var Super = _getPrototypeOf(Derived), result;
134
+ if (hasNativeReflectConstruct) {
135
+ var NewTarget = _getPrototypeOf(this).constructor;
136
+ result = Reflect.construct(Super, arguments, NewTarget);
137
+ } else {
138
+ result = Super.apply(this, arguments);
139
+ }
140
+ return _possibleConstructorReturn(this, result);
141
+ };
142
+ }
143
+ var __generator = this && this.__generator || function(thisArg, body) {
144
+ var f, y, t, g, _ = {
145
+ label: 0,
146
+ sent: function() {
147
+ if (t[0] & 1) throw t[1];
148
+ return t[1];
149
+ },
150
+ trys: [],
151
+ ops: []
152
+ };
153
+ return g = {
154
+ next: verb(0),
155
+ "throw": verb(1),
156
+ "return": verb(2)
157
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
158
+ return this;
159
+ }), g;
160
+ function verb(n) {
161
+ return function(v) {
162
+ return step([
163
+ n,
164
+ v
165
+ ]);
166
+ };
167
+ }
168
+ function step(op) {
169
+ if (f) throw new TypeError("Generator is already executing.");
170
+ while(_)try {
171
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
172
+ if (y = 0, t) op = [
173
+ op[0] & 2,
174
+ t.value
175
+ ];
176
+ switch(op[0]){
177
+ case 0:
178
+ case 1:
179
+ t = op;
180
+ break;
181
+ case 4:
182
+ _.label++;
183
+ return {
184
+ value: op[1],
185
+ done: false
186
+ };
187
+ case 5:
188
+ _.label++;
189
+ y = op[1];
190
+ op = [
191
+ 0
192
+ ];
193
+ continue;
194
+ case 7:
195
+ op = _.ops.pop();
196
+ _.trys.pop();
197
+ continue;
198
+ default:
199
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
200
+ _ = 0;
201
+ continue;
202
+ }
203
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
204
+ _.label = op[1];
205
+ break;
206
+ }
207
+ if (op[0] === 6 && _.label < t[1]) {
208
+ _.label = t[1];
209
+ t = op;
210
+ break;
211
+ }
212
+ if (t && _.label < t[2]) {
213
+ _.label = t[2];
214
+ _.ops.push(op);
215
+ break;
216
+ }
217
+ if (t[2]) _.ops.pop();
218
+ _.trys.pop();
219
+ continue;
220
+ }
221
+ op = body.call(thisArg, _);
222
+ } catch (e) {
223
+ op = [
224
+ 6,
225
+ e
226
+ ];
227
+ y = 0;
228
+ } finally{
229
+ f = t = 0;
230
+ }
231
+ if (op[0] & 5) throw op[1];
232
+ return {
233
+ value: op[0] ? op[1] : void 0,
234
+ done: true
235
+ };
236
+ }
237
+ };
238
+ import { EventEmitter } from "events";
239
+ import webpackDevMiddleware from "@modern-js/utils/webpack-dev-middleware";
240
+ import DevServerPlugin from "./dev-server-plugin";
241
+ import SocketServer from "./socket-server";
242
+ var noop = function() {};
243
+ var DevMiddleware = /*#__PURE__*/ function(EventEmitter) {
244
+ "use strict";
245
+ _inherits(DevMiddleware, EventEmitter);
246
+ var _super = _createSuper(DevMiddleware);
247
+ function DevMiddleware(param) {
248
+ var compiler = param.compiler, dev = param.dev, devMiddleware = param.devMiddleware;
249
+ _classCallCheck(this, DevMiddleware);
250
+ var _this;
251
+ _this = _super.call(this);
252
+ _defineProperty(_assertThisInitialized(_this), "middleware", void 0);
253
+ _defineProperty(_assertThisInitialized(_this), "compiler", void 0);
254
+ _defineProperty(_assertThisInitialized(_this), "devOptions", void 0);
255
+ _defineProperty(_assertThisInitialized(_this), "socketServer", void 0);
256
+ _this.compiler = compiler;
257
+ _this.devOptions = dev;
258
+ _this.socketServer = new SocketServer(dev);
259
+ if (_this.compiler) {
260
+ _this.setupDevServerPlugin();
261
+ _this.setupHooks();
262
+ _this.middleware = _this.setupDevMiddleware(devMiddleware);
263
+ }
264
+ return _this;
265
+ }
266
+ _createClass(DevMiddleware, [
267
+ {
268
+ key: "init",
269
+ value: function init(app) {
270
+ var _this = this;
271
+ app.on("listening", function() {
272
+ _this.socketServer.prepare(app);
273
+ });
274
+ var _this1 = this;
275
+ app.on("close", /*#__PURE__*/ _asyncToGenerator(function() {
276
+ var ref;
277
+ return __generator(this, function(_state) {
278
+ (ref = _this1.middleware) === null || ref === void 0 ? void 0 : ref.close(noop);
279
+ _this1.socketServer.close();
280
+ return [
281
+ 2
282
+ ];
283
+ });
284
+ }));
285
+ }
286
+ },
287
+ {
288
+ key: "setupDevServerPlugin",
289
+ value: function setupDevServerPlugin() {
290
+ var _this = this;
291
+ var devOptions = this.devOptions;
292
+ if (this.compiler.compilers) {
293
+ this.compiler.compilers.forEach(function(target) {
294
+ if (_this.isClientCompiler(target)) {
295
+ new DevServerPlugin(devOptions).apply(target);
296
+ }
297
+ });
298
+ } else {
299
+ new DevServerPlugin(devOptions).apply(this.compiler);
300
+ }
301
+ }
302
+ },
303
+ {
304
+ key: "sockWrite",
305
+ value: function sockWrite(type, data) {
306
+ this.socketServer.sockWrite(type, data);
307
+ }
308
+ },
309
+ {
310
+ key: "setupHooks",
311
+ value: function setupHooks() {
312
+ var _this = this;
313
+ var invalidPlugin = function() {
314
+ _this.socketServer.sockWrite("invalid");
315
+ };
316
+ var addHooks = function(compiler) {
317
+ if (compiler.name === "server") {
318
+ return;
319
+ }
320
+ var _hooks = compiler.hooks, compile = _hooks.compile, invalid = _hooks.invalid, done = _hooks.done;
321
+ compile.tap("modern-dev-server", invalidPlugin);
322
+ invalid.tap("modern-dev-server", invalidPlugin);
323
+ done.tap("modern-dev-server", function(stats) {
324
+ _this.socketServer.updateStats(stats);
325
+ _this.emit("change", stats);
326
+ });
327
+ };
328
+ if (this.compiler.compilers) {
329
+ this.compiler.compilers.forEach(addHooks);
330
+ } else {
331
+ addHooks(this.compiler);
332
+ }
333
+ }
334
+ },
335
+ {
336
+ key: "setupDevMiddleware",
337
+ value: function setupDevMiddleware() {
338
+ var devMiddleware = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : webpackDevMiddleware;
339
+ var devOptions = this.devOptions;
340
+ var middleware = devMiddleware(this.compiler, _objectSpread({
341
+ headers: devOptions.headers,
342
+ stats: false
343
+ }, devOptions.devMiddleware));
344
+ return middleware;
345
+ }
346
+ },
347
+ {
348
+ key: "isClientCompiler",
349
+ value: function isClientCompiler(compiler) {
350
+ var target = compiler.options.target;
351
+ if (target) {
352
+ if (Array.isArray(target)) {
353
+ return !target.includes("node");
354
+ }
355
+ return target !== "node";
356
+ }
357
+ return compiler.name === "client";
358
+ }
359
+ }
360
+ ]);
361
+ return DevMiddleware;
362
+ }(EventEmitter);
363
+ export { DevMiddleware as default };