webpack-dev-service 0.5.1 → 0.5.3

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 (41) hide show
  1. package/README.md +5 -2
  2. package/client/cjs/client.cjs +111 -109
  3. package/client/cjs/events.cjs +33 -32
  4. package/client/cjs/hot.cjs +61 -60
  5. package/client/cjs/index.cjs +10 -8
  6. package/client/cjs/main.cjs +63 -60
  7. package/client/cjs/ui/overlay.cjs +64 -63
  8. package/client/cjs/ui/progress.cjs +35 -35
  9. package/client/cjs/ui/utils/ansi/index.cjs +439 -413
  10. package/client/cjs/ui/utils/ansi/regx.cjs +8 -8
  11. package/client/cjs/ui/utils/ansi/utils.cjs +14 -14
  12. package/client/cjs/ui/utils/index.cjs +99 -99
  13. package/client/esm/client.js +111 -109
  14. package/client/esm/events.js +33 -32
  15. package/client/esm/hot.js +61 -60
  16. package/client/esm/index.js +8 -8
  17. package/client/esm/main.js +63 -60
  18. package/client/esm/ui/overlay.js +64 -63
  19. package/client/esm/ui/progress.js +35 -35
  20. package/client/esm/ui/utils/ansi/index.js +439 -413
  21. package/client/esm/ui/utils/ansi/regx.js +8 -8
  22. package/client/esm/ui/utils/ansi/utils.js +14 -14
  23. package/client/esm/ui/utils/index.js +99 -99
  24. package/package.json +15 -19
  25. package/server/cjs/dev.cjs +30 -36
  26. package/server/cjs/hot.cjs +174 -177
  27. package/server/cjs/index.cjs +16 -17
  28. package/server/esm/dev.js +28 -32
  29. package/server/esm/hot.js +171 -172
  30. package/server/esm/index.js +14 -13
  31. package/types/client/client.d.ts +7 -7
  32. package/types/client/events.d.ts +10 -10
  33. package/types/client/message.d.ts +25 -25
  34. package/types/client/ui/overlay.d.ts +12 -12
  35. package/types/client/ui/progress.d.ts +8 -8
  36. package/types/client/ui/utils/ansi/enum.d.ts +7 -7
  37. package/types/client/ui/utils/ansi/index.d.ts +11 -11
  38. package/types/client/ui/utils/ansi/interface.d.ts +46 -42
  39. package/types/server/dev.d.ts +4 -4
  40. package/types/server/hot.d.ts +5 -5
  41. package/types/server/index.d.ts +2 -2
package/README.md CHANGED
@@ -100,8 +100,9 @@ const compiler = webpack({
100
100
  {
101
101
  loader: 'swc-loader',
102
102
  options: {
103
- swcrc: false,
104
103
  jsc: {
104
+ target: 'es2015',
105
+ externalHelpers: true,
105
106
  parser: {
106
107
  tsx: true,
107
108
  syntax: 'typescript'
@@ -111,7 +112,9 @@ const compiler = webpack({
111
112
  runtime: 'automatic'
112
113
  }
113
114
  },
114
- externalHelpers: true
115
+ experimental: {
116
+ cacheRoot: path.resolve('node_modules/.cache/swc')
117
+ }
115
118
  },
116
119
  env: {
117
120
  targets: ['defaults', 'not IE >= 0']
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.1
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.3
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,111 +18,113 @@ 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
- } 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']
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
+ }
58
51
  };
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;
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;
113
91
  }
114
- events.emit(action, payload, options);
115
- }
116
92
  };
117
- ws.onclose = () => {
118
- overlay$1.hide();
119
- progress$1.hide();
120
- setTimeout(() => {
121
- createWebSocket(url);
122
- }, RETRY_INTERVAL);
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
+ };
123
126
  };
124
- };
125
- createWebSocket(`${options.origin}${options.path}`);
127
+ createWebSocket(`${options.origin}${options.path}`);
126
128
  }
127
129
 
128
130
  module.exports = createClient;
@@ -1,23 +1,23 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.1
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.3
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,25 +38,26 @@ 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
- } else {
49
- events[event] = [];
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
+ }
50
52
  }
51
- }
52
53
  }
53
54
  function emit(event, message, options) {
54
- const callbacks = events[event];
55
- if (callbacks && callbacks.length > 0) {
56
- for (const callback of callbacks) {
57
- callback(message, options);
55
+ const callbacks = events[event];
56
+ if (callbacks && callbacks.length > 0) {
57
+ for (const callback of callbacks) {
58
+ callback(message, options);
59
+ }
58
60
  }
59
- }
60
61
  }
61
62
 
62
63
  exports.emit = emit;
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.1
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.3
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,55 +54,56 @@ 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) {
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;
83
95
  case 'fail':
84
96
  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;
95
- case 'fail':
96
- case 'abort':
97
- // Call fallback.
98
- fallback(error);
99
- break;
100
- }
101
- } else {
102
- // HMR disabled.
103
- fallback();
97
+ // Call fallback.
98
+ fallback(error);
99
+ break;
100
+ }
101
+ }
102
+ else {
103
+ // HMR disabled.
104
+ fallback();
105
+ }
104
106
  }
105
- }
106
107
  }
107
108
 
108
109
  exports.applyUpdate = applyUpdate;
@@ -1,15 +1,17 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.1
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.3
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
+
14
16
  exports.off = events.off;
15
17
  exports.on = events.on;