-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
40 lines (28 loc) · 1023 Bytes
/
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
'use strict';
const PLUGIN_NAME = 'gulp-sass-variables';
const PluginError = require('plugin-error');
const through = require('through2');
const getVariablesBuffer = function(sassVariables, file) {
let str = '';
for(let variable in sassVariables) {
str += variable + ': ' + JSON.stringify(sassVariables[variable]) + ';\n';
}
return new Buffer(str, file);
}
module.exports = function(sassVariables) {
return through.obj(function (file, encoding, cb) {
if(file.isNull()) {
return cb(null, file);
}
if(file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
if(sassVariables && typeof sassVariables === 'object') {
let variablesBuffer = getVariablesBuffer(sassVariables, file);
file.contents = Buffer.concat([variablesBuffer, file.contents], variablesBuffer.length + file.contents.length);
} else {
return cb(new PluginError(PLUGIN_NAME, 'Variables object expected'));
}
return cb(null, file);
});
};