You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
"use strict";
|
|
|
|
const budo = require("budo");
|
|
const defaultValue = require("default-value");
|
|
const assureArray = require("assure-array");
|
|
const path = require("path");
|
|
const entities = require("entities");
|
|
|
|
let reloadClientTag = "<script src=\"/budo/livereload.js\"></script>";
|
|
|
|
function monkeyPatchWrite(res) {
|
|
// NOTE: This is a hack, to auto-inject the Budo livereload client in any outgoing HTML response (but only in development mode).
|
|
let write = res.write.bind(res);
|
|
|
|
res.write = function monkeyPatchedWrite(... args) {
|
|
if (res.getHeader("content-type").startsWith("text/html")) {
|
|
let contentLength = res.getHeader("content-length");
|
|
|
|
if (contentLength != null) {
|
|
// Compensate for the additional bytes introduced by our injected script tag
|
|
res.setHeader("content-length", contentLength + reloadClientTag.length);
|
|
}
|
|
|
|
write(reloadClientTag);
|
|
}
|
|
|
|
// Reset the write method back to the original method; we don't need to get in the way anymore
|
|
res.write = write;
|
|
|
|
write(... args);
|
|
};
|
|
}
|
|
|
|
function createExpressMiddleware(app) {
|
|
return function expressMiddleware(req, res, next) {
|
|
monkeyPatchWrite(res);
|
|
|
|
app.handle(req, res, (err) => {
|
|
if (err != null && err instanceof Error) {
|
|
res
|
|
.status(500)
|
|
.send(`${reloadClientTag}<pre>${entities.escape(err.stack)}</pre>`);
|
|
} else {
|
|
next(err);
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
module.exports = function ({ options, staticPath, staticBasePath, entryPaths, host }) {
|
|
let middlewareList;
|
|
|
|
if (options.middleware != null) {
|
|
middlewareList = assureArray(options.middleware).concat([
|
|
createExpressMiddleware(options.expressApp)
|
|
]);
|
|
} else {
|
|
middlewareList = [
|
|
createExpressMiddleware(options.expressApp)
|
|
];
|
|
}
|
|
|
|
let budoOptions = {
|
|
watchGlob: staticPath(options.livereloadPattern),
|
|
live: options.livereloadPattern,
|
|
livePort: defaultValue(options.livereloadPort, 35729),
|
|
stream: defaultValue(options.stream, process.stdout),
|
|
port: options.port,
|
|
host: host,
|
|
dir: staticBasePath,
|
|
serve: (options.staticPrefix != null)
|
|
? path.join(options.staticPrefix, options.bundlePath)
|
|
: options.bundlePath,
|
|
browserify: options.browserify,
|
|
middleware: middlewareList,
|
|
debug: defaultValue(options.sourceMaps, true),
|
|
};
|
|
|
|
let devServer = budo(entryPaths, budoOptions).on("connect", (event) => {
|
|
let reloadServer = event.webSocketServer;
|
|
|
|
reloadServer.once("connection", (_socket) => {
|
|
// This is to make sure the browser also reloads after the process has auto-restarted (eg. using `nodemon`)
|
|
console.log("Triggering initial page refresh for new client...");
|
|
devServer.reload("*");
|
|
});
|
|
});
|
|
};
|