Skip to content

Commit

Permalink
Fixed unreliable hot reloading (#29)
Browse files Browse the repository at this point in the history
* fix: unreliable hot reload

1. remove cwd option to yield absolute paths
2. add debounce to avoid multiple restarts happen unexpectedly when more than one file change

* refactor: use debounce package
  • Loading branch information
tungv authored and leo committed Aug 26, 2017
1 parent de0d1b8 commit 49e1c8d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/listening.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Native
const path = require('path')
const debounce = require('debounce')

// Packages
const { write: copy } = require('clipboardy')
Expand Down Expand Up @@ -76,7 +77,6 @@ module.exports = async (server, inUse, flags, sockets) => {
const watchConfig = {
usePolling: flags.poll,
ignoreInitial: true,
cwd: process.cwd(),
ignored: [
/\.git|node_modules|\.nyc_output|\.sass-cache|coverage/,
/\.swp$/
Expand All @@ -102,21 +102,24 @@ module.exports = async (server, inUse, flags, sockets) => {
const watcher = watch(toWatch, watchConfig)

// Ensure that the server gets restarted if a file changes
watcher.on('all', (event, filePath) => {
const location = path.relative(process.cwd(), filePath)
watcher.on(
'all',
debounce((event, filePath) => {
const location = path.relative(process.cwd(), filePath)

console.log(
`\n${chalk.blue('File changed:')} ${location} - Restarting server...`
)
console.log(
`\n${chalk.blue('File changed:')} ${location} - Restarting server...`
)

destroySockets(sockets)
destroySockets(sockets)

// Ensure the same port when restarting server
flags.port = details.port
// Ensure the same port when restarting server
flags.port = details.port

// Restart server
server.close(restartServer.bind(this, file, flags, watcher))
})
// Restart server
server.close(restartServer.bind(this, file, flags, watcher))
}, 10)
)
}

if (flags.restarted) {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"chalk": "2.1.0",
"chokidar": "1.7.0",
"clipboardy": "1.1.4",
"debounce": "1.0.2",
"get-port": "3.1.0",
"ip": "1.1.5",
"jsome": "2.3.26",
Expand Down

0 comments on commit 49e1c8d

Please sign in to comment.