-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (49 loc) · 1.65 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
var stylus = require('stylus'),
path = require('path'),
fs = require('fs');
var createStylusPreprocessor = function (args, config, logger, helper) {
config = config || {};
var defaultOptions = {
compress: false,
save: false
};
var options = helper.merge(defaultOptions, args.options || {}, config.options || {});
var log = logger.create('preprocessor:stylus');
var transformPath = args.transformPath || config.transformPath || function (filePath) {
return filePath.replace(/\.styl$/, '.css');
};
var rendered = function (done, filePath, error, content) {
if (error !== null && error !== undefined) {
log.error(error.stack);
} else {
if (options.save) {
var p = path.resolve(filePath.replace(/\/([\.a-zA-Z0-9\-\_]+).css$/, '/'));
helper.mkdirIfNotExists(p, function () {
var n = filePath.match(/[a-zA-Z\-\.\_]+.css$/).reverse()[0];
fs.writeFile(path.join(p, n), content, 'utf-8', function (error) {
if (error) {
log.error("Error:%s", error);
}
done(content);
});
});
} else {
done(content);
}
}
};
return function (content, file, done) {
file.path = transformPath(file.originalPath);
try {
stylus.render(content, options, rendered.bind(null, done, file.path));
} catch (error) {
log.error('%s\n at %s', error.message, file.originalPath);
return;
}
};
};
createStylusPreprocessor.$inject = ['args', 'config.stylusPreprocessor', 'logger', 'helper'];
// PUBLISH DI MODULE
module.exports = {
'preprocessor:stylus': ['factory', createStylusPreprocessor]
};