UNPKG

1.02 kBJavaScriptView Raw
1'use strict';
2
3var through = require('through2');
4var stylus = require('accord').load('stylus');
5var gutil = require('gulp-util');
6var rext = require('replace-ext');
7var path = require('path');
8var _ = require('lodash');
9
10var PLUGIN_NAME = 'gulp-stylus';
11
12module.exports = function (options) {
13 var opts = _.assign({}, options);
14
15 return through.obj(function (file, enc, cb) {
16
17 if (file.isStream()) {
18 return cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
19 }
20 if (file.isNull()){
21 return cb(null, file);
22 }
23 if (path.extname(file.path) === '.css'){
24 return cb(null, file);
25 }
26 opts.filename = file.path;
27
28 stylus.render(file.contents.toString('utf8'), opts)
29 .catch(function(err){
30 return cb(new gutil.PluginError(PLUGIN_NAME, err));
31 })
32 .then(function(css){
33 if (css !== undefined){
34 file.path = rext(file.path, '.css');
35 file.contents = new Buffer(css);
36 return cb(null, file);
37 }
38 });
39 });
40
41};