forked from gabrielflorit/gulp-smoosher
/
index.js
61 lines (45 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
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) {
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.isBuffer()) {
var rePattern = /<!-- smoosh -->([\s\S]*?)<!-- endsmoosh -->/g;
var input = String(file.contents);
var output = input.replace(rePattern, function(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>');
});
$('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, '');
file.contents = new Buffer(output);
return callback(null, file);
}
});
};