Skip to content

Commit fc4fe32

Browse files
EslamHikohiroppy
authored andcommittedMay 16, 2019
feat(server): add liveReload option to enable/disable live reloading (#1889)
* feat(server): add liveReload option to enable/disable live reloading * test(server): move live reload tests to new file * fix(server): use this.options * test: use master's snapshot * test: update snapshot * test: update snapshot 2 * test: fix snapshot
1 parent 1c2beff commit fc4fe32

File tree

7 files changed

+148
-7
lines changed

7 files changed

+148
-7
lines changed
 

‎bin/options.js

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const options = {
2121
type: 'boolean',
2222
describe: 'Lazy',
2323
},
24+
liveReload: {
25+
type: 'boolean',
26+
describe: 'Enables/Disables live reloading on changing files',
27+
default: true,
28+
},
2429
serveIndex: {
2530
type: 'boolean',
2631
describe: 'Enables/Disables serveIndex middleware',

‎client-src/default/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ if (!urlParts.port || urlParts.port === '0') {
4747
}
4848

4949
let hot = false;
50+
let liveReload = false;
5051
let initial = true;
5152
let currentHash = '';
5253
let useWarningOverlay = false;
@@ -85,6 +86,10 @@ const onSocketMsg = {
8586
hot = true;
8687
log.info('[WDS] Hot Module Replacement enabled.');
8788
},
89+
liveReload() {
90+
liveReload = true;
91+
log.info('[WDS] Live Reloading enabled.');
92+
},
8893
invalid() {
8994
log.info('[WDS] App updated. Recompiling...');
9095
// fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
@@ -271,7 +276,9 @@ function reloadApp() {
271276
// broadcast update to window
272277
self.postMessage(`webpackHotUpdate${currentHash}`, '*');
273278
}
274-
} else {
279+
}
280+
// allow refreshing the page only if liveReload isn't disabled
281+
if (liveReload) {
275282
let rootWindow = self;
276283
// use parent window for reload (in case we're in an iframe with no valid src)
277284
const intervalId = self.setInterval(() => {

‎lib/Server.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ class Server {
717717
this.sockWrite([connection], 'hot');
718718
}
719719

720+
if (this.options.liveReload !== false) {
721+
this.sockWrite([connection], 'liveReload', this.options.liveReload);
722+
}
723+
720724
if (this.progress) {
721725
this.sockWrite([connection], 'progress', this.progress);
722726
}
@@ -997,11 +1001,12 @@ class Server {
9971001
};
9981002

9991003
const watcher = chokidar.watch(watchPath, watchOptions);
1000-
1001-
watcher.on('change', () => {
1002-
this.sockWrite(this.sockets, 'content-changed');
1003-
});
1004-
1004+
// disabling refreshing on changing the content
1005+
if (this.options.liveReload !== false) {
1006+
watcher.on('change', () => {
1007+
this.sockWrite(this.sockets, 'content-changed');
1008+
});
1009+
}
10051010
this.contentBaseWatchers.push(watcher);
10061011
}
10071012

‎lib/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
"lazy": {
169169
"type": "boolean"
170170
},
171+
"liveReload": {
172+
"type": "boolean"
173+
},
171174
"log": {
172175
"instanceof": "Function"
173176
},
@@ -377,6 +380,7 @@
377380
"inline": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverinline)",
378381
"key": "should be {String|Buffer}",
379382
"lazy": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverlazy-)",
383+
"liveReload": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverlivereload-)",
380384
"log": "should be {Function}",
381385
"logLevel": "should be {String} and equal to one of the allowed values\n\n [ 'info', 'warn', 'error', 'debug', 'trace', 'silent' ]\n\n (https://github.com/webpack/webpack-dev-middleware#loglevel)",
382386
"logTime": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#logtime)",

‎lib/utils/createConfig.js

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ function createConfig(config, argv, { port }) {
4242
options.sockPort = argv.sockPort;
4343
}
4444

45+
if (argv.liveReload === false) {
46+
options.liveReload = false;
47+
}
48+
4549
if (argv.progress) {
4650
options.progress = argv.progress;
4751
}

‎test/LiveReload.test.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const fs = require('fs');
5+
const helper = require('./helper');
6+
const config = require('./fixtures/contentbase-config/webpack.config');
7+
8+
const contentBasePublic = path.join(
9+
__dirname,
10+
'fixtures/contentbase-config/public'
11+
);
12+
13+
describe('liveReload', () => {
14+
let server;
15+
describe('Test disabling live reloading', () => {
16+
const nestedFile = path.join(contentBasePublic, 'assets/example.txt');
17+
18+
jest.setTimeout(30000);
19+
20+
beforeAll((done) => {
21+
server = helper.start(
22+
config,
23+
{
24+
contentBase: contentBasePublic,
25+
watchContentBase: true,
26+
liveReload: false,
27+
},
28+
done
29+
);
30+
});
31+
32+
afterAll((done) => {
33+
helper.close(() => {
34+
done();
35+
});
36+
fs.truncateSync(nestedFile);
37+
});
38+
39+
it('Should not reload on changing files', (done) => {
40+
let reloaded = false;
41+
42+
server.contentBaseWatchers[0].on('change', () => {
43+
// it means that file has changed
44+
45+
// simulating server behaviour
46+
if (server.options.liveReload !== false) {
47+
Object.defineProperty(window.location, 'reload', {
48+
configurable: true,
49+
});
50+
window.location.reload = jest.fn();
51+
window.location.reload();
52+
reloaded = true;
53+
}
54+
expect(reloaded).toBe(false);
55+
56+
done();
57+
});
58+
59+
// change file content
60+
setTimeout(() => {
61+
fs.writeFileSync(nestedFile, 'Heyo', 'utf8');
62+
}, 1000);
63+
});
64+
});
65+
66+
describe('Testing live reloading', () => {
67+
const nestedFile = path.join(contentBasePublic, 'assets/example.txt');
68+
69+
jest.setTimeout(30000);
70+
71+
beforeAll((done) => {
72+
server = helper.start(
73+
config,
74+
{
75+
contentBase: contentBasePublic,
76+
watchContentBase: true,
77+
liveReload: true,
78+
},
79+
done
80+
);
81+
});
82+
83+
afterAll((done) => {
84+
helper.close(() => {
85+
done();
86+
});
87+
fs.truncateSync(nestedFile);
88+
});
89+
90+
it('Should reload on changing files', (done) => {
91+
let reloaded = false;
92+
93+
server.contentBaseWatchers[0].on('change', () => {
94+
// it means that files has changed
95+
96+
// simulating server behaviour
97+
if (server.options.liveReload !== false) {
98+
Object.defineProperty(window.location, 'reload', {
99+
configurable: true,
100+
});
101+
window.location.reload = jest.fn();
102+
window.location.reload();
103+
reloaded = true;
104+
}
105+
expect(reloaded).toBe(true);
106+
107+
done();
108+
});
109+
110+
// change file content
111+
setTimeout(() => {
112+
fs.writeFileSync(nestedFile, 'Heyo', 'utf8');
113+
}, 1000);
114+
});
115+
});
116+
});

‎test/__snapshots__/Routes.test.js.snap

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.