Skip to content

refactor: resolve todo #2619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,8 @@ class Server {
this.sockWrite([connection], 'hot');
}

// TODO: change condition at major version
if (this.options.liveReload !== false) {
this.sockWrite([connection], 'liveReload', this.options.liveReload);
if (this.options.liveReload) {
this.sockWrite([connection], 'liveReload');
}

if (this.options.progress) {
Expand Down
22 changes: 9 additions & 13 deletions lib/utils/normalizeOptions.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
'use strict';

/* eslint-disable
no-undefined
*/

function normalizeOptions(compiler, options) {
// Setup default value
options.serveIndex =
typeof options.serveIndex !== 'undefined' ? options.serveIndex : true;
options.contentBase =
options.contentBase !== undefined ? options.contentBase : process.cwd();
typeof options.contentBase !== 'undefined'
? options.contentBase
: process.cwd();
options.contentBasePublicPath = options.contentBasePublicPath || '/';
options.watchOptions = options.watchOptions || {};
options.hot =
typeof options.hot === 'boolean' || options.hot === 'only'
? options.hot
: true;
options.serveIndex =
options.serveIndex !== undefined ? options.serveIndex : true;
options.watchOptions = options.watchOptions || {};
options.liveReload =
typeof options.liveReload !== 'undefined' ? options.liveReload : true;
options.stats =
options.stats && Object.keys(options.stats).length !== 0
? options.stats
: {};

// normalize transportMode option
if (options.transportMode === undefined) {
if (typeof options.transportMode === 'undefined') {
options.transportMode = {
server: 'ws',
client: 'ws',
Expand All @@ -42,10 +42,6 @@ function normalizeOptions(compiler, options) {
}
}

if (!options.watchOptions) {
options.watchOptions = {};
}

if (!options.clientOptions) {
options.clientOptions = {};
}
Expand Down
4 changes: 2 additions & 2 deletions test/server/liveReload-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('liveReload option', () => {
// it means that file has changed

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

// simulating server behaviour
if (server.options.liveReload !== false) {
if (server.options.liveReload) {
Object.defineProperty(window, 'location', {
writable: true,
value: { assign: jest.fn() },
Expand Down
104 changes: 104 additions & 0 deletions test/server/utils/__snapshots__/normalizeOptions.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -26,6 +27,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -43,6 +45,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -64,6 +67,7 @@ Object {
],
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -82,6 +86,7 @@ Object {
"contentBase": "/path/to/dist",
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -99,6 +104,97 @@ Object {
},
"contentBasePublicPath": "/content-base-public-path",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
"client": "ws",
"server": "ws",
},
"watchOptions": Object {},
}
`;

exports[`normalizeOptions hot is false should set correct options 1`] = `
Object {
"clientOptions": Object {
"path": "/ws",
},
"contentBasePublicPath": "/",
"hot": false,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
"client": "ws",
"server": "ws",
},
"watchOptions": Object {},
}
`;

exports[`normalizeOptions hot is only should set correct options 1`] = `
Object {
"clientOptions": Object {
"path": "/ws",
},
"contentBasePublicPath": "/",
"hot": "only",
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
"client": "ws",
"server": "ws",
},
"watchOptions": Object {},
}
`;

exports[`normalizeOptions hot is true should set correct options 1`] = `
Object {
"clientOptions": Object {
"path": "/ws",
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
"client": "ws",
"server": "ws",
},
"watchOptions": Object {},
}
`;

exports[`normalizeOptions liveReload is false should set correct options 1`] = `
Object {
"clientOptions": Object {
"path": "/ws",
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": false,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
"client": "ws",
"server": "ws",
},
"watchOptions": Object {},
}
`;

exports[`normalizeOptions liveReload is true should set correct options 1`] = `
Object {
"clientOptions": Object {
"path": "/ws",
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -116,6 +212,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -133,6 +230,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -150,6 +248,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -167,6 +266,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -184,6 +284,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -201,6 +302,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -218,6 +320,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand All @@ -235,6 +338,7 @@ Object {
},
"contentBasePublicPath": "/",
"hot": true,
"liveReload": true,
"serveIndex": true,
"stats": Object {},
"transportMode": Object {
Expand Down
40 changes: 40 additions & 0 deletions test/server/utils/normalizeOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,46 @@ describe('normalizeOptions', () => {
},
optionsResults: null,
},
{
title: 'liveReload is true',
multiCompiler: false,
options: {
liveReload: true,
},
optionsResults: null,
},
{
title: 'liveReload is false',
multiCompiler: false,
options: {
liveReload: false,
},
optionsResults: null,
},
{
title: 'hot is true',
multiCompiler: false,
options: {
hot: true,
},
optionsResults: null,
},
{
title: 'hot is false',
multiCompiler: false,
options: {
hot: false,
},
optionsResults: null,
},
{
title: 'hot is only',
multiCompiler: false,
options: {
hot: 'only',
},
optionsResults: null,
},
];

cases.forEach((data) => {
Expand Down