UNPKG

1.14 kBJavaScriptView Raw
1'use strict'
2
3const join = require('path').join
4const readFileSync = require('fs').readFileSync
5const dirname = require('path').dirname
6
7module.exports = function buildCss (options, done) {
8 buildStylus(join(__dirname, '../data/style.styl'), options, done)
9}
10
11function buildStylus (filepath, options, done) {
12 try {
13 const stylus = require('stylus')
14 const postcss = require('postcss')
15 const autoprefixer = require('autoprefixer')({})
16
17 let data = readFileSync(filepath, 'utf-8')
18
19 let s = stylus(data)
20 .set('filename', filepath)
21 .set('include css', true)
22 .set('hoist atrules', true)
23 .include(join(__dirname, '../node_modules'))
24
25 if (options && options.compress) {
26 s = s.set('compress', true)
27 }
28
29 if (options && options.imports) {
30 options.imports.forEach((external) => {
31 s = s
32 .import(external)
33 .include(dirname(external))
34 })
35 }
36
37 s.render((err, result) => {
38 if (err) return done(err)
39 let css = result
40 css = postcss([autoprefixer]).process(css).css
41 done(null, css)
42 })
43 } catch (err) {
44 done(err)
45 }
46}