Skip to content

Commit

Permalink
use .match to inject the script, instead of cheerio
Browse files Browse the repository at this point in the history
  • Loading branch information
w33ble committed Apr 22, 2014
1 parent 99b669a commit 275274e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
20 changes: 10 additions & 10 deletions lib/util/liveReloadStream.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var through2 = require('through2');
var cheerio = require('cheerio');
var map = require('through2-map');
var match = /<\/body>/i;
var script = function(port) {
return "<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':"+port+"/livereload.js?snipver=1\"></' + 'script>')</script>";
};

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();
return map({wantStrings: true}, function(chunk) {
if (chunk.indexOf(match)) {
chunk = chunk.replace(match, script(lrPort)+'</body>');
}
return chunk;
});
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
"gulp": "~3.2.0",
"gulp-util": "~2.2.0",
"portfinder": "~0.2.1",
"cheerio": "~0.13.1",
"minimist": "0.0.8",
"mime": "~1.2.11",
"through2": "~0.4.1"
"through2-map": "~1.2.1"
},
"devDependencies": {
"mocha": "*",
Expand Down

0 comments on commit 275274e

Please sign in to comment.