-
Notifications
You must be signed in to change notification settings - Fork 2
/
lasso-stylus-plugin.js
141 lines (116 loc) · 3.97 KB
/
lasso-stylus-plugin.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var stylus = require('stylus');
var fs = require('fs');
var nodePath = require('path');
function handleIncludes(renderer, includes) {
if (includes) {
includes.forEach(function(include) {
renderer = renderer.include(include);
});
}
return renderer;
}
function handleDefines(renderer, defines) {
if (defines) {
Object.keys(defines).forEach(function(name) {
var fn = defines[name];
renderer = renderer.define(name, fn);
});
}
return renderer;
}
function handleOptions(renderer, options) {
renderer = handleIncludes(renderer, options.includes);
renderer = handleIncludes(renderer, options.paths);
var imports = options.imports;
if (imports) {
imports.forEach(function(imp) {
renderer = renderer.import(imp);
});
}
renderer = handleDefines(renderer, options.define);
renderer = handleDefines(renderer, options.defines);
var set = options.set;
if (set) {
Object.keys(set).forEach(function(setting) {
var value = set[setting];
renderer = renderer.set(setting, value);
});
}
var use = options.use;
if (use) {
if (Array.isArray(use)) {
use.forEach(function(use) {
renderer = renderer.use(use);
});
} else {
renderer = renderer.use(use);
}
}
return renderer;
}
module.exports = function(lasso, config) {
lasso.dependencies.registerStyleSheetType(
'styl',
{
properties: {
'path': 'string',
'paths': 'string',
'set': 'object',
'defines': 'object',
'includes': 'array',
'imports': 'array',
'use': 'array'
},
init: function(lassoContext, callback) {
if (!this.path) {
var error = new Error('"path" is required');
if (callback) return callback(error);
throw error;
}
var _this = this;
if (this.includes) {
this.includes = this.includes.map(function(path) {
return _this.resolvePath(path);
});
}
if (this.paths) {
this.paths = this.paths.map(function(path) {
return _this.resolvePath(path);
});
}
this.path = this.resolvePath(this.path);
if (callback) callback();
},
read: function(lassoContext, callback) {
return new Promise((resolve, reject) => {
callback = callback || function (err, res) {
return err ? reject(err) : resolve(res);
};
var path = this.path;
var _this = this;
fs.readFile(path, {encoding: 'utf8'}, function(err, stylusCode) {
if (err) {
return callback(err);
}
var renderer = stylus(stylusCode).set('filename', path);
renderer = handleOptions(renderer, config);
renderer = handleOptions(renderer, _this);
renderer.include(nodePath.dirname(path));
renderer.render(function(err, css) {
if (err) {
return callback(err);
}
callback(null, css);
});
});
});
},
getSourceFile: function() {
return this.path;
},
getLastModified: function(lassoContext, callback) {
return callback ? callback(null, -1) : -1;
}
}
);
};