Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip remote urls, inject scripts that contain html properly #2

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 38 additions & 30 deletions index.js
Expand Up @@ -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 = /<!-- 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');
$(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');
$(element).replaceWith('<script>' + fs.readFileSync(path.join(file.base, src), 'utf8') + '</script>');
});
$('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);
}

});
};
});
};