1 | 'use strict';
|
2 |
|
3 | var through = require('through2');
|
4 | var stylus = require('accord').load('stylus');
|
5 | var gutil = require('gulp-util');
|
6 | var rext = require('replace-ext');
|
7 | var path = require('path');
|
8 | var _ = require('lodash');
|
9 |
|
10 | var PLUGIN_NAME = 'gulp-stylus';
|
11 |
|
12 | module.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 | };
|