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