Skip to content

Commit af4cd33

Browse files
Jasper De Moordevongovett
authored andcommitted
Put filewatcher in a worker, for better stability and performance (#1368)
1 parent f743317 commit af4cd33

6 files changed

Lines changed: 123 additions & 60 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
"babylon-walk": "^1.0.2",
2424
"browserslist": "^3.2.6",
2525
"chalk": "^2.1.0",
26-
"chokidar": "^2.0.3",
2726
"command-exists": "^1.2.6",
2827
"commander": "^2.11.0",
2928
"cross-spawn": "^6.0.4",
3029
"cssnano": "^3.10.0",
3130
"deasync": "^0.1.13",
3231
"dotenv": "^5.0.0",
3332
"filesize": "^3.6.0",
33+
"fswatcher-child": "^1.0.3",
3434
"get-port": "^3.2.0",
3535
"glob": "^7.1.2",
3636
"grapheme-breaker": "^0.3.2",

src/Bundler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ class Bundler extends EventEmitter {
324324

325325
if (this.options.watch) {
326326
this.watcher = new Watcher();
327+
// Wait for ready event for reliable testing on watcher
328+
if (process.env.NODE_ENV === 'test' && !this.watcher.ready) {
329+
await new Promise(resolve => this.watcher.once('ready', resolve));
330+
}
327331
this.watcher.on('change', this.onChange.bind(this));
328332
}
329333

src/Watcher.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {FSWatcher} = require('chokidar');
1+
const FSWatcher = require('fswatcher-child');
22
const Path = require('path');
33

44
/**
@@ -13,20 +13,12 @@ class Watcher {
1313
this.watcher = new FSWatcher({
1414
useFsEvents: this.shouldWatchDirs,
1515
ignoreInitial: true,
16+
ignorePermissionErrors: true,
1617
ignored: /\.cache|\.git/
1718
});
1819

1920
this.watchedDirectories = new Map();
20-
21-
// Only close the watcher after the ready event is emitted
22-
this.ready = false;
2321
this.stopped = false;
24-
this.watcher.once('ready', () => {
25-
this.ready = true;
26-
if (this.stopped) {
27-
this.watcher.close();
28-
}
29-
});
3022
}
3123

3224
/**
@@ -124,14 +116,19 @@ class Watcher {
124116
this.watcher.on(event, callback);
125117
}
126118

119+
/**
120+
* Add an event handler
121+
*/
122+
once(event, callback) {
123+
this.watcher.once(event, callback);
124+
}
125+
127126
/**
128127
* Stop watching all paths
129128
*/
130129
stop() {
131130
this.stopped = true;
132-
if (this.ready) {
133-
this.watcher.close();
134-
}
131+
this.watcher.close();
135132
}
136133
}
137134

test/hmr.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ describe('hmr', function() {
5454

5555
const buildEnd = nextEvent(b, 'buildEnd');
5656

57+
if (process.platform === 'win32') {
58+
await sleep(100);
59+
}
60+
5761
fs.writeFileSync(
5862
__dirname + '/input/local.js',
5963
'exports.a = 5;\nexports.b = 5;'
@@ -99,6 +103,10 @@ describe('hmr', function() {
99103

100104
const buildEnd = nextEvent(b, 'buildEnd');
101105

106+
if (process.platform === 'win32') {
107+
await sleep(100);
108+
}
109+
102110
fs.writeFileSync(
103111
__dirname + '/input/local.js',
104112
'exports.a = 5; exports.b = 5;'
@@ -123,6 +131,10 @@ describe('hmr', function() {
123131

124132
const buildEnd = nextEvent(b, 'buildEnd');
125133

134+
if (process.platform === 'win32') {
135+
await sleep(100);
136+
}
137+
126138
fs.writeFileSync(
127139
__dirname + '/input/local.js',
128140
'require("fs"); exports.a = 5; exports.b = 5;'
@@ -145,6 +157,10 @@ describe('hmr', function() {
145157

146158
const buildEnd = nextEvent(b, 'buildEnd');
147159

160+
if (process.platform === 'win32') {
161+
await sleep(100);
162+
}
163+
148164
fs.writeFileSync(
149165
__dirname + '/input/local.js',
150166
'require("fs"; exports.a = 5; exports.b = 5;'
@@ -173,6 +189,10 @@ describe('hmr', function() {
173189
b = bundler(__dirname + '/input/index.js', {watch: true, hmr: true});
174190
await b.bundle();
175191

192+
if (process.platform === 'win32') {
193+
await sleep(100);
194+
}
195+
176196
fs.writeFileSync(
177197
__dirname + '/input/local.js',
178198
'require("fs"; exports.a = 5; exports.b = 5;'
@@ -195,6 +215,10 @@ describe('hmr', function() {
195215

196216
const firstBuildEnd = nextEvent(b, 'buildEnd');
197217

218+
if (process.platform === 'win32') {
219+
await sleep(100);
220+
}
221+
198222
fs.writeFileSync(
199223
__dirname + '/input/local.js',
200224
'require("fs"; exports.a = 5; exports.b = 5;'
@@ -233,6 +257,10 @@ describe('hmr', function() {
233257

234258
assert.deepEqual(outputs, [3]);
235259

260+
if (process.platform === 'win32') {
261+
await sleep(100);
262+
}
263+
236264
fs.writeFileSync(
237265
__dirname + '/input/local.js',
238266
'exports.a = 5; exports.b = 5;'
@@ -261,6 +289,10 @@ describe('hmr', function() {
261289

262290
assert.deepEqual(outputs, [3]);
263291

292+
if (process.platform === 'win32') {
293+
await sleep(100);
294+
}
295+
264296
fs.writeFileSync(
265297
__dirname + '/input/local.js',
266298
'exports.a = 5; exports.b = 5;'
@@ -292,6 +324,10 @@ describe('hmr', function() {
292324
await sleep(50);
293325
assert.deepEqual(outputs, [3]);
294326

327+
if (process.platform === 'win32') {
328+
await sleep(100);
329+
}
330+
295331
fs.writeFileSync(
296332
__dirname + '/input/local.js',
297333
'exports.a = 5; exports.b = 5;'
@@ -324,6 +360,10 @@ describe('hmr', function() {
324360

325361
let spy = sinon.spy(ctx.document.body, 'appendChild');
326362

363+
if (process.platform === 'win32') {
364+
await sleep(100);
365+
}
366+
327367
fs.writeFileSync(
328368
__dirname + '/input/local.js',
329369
'require("fs"; exports.a = 5; exports.b = 5;'
@@ -362,6 +402,10 @@ describe('hmr', function() {
362402
let appendSpy = sinon.spy(ctx.document.body, 'appendChild');
363403
let removeSpy = sinon.spy(ctx.document.getElementById('tmp'), 'remove');
364404

405+
if (process.platform === 'win32') {
406+
await sleep(100);
407+
}
408+
365409
fs.writeFileSync(
366410
__dirname + '/input/local.js',
367411
'require("fs"; exports.a = 5; exports.b = 5;'
@@ -401,6 +445,10 @@ describe('hmr', function() {
401445

402446
const buildEnd = nextEvent(b, 'buildEnd');
403447

448+
if (process.platform === 'win32') {
449+
await sleep(100);
450+
}
451+
404452
fs.writeFileSync(
405453
__dirname + '/input/local.js',
406454
'exports.a = 5;\nexports.b = 5;'
@@ -434,6 +482,10 @@ describe('hmr', function() {
434482

435483
const buildEnd = nextEvent(b, 'buildEnd');
436484

485+
if (process.platform === 'win32') {
486+
await sleep(100);
487+
}
488+
437489
fs.writeFileSync(
438490
__dirname + '/input/local.js',
439491
'exports.a = 5;\nexports.b = 5;'

test/watcher.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ describe('watcher', function() {
239239
fs.readFileSync(__dirname + '/input/.babelrc', 'utf8')
240240
);
241241
babelrc.presets[0][1].targets.browsers.push('IE >= 11');
242+
243+
await sleep(100);
244+
242245
fs.writeFileSync(__dirname + '/input/.babelrc', JSON.stringify(babelrc));
243246

244247
await nextBundle(b);

0 commit comments

Comments
 (0)