-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (55 loc) · 2.03 KB
/
index.js
File metadata and controls
59 lines (55 loc) · 2.03 KB
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
'use strict';
var gulp = require('gulp');
var util = require('gulp-util');
var fs = require("fs")
var path = require('path');
var through = require('through2');
module.exports = function(values) {
var config = {
extensions: ['js', 'css', 'html'].join('|'),
tags: false
}
if(values && values.extensions && values.extensions[0]){
config.extensions = values.extensions.join('|');
}
if(values && values.tags && values.tags[0]){
config.tags = values.tags;
}
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
return cb();
} else if (file.isStream()) {
cb();
} else if (file.isBuffer()) {
var res = [],
regex = /([^'"# \(\)\?]+\.(extList))\b/ig;
var srcString = file.contents,foundString,result;
if (config.tags) {
config.tags.forEach(function(tag) {
var tagMatch = /<!-- start:key -->([\s\S]*?)<!-- end:key -->/gmi;
var match = new RegExp(tagMatch.source.replace('key', tag), 'gi');
match = new RegExp(match.source.replace('key', tag), 'gi');
while ((result = match.exec(srcString))) {
foundString += result[1];
}
});
srcString = foundString;
}
var match = new RegExp(regex.source.replace('extList', config.extensions), 'ig');
while ((result = match.exec(srcString))) {
if (!path.isAbsolute(result[1])) {
result[1] = path.join(file.base, result[1]);
}
if (fs.existsSync(result[1])) {
var asset = new util.File({
path: result[1],
base: path.parse(result[1]).dir,
contents: fs.readFileSync(result[1])
});
this.push(asset);
}
}
cb();
}
});
}