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
package/server/esm/hot.js CHANGED
@@ -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
  import WebSocket, { WebSocketServer } from 'ws';
11
11
  import webpack from 'webpack';
12
12
 
@@ -15,192 +15,193 @@ import webpack from 'webpack';
15
15
  */
16
16
  const WEBSOCKET_RE = /^websocket$/i;
17
17
  function isObject(value) {
18
- return Object.prototype.toString.call(value) === '[object Object]';
18
+ return Object.prototype.toString.call(value) === '[object Object]';
19
19
  }
20
20
  function resolveStatsOptions(compiler) {
21
- const options = {
22
- all: false,
23
- hash: true,
24
- colors: true,
25
- errors: true,
26
- assets: false,
27
- builtAt: true,
28
- warnings: true,
29
- errorDetails: false
30
- };
31
- const { stats } = compiler.options;
32
- if (isObject(stats)) {
33
- const { warningsFilter } = stats;
34
- if (warningsFilter !== undefined) {
35
- options.warningsFilter = warningsFilter;
36
- }
21
+ const options = {
22
+ all: false,
23
+ hash: true,
24
+ colors: true,
25
+ errors: true,
26
+ assets: false,
27
+ builtAt: true,
28
+ warnings: true,
29
+ errorDetails: false
30
+ };
31
+ const { stats } = compiler.options;
32
+ if (isObject(stats)) {
33
+ const { warningsFilter } = stats;
34
+ if (warningsFilter !== undefined) {
35
+ options.warningsFilter = warningsFilter;
37
36
  }
38
- return options;
37
+ }
38
+ return options;
39
39
  }
40
40
  function normalize(path) {
41
- const segments = [];
42
- const parts = path.split(/[\\/]+/);
43
- for (const segment of parts) {
44
- switch (segment) {
45
- case '.':
46
- break;
47
- case '..':
48
- segments.pop();
49
- break;
50
- default:
51
- segments.push(segment);
52
- break;
53
- }
41
+ const segments = [];
42
+ const parts = path.split(/[\\/]+/);
43
+ for (const segment of parts) {
44
+ switch (segment) {
45
+ case '.':
46
+ break;
47
+ case '..':
48
+ segments.pop();
49
+ break;
50
+ default:
51
+ segments.push(segment);
52
+ break;
54
53
  }
55
- const pathname = segments.join('/');
56
- return pathname.startsWith('/') ? pathname : `/${pathname}`;
54
+ }
55
+ const pathname = segments.join('/');
56
+ return pathname.startsWith('/') ? pathname : `/${pathname}`;
57
57
  }
58
58
  function resolveOptions(options) {
59
- const settings = {
60
- hmr: true,
61
- path: '/hot',
62
- progress: true,
63
- ...options
64
- };
65
- settings.path = normalize(settings.path);
66
- return settings;
59
+ const settings = {
60
+ hmr: true,
61
+ path: '/hot',
62
+ progress: true,
63
+ ...options
64
+ };
65
+ settings.path = normalize(settings.path);
66
+ return settings;
67
67
  }
68
68
  function isUpgradable(context, detector) {
69
- const { upgrade } = context.headers;
70
- return !!upgrade && detector.test(upgrade.trim());
69
+ const { upgrade } = context.headers;
70
+ return !!upgrade && detector.test(upgrade.trim());
71
71
  }
72
72
  function hasProblems(problems) {
73
- return !!problems && problems.length > 0;
73
+ return !!problems && problems.length > 0;
74
74
  }
75
75
  class HotServer {
76
- stats;
77
- compiler;
78
- server;
79
- options;
80
- name = 'webpack-hot-middleware';
81
- logger;
82
- constructor(compiler, options) {
83
- this.compiler = compiler;
84
- this.options = resolveOptions(options);
85
- this.logger = compiler.getInfrastructureLogger(this.name);
86
- this.server = new WebSocketServer({ path: this.options.path, noServer: true });
87
- this.setupWss();
88
- this.setupHooks();
89
- this.setupPlugins();
76
+ stats;
77
+ compiler;
78
+ server;
79
+ options;
80
+ name = 'webpack-hot-middleware';
81
+ logger;
82
+ constructor(compiler, options) {
83
+ this.compiler = compiler;
84
+ this.options = resolveOptions(options);
85
+ this.logger = compiler.getInfrastructureLogger(this.name);
86
+ this.server = new WebSocketServer({ path: this.options.path, noServer: true });
87
+ this.setupWss();
88
+ this.setupHooks();
89
+ this.setupPlugins();
90
+ }
91
+ setupWss() {
92
+ const { server, logger } = this;
93
+ server.on('error', error => {
94
+ logger.error(error.message);
95
+ });
96
+ server.on('connection', client => {
97
+ if (this.stats) {
98
+ this.broadcastStats([client], this.stats);
99
+ }
100
+ });
101
+ }
102
+ setupHooks() {
103
+ const { compiler } = this;
104
+ const { hooks } = compiler;
105
+ const statsOptions = resolveStatsOptions(compiler);
106
+ hooks.done.tapAsync(this.name, (stats, next) => {
107
+ next();
108
+ this.stats = stats.toJson(statsOptions);
109
+ this.broadcastStats(this.clients(), this.stats);
110
+ });
111
+ hooks.invalid.tap(this.name, (path, builtAt) => {
112
+ this.broadcast(this.clients(), 'invalid', { path, builtAt });
113
+ });
114
+ }
115
+ setupPlugins() {
116
+ const { options, compiler } = this;
117
+ const plugins = [
118
+ new webpack.NoEmitOnErrorsPlugin(),
119
+ new webpack.DefinePlugin({
120
+ __WDS_HOT_OPTIONS__: JSON.stringify({
121
+ ...options,
122
+ name: compiler.name
123
+ })
124
+ })
125
+ ];
126
+ if (options.hmr) {
127
+ plugins.push(new webpack.HotModuleReplacementPlugin());
90
128
  }
91
- setupWss() {
92
- const { server, logger } = this;
93
- server.on('error', error => {
94
- logger.error(error.message);
95
- });
96
- server.on('connection', client => {
97
- if (this.stats) {
98
- this.broadcastStats([client], this.stats);
129
+ if (options.progress) {
130
+ let value = 0;
131
+ plugins.push(
132
+ new webpack.ProgressPlugin((percentage, status, message) => {
133
+ const nextValue = Math.floor(percentage * 100);
134
+ if (nextValue > value || nextValue === 0) {
135
+ value = nextValue;
136
+ switch (value) {
137
+ case 0:
138
+ status = 'start';
139
+ message = 'end idle';
140
+ break;
141
+ case 100:
142
+ status = 'finish';
143
+ message = 'begin idle';
144
+ break;
99
145
  }
100
- });
146
+ this.broadcast(this.clients(), 'progress', { status, message, value });
147
+ }
148
+ })
149
+ );
101
150
  }
102
- setupHooks() {
103
- const { compiler } = this;
104
- const { hooks } = compiler;
105
- const statsOptions = resolveStatsOptions(compiler);
106
- hooks.done.tapAsync(this.name, (stats, next) => {
107
- next();
108
- this.stats = stats.toJson(statsOptions);
109
- this.broadcastStats(this.clients(), this.stats);
110
- });
111
- hooks.invalid.tap(this.name, (path, builtAt) => {
112
- this.broadcast(this.clients(), 'invalid', { path, builtAt });
113
- });
151
+ for (const plugin of plugins) {
152
+ plugin.apply(compiler);
114
153
  }
115
- setupPlugins() {
116
- const { options, compiler } = this;
117
- const plugins = [
118
- new webpack.NoEmitOnErrorsPlugin(),
119
- new webpack.DefinePlugin({
120
- __WDS_HOT_OPTIONS__: JSON.stringify({
121
- ...options,
122
- name: compiler.name
123
- })
124
- })
125
- ];
126
- if (options.hmr) {
127
- plugins.push(new webpack.HotModuleReplacementPlugin());
128
- }
129
- if (options.progress) {
130
- let value = 0;
131
- plugins.push(new webpack.ProgressPlugin((percentage, status, message) => {
132
- const nextValue = Math.floor(percentage * 100);
133
- if (nextValue > value || nextValue === 0) {
134
- value = nextValue;
135
- switch (value) {
136
- case 0:
137
- status = 'start';
138
- message = 'end idle';
139
- break;
140
- case 100:
141
- status = 'finish';
142
- message = 'begin idle';
143
- break;
144
- }
145
- this.broadcast(this.clients(), 'progress', { status, message, value });
146
- }
147
- }));
148
- }
149
- for (const plugin of plugins) {
150
- plugin.apply(compiler);
151
- }
154
+ }
155
+ clients() {
156
+ return this.server.clients;
157
+ }
158
+ upgrade(context) {
159
+ const { server } = this;
160
+ const { req: request } = context;
161
+ if (isUpgradable(context, WEBSOCKET_RE) && server.shouldHandle(request)) {
162
+ context.respond = false;
163
+ const { socket } = context;
164
+ const head = Buffer.alloc(0);
165
+ server.handleUpgrade(request, socket, head, client => {
166
+ server.emit('connection', client, request);
167
+ });
168
+ return true;
152
169
  }
153
- clients() {
154
- return this.server.clients;
170
+ return false;
171
+ }
172
+ broadcast(clients, action, payload) {
173
+ for (const client of clients) {
174
+ if (client.readyState === WebSocket.OPEN) {
175
+ client.send(JSON.stringify({ action, payload }));
176
+ }
155
177
  }
156
- upgrade(context) {
157
- const { server } = this;
158
- const { req: request } = context;
159
- if (isUpgradable(context, WEBSOCKET_RE) && server.shouldHandle(request)) {
160
- context.respond = false;
161
- const { socket } = context;
162
- const head = Buffer.alloc(0);
163
- server.handleUpgrade(request, socket, head, client => {
164
- server.emit('connection', client, request);
165
- });
166
- return true;
167
- }
168
- return false;
169
- }
170
- broadcast(clients, action, payload) {
171
- for (const client of clients) {
172
- if (client.readyState === WebSocket.OPEN) {
173
- client.send(JSON.stringify({ action, payload }));
174
- }
175
- }
176
- }
177
- broadcastStats(clients, stats) {
178
- if (clients.size > 0 || clients.length > 0) {
179
- const { hash, builtAt, errors, warnings } = stats;
180
- this.broadcast(clients, 'hash', { hash });
181
- if (hasProblems(errors) || hasProblems(warnings)) {
182
- this.broadcast(clients, 'problems', { errors, warnings, builtAt });
183
- }
184
- else {
185
- this.broadcast(clients, 'ok', { builtAt });
186
- }
187
- }
178
+ }
179
+ broadcastStats(clients, stats) {
180
+ if (clients.size > 0 || clients.length > 0) {
181
+ const { hash, builtAt, errors, warnings } = stats;
182
+ this.broadcast(clients, 'hash', { hash });
183
+ if (hasProblems(errors) || hasProblems(warnings)) {
184
+ this.broadcast(clients, 'problems', { errors, warnings, builtAt });
185
+ } else {
186
+ this.broadcast(clients, 'ok', { builtAt });
187
+ }
188
188
  }
189
+ }
189
190
  }
190
191
  function hot(compiler, options = {}) {
191
- const server = new HotServer(compiler, options);
192
- const hotMiddleware = async (context, next) => {
193
- if (!server.upgrade(context)) {
194
- await next();
195
- }
196
- };
197
- hotMiddleware.clients = () => {
198
- return server.clients();
199
- };
200
- hotMiddleware.broadcast = (clients, action, payload) => {
201
- server.broadcast(clients, action, payload);
202
- };
203
- return hotMiddleware;
192
+ const server = new HotServer(compiler, options);
193
+ const hotMiddleware = async (context, next) => {
194
+ if (!server.upgrade(context)) {
195
+ await next();
196
+ }
197
+ };
198
+ hotMiddleware.clients = () => {
199
+ return server.clients();
200
+ };
201
+ hotMiddleware.broadcast = (clients, action, payload) => {
202
+ server.broadcast(clients, action, payload);
203
+ };
204
+ return hotMiddleware;
204
205
  }
205
206
 
206
207
  export { hot as default };
@@ -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
  import compose from 'koa-compose';
11
11
  import dev from './dev.js';
12
12
  import hot from './hot.js';
@@ -15,12 +15,11 @@ import hot from './hot.js';
15
15
  * @module index
16
16
  */
17
17
  function server(compiler, options = {}) {
18
- const { hot: hotOptions, ...devOptions } = options;
19
- const devMiddleware = dev(compiler, devOptions);
20
- if (hotOptions === false)
21
- return devMiddleware;
22
- const hotMiddleware = hot(compiler, hotOptions);
23
- return Object.assign(compose([devMiddleware, hotMiddleware]), devMiddleware, hotMiddleware);
18
+ const { hot: hotOptions, ...devOptions } = options;
19
+ const devMiddleware = dev(compiler, devOptions);
20
+ if (hotOptions === false) return devMiddleware;
21
+ const hotMiddleware = hot(compiler, hotOptions);
22
+ return Object.assign(compose([devMiddleware, hotMiddleware]), devMiddleware, hotMiddleware);
24
23
  }
25
24
 
26
25
  export { server as default };
@@ -2,12 +2,12 @@
2
2
  * @module client
3
3
  */
4
4
  export interface Options {
5
- readonly hmr: boolean;
6
- readonly name: string;
7
- readonly path: string;
8
- readonly live: boolean;
9
- readonly origin: string;
10
- readonly overlay: boolean;
11
- readonly progress: boolean;
5
+ readonly hmr: boolean;
6
+ readonly name: string;
7
+ readonly path: string;
8
+ readonly live: boolean;
9
+ readonly origin: string;
10
+ readonly overlay: boolean;
11
+ readonly progress: boolean;
12
12
  }
13
13
  export default function createClient(options: Options): void;
@@ -4,18 +4,18 @@
4
4
  import { Options } from './client';
5
5
  import * as Message from './message';
6
6
  interface Messages {
7
- ok: Message.OK['payload'];
8
- hash: Message.Hash['payload'];
9
- invalid: Message.Invalid['payload'];
10
- problems: Message.Problems['payload'];
11
- progress: Message.Progress['payload'];
7
+ ok: Message.OK['payload'];
8
+ hash: Message.Hash['payload'];
9
+ invalid: Message.Invalid['payload'];
10
+ problems: Message.Problems['payload'];
11
+ progress: Message.Progress['payload'];
12
12
  }
13
13
  interface Events {
14
- ok: (message: Messages['ok'], options: Options) => void;
15
- hash: (message: Messages['hash'], options: Options) => void;
16
- invalid: (message: Messages['invalid'], options: Options) => void;
17
- problems: (message: Messages['problems'], options: Options) => void;
18
- progress: (message: Messages['progress'], options: Options) => void;
14
+ ok: (message: Messages['ok'], options: Options) => void;
15
+ hash: (message: Messages['hash'], options: Options) => void;
16
+ invalid: (message: Messages['invalid'], options: Options) => void;
17
+ problems: (message: Messages['problems'], options: Options) => void;
18
+ progress: (message: Messages['progress'], options: Options) => void;
19
19
  }
20
20
  /**
21
21
  * @function on
@@ -3,37 +3,37 @@
3
3
  */
4
4
  import { StatsError } from 'webpack';
5
5
  export interface Invalid {
6
- action: 'invalid';
7
- payload: {
8
- path: string;
9
- builtAt: number;
10
- };
6
+ action: 'invalid';
7
+ payload: {
8
+ path: string;
9
+ builtAt: number;
10
+ };
11
11
  }
12
12
  export interface Progress {
13
- action: 'progress';
14
- payload: {
15
- value: number;
16
- status: string;
17
- message: string;
18
- };
13
+ action: 'progress';
14
+ payload: {
15
+ value: number;
16
+ status: string;
17
+ message: string;
18
+ };
19
19
  }
20
20
  export interface Hash {
21
- action: 'hash';
22
- payload: {
23
- hash: string;
24
- };
21
+ action: 'hash';
22
+ payload: {
23
+ hash: string;
24
+ };
25
25
  }
26
26
  export interface Problems {
27
- action: 'problems';
28
- payload: {
29
- builtAt: number;
30
- errors: StatsError[];
31
- warnings: StatsError[];
32
- };
27
+ action: 'problems';
28
+ payload: {
29
+ builtAt: number;
30
+ errors: StatsError[];
31
+ warnings: StatsError[];
32
+ };
33
33
  }
34
34
  export interface OK {
35
- action: 'ok';
36
- payload: {
37
- builtAt: number;
38
- };
35
+ action: 'ok';
36
+ payload: {
37
+ builtAt: number;
38
+ };
39
39
  }
@@ -4,16 +4,16 @@
4
4
  */
5
5
  import { StatsError } from 'webpack';
6
6
  export default class Overlay {
7
- private hidden;
8
- private readonly name;
9
- private readonly close;
10
- private readonly aside;
11
- private readonly errorsList;
12
- private readonly errorsTitle;
13
- private readonly warningsList;
14
- private readonly warningsTitle;
15
- constructor(name: string);
16
- setProblems(type: 'errors' | 'warnings', problems: StatsError[]): void;
17
- show(): void;
18
- hide(): void;
7
+ private hidden;
8
+ private readonly name;
9
+ private readonly close;
10
+ private readonly aside;
11
+ private readonly errorsList;
12
+ private readonly errorsTitle;
13
+ private readonly warningsList;
14
+ private readonly warningsTitle;
15
+ constructor(name: string);
16
+ setProblems(type: 'errors' | 'warnings', problems: StatsError[]): void;
17
+ show(): void;
18
+ hide(): void;
19
19
  }
@@ -4,12 +4,12 @@
4
4
  * @see https://www.zhangxinxu.com/wordpress/2015/07/svg-circle-loading
5
5
  */
6
6
  export default class Progress {
7
- private timer?;
8
- private hidden;
9
- private readonly svg;
10
- private readonly track;
11
- constructor();
12
- update(value: number): void;
13
- show(): void;
14
- hide(): void;
7
+ private timer?;
8
+ private hidden;
9
+ private readonly svg;
10
+ private readonly track;
11
+ constructor();
12
+ update(value: number): void;
13
+ show(): void;
14
+ hide(): void;
15
15
  }
@@ -2,11 +2,11 @@
2
2
  * @module enum
3
3
  */
4
4
  export declare const enum TokenType {
5
- EOS = 0,
6
- ESC = 1,
7
- OSC = 2,
8
- SGR = 3,
9
- TEXT = 4,
10
- INCESC = 5,
11
- UNKNOWN = 6
5
+ EOS = 0,
6
+ ESC = 1,
7
+ OSC = 2,
8
+ SGR = 3,
9
+ TEXT = 4,
10
+ INCESC = 5,
11
+ UNKNOWN = 6
12
12
  }
@@ -4,15 +4,15 @@
4
4
  import { AnsiBlock, AnsiColor, AnsiStyle, Theme } from './interface';
5
5
  export type { AnsiBlock, AnsiStyle };
6
6
  export default class Ansi {
7
- protected buffer: string;
8
- protected style: AnsiStyle;
9
- protected colors256: AnsiColor[];
10
- protected colors16: AnsiColor[][];
11
- constructor(theme?: Theme);
12
- private read;
13
- private reset;
14
- private process;
15
- private block;
16
- write(text: string, callback: (block: AnsiBlock) => void): void;
17
- flush(callback: (block: AnsiBlock) => void): void;
7
+ protected buffer: string;
8
+ protected style: AnsiStyle;
9
+ protected colors256: AnsiColor[];
10
+ protected colors16: AnsiColor[][];
11
+ constructor(theme?: Theme);
12
+ private read;
13
+ private reset;
14
+ private process;
15
+ private block;
16
+ write(text: string, callback: (block: AnsiBlock) => void): void;
17
+ flush(callback: (block: AnsiBlock) => void): void;
18
18
  }