@modern-js/server 2.0.0-beta.3 → 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 +79 -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 -54
  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 +191 -215
  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 -64
  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 +234 -237
  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 -1
  60. package/dist/types/index.d.ts +2 -0
  61. package/package.json +16 -13
  62. package/temp-fix-hmr.js +483 -0
@@ -0,0 +1,92 @@
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 _classCallCheck(instance, Constructor) {
8
+ if (!(instance instanceof Constructor)) {
9
+ throw new TypeError("Cannot call a class as a function");
10
+ }
11
+ }
12
+ function _getPrototypeOf(o) {
13
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
14
+ return o.__proto__ || Object.getPrototypeOf(o);
15
+ };
16
+ return _getPrototypeOf(o);
17
+ }
18
+ function _inherits(subClass, superClass) {
19
+ if (typeof superClass !== "function" && superClass !== null) {
20
+ throw new TypeError("Super expression must either be null or a function");
21
+ }
22
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
23
+ constructor: {
24
+ value: subClass,
25
+ writable: true,
26
+ configurable: true
27
+ }
28
+ });
29
+ if (superClass) _setPrototypeOf(subClass, superClass);
30
+ }
31
+ function _possibleConstructorReturn(self, call) {
32
+ if (call && (_typeof(call) === "object" || typeof call === "function")) {
33
+ return call;
34
+ }
35
+ return _assertThisInitialized(self);
36
+ }
37
+ function _setPrototypeOf(o, p) {
38
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
39
+ o.__proto__ = p;
40
+ return o;
41
+ };
42
+ return _setPrototypeOf(o, p);
43
+ }
44
+ var _typeof = function(obj) {
45
+ "@swc/helpers - typeof";
46
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
47
+ };
48
+ function _isNativeReflectConstruct() {
49
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
50
+ if (Reflect.construct.sham) return false;
51
+ if (typeof Proxy === "function") return true;
52
+ try {
53
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
54
+ return true;
55
+ } catch (e) {
56
+ return false;
57
+ }
58
+ }
59
+ function _createSuper(Derived) {
60
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
61
+ return function _createSuperInternal() {
62
+ var Super = _getPrototypeOf(Derived), result;
63
+ if (hasNativeReflectConstruct) {
64
+ var NewTarget = _getPrototypeOf(this).constructor;
65
+ result = Reflect.construct(Super, arguments, NewTarget);
66
+ } else {
67
+ result = Super.apply(this, arguments);
68
+ }
69
+ return _possibleConstructorReturn(this, result);
70
+ };
71
+ }
72
+ import { Server } from "@modern-js/prod-server";
73
+ import { ModernDevServer } from "./dev-server";
74
+ var createDevServer = function(options) {
75
+ return new ModernDevServer(options);
76
+ };
77
+ var DevServer = /*#__PURE__*/ function(Server) {
78
+ "use strict";
79
+ _inherits(DevServer, Server);
80
+ var _super = _createSuper(DevServer);
81
+ function DevServer(options) {
82
+ _classCallCheck(this, DevServer);
83
+ var _this;
84
+ _this = _super.call(this, options);
85
+ if (options.dev) {
86
+ _this.serverImpl = createDevServer;
87
+ }
88
+ return _this;
89
+ }
90
+ return DevServer;
91
+ }(Server);
92
+ export { DevServer };
@@ -0,0 +1 @@
1
+ "use strict";
@@ -11,6 +11,8 @@ export declare type MockApi = {
11
11
  };
12
12
  declare const createFunctionDataHandler: (method: string, handler: (req: IncomingMessage, res: ServerResponse, next: NextFunction) => void) => (context: ModernServerContext, next: NextFunction) => Promise<void>;
13
13
  declare const createStaticDataHandler: (method: string, handler: Record<string, any>) => (context: ModernServerContext) => void;
14
+
14
15
  declare const _default: (filepath: string) => MockApi[];
16
+
15
17
  export default _default;
16
18
  export declare const getMatched: (context: ModernServerContext, mockApiList: MockApi[]) => MockApi | undefined;
@@ -14,6 +14,7 @@ export interface TreeNode {
14
14
  * `module.parent` is the module that first required. so we have to implement
15
15
  * a new tree which revisit the cache tree to find all parent node
16
16
  */
17
+
17
18
  export declare class DependencyTree {
18
19
  private readonly tree;
19
20
  private readonly ignore;
@@ -24,6 +25,7 @@ export declare class DependencyTree {
24
25
  *
25
26
  * @param cache
26
27
  */
28
+
27
29
  update(cache: any): void;
28
30
  private shouldIgnore;
29
31
  }
@@ -5,7 +5,7 @@ export declare const defaultWatchOptions: {
5
5
  ignored: RegExp;
6
6
  };
7
7
  export declare const getWatchedFiles: (watcher: FSWatcher) => string[];
8
- export declare const mergeWatchOptions: (options?: any) => any;
8
+ export declare const mergeWatchOptions: (options?: WatchOptions) => WatchOptions;
9
9
  export default class Watcher {
10
10
  private dependencyTree;
11
11
  private watcher;
@@ -2,5 +2,7 @@ import { DevServer as Server } from './server';
2
2
  import type { ModernDevServerOptions } from './types';
3
3
  export { Server };
4
4
  export type { ModernDevServerOptions };
5
+
5
6
  declare const _default: (options: ModernDevServerOptions) => Promise<Server>;
7
+
6
8
  export default _default;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.0.0-beta.3",
14
+ "version": "2.0.0-beta.4",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -35,10 +35,10 @@
35
35
  "minimatch": "^3.0.4",
36
36
  "path-to-regexp": "^6.2.0",
37
37
  "ws": "^8.2.0",
38
- "@modern-js/prod-server": "2.0.0-beta.3",
39
- "@modern-js/server-utils": "2.0.0-beta.3",
40
- "@modern-js/types": "2.0.0-beta.3",
41
- "@modern-js/utils": "2.0.0-beta.3"
38
+ "@modern-js/prod-server": "2.0.0-beta.4",
39
+ "@modern-js/server-utils": "2.0.0-beta.4",
40
+ "@modern-js/types": "2.0.0-beta.4",
41
+ "@modern-js/utils": "2.0.0-beta.4"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/connect-history-api-fallback": "^1.3.5",
@@ -53,27 +53,30 @@
53
53
  "typescript": "^4",
54
54
  "webpack": "^5.75.0",
55
55
  "websocket": "^1",
56
- "@modern-js/server-core": "2.0.0-beta.3",
57
- "@scripts/build": "2.0.0-beta.3",
58
- "@scripts/jest-config": "2.0.0-beta.3"
56
+ "@modern-js/server-core": "2.0.0-beta.4",
57
+ "@scripts/build": "2.0.0-beta.4",
58
+ "@scripts/jest-config": "2.0.0-beta.4"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "ts-node": "^10.1.0",
62
62
  "tsconfig-paths": ">= 3.0.0 || >= 4.0.0"
63
63
  },
64
- "sideEffects": false,
65
- "modernConfig": {
66
- "output": {
67
- "packageMode": "node-js"
64
+ "peerDependenciesMeta": {
65
+ "ts-node": {
66
+ "optional": true
67
+ },
68
+ "tsconfig-paths": {
69
+ "optional": true
68
70
  }
69
71
  },
72
+ "sideEffects": false,
70
73
  "publishConfig": {
71
74
  "registry": "https://registry.npmjs.org/",
72
75
  "access": "public"
73
76
  },
74
77
  "scripts": {
75
78
  "new": "modern-lib new",
76
- "build": "modern-lib build",
79
+ "build": "modern-lib build && node ./temp-fix-hmr.js",
77
80
  "dev": "modern-lib build --watch",
78
81
  "test": "jest"
79
82
  }
@@ -0,0 +1,483 @@
1
+ // FIXME: 临时修复 HMR 代码
2
+ // TODO: 修复 HMR 问题后移除
3
+ // eslint-disable-next-line eslint-comments/disable-enable-pair
4
+ /* eslint-disable max-lines */
5
+
6
+ const path = require('path');
7
+ const { fs } = require('@modern-js/utils');
8
+
9
+ fs.outputFileSync(
10
+ path.join(
11
+ __dirname,
12
+ 'dist/js/node/dev-tools/dev-middleware/hmr-client/index.js',
13
+ ),
14
+ `var __create = Object.create;
15
+ var __defProp = Object.defineProperty;
16
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
17
+ var __getOwnPropNames = Object.getOwnPropertyNames;
18
+ var __getProtoOf = Object.getPrototypeOf;
19
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
32
+ var import_strip_ansi = __toESM(require("@modern-js/utils/strip-ansi"));
33
+ var import_format = require("@modern-js/utils/format");
34
+ var import_createSocketUrl = require("./createSocketUrl");
35
+ const hadRuntimeError = false;
36
+ const socketUrl = (0, import_createSocketUrl.createSocketUrl)(__resourceQuery);
37
+ const connection = new WebSocket(socketUrl);
38
+ connection.onclose = function() {
39
+ if (typeof console !== "undefined" && typeof console.info === "function") {
40
+ console.info(
41
+ "The development server has disconnected. Refresh the page if necessary."
42
+ );
43
+ }
44
+ };
45
+ let isFirstCompilation = true;
46
+ let mostRecentCompilationHash = null;
47
+ let hasCompileErrors = false;
48
+ function clearOutdatedErrors() {
49
+ if (typeof console !== "undefined" && typeof console.clear === "function") {
50
+ if (hasCompileErrors) {
51
+ console.clear();
52
+ }
53
+ }
54
+ }
55
+ function handleSuccess() {
56
+ clearOutdatedErrors();
57
+ const isHotUpdate = !isFirstCompilation;
58
+ isFirstCompilation = false;
59
+ hasCompileErrors = false;
60
+ if (isHotUpdate) {
61
+ tryApplyUpdates();
62
+ }
63
+ }
64
+ function handleWarnings(warnings) {
65
+ clearOutdatedErrors();
66
+ const isHotUpdate = !isFirstCompilation;
67
+ isFirstCompilation = false;
68
+ hasCompileErrors = false;
69
+ function printWarnings() {
70
+ const formatted = (0, import_format.formatWebpackMessages)({
71
+ warnings,
72
+ errors: []
73
+ });
74
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
75
+ for (let i = 0; i < formatted.warnings.length; i++) {
76
+ if (i === 5) {
77
+ console.warn(
78
+ "There were more warnings in other files. You can find a complete log in the terminal."
79
+ );
80
+ break;
81
+ }
82
+ console.warn((0, import_strip_ansi.default)(formatted.warnings[i]));
83
+ }
84
+ }
85
+ }
86
+ printWarnings();
87
+ if (isHotUpdate) {
88
+ tryApplyUpdates();
89
+ }
90
+ }
91
+ function handleErrors(errors) {
92
+ clearOutdatedErrors();
93
+ isFirstCompilation = false;
94
+ hasCompileErrors = true;
95
+ const formatted = (0, import_format.formatWebpackMessages)({
96
+ errors,
97
+ warnings: []
98
+ });
99
+ if (typeof console !== "undefined" && typeof console.error === "function") {
100
+ for (const error of formatted.errors) {
101
+ console.error((0, import_strip_ansi.default)(error));
102
+ }
103
+ }
104
+ }
105
+ function handleAvailableHash(hash) {
106
+ mostRecentCompilationHash = hash;
107
+ }
108
+ connection.onmessage = function(e) {
109
+ const message = JSON.parse(e.data);
110
+ switch (message.type) {
111
+ case "hash":
112
+ handleAvailableHash(message.data);
113
+ break;
114
+ case "still-ok":
115
+ case "ok":
116
+ handleSuccess();
117
+ break;
118
+ case "content-changed":
119
+ window.location.reload();
120
+ break;
121
+ case "warnings":
122
+ handleWarnings(message.data);
123
+ break;
124
+ case "errors":
125
+ handleErrors(message.data);
126
+ break;
127
+ default:
128
+ }
129
+ };
130
+ function isUpdateAvailable() {
131
+ return mostRecentCompilationHash !== __webpack_hash__;
132
+ }
133
+ function canApplyUpdates() {
134
+ return module.hot.status() === "idle";
135
+ }
136
+ function tryApplyUpdates() {
137
+ if (!module.hot) {
138
+ window.location.reload();
139
+ return;
140
+ }
141
+ if (!isUpdateAvailable() || !canApplyUpdates()) {
142
+ return;
143
+ }
144
+ function handleApplyUpdates(err, updatedModules) {
145
+ const wantsForcedReload = err || !updatedModules || hadRuntimeError;
146
+ if (wantsForcedReload) {
147
+ window.location.reload();
148
+ return;
149
+ }
150
+ if (isUpdateAvailable()) {
151
+ tryApplyUpdates();
152
+ }
153
+ }
154
+ const result = module.hot.check(true, handleApplyUpdates);
155
+ if (result == null ? void 0 : result.then) {
156
+ result.then(
157
+ (updatedModules) => {
158
+ handleApplyUpdates(null, updatedModules);
159
+ },
160
+ (err) => {
161
+ handleApplyUpdates(err, null);
162
+ }
163
+ );
164
+ }
165
+ }
166
+ `,
167
+ );
168
+
169
+ fs.outputFileSync(
170
+ path.join(
171
+ __dirname,
172
+ 'dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js',
173
+ ),
174
+ `var __getOwnPropNames = Object.getOwnPropertyNames;
175
+ var __commonJS = (cb, mod) => function __require() {
176
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
177
+ };
178
+ import stripAnsi from "@modern-js/utils/strip-ansi";
179
+ import { formatWebpackMessages } from "@modern-js/utils/format";
180
+ import { createSocketUrl } from "./createSocketUrl";
181
+ var require_hmr_client = __commonJS({
182
+ "src/dev-tools/dev-middleware/hmr-client/index.ts"(exports, module) {
183
+ const hadRuntimeError = false;
184
+ const socketUrl = createSocketUrl(__resourceQuery);
185
+ const connection = new WebSocket(socketUrl);
186
+ connection.onclose = function() {
187
+ if (typeof console !== "undefined" && typeof console.info === "function") {
188
+ console.info(
189
+ "The development server has disconnected. Refresh the page if necessary."
190
+ );
191
+ }
192
+ };
193
+ let isFirstCompilation = true;
194
+ let mostRecentCompilationHash = null;
195
+ let hasCompileErrors = false;
196
+ function clearOutdatedErrors() {
197
+ if (typeof console !== "undefined" && typeof console.clear === "function") {
198
+ if (hasCompileErrors) {
199
+ console.clear();
200
+ }
201
+ }
202
+ }
203
+ function handleSuccess() {
204
+ clearOutdatedErrors();
205
+ const isHotUpdate = !isFirstCompilation;
206
+ isFirstCompilation = false;
207
+ hasCompileErrors = false;
208
+ if (isHotUpdate) {
209
+ tryApplyUpdates();
210
+ }
211
+ }
212
+ function handleWarnings(warnings) {
213
+ clearOutdatedErrors();
214
+ const isHotUpdate = !isFirstCompilation;
215
+ isFirstCompilation = false;
216
+ hasCompileErrors = false;
217
+ function printWarnings() {
218
+ const formatted = formatWebpackMessages({
219
+ warnings,
220
+ errors: []
221
+ });
222
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
223
+ for (let i = 0; i < formatted.warnings.length; i++) {
224
+ if (i === 5) {
225
+ console.warn(
226
+ "There were more warnings in other files. You can find a complete log in the terminal."
227
+ );
228
+ break;
229
+ }
230
+ console.warn(stripAnsi(formatted.warnings[i]));
231
+ }
232
+ }
233
+ }
234
+ printWarnings();
235
+ if (isHotUpdate) {
236
+ tryApplyUpdates();
237
+ }
238
+ }
239
+ function handleErrors(errors) {
240
+ clearOutdatedErrors();
241
+ isFirstCompilation = false;
242
+ hasCompileErrors = true;
243
+ const formatted = formatWebpackMessages({
244
+ errors,
245
+ warnings: []
246
+ });
247
+ if (typeof console !== "undefined" && typeof console.error === "function") {
248
+ for (const error of formatted.errors) {
249
+ console.error(stripAnsi(error));
250
+ }
251
+ }
252
+ }
253
+ function handleAvailableHash(hash) {
254
+ mostRecentCompilationHash = hash;
255
+ }
256
+ connection.onmessage = function(e) {
257
+ const message = JSON.parse(e.data);
258
+ switch (message.type) {
259
+ case "hash":
260
+ handleAvailableHash(message.data);
261
+ break;
262
+ case "still-ok":
263
+ case "ok":
264
+ handleSuccess();
265
+ break;
266
+ case "content-changed":
267
+ window.location.reload();
268
+ break;
269
+ case "warnings":
270
+ handleWarnings(message.data);
271
+ break;
272
+ case "errors":
273
+ handleErrors(message.data);
274
+ break;
275
+ default:
276
+ }
277
+ };
278
+ function isUpdateAvailable() {
279
+ return mostRecentCompilationHash !== __webpack_hash__;
280
+ }
281
+ function canApplyUpdates() {
282
+ return module.hot.status() === "idle";
283
+ }
284
+ function tryApplyUpdates() {
285
+ if (!module.hot) {
286
+ window.location.reload();
287
+ return;
288
+ }
289
+ if (!isUpdateAvailable() || !canApplyUpdates()) {
290
+ return;
291
+ }
292
+ function handleApplyUpdates(err, updatedModules) {
293
+ const wantsForcedReload = err || !updatedModules || hadRuntimeError;
294
+ if (wantsForcedReload) {
295
+ window.location.reload();
296
+ return;
297
+ }
298
+ if (isUpdateAvailable()) {
299
+ tryApplyUpdates();
300
+ }
301
+ }
302
+ const result = module.hot.check(true, handleApplyUpdates);
303
+ if (result == null ? void 0 : result.then) {
304
+ result.then(
305
+ (updatedModules) => {
306
+ handleApplyUpdates(null, updatedModules);
307
+ },
308
+ (err) => {
309
+ handleApplyUpdates(err, null);
310
+ }
311
+ );
312
+ }
313
+ }
314
+ }
315
+ });
316
+ export default require_hmr_client();
317
+ `,
318
+ );
319
+
320
+ fs.outputFileSync(
321
+ path.join(
322
+ __dirname,
323
+ 'dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/index.js',
324
+ ),
325
+ `var __getOwnPropNames = Object.getOwnPropertyNames;
326
+ var __commonJS = function(cb, mod) {
327
+ return function __require() {
328
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = {
329
+ exports: {}
330
+ }).exports, mod), mod.exports;
331
+ };
332
+ };
333
+ import stripAnsi from "@modern-js/utils/strip-ansi";
334
+ import { formatWebpackMessages } from "@modern-js/utils/format";
335
+ import { createSocketUrl } from "./createSocketUrl";
336
+ var require_hmr_client = __commonJS({
337
+ "src/dev-tools/dev-middleware/hmr-client/index.ts": function(exports, module) {
338
+ var clearOutdatedErrors = function clearOutdatedErrors() {
339
+ if (typeof console !== "undefined" && typeof console.clear === "function") {
340
+ if (hasCompileErrors) {
341
+ console.clear();
342
+ }
343
+ }
344
+ };
345
+ var handleSuccess = function handleSuccess() {
346
+ clearOutdatedErrors();
347
+ var isHotUpdate = !isFirstCompilation;
348
+ isFirstCompilation = false;
349
+ hasCompileErrors = false;
350
+ if (isHotUpdate) {
351
+ tryApplyUpdates();
352
+ }
353
+ };
354
+ var handleWarnings = function handleWarnings(warnings) {
355
+ clearOutdatedErrors();
356
+ var isHotUpdate = !isFirstCompilation;
357
+ isFirstCompilation = false;
358
+ hasCompileErrors = false;
359
+ function printWarnings() {
360
+ var formatted = formatWebpackMessages({
361
+ warnings: warnings,
362
+ errors: []
363
+ });
364
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
365
+ for(var i = 0; i < formatted.warnings.length; i++){
366
+ if (i === 5) {
367
+ console.warn("There were more warnings in other files. You can find a complete log in the terminal.");
368
+ break;
369
+ }
370
+ console.warn(stripAnsi(formatted.warnings[i]));
371
+ }
372
+ }
373
+ }
374
+ printWarnings();
375
+ if (isHotUpdate) {
376
+ tryApplyUpdates();
377
+ }
378
+ };
379
+ var handleErrors = function handleErrors(errors) {
380
+ clearOutdatedErrors();
381
+ isFirstCompilation = false;
382
+ hasCompileErrors = true;
383
+ var formatted = formatWebpackMessages({
384
+ errors: errors,
385
+ warnings: []
386
+ });
387
+ if (typeof console !== "undefined" && typeof console.error === "function") {
388
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
389
+ try {
390
+ for(var _iterator = formatted.errors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
391
+ var error = _step.value;
392
+ console.error(stripAnsi(error));
393
+ }
394
+ } catch (err) {
395
+ _didIteratorError = true;
396
+ _iteratorError = err;
397
+ } finally{
398
+ try {
399
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
400
+ _iterator.return();
401
+ }
402
+ } finally{
403
+ if (_didIteratorError) {
404
+ throw _iteratorError;
405
+ }
406
+ }
407
+ }
408
+ }
409
+ };
410
+ var handleAvailableHash = function handleAvailableHash(hash) {
411
+ mostRecentCompilationHash = hash;
412
+ };
413
+ var isUpdateAvailable = function isUpdateAvailable() {
414
+ return mostRecentCompilationHash !== __webpack_hash__;
415
+ };
416
+ var canApplyUpdates = function canApplyUpdates() {
417
+ return module.hot.status() === "idle";
418
+ };
419
+ var hadRuntimeError = false;
420
+ var socketUrl = createSocketUrl(__resourceQuery);
421
+ var connection = new WebSocket(socketUrl);
422
+ connection.onclose = function() {
423
+ if (typeof console !== "undefined" && typeof console.info === "function") {
424
+ console.info("The development server has disconnected. Refresh the page if necessary.");
425
+ }
426
+ };
427
+ var isFirstCompilation = true;
428
+ var mostRecentCompilationHash = null;
429
+ var hasCompileErrors = false;
430
+ connection.onmessage = function(e) {
431
+ var message = JSON.parse(e.data);
432
+ switch(message.type){
433
+ case "hash":
434
+ handleAvailableHash(message.data);
435
+ break;
436
+ case "still-ok":
437
+ case "ok":
438
+ handleSuccess();
439
+ break;
440
+ case "content-changed":
441
+ window.location.reload();
442
+ break;
443
+ case "warnings":
444
+ handleWarnings(message.data);
445
+ break;
446
+ case "errors":
447
+ handleErrors(message.data);
448
+ break;
449
+ default:
450
+ }
451
+ };
452
+ function tryApplyUpdates() {
453
+ if (!module.hot) {
454
+ window.location.reload();
455
+ return;
456
+ }
457
+ if (!isUpdateAvailable() || !canApplyUpdates()) {
458
+ return;
459
+ }
460
+ function handleApplyUpdates(err, updatedModules) {
461
+ var wantsForcedReload = err || !updatedModules || hadRuntimeError;
462
+ if (wantsForcedReload) {
463
+ window.location.reload();
464
+ return;
465
+ }
466
+ if (isUpdateAvailable()) {
467
+ tryApplyUpdates();
468
+ }
469
+ }
470
+ var result = module.hot.check(true, handleApplyUpdates);
471
+ if (result === null || result === void 0 ? void 0 : result.then) {
472
+ result.then(function(updatedModules) {
473
+ handleApplyUpdates(null, updatedModules);
474
+ }, function(err) {
475
+ handleApplyUpdates(err, null);
476
+ });
477
+ }
478
+ }
479
+ }
480
+ });
481
+ export default require_hmr_client();
482
+ `,
483
+ );