-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use .match to inject the script, instead of cheerio
- Loading branch information
Showing
2 changed files
with
11 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters