-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (52 loc) · 1.54 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
62
63
64
65
66
67
'use strict';
var stylus = require('stylus'),
gutil = require('gulp-util'),
PluginError = gutil.PluginError,
log = gutil.log,
Transform = require('stream').Transform,
defaults = require('lodash.defaults'),
PLUGIN_NAME = 'gulp-stylus';
function formatException(e) {
return e.name+' in plugin \''+gutil.colors.cyan(PLUGIN_NAME)+'\''+': '+e.message
}
function gulpstylus(options) {
var stream = new Transform({ objectMode: true }),
opts = defaults({}, options ,{
compress: false,
silent: false
});
stream._transform = function(file, unused, done) {
if (file.isNull()) {
stream.push(file);
return done();
}
if (file.isStream()) {
return done(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
var s = stylus(file.contents.toString('utf8')).set('filename', file.path);
if (opts.set) {
var key, _ref = opts.set;
for (key in _ref) {
s.set(key, _ref[key]);
}
}
try {
var css = s.render();
file.path = gutil.replaceExtension(file.path, '.css');
file.contents = new Buffer(css);
stream.push(file);
done();
} catch (e) {
if (opts.silent) {
log(formatException(e));
file.path = gutil.replaceExtension(file.path, '.css');
file.contents = new Buffer('');
stream.push(file);
return done();
}
done(new PluginError(PLUGIN_NAME, e));
}
};
return stream;
}
module.exports = gulpstylus;