1 | const fs = require('fs')
|
2 | const path = require('path')
|
3 | const stylus = require('stylus')
|
4 |
|
5 | module.exports = function compileStylus (content, file, config) {
|
6 | return new Promise((resolve, reject) => {
|
7 | if (!content && !fs.existsSync(file)) {
|
8 | return resolve({
|
9 | css: ''
|
10 | })
|
11 | }
|
12 | if (!content) {
|
13 | content = fs.readFileSync(file).toString()
|
14 | }
|
15 | const opath = path.parse(file)
|
16 | config.paths = [opath.dir].concat(config.paths || [])
|
17 | config.filename = opath.base
|
18 | const instance = stylus(content, { filename: file })
|
19 | for (const k in config) {
|
20 | instance.set(k, config[k])
|
21 | }
|
22 | let imports = instance.deps()
|
23 | instance.render((err, css) => {
|
24 | if (err) {
|
25 | return reject(err)
|
26 | }
|
27 | resolve({
|
28 | css,
|
29 | imports
|
30 | })
|
31 | })
|
32 | })
|
33 | }
|