Skip to content

Commit

Permalink
refactor live reload injection stream
Browse files Browse the repository at this point in the history
  • Loading branch information
w33ble committed Apr 20, 2014
1 parent 51a695c commit dcd1457
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
13 changes: 2 additions & 11 deletions lib/serve/createServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = "<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':"+lrPort+"/livereload.js?snipver=1\"></' + 'script>')</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
Expand Down
14 changes: 14 additions & 0 deletions lib/util/liveReloadStream.js
Original file line number Diff line number Diff line change
@@ -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 = "<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':"+lrPort+"/livereload.js?snipver=1\"></' + 'script>')</script>";
var html = file.toString('utf8');
var $ = cheerio.load(html);
$('body').append(script);

this.push($.html());
next();
});
};

0 comments on commit dcd1457

Please sign in to comment.