diff --git a/lib/serve/createServer.js b/lib/serve/createServer.js index 610312e..216039f 100644 --- a/lib/serve/createServer.js +++ b/lib/serve/createServer.js @@ -3,9 +3,8 @@ var path = require('path'); var http = require('http'); var pf = require('portfinder'); var mime = require('mime'); -var through2 = require('through2'); -var cheerio = require('cheerio'); var exec = require('child_process').exec; +var lrStream = require('../util/liveReloadStream'); module.exports = function (port, lrPort, open, next) { var server = http.createServer(function(req, res) { @@ -28,15 +27,7 @@ module.exports = function (port, lrPort, open, next) { // if html, inject lr if (filePath.match(/htm(l)?$/)) { fileStream - .pipe(through2.obj(function(file, enc, next) { - var script = ""; - var html = file.toString('utf8'); - var $ = cheerio.load(html); - $('body').append(script); - - this.push($.html()); - next(); - })) + .pipe(lrStream(lrPort)) .pipe(res); } else { // stream file to response diff --git a/lib/util/liveReloadStream.js b/lib/util/liveReloadStream.js new file mode 100644 index 0000000..7ce9afd --- /dev/null +++ b/lib/util/liveReloadStream.js @@ -0,0 +1,14 @@ +var through2 = require('through2'); +var cheerio = require('cheerio'); + +module.exports = function (lrPort) { + return through2.obj(function(file, enc, next) { + var script = ""; + var html = file.toString('utf8'); + var $ = cheerio.load(html); + $('body').append(script); + + this.push($.html()); + next(); + }); +}; \ No newline at end of file