Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ashaffer committed Mar 8, 2014
1 parent 17f5ff7 commit bfff24f
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions index.js
Expand Up @@ -8,54 +8,54 @@ var path = require('path');
var url = require('url');

function isLocal(link) {
return link && ! url.parse(link).hostname;
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 = /<!-- smoosh -->([\s\S]*?)<!-- endsmoosh -->/g;
var rePattern = /<!-- smoosh -->([\s\S]*?)<!-- endsmoosh -->/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');
isLocal(href) && $(element).replaceWith('<style>' + fs.readFileSync(path.join(file.base, href), 'utf8') + '</style>');
});
$('link').each(function(index, element) {
var href = $(element).attr('href');
isLocal(href) && $(element).replaceWith('<style>' + fs.readFileSync(path.join(file.base, href), 'utf8') + '</style>');
});

$('script').each(function(index, element) {
var src = $(element).attr('src');
if(isLocal(src)) {
$(element).attr('src', '')
.text(fs.readFileSync(path.join(file.base, src)));
}
});
$('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(/<!-- smoosh -->\n|<!-- endsmoosh -->\n/g, '');
return $.html();
}).replace(/<!-- smoosh -->\n|<!-- endsmoosh -->\n/g, '');

file.contents = new Buffer(output);
file.contents = new Buffer(output);

return callback(null, file);
}
return callback(null, file);
}

});
});
};

0 comments on commit bfff24f

Please sign in to comment.