/
stylus.js
65 lines (45 loc) · 1.42 KB
/
stylus.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
// Stylus Renderer that understands `@[<pkgName>]/filename` imports.
//
'use strict';
var _ = require('lodash');
var path = require('path');
var stylus = require('stylus');
////////////////////////////////////////////////////////////////////////////////
// keep reference to original lookup function of stylus
var origFind = stylus.utils.find;
// resolves `<node_module>/path/name` pathnames
function resolvePath(file) {
file = String(file);
if (file[0] !== '.') {
try {
file = require.resolve(file);
} catch (err) {
// do nothing - stylus should report itself
}
}
return file;
}
////////////////////////////////////////////////////////////////////////////////
module.exports = function (data, filename) {
var renderer,
result,
imports = [];
renderer = stylus(data, {
paths: [ path.dirname(filename) ],
filename: filename,
_imports: imports,
'include css': true // include CSS on `@import`
});
// monkey-patch lookup with resolver
stylus.utils.find = function (lookupFile, lookupPaths, thisFilename) {
return origFind(resolvePath(lookupFile), lookupPaths, thisFilename);
};
// render stylus file and restore lookup function
result = renderer.render();
stylus.utils.find = origFind;
return {
css: result,
// array of imported file names, including current one
imports: _.map(imports, function (entry) { return entry.path; })
};
};