-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (134 loc) · 4.13 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
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
142
143
144
145
146
147
148
149
150
151
152
153
'use strict'
const path = require('path')
const fs = require('fs')
const stylus = require('stylus')
const clone = require('clone')
exports.name = 'stylus'
exports.inputFormats = ['styl', 'stylus']
exports.outputFormat = 'css'
/**
* Retrieves a Stylus renderer from the given options.
*/
function getRenderer(str, options, locals) {
const renderer = stylus(str)
// Special handling for stylus js api functions
// given { define: { foo: 'bar', baz: 'quux' } }
// runs renderer.define('foo', 'bar').define('baz', 'quux')
const allowed = ['set', 'define']
const special = {}
const allowedSingle = ['include', 'import', 'use']
const specialSingle = {}
const normal = clone(options)
for (const option in options) {
// If the option is a special option
if (allowed.includes(options)) {
// Add it to the special object
special[option] = options[option]
// Remove it from the options passed to stylus
delete normal[option]
} else if (allowedSingle.includes(option)) {
// If the option is a specialSingle option
// Add it to the specialSingle object
specialSingle[option] = options[option]
// Remove it from the options passed to stylus
delete normal[option]
}
}
// Special options through their function names
// Each method (i.e. `set` or `define`)
for (const method in special) {
if ({}.hasOwnProperty.call(special, method)) {
// Each variable in the method
for (const variable in special[method]) {
if ({}.hasOwnProperty.call(special[method], variable)) {
// Set it using stylus
// Example: stylus.set(variable, value)
renderer[method](variable, special[method][variable])
}
}
}
}
// Special options with single parameter through their function names
// Each method (i.e. `use`, `import`, or `include`)
for (const method in specialSingle) {
if ({}.hasOwnProperty.call(specialSingle, method)) {
let imports = []
// If it is an array (typeof array is object)
if (typeof specialSingle[method] === 'object') {
imports = specialSingle[method]
} else {
// Make it an array
imports = [specialSingle[method]]
}
for (const i in imports) {
if ({}.hasOwnProperty.call(imports, i)) {
let fn = imports[i]
// If it's a string, require it
if (typeof fn === 'string') {
fn = require(fn)()
}
// Otherwise use it as-is, like stylus.include(fn)
renderer[method](fn)
}
}
}
}
// Normal options through set()
for (const key in normal) {
if ({}.hasOwnProperty.call(normal, key)) {
renderer.set(key, normal[key])
}
}
// Register locals as defines.
for (const key in (locals || {})) {
if ({}.hasOwnProperty.call((locals || {}), key)) {
renderer.define(key, locals[key])
}
}
return renderer
}
exports.render = function (str, options, locals) {
let result
getRenderer(str, options, locals).render((err, res) => {
if (err) {
throw err
}
// @TODO How do we know what the dependencies are?
// @TODO This is not guarenteed to work in the callback chain.
result = res
})
if (!result) {
throw new Error('stylus compilation could not complete synchronously.')
}
return result
}
exports.renderFile = function (filename, options, locals) {
options = options || {}
options.filename = path.resolve(filename)
const str = fs.readFileSync(filename, 'utf8')
return exports.render(str, options, locals)
}
exports.renderAsync = function (str, options, locals) {
return new Promise((resolve, reject) => {
getRenderer(str, options, locals).render((err, res) => {
if (err) {
reject(err)
} else {
resolve(res)
}
})
})
}
exports.renderFileAsync = function (filename, options, locals) {
options = options || {}
options.filename = path.resolve(filename)
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf-8', (err, str) => {
if (err) {
reject(err)
} else {
resolve(exports.renderAsync(str, options, locals))
}
})
})
}