diff --git a/index.js b/index.js index 7a9c000..523ac62 100644 --- a/index.js +++ b/index.js @@ -5,49 +5,57 @@ var cheerio = require('cheerio'); var replace = require('gulp-replace'); var fs = require('fs'); var path = require('path'); +var url = require('url'); + +function isLocal(link) { + return link && ! url.parse(link).hostname; +} module.exports = function() { - // create a stream through which each file will pass - return through.obj(function(file, enc, callback) { + // create a stream through which each file will pass + return through.obj(function(file, enc, callback) { - if (file.isNull()) { - this.push(file); // do nothing if no contents - return callback(); - } + if (file.isNull()) { + this.push(file); // do nothing if no contents + return callback(); + } - if (file.isStream()) { - this.emit('error', new gutil.PluginError('gulp-smoosher', 'Streaming not supported')); - return callback(); - } + if (file.isStream()) { + this.emit('error', new gutil.PluginError('gulp-smoosher', 'Streaming not supported')); + return callback(); + } - if (file.isBuffer()) { + if (file.isBuffer()) { - var rePattern = /([\s\S]*?)/g; + var rePattern = /([\s\S]*?)/g; - var input = String(file.contents); + var input = String(file.contents); - var output = input.replace(rePattern, function(match) { + var output = input.replace(rePattern, function(match) { - var $ = cheerio.load(match); + var $ = cheerio.load(match); - $('link').each(function(index, element) { - var href = $(element).attr('href'); - $(element).replaceWith(''); - }); + $('link').each(function(index, element) { + var href = $(element).attr('href'); + isLocal(href) && $(element).replaceWith(''); + }); - $('script').each(function(index, element) { - var src = $(element).attr('src'); - $(element).replaceWith(''); - }); + $('script').each(function(index, element) { + var src = $(element).attr('src'); + if(isLocal(src)) { + $(element).attr('src', '') + .text(fs.readFileSync(path.join(file.base, src))); + } + }); - return $.html(); - }).replace(/\n|\n/g, ''); + return $.html(); + }).replace(/\n|\n/g, ''); - file.contents = new Buffer(output); + file.contents = new Buffer(output); - return callback(null, file); - } + return callback(null, file); + } - }); -}; \ No newline at end of file + }); +};