Skip to content

Commit 76bcd84

Browse files
refactor: resolve todo (#2619)
1 parent 25cc479 commit 76bcd84

File tree

5 files changed

+157
-18
lines changed

5 files changed

+157
-18
lines changed

lib/Server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,8 @@ class Server {
642642
this.sockWrite([connection], 'hot');
643643
}
644644

645-
// TODO: change condition at major version
646-
if (this.options.liveReload !== false) {
647-
this.sockWrite([connection], 'liveReload', this.options.liveReload);
645+
if (this.options.liveReload) {
646+
this.sockWrite([connection], 'liveReload');
648647
}
649648

650649
if (this.options.progress) {

lib/utils/normalizeOptions.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
'use strict';
22

3-
/* eslint-disable
4-
no-undefined
5-
*/
6-
73
function normalizeOptions(compiler, options) {
84
// Setup default value
5+
options.serveIndex =
6+
typeof options.serveIndex !== 'undefined' ? options.serveIndex : true;
97
options.contentBase =
10-
options.contentBase !== undefined ? options.contentBase : process.cwd();
8+
typeof options.contentBase !== 'undefined'
9+
? options.contentBase
10+
: process.cwd();
1111
options.contentBasePublicPath = options.contentBasePublicPath || '/';
12+
options.watchOptions = options.watchOptions || {};
1213
options.hot =
1314
typeof options.hot === 'boolean' || options.hot === 'only'
1415
? options.hot
1516
: true;
16-
options.serveIndex =
17-
options.serveIndex !== undefined ? options.serveIndex : true;
18-
options.watchOptions = options.watchOptions || {};
17+
options.liveReload =
18+
typeof options.liveReload !== 'undefined' ? options.liveReload : true;
1919
options.stats =
2020
options.stats && Object.keys(options.stats).length !== 0
2121
? options.stats
2222
: {};
2323

2424
// normalize transportMode option
25-
if (options.transportMode === undefined) {
25+
if (typeof options.transportMode === 'undefined') {
2626
options.transportMode = {
2727
server: 'ws',
2828
client: 'ws',
@@ -42,10 +42,6 @@ function normalizeOptions(compiler, options) {
4242
}
4343
}
4444

45-
if (!options.watchOptions) {
46-
options.watchOptions = {};
47-
}
48-
4945
if (!options.clientOptions) {
5046
options.clientOptions = {};
5147
}

test/server/liveReload-option.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('liveReload option', () => {
4242
// it means that file has changed
4343

4444
// simulating server behaviour
45-
if (server.options.liveReload !== false) {
45+
if (server.options.liveReload) {
4646
// issue: https://github.com/facebook/jest/issues/9471
4747
Object.defineProperty(window, 'location', {
4848
writable: true,
@@ -97,7 +97,7 @@ describe('liveReload option', () => {
9797
// it means that files has changed
9898

9999
// simulating server behaviour
100-
if (server.options.liveReload !== false) {
100+
if (server.options.liveReload) {
101101
Object.defineProperty(window, 'location', {
102102
writable: true,
103103
value: { assign: jest.fn() },

test/server/utils/__snapshots__/normalizeOptions.test.js.snap

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Object {
99
},
1010
"contentBasePublicPath": "/",
1111
"hot": true,
12+
"liveReload": true,
1213
"serveIndex": true,
1314
"stats": Object {},
1415
"transportMode": Object {
@@ -26,6 +27,7 @@ Object {
2627
},
2728
"contentBasePublicPath": "/",
2829
"hot": true,
30+
"liveReload": true,
2931
"serveIndex": true,
3032
"stats": Object {},
3133
"transportMode": Object {
@@ -43,6 +45,7 @@ Object {
4345
},
4446
"contentBasePublicPath": "/",
4547
"hot": true,
48+
"liveReload": true,
4649
"serveIndex": true,
4750
"stats": Object {},
4851
"transportMode": Object {
@@ -64,6 +67,7 @@ Object {
6467
],
6568
"contentBasePublicPath": "/",
6669
"hot": true,
70+
"liveReload": true,
6771
"serveIndex": true,
6872
"stats": Object {},
6973
"transportMode": Object {
@@ -82,6 +86,7 @@ Object {
8286
"contentBase": "/path/to/dist",
8387
"contentBasePublicPath": "/",
8488
"hot": true,
89+
"liveReload": true,
8590
"serveIndex": true,
8691
"stats": Object {},
8792
"transportMode": Object {
@@ -99,6 +104,97 @@ Object {
99104
},
100105
"contentBasePublicPath": "/content-base-public-path",
101106
"hot": true,
107+
"liveReload": true,
108+
"serveIndex": true,
109+
"stats": Object {},
110+
"transportMode": Object {
111+
"client": "ws",
112+
"server": "ws",
113+
},
114+
"watchOptions": Object {},
115+
}
116+
`;
117+
118+
exports[`normalizeOptions hot is false should set correct options 1`] = `
119+
Object {
120+
"clientOptions": Object {
121+
"path": "/ws",
122+
},
123+
"contentBasePublicPath": "/",
124+
"hot": false,
125+
"liveReload": true,
126+
"serveIndex": true,
127+
"stats": Object {},
128+
"transportMode": Object {
129+
"client": "ws",
130+
"server": "ws",
131+
},
132+
"watchOptions": Object {},
133+
}
134+
`;
135+
136+
exports[`normalizeOptions hot is only should set correct options 1`] = `
137+
Object {
138+
"clientOptions": Object {
139+
"path": "/ws",
140+
},
141+
"contentBasePublicPath": "/",
142+
"hot": "only",
143+
"liveReload": true,
144+
"serveIndex": true,
145+
"stats": Object {},
146+
"transportMode": Object {
147+
"client": "ws",
148+
"server": "ws",
149+
},
150+
"watchOptions": Object {},
151+
}
152+
`;
153+
154+
exports[`normalizeOptions hot is true should set correct options 1`] = `
155+
Object {
156+
"clientOptions": Object {
157+
"path": "/ws",
158+
},
159+
"contentBasePublicPath": "/",
160+
"hot": true,
161+
"liveReload": true,
162+
"serveIndex": true,
163+
"stats": Object {},
164+
"transportMode": Object {
165+
"client": "ws",
166+
"server": "ws",
167+
},
168+
"watchOptions": Object {},
169+
}
170+
`;
171+
172+
exports[`normalizeOptions liveReload is false should set correct options 1`] = `
173+
Object {
174+
"clientOptions": Object {
175+
"path": "/ws",
176+
},
177+
"contentBasePublicPath": "/",
178+
"hot": true,
179+
"liveReload": false,
180+
"serveIndex": true,
181+
"stats": Object {},
182+
"transportMode": Object {
183+
"client": "ws",
184+
"server": "ws",
185+
},
186+
"watchOptions": Object {},
187+
}
188+
`;
189+
190+
exports[`normalizeOptions liveReload is true should set correct options 1`] = `
191+
Object {
192+
"clientOptions": Object {
193+
"path": "/ws",
194+
},
195+
"contentBasePublicPath": "/",
196+
"hot": true,
197+
"liveReload": true,
102198
"serveIndex": true,
103199
"stats": Object {},
104200
"transportMode": Object {
@@ -116,6 +212,7 @@ Object {
116212
},
117213
"contentBasePublicPath": "/",
118214
"hot": true,
215+
"liveReload": true,
119216
"serveIndex": true,
120217
"stats": Object {},
121218
"transportMode": Object {
@@ -133,6 +230,7 @@ Object {
133230
},
134231
"contentBasePublicPath": "/",
135232
"hot": true,
233+
"liveReload": true,
136234
"serveIndex": true,
137235
"stats": Object {},
138236
"transportMode": Object {
@@ -150,6 +248,7 @@ Object {
150248
},
151249
"contentBasePublicPath": "/",
152250
"hot": true,
251+
"liveReload": true,
153252
"serveIndex": true,
154253
"stats": Object {},
155254
"transportMode": Object {
@@ -167,6 +266,7 @@ Object {
167266
},
168267
"contentBasePublicPath": "/",
169268
"hot": true,
269+
"liveReload": true,
170270
"serveIndex": true,
171271
"stats": Object {},
172272
"transportMode": Object {
@@ -184,6 +284,7 @@ Object {
184284
},
185285
"contentBasePublicPath": "/",
186286
"hot": true,
287+
"liveReload": true,
187288
"serveIndex": true,
188289
"stats": Object {},
189290
"transportMode": Object {
@@ -201,6 +302,7 @@ Object {
201302
},
202303
"contentBasePublicPath": "/",
203304
"hot": true,
305+
"liveReload": true,
204306
"serveIndex": true,
205307
"stats": Object {},
206308
"transportMode": Object {
@@ -218,6 +320,7 @@ Object {
218320
},
219321
"contentBasePublicPath": "/",
220322
"hot": true,
323+
"liveReload": true,
221324
"serveIndex": true,
222325
"stats": Object {},
223326
"transportMode": Object {
@@ -235,6 +338,7 @@ Object {
235338
},
236339
"contentBasePublicPath": "/",
237340
"hot": true,
341+
"liveReload": true,
238342
"serveIndex": true,
239343
"stats": Object {},
240344
"transportMode": Object {

test/server/utils/normalizeOptions.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,46 @@ describe('normalizeOptions', () => {
133133
},
134134
optionsResults: null,
135135
},
136+
{
137+
title: 'liveReload is true',
138+
multiCompiler: false,
139+
options: {
140+
liveReload: true,
141+
},
142+
optionsResults: null,
143+
},
144+
{
145+
title: 'liveReload is false',
146+
multiCompiler: false,
147+
options: {
148+
liveReload: false,
149+
},
150+
optionsResults: null,
151+
},
152+
{
153+
title: 'hot is true',
154+
multiCompiler: false,
155+
options: {
156+
hot: true,
157+
},
158+
optionsResults: null,
159+
},
160+
{
161+
title: 'hot is false',
162+
multiCompiler: false,
163+
options: {
164+
hot: false,
165+
},
166+
optionsResults: null,
167+
},
168+
{
169+
title: 'hot is only',
170+
multiCompiler: false,
171+
options: {
172+
hot: 'only',
173+
},
174+
optionsResults: null,
175+
},
136176
];
137177

138178
cases.forEach((data) => {

0 commit comments

Comments
 (0)