webpack-dev-service 0.5.4 → 0.5.6

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 (40) hide show
  1. package/client/cjs/client.cjs +109 -111
  2. package/client/cjs/events.cjs +32 -33
  3. package/client/cjs/hot.cjs +60 -61
  4. package/client/cjs/index.cjs +8 -10
  5. package/client/cjs/main.cjs +60 -63
  6. package/client/cjs/ui/overlay.cjs +63 -64
  7. package/client/cjs/ui/progress.cjs +35 -35
  8. package/client/cjs/ui/utils/ansi/index.cjs +413 -439
  9. package/client/cjs/ui/utils/ansi/regx.cjs +8 -8
  10. package/client/cjs/ui/utils/ansi/utils.cjs +14 -14
  11. package/client/cjs/ui/utils/index.cjs +99 -99
  12. package/client/esm/client.js +109 -111
  13. package/client/esm/events.js +32 -33
  14. package/client/esm/hot.js +60 -61
  15. package/client/esm/index.js +8 -8
  16. package/client/esm/main.js +60 -63
  17. package/client/esm/ui/overlay.js +63 -64
  18. package/client/esm/ui/progress.js +35 -35
  19. package/client/esm/ui/utils/ansi/index.js +413 -439
  20. package/client/esm/ui/utils/ansi/regx.js +8 -8
  21. package/client/esm/ui/utils/ansi/utils.js +14 -14
  22. package/client/esm/ui/utils/index.js +99 -99
  23. package/package.json +10 -9
  24. package/server/cjs/dev.cjs +36 -30
  25. package/server/cjs/hot.cjs +177 -174
  26. package/server/cjs/index.cjs +17 -16
  27. package/server/esm/dev.js +32 -28
  28. package/server/esm/hot.js +172 -171
  29. package/server/esm/index.js +13 -14
  30. package/types/client/client.d.ts +7 -7
  31. package/types/client/events.d.ts +10 -10
  32. package/types/client/message.d.ts +25 -25
  33. package/types/client/ui/overlay.d.ts +12 -12
  34. package/types/client/ui/progress.d.ts +8 -8
  35. package/types/client/ui/utils/ansi/enum.d.ts +7 -7
  36. package/types/client/ui/utils/ansi/index.d.ts +11 -11
  37. package/types/client/ui/utils/ansi/interface.d.ts +42 -46
  38. package/types/server/dev.d.ts +4 -4
  39. package/types/server/hot.d.ts +5 -5
  40. package/types/server/index.d.ts +2 -2
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.4
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A koa 2 middleware for webpack development and hot reloading.
7
- * @see https://github.com/nuintun/webpack-dev-service#readme
8
- */
9
-
2
+ * @package webpack-dev-service
3
+ * @license MIT
4
+ * @version 0.5.6
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A koa 2 middleware for webpack development and hot reloading.
7
+ * @see https://github.com/nuintun/webpack-dev-service#readme
8
+ */
9
+
10
10
  'use strict';
11
11
 
12
12
  const events = require('./events.cjs');
@@ -18,113 +18,111 @@ const hot = require('./hot.cjs');
18
18
  * @module client
19
19
  */
20
20
  function createClient(options) {
21
- let updateTimer;
22
- const UPDATE_DELAY = 128;
23
- const RETRY_INTERVAL = 3000;
24
- const progress$1 = new progress();
25
- const overlay$1 = new overlay(options.name);
26
- const fallback = (error) => {
27
- if (options.live) {
28
- self.location.reload();
29
- }
30
- else if (error) {
31
- console.error(error);
32
- console.warn('Use fallback update but you turn off live reload, please reload by yourself.');
33
- }
34
- };
35
- const applyUpdateAsync = () => {
36
- updateTimer = self.setTimeout(() => {
37
- hot.applyUpdate(options.hmr, fallback);
38
- }, UPDATE_DELAY);
39
- };
40
- const onInvalid = () => {
41
- clearTimeout(updateTimer);
42
- if (options.progress) {
43
- progress$1.update(0);
44
- progress$1.show();
45
- }
46
- };
47
- const onProgress = ({ value }) => {
48
- if (options.progress) {
49
- progress$1.update(value);
50
- }
21
+ let updateTimer;
22
+ const UPDATE_DELAY = 128;
23
+ const RETRY_INTERVAL = 3000;
24
+ const progress$1 = new progress();
25
+ const overlay$1 = new overlay(options.name);
26
+ const fallback = error => {
27
+ if (options.live) {
28
+ self.location.reload();
29
+ } else if (error) {
30
+ console.error(error);
31
+ console.warn('Use fallback update but you turn off live reload, please reload by yourself.');
32
+ }
33
+ };
34
+ const applyUpdateAsync = () => {
35
+ updateTimer = self.setTimeout(() => {
36
+ hot.applyUpdate(options.hmr, fallback);
37
+ }, UPDATE_DELAY);
38
+ };
39
+ const onInvalid = () => {
40
+ clearTimeout(updateTimer);
41
+ if (options.progress) {
42
+ progress$1.update(0);
43
+ progress$1.show();
44
+ }
45
+ };
46
+ const onProgress = ({ value }) => {
47
+ if (options.progress) {
48
+ progress$1.update(value);
49
+ }
50
+ };
51
+ const onHash = ({ hash }) => {
52
+ hot.setHash(hash);
53
+ };
54
+ const setProblems = (type, problems) => {
55
+ const maps = {
56
+ errors: ['Error', 'error'],
57
+ warnings: ['Warning', 'warn']
51
58
  };
52
- const onHash = ({ hash }) => {
53
- hot.setHash(hash);
54
- };
55
- const setProblems = (type, problems) => {
56
- const maps = {
57
- errors: ['Error', 'error'],
58
- warnings: ['Warning', 'warn']
59
- };
60
- if (options.overlay) {
61
- overlay$1.setProblems(type, problems);
62
- }
63
- const [name, method] = maps[type];
64
- const debug = console[method];
65
- for (const { moduleName, message } of problems) {
66
- debug(`\x1b[0m${name} in ${moduleName}\r\n\x1b[0m${message}`);
67
- }
68
- };
69
- const onProblems = ({ errors, warnings }) => {
70
- progress$1.hide();
71
- setProblems('errors', errors);
72
- setProblems('warnings', warnings);
73
- if (options.overlay) {
74
- overlay$1.show();
75
- }
76
- if (errors.length <= 0) {
77
- applyUpdateAsync();
78
- }
79
- };
80
- const onSuccess = () => {
81
- overlay$1.hide();
82
- progress$1.hide();
83
- applyUpdateAsync();
84
- };
85
- const parseMessage = (message) => {
86
- try {
87
- return JSON.parse(message.data);
88
- }
89
- catch {
90
- return null;
59
+ if (options.overlay) {
60
+ overlay$1.setProblems(type, problems);
61
+ }
62
+ const [name, method] = maps[type];
63
+ const debug = console[method];
64
+ for (const { moduleName, message } of problems) {
65
+ debug(`\x1b[0m${name} in ${moduleName}\r\n\x1b[0m${message}`);
66
+ }
67
+ };
68
+ const onProblems = ({ errors, warnings }) => {
69
+ progress$1.hide();
70
+ setProblems('errors', errors);
71
+ setProblems('warnings', warnings);
72
+ if (options.overlay) {
73
+ overlay$1.show();
74
+ }
75
+ if (errors.length <= 0) {
76
+ applyUpdateAsync();
77
+ }
78
+ };
79
+ const onSuccess = () => {
80
+ overlay$1.hide();
81
+ progress$1.hide();
82
+ applyUpdateAsync();
83
+ };
84
+ const parseMessage = message => {
85
+ try {
86
+ return JSON.parse(message.data);
87
+ } catch {
88
+ return null;
89
+ }
90
+ };
91
+ const createWebSocket = url => {
92
+ const ws = new WebSocket(url);
93
+ ws.onmessage = message => {
94
+ const parsed = parseMessage(message);
95
+ if (parsed) {
96
+ const { action, payload } = parsed;
97
+ switch (action) {
98
+ case 'invalid':
99
+ onInvalid();
100
+ break;
101
+ case 'progress':
102
+ onProgress(payload);
103
+ break;
104
+ case 'hash':
105
+ onHash(payload);
106
+ break;
107
+ case 'problems':
108
+ onProblems(payload);
109
+ break;
110
+ case 'ok':
111
+ onSuccess();
112
+ break;
91
113
  }
114
+ events.emit(action, payload, options);
115
+ }
92
116
  };
93
- const createWebSocket = (url) => {
94
- const ws = new WebSocket(url);
95
- ws.onmessage = (message) => {
96
- const parsed = parseMessage(message);
97
- if (parsed) {
98
- const { action, payload } = parsed;
99
- switch (action) {
100
- case 'invalid':
101
- onInvalid();
102
- break;
103
- case 'progress':
104
- onProgress(payload);
105
- break;
106
- case 'hash':
107
- onHash(payload);
108
- break;
109
- case 'problems':
110
- onProblems(payload);
111
- break;
112
- case 'ok':
113
- onSuccess();
114
- break;
115
- }
116
- events.emit(action, payload, options);
117
- }
118
- };
119
- ws.onclose = () => {
120
- overlay$1.hide();
121
- progress$1.hide();
122
- setTimeout(() => {
123
- createWebSocket(url);
124
- }, RETRY_INTERVAL);
125
- };
117
+ ws.onclose = () => {
118
+ overlay$1.hide();
119
+ progress$1.hide();
120
+ setTimeout(() => {
121
+ createWebSocket(url);
122
+ }, RETRY_INTERVAL);
126
123
  };
127
- createWebSocket(`${options.origin}${options.path}`);
124
+ };
125
+ createWebSocket(`${options.origin}${options.path}`);
128
126
  }
129
127
 
130
128
  module.exports = createClient;
@@ -1,23 +1,23 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.4
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A koa 2 middleware for webpack development and hot reloading.
7
- * @see https://github.com/nuintun/webpack-dev-service#readme
8
- */
9
-
2
+ * @package webpack-dev-service
3
+ * @license MIT
4
+ * @version 0.5.6
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A koa 2 middleware for webpack development and hot reloading.
7
+ * @see https://github.com/nuintun/webpack-dev-service#readme
8
+ */
9
+
10
10
  'use strict';
11
11
 
12
12
  /**
13
13
  * @module events
14
14
  */
15
15
  const events = {
16
- ok: [],
17
- hash: [],
18
- invalid: [],
19
- problems: [],
20
- progress: []
16
+ ok: [],
17
+ hash: [],
18
+ invalid: [],
19
+ problems: [],
20
+ progress: []
21
21
  };
22
22
  /**
23
23
  * @function on
@@ -26,10 +26,10 @@ const events = {
26
26
  * @param callback Event listener callback.
27
27
  */
28
28
  function on(event, callback) {
29
- const callbacks = events[event];
30
- if (callbacks) {
31
- callbacks.push(callback);
32
- }
29
+ const callbacks = events[event];
30
+ if (callbacks) {
31
+ callbacks.push(callback);
32
+ }
33
33
  }
34
34
  /**
35
35
  * @function off
@@ -38,26 +38,25 @@ function on(event, callback) {
38
38
  * @param callback Event listener callback.
39
39
  */
40
40
  function off(event, callback) {
41
- const callbacks = events[event];
42
- if (callbacks) {
43
- if (callback) {
44
- const index = callbacks.indexOf(callback);
45
- if (index >= 0) {
46
- callbacks.splice(index, 1);
47
- }
48
- }
49
- else {
50
- events[event] = [];
51
- }
41
+ const callbacks = events[event];
42
+ if (callbacks) {
43
+ if (callback) {
44
+ const index = callbacks.indexOf(callback);
45
+ if (index >= 0) {
46
+ callbacks.splice(index, 1);
47
+ }
48
+ } else {
49
+ events[event] = [];
52
50
  }
51
+ }
53
52
  }
54
53
  function emit(event, message, options) {
55
- const callbacks = events[event];
56
- if (callbacks && callbacks.length > 0) {
57
- for (const callback of callbacks) {
58
- callback(message, options);
59
- }
54
+ const callbacks = events[event];
55
+ if (callbacks && callbacks.length > 0) {
56
+ for (const callback of callbacks) {
57
+ callback(message, options);
60
58
  }
59
+ }
61
60
  }
62
61
 
63
62
  exports.emit = emit;
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.4
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A koa 2 middleware for webpack development and hot reloading.
7
- * @see https://github.com/nuintun/webpack-dev-service#readme
8
- */
9
-
2
+ * @package webpack-dev-service
3
+ * @license MIT
4
+ * @version 0.5.6
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A koa 2 middleware for webpack development and hot reloading.
7
+ * @see https://github.com/nuintun/webpack-dev-service#readme
8
+ */
9
+
10
10
  'use strict';
11
11
 
12
12
  /**
@@ -23,9 +23,9 @@ let status = 'idle';
23
23
  * @description Is there a newer version of this code available.
24
24
  */
25
25
  function isUpdateAvailable() {
26
- // __webpack_hash__ is the hash of the current compilation.
27
- // It's a global variable injected by webpack.
28
- return hash !== __webpack_hash__;
26
+ // __webpack_hash__ is the hash of the current compilation.
27
+ // It's a global variable injected by webpack.
28
+ return hash !== __webpack_hash__;
29
29
  }
30
30
  /**
31
31
  * @function setHash
@@ -33,7 +33,7 @@ function isUpdateAvailable() {
33
33
  * @param value - The new hash value.
34
34
  */
35
35
  function setHash(value) {
36
- hash = value;
36
+ hash = value;
37
37
  }
38
38
  /**
39
39
  * @function setStatus
@@ -41,11 +41,11 @@ function setHash(value) {
41
41
  * @param value The new status of the hot update.
42
42
  */
43
43
  function setStatus(value) {
44
- status = value;
44
+ status = value;
45
45
  }
46
46
  // Initialize status.
47
47
  if (module.hot) {
48
- setStatus(module.hot.status());
48
+ setStatus(module.hot.status());
49
49
  }
50
50
  /**
51
51
  * @function applyUpdate
@@ -54,56 +54,55 @@ if (module.hot) {
54
54
  * @param fallback Fallback function when HMR fail.
55
55
  */
56
56
  function applyUpdate(hmr, fallback) {
57
- // Update available.
58
- if (isUpdateAvailable()) {
59
- // HMR enabled.
60
- if (hmr && module.hot) {
61
- switch (status) {
62
- case 'idle':
63
- // Set status.
64
- setStatus('check');
65
- // Auto check and apply updates.
66
- module.hot
67
- .check(true)
68
- .then(updated => {
69
- // Update status.
70
- setStatus(module.hot.status());
71
- // When updated modules is available,
72
- // it indicates server is ready to serve new bundle.
73
- if (updated) {
74
- // While update completed, do it again until no update available.
75
- applyUpdate(hmr, fallback);
76
- }
77
- })
78
- .catch((exception) => {
79
- // Get status.
80
- const status = module.hot.status();
81
- // Update status.
82
- switch (status) {
83
- case 'fail':
84
- case 'abort':
85
- setStatus(status);
86
- default:
87
- setStatus('fail');
88
- }
89
- // Cache error.
90
- error = exception;
91
- // Call fallback.
92
- fallback(error);
93
- });
94
- break;
57
+ // Update available.
58
+ if (isUpdateAvailable()) {
59
+ // HMR enabled.
60
+ if (hmr && module.hot) {
61
+ switch (status) {
62
+ case 'idle':
63
+ // Set status.
64
+ setStatus('check');
65
+ // Auto check and apply updates.
66
+ module.hot
67
+ .check(true)
68
+ .then(updated => {
69
+ // Update status.
70
+ setStatus(module.hot.status());
71
+ // When updated modules is available,
72
+ // it indicates server is ready to serve new bundle.
73
+ if (updated) {
74
+ // While update completed, do it again until no update available.
75
+ applyUpdate(hmr, fallback);
76
+ }
77
+ })
78
+ .catch(exception => {
79
+ // Get status.
80
+ const status = module.hot.status();
81
+ // Update status.
82
+ switch (status) {
95
83
  case 'fail':
96
84
  case 'abort':
97
- // Call fallback.
98
- fallback(error);
99
- break;
100
- }
101
- }
102
- else {
103
- // HMR disabled.
104
- fallback();
105
- }
85
+ setStatus(status);
86
+ default:
87
+ setStatus('fail');
88
+ }
89
+ // Cache error.
90
+ error = exception;
91
+ // Call fallback.
92
+ fallback(error);
93
+ });
94
+ break;
95
+ case 'fail':
96
+ case 'abort':
97
+ // Call fallback.
98
+ fallback(error);
99
+ break;
100
+ }
101
+ } else {
102
+ // HMR disabled.
103
+ fallback();
106
104
  }
105
+ }
107
106
  }
108
107
 
109
108
  exports.applyUpdate = applyUpdate;
@@ -1,17 +1,15 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.4
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A koa 2 middleware for webpack development and hot reloading.
7
- * @see https://github.com/nuintun/webpack-dev-service#readme
8
- */
9
-
2
+ * @package webpack-dev-service
3
+ * @license MIT
4
+ * @version 0.5.6
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A koa 2 middleware for webpack development and hot reloading.
7
+ * @see https://github.com/nuintun/webpack-dev-service#readme
8
+ */
9
+
10
10
  'use strict';
11
11
 
12
12
  const events = require('./events.cjs');
13
13
 
14
-
15
-
16
14
  exports.off = events.off;
17
15
  exports.on = events.on;
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.4
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A koa 2 middleware for webpack development and hot reloading.
7
- * @see https://github.com/nuintun/webpack-dev-service#readme
8
- */
9
-
2
+ * @package webpack-dev-service
3
+ * @license MIT
4
+ * @version 0.5.6
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A koa 2 middleware for webpack development and hot reloading.
7
+ * @see https://github.com/nuintun/webpack-dev-service#readme
8
+ */
9
+
10
10
  'use strict';
11
11
 
12
12
  const client = require('./client.cjs');
@@ -14,68 +14,65 @@ const client = require('./client.cjs');
14
14
  /**
15
15
  * @module main
16
16
  */
17
- const isTLS = (protocol) => {
18
- return protocol === 'https:';
17
+ const isTLS = protocol => {
18
+ return protocol === 'https:';
19
19
  };
20
20
  const getCurrentScript = () => {
21
- const { currentScript } = document;
22
- if (currentScript) {
23
- return currentScript;
24
- }
21
+ const { currentScript } = document;
22
+ if (currentScript) {
23
+ return currentScript;
24
+ }
25
25
  };
26
26
  const resolveProtocol = (params, protocol) => {
27
- switch (params.get('tls')) {
28
- case 'true':
29
- return 'wss:';
30
- case 'false':
31
- return 'ws:';
32
- default:
33
- return isTLS(protocol) ? 'wss:' : 'ws:';
34
- }
27
+ switch (params.get('tls')) {
28
+ case 'true':
29
+ return 'wss:';
30
+ case 'false':
31
+ return 'ws:';
32
+ default:
33
+ return isTLS(protocol) ? 'wss:' : 'ws:';
34
+ }
35
35
  };
36
- const resolveOrigin = (params) => {
37
- const { location } = self;
38
- let host = params.get('host');
39
- let protocol;
40
- if (host) {
41
- protocol = resolveProtocol(params, location.protocol);
36
+ const resolveOrigin = params => {
37
+ const { location } = self;
38
+ let host = params.get('host');
39
+ let protocol;
40
+ if (host) {
41
+ protocol = resolveProtocol(params, location.protocol);
42
+ } else {
43
+ const script = getCurrentScript();
44
+ if (script) {
45
+ const { src } = script;
46
+ const url = new URL(src, location.href);
47
+ host = url.host;
48
+ protocol = resolveProtocol(params, url.protocol);
49
+ } else {
50
+ host = location.host;
51
+ protocol = resolveProtocol(params, location.protocol);
42
52
  }
43
- else {
44
- const script = getCurrentScript();
45
- if (script) {
46
- const { src } = script;
47
- const url = new URL(src, location.href);
48
- host = url.host;
49
- protocol = resolveProtocol(params, url.protocol);
50
- }
51
- else {
52
- host = location.host;
53
- protocol = resolveProtocol(params, location.protocol);
54
- }
55
- }
56
- return `${protocol}//${host}`;
53
+ }
54
+ return `${protocol}//${host}`;
57
55
  };
58
56
  const resolveOptions = () => {
59
- const params = new URLSearchParams(__resourceQuery);
60
- const origin = resolveOrigin(params);
61
- const hmr = params.get('hmr') !== 'false';
62
- const live = params.get('live') !== 'false';
63
- const overlay = params.get('overlay') !== 'false';
64
- const progress = params.get('progress') !== 'false';
65
- try {
66
- const options = __WDS_HOT_OPTIONS__;
67
- return {
68
- live,
69
- origin,
70
- overlay,
71
- name: options.name,
72
- path: options.path,
73
- hmr: options.hmr === false ? false : hmr,
74
- progress: options.progress === false ? false : progress
75
- };
76
- }
77
- catch {
78
- throw new Error('Imported the hot client but the hot server is not enabled.');
79
- }
57
+ const params = new URLSearchParams(__resourceQuery);
58
+ const origin = resolveOrigin(params);
59
+ const hmr = params.get('hmr') !== 'false';
60
+ const live = params.get('live') !== 'false';
61
+ const overlay = params.get('overlay') !== 'false';
62
+ const progress = params.get('progress') !== 'false';
63
+ try {
64
+ const options = __WDS_HOT_OPTIONS__;
65
+ return {
66
+ live,
67
+ origin,
68
+ overlay,
69
+ name: options.name,
70
+ path: options.path,
71
+ hmr: options.hmr === false ? false : hmr,
72
+ progress: options.progress === false ? false : progress
73
+ };
74
+ } catch {
75
+ throw new Error('Imported the hot client but the hot server is not enabled.');
76
+ }
80
77
  };
81
78
  client(resolveOptions());