1 | 'use strict'
|
2 |
|
3 | const join = require('path').join
|
4 | const readFileSync = require('fs').readFileSync
|
5 | const dirname = require('path').dirname
|
6 |
|
7 | module.exports = function buildCss (options, done) {
|
8 | buildStylus(join(__dirname, '../data/style.styl'), options, done)
|
9 | }
|
10 |
|
11 | function 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 | }
|