/
stylus_engine.js
126 lines (93 loc) · 2.61 KB
/
stylus_engine.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* class StylusEngine
*
* Engine for the Stylus compiler. You will need `stylus` Node module installed
* in order to use [[Mincer]] with `*.stylus` files:
*
* npm install stylus
*
*
* ##### SUBCLASS OF
*
* [[Template]]
**/
'use strict';
// stdlib
var path = require('path');
// 3rd-party
var _ = require('lodash');
var stylus; // initialized later
// internal
var Template = require('../template');
var prop = require('../common').prop;
////////////////////////////////////////////////////////////////////////////////
// Class constructor
var StylusEngine = module.exports = function StylusEngine() {
Template.apply(this, arguments);
stylus = stylus || Template.libs.stylus || require('stylus');
};
require('util').inherits(StylusEngine, Template);
// Internal (private) configurator storage
var configurator = function noop() {};
/**
* StylusEngine.configure(fn) -> Void
* - fn (Function)
*
* Sets `function`, that will be running everytime engine will run renderer.
*
* var nib = require('nib');
*
* Stylus.configure(function (style) {
* style.use(nib());
* });
*
* Default: `function (_) { 'do nothing'; }`
**/
StylusEngine.configure = function (fn) {
configurator = fn;
};
// Render data
StylusEngine.prototype.evaluate = function (context, locals) {
var style = stylus(this.data, {
paths: [path.dirname(this.file)].concat(context.environment.paths),
filename: this.file,
_imports: []
});
var error = null,
result = null;
// define helpers from the list of passed locals
_.forEach(locals, function (func, name) {
// Stylus determine how much arguments to pass on helper function's length
// So we keep amount of arguments reasonable big...
style.define(name, function (a, b, c, d, e, f, g, h) {
/*jshint unused:false*/
var o = {};
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].forEach(function (k, i) {
o[k] = (this[i] || {}).val;
}, arguments);
if (!('context' in this)) {
this.context = context;
}
return func.call(this, o.a, o.b, o.c, o.d, o.e, o.f, o.g, o.h);
});
});
// run registered configurator
configurator(style);
style.render(function (err, css) {
if (err) {
error = err;
return;
}
// add Stylus `@import`s as dependencies of current asset
_.forEach(style.options._imports, function (imported) {
context.dependOn(imported.path);
});
result = css;
});
if(error){
throw error;
}
return result;
};
// Expose default MimeType of an engine
prop(StylusEngine, 'defaultMimeType', 'text/css');