forked from DeathMark/gulp-scss-combine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (40 loc) · 1.12 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
'use strict';
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');
var throught = require('through2');
function scssCombine(content, baseDir) {
var regex = /@import[^'"]+?['"](.+?)['"];?/g;
if (regex.test(content)) {
content = content.replace(regex, function(m, capture) {
var parse = path.parse(path.resolve(baseDir, capture));
var file = parse.dir + '/_' + parse.name;
if (fs.existsSync(file + '.scss')) {
file = file + '.scss';
} else if (fs.existsSync(file + '.scss.liquid')) {
file = file + '.scss.liquid';
} else {
return '';
}
return fs.readFileSync(file);
});
}
return content;
}
module.exports = function(opts) {
return throught.obj(function(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-scss-combine', 'Streaming not supported'));
return;
}
if (path.basename(file.path).indexOf('_') === 0) {
return cb();
}
file.contents = new Buffer(scssCombine(file.contents.toString(), path.dirname(file.path)));
setImmediate(cb, null, file);
});
};