Skip to content
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

Added beforeReload hook #366

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ var params = {
wait: 1000, // Waits for all changes, before reloading. Defaults to 0 sec.
mount: [['/components', './node_modules']], // Mount a directory to a route.
logLevel: 2, // 0 = errors only, 1 = some, 2 = lots
middleware: [function(req, res, next) { next(); }] // Takes an array of Connect-compatible middleware that are injected into the server middleware stack
middleware: [function(req, res, next) { next(); }], // Takes an array of Connect-compatible middleware that are injected into the server middleware stack
beforeReload: function(changePath) { console.log(changePath); }, // Run function every time a change is detected. Return a Promise to delay browser reload.
};
liveServer.start(params);
```
Expand Down
33 changes: 24 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,32 @@ LiveServer.start = function(options) {
ignored: ignored,
ignoreInitial: true
});



function handleChange(changePath) {
var cssChange = path.extname(changePath) === ".css" && !noCssInject;
if (LiveServer.logLevel >= 1) {
if (cssChange)
console.log("CSS change detected".magenta, changePath);
else console.log("Change detected".cyan, changePath);
function reload() {
var cssChange = path.extname(changePath) === ".css" && !noCssInject;
if (LiveServer.logLevel >= 1) {
if (cssChange)
console.log("CSS change detected".magenta, changePath);
else console.log("Change detected".cyan, changePath);
}
clients.forEach(function(ws) {
if (ws)
ws.send(cssChange ? 'refreshcss' : 'reload');
});
}
clients.forEach(function(ws) {
if (ws)
ws.send(cssChange ? 'refreshcss' : 'reload');
});
// Run beforeReload hook if set
if(options.beforeReload){
var updateResult = options.beforeReload(changePath)
// If beforeReload is a promise/async, wait
if(!!updateResult && typeof updateResult.then === 'function'){
updateResult.then(reload)
return
}
}
reload()
}
LiveServer.watcher
.on("change", handleChange)
Expand Down