webpack-dev-service 0.5.5 → 0.5.7

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/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 +414 -440
  9. package/client/cjs/ui/utils/ansi/{regx.cjs → regexp.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 +414 -440
  20. package/client/esm/ui/utils/ansi/{regx.js → regexp.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 +6 -6
  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
  41. /package/types/client/ui/utils/ansi/{regx.d.ts → regexp.d.ts} +0 -0
@@ -1,213 +1,216 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.5
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.7
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 WebSocket = require('ws');
13
13
  const webpack = require('webpack');
14
14
 
15
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
+ function _interopDefault(e) {
16
+ return e && e.__esModule ? e : { default: e };
17
+ }
16
18
 
17
- const WebSocket__default = /*#__PURE__*/_interopDefault(WebSocket);
18
- const webpack__default = /*#__PURE__*/_interopDefault(webpack);
19
+ const WebSocket__default = /*#__PURE__*/ _interopDefault(WebSocket);
20
+ const webpack__default = /*#__PURE__*/ _interopDefault(webpack);
19
21
 
20
22
  /**
21
23
  * @module hot
22
24
  */
23
25
  const WEBSOCKET_RE = /^websocket$/i;
24
26
  function isObject(value) {
25
- return Object.prototype.toString.call(value) === '[object Object]';
27
+ return Object.prototype.toString.call(value) === '[object Object]';
26
28
  }
27
29
  function resolveStatsOptions(compiler) {
28
- const options = {
29
- all: false,
30
- hash: true,
31
- colors: true,
32
- errors: true,
33
- assets: false,
34
- builtAt: true,
35
- warnings: true,
36
- errorDetails: false
37
- };
38
- const { stats } = compiler.options;
39
- if (isObject(stats)) {
40
- const { warningsFilter } = stats;
41
- if (warningsFilter !== undefined) {
42
- options.warningsFilter = warningsFilter;
43
- }
30
+ const options = {
31
+ all: false,
32
+ hash: true,
33
+ colors: true,
34
+ errors: true,
35
+ assets: false,
36
+ builtAt: true,
37
+ warnings: true,
38
+ errorDetails: false
39
+ };
40
+ const { stats } = compiler.options;
41
+ if (isObject(stats)) {
42
+ const { warningsFilter } = stats;
43
+ if (warningsFilter !== undefined) {
44
+ options.warningsFilter = warningsFilter;
44
45
  }
45
- return options;
46
+ }
47
+ return options;
46
48
  }
47
49
  function normalize(path) {
48
- const segments = [];
49
- const parts = path.split(/[\\/]+/);
50
- for (const segment of parts) {
51
- switch (segment) {
52
- case '.':
53
- break;
54
- case '..':
55
- segments.pop();
56
- break;
57
- default:
58
- segments.push(segment);
59
- break;
60
- }
50
+ const segments = [];
51
+ const parts = path.split(/[\\/]+/);
52
+ for (const segment of parts) {
53
+ switch (segment) {
54
+ case '.':
55
+ break;
56
+ case '..':
57
+ segments.pop();
58
+ break;
59
+ default:
60
+ segments.push(segment);
61
+ break;
61
62
  }
62
- const pathname = segments.join('/');
63
- return pathname.startsWith('/') ? pathname : `/${pathname}`;
63
+ }
64
+ const pathname = segments.join('/');
65
+ return pathname.startsWith('/') ? pathname : `/${pathname}`;
64
66
  }
65
67
  function resolveOptions(options) {
66
- const settings = {
67
- hmr: true,
68
- path: '/hot',
69
- progress: true,
70
- ...options
71
- };
72
- settings.path = normalize(settings.path);
73
- return settings;
68
+ const settings = {
69
+ hmr: true,
70
+ path: '/hot',
71
+ progress: true,
72
+ ...options
73
+ };
74
+ settings.path = normalize(settings.path);
75
+ return settings;
74
76
  }
75
77
  function isUpgradable(context, detector) {
76
- const { upgrade } = context.headers;
77
- return !!upgrade && detector.test(upgrade.trim());
78
+ const { upgrade } = context.headers;
79
+ return !!upgrade && detector.test(upgrade.trim());
78
80
  }
79
81
  function hasProblems(problems) {
80
- return !!problems && problems.length > 0;
82
+ return !!problems && problems.length > 0;
81
83
  }
82
84
  class HotServer {
83
- stats;
84
- compiler;
85
- server;
86
- options;
87
- name = 'webpack-hot-middleware';
88
- logger;
89
- constructor(compiler, options) {
90
- this.compiler = compiler;
91
- this.options = resolveOptions(options);
92
- this.logger = compiler.getInfrastructureLogger(this.name);
93
- this.server = new WebSocket.WebSocketServer({ path: this.options.path, noServer: true });
94
- this.setupWss();
95
- this.setupHooks();
96
- this.setupPlugins();
85
+ stats;
86
+ compiler;
87
+ server;
88
+ options;
89
+ name = 'webpack-hot-middleware';
90
+ logger;
91
+ constructor(compiler, options) {
92
+ this.compiler = compiler;
93
+ this.options = resolveOptions(options);
94
+ this.logger = compiler.getInfrastructureLogger(this.name);
95
+ this.server = new WebSocket.WebSocketServer({ path: this.options.path, noServer: true });
96
+ this.setupWss();
97
+ this.setupHooks();
98
+ this.setupPlugins();
99
+ }
100
+ setupWss() {
101
+ const { server, logger } = this;
102
+ server.on('error', error => {
103
+ logger.error(error.message);
104
+ });
105
+ server.on('connection', client => {
106
+ if (this.stats) {
107
+ this.broadcastStats([client], this.stats);
108
+ }
109
+ });
110
+ }
111
+ setupHooks() {
112
+ const { compiler } = this;
113
+ const { hooks } = compiler;
114
+ const statsOptions = resolveStatsOptions(compiler);
115
+ hooks.done.tapAsync(this.name, (stats, next) => {
116
+ next();
117
+ this.stats = stats.toJson(statsOptions);
118
+ this.broadcastStats(this.clients(), this.stats);
119
+ });
120
+ hooks.invalid.tap(this.name, (path, builtAt) => {
121
+ this.broadcast(this.clients(), 'invalid', { path, builtAt });
122
+ });
123
+ }
124
+ setupPlugins() {
125
+ const { options, compiler } = this;
126
+ const plugins = [
127
+ new webpack__default.default.NoEmitOnErrorsPlugin(),
128
+ new webpack__default.default.DefinePlugin({
129
+ __WDS_HOT_OPTIONS__: JSON.stringify({
130
+ ...options,
131
+ name: compiler.name
132
+ })
133
+ })
134
+ ];
135
+ if (options.hmr) {
136
+ plugins.push(new webpack__default.default.HotModuleReplacementPlugin());
97
137
  }
98
- setupWss() {
99
- const { server, logger } = this;
100
- server.on('error', error => {
101
- logger.error(error.message);
102
- });
103
- server.on('connection', client => {
104
- if (this.stats) {
105
- this.broadcastStats([client], this.stats);
138
+ if (options.progress) {
139
+ let value = 0;
140
+ plugins.push(
141
+ new webpack__default.default.ProgressPlugin((percentage, status, message) => {
142
+ const nextValue = Math.floor(percentage * 100);
143
+ if (nextValue > value || nextValue === 0) {
144
+ value = nextValue;
145
+ switch (value) {
146
+ case 0:
147
+ status = 'start';
148
+ message = 'end idle';
149
+ break;
150
+ case 100:
151
+ status = 'finish';
152
+ message = 'begin idle';
153
+ break;
106
154
  }
107
- });
108
- }
109
- setupHooks() {
110
- const { compiler } = this;
111
- const { hooks } = compiler;
112
- const statsOptions = resolveStatsOptions(compiler);
113
- hooks.done.tapAsync(this.name, (stats, next) => {
114
- next();
115
- this.stats = stats.toJson(statsOptions);
116
- this.broadcastStats(this.clients(), this.stats);
117
- });
118
- hooks.invalid.tap(this.name, (path, builtAt) => {
119
- this.broadcast(this.clients(), 'invalid', { path, builtAt });
120
- });
155
+ this.broadcast(this.clients(), 'progress', { status, message, value });
156
+ }
157
+ })
158
+ );
121
159
  }
122
- setupPlugins() {
123
- const { options, compiler } = this;
124
- const plugins = [
125
- new webpack__default.default.NoEmitOnErrorsPlugin(),
126
- new webpack__default.default.DefinePlugin({
127
- __WDS_HOT_OPTIONS__: JSON.stringify({
128
- ...options,
129
- name: compiler.name
130
- })
131
- })
132
- ];
133
- if (options.hmr) {
134
- plugins.push(new webpack__default.default.HotModuleReplacementPlugin());
135
- }
136
- if (options.progress) {
137
- let value = 0;
138
- plugins.push(new webpack__default.default.ProgressPlugin((percentage, status, message) => {
139
- const nextValue = Math.floor(percentage * 100);
140
- if (nextValue > value || nextValue === 0) {
141
- value = nextValue;
142
- switch (value) {
143
- case 0:
144
- status = 'start';
145
- message = 'end idle';
146
- break;
147
- case 100:
148
- status = 'finish';
149
- message = 'begin idle';
150
- break;
151
- }
152
- this.broadcast(this.clients(), 'progress', { status, message, value });
153
- }
154
- }));
155
- }
156
- for (const plugin of plugins) {
157
- plugin.apply(compiler);
158
- }
160
+ for (const plugin of plugins) {
161
+ plugin.apply(compiler);
159
162
  }
160
- clients() {
161
- return this.server.clients;
163
+ }
164
+ clients() {
165
+ return this.server.clients;
166
+ }
167
+ upgrade(context) {
168
+ const { server } = this;
169
+ const { req: request } = context;
170
+ if (isUpgradable(context, WEBSOCKET_RE) && server.shouldHandle(request)) {
171
+ context.respond = false;
172
+ const { socket } = context;
173
+ const head = Buffer.alloc(0);
174
+ server.handleUpgrade(request, socket, head, client => {
175
+ server.emit('connection', client, request);
176
+ });
177
+ return true;
162
178
  }
163
- upgrade(context) {
164
- const { server } = this;
165
- const { req: request } = context;
166
- if (isUpgradable(context, WEBSOCKET_RE) && server.shouldHandle(request)) {
167
- context.respond = false;
168
- const { socket } = context;
169
- const head = Buffer.alloc(0);
170
- server.handleUpgrade(request, socket, head, client => {
171
- server.emit('connection', client, request);
172
- });
173
- return true;
174
- }
175
- return false;
179
+ return false;
180
+ }
181
+ broadcast(clients, action, payload) {
182
+ for (const client of clients) {
183
+ if (client.readyState === WebSocket__default.default.OPEN) {
184
+ client.send(JSON.stringify({ action, payload }));
185
+ }
176
186
  }
177
- broadcast(clients, action, payload) {
178
- for (const client of clients) {
179
- if (client.readyState === WebSocket__default.default.OPEN) {
180
- client.send(JSON.stringify({ action, payload }));
181
- }
182
- }
183
- }
184
- broadcastStats(clients, stats) {
185
- if (clients.size > 0 || clients.length > 0) {
186
- const { hash, builtAt, errors, warnings } = stats;
187
- this.broadcast(clients, 'hash', { hash });
188
- if (hasProblems(errors) || hasProblems(warnings)) {
189
- this.broadcast(clients, 'problems', { errors, warnings, builtAt });
190
- }
191
- else {
192
- this.broadcast(clients, 'ok', { builtAt });
193
- }
194
- }
187
+ }
188
+ broadcastStats(clients, stats) {
189
+ if (clients.size > 0 || clients.length > 0) {
190
+ const { hash, builtAt, errors, warnings } = stats;
191
+ this.broadcast(clients, 'hash', { hash });
192
+ if (hasProblems(errors) || hasProblems(warnings)) {
193
+ this.broadcast(clients, 'problems', { errors, warnings, builtAt });
194
+ } else {
195
+ this.broadcast(clients, 'ok', { builtAt });
196
+ }
195
197
  }
198
+ }
196
199
  }
197
200
  function hot(compiler, options = {}) {
198
- const server = new HotServer(compiler, options);
199
- const hotMiddleware = async (context, next) => {
200
- if (!server.upgrade(context)) {
201
- await next();
202
- }
203
- };
204
- hotMiddleware.clients = () => {
205
- return server.clients();
206
- };
207
- hotMiddleware.broadcast = (clients, action, payload) => {
208
- server.broadcast(clients, action, payload);
209
- };
210
- return hotMiddleware;
201
+ const server = new HotServer(compiler, options);
202
+ const hotMiddleware = async (context, next) => {
203
+ if (!server.upgrade(context)) {
204
+ await next();
205
+ }
206
+ };
207
+ hotMiddleware.clients = () => {
208
+ return server.clients();
209
+ };
210
+ hotMiddleware.broadcast = (clients, action, payload) => {
211
+ server.broadcast(clients, action, payload);
212
+ };
213
+ return hotMiddleware;
211
214
  }
212
215
 
213
216
  module.exports = hot;
@@ -1,32 +1,33 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.5
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.7
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 compose = require('koa-compose');
13
13
  const dev = require('./dev.cjs');
14
14
  const hot = require('./hot.cjs');
15
15
 
16
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
16
+ function _interopDefault(e) {
17
+ return e && e.__esModule ? e : { default: e };
18
+ }
17
19
 
18
- const compose__default = /*#__PURE__*/_interopDefault(compose);
20
+ const compose__default = /*#__PURE__*/ _interopDefault(compose);
19
21
 
20
22
  /**
21
23
  * @module index
22
24
  */
23
25
  function server(compiler, options = {}) {
24
- const { hot: hotOptions, ...devOptions } = options;
25
- const devMiddleware = dev(compiler, devOptions);
26
- if (hotOptions === false)
27
- return devMiddleware;
28
- const hotMiddleware = hot(compiler, hotOptions);
29
- return Object.assign(compose__default.default([devMiddleware, hotMiddleware]), devMiddleware, hotMiddleware);
26
+ const { hot: hotOptions, ...devOptions } = options;
27
+ const devMiddleware = dev(compiler, devOptions);
28
+ if (hotOptions === false) return devMiddleware;
29
+ const hotMiddleware = hot(compiler, hotOptions);
30
+ return Object.assign(compose__default.default([devMiddleware, hotMiddleware]), devMiddleware, hotMiddleware);
30
31
  }
31
32
 
32
33
  module.exports = server;
package/server/esm/dev.js CHANGED
@@ -1,38 +1,42 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.5
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.7
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
  import webpackDevMiddleware from 'webpack-dev-middleware';
11
11
 
12
12
  /**
13
13
  * @module dev
14
14
  */
15
15
  function dev(compiler, options) {
16
- const middleware = webpackDevMiddleware(compiler, options);
17
- const devMiddleware = async (context, next) => {
18
- context.remove('Content-Type');
19
- await middleware(context.req, {
20
- locals: context.state,
21
- send(body) {
22
- context.body = body;
23
- },
24
- status(statusCode) {
25
- context.status = statusCode;
26
- },
27
- set(field, value) {
28
- context.response.set(field, value);
29
- },
30
- get(field) {
31
- return context.response.get(field);
32
- }
33
- }, next);
34
- };
35
- return Object.assign(devMiddleware, middleware);
16
+ const middleware = webpackDevMiddleware(compiler, options);
17
+ const devMiddleware = async (context, next) => {
18
+ context.remove('Content-Type');
19
+ await middleware(
20
+ context.req,
21
+ {
22
+ locals: context.state,
23
+ send(body) {
24
+ context.body = body;
25
+ },
26
+ status(statusCode) {
27
+ context.status = statusCode;
28
+ },
29
+ set(field, value) {
30
+ context.response.set(field, value);
31
+ },
32
+ get(field) {
33
+ return context.response.get(field);
34
+ }
35
+ },
36
+ next
37
+ );
38
+ };
39
+ return Object.assign(devMiddleware, middleware);
36
40
  }
37
41
 
38
42
  export { dev as default };