UNPKG

1.56 kBJavaScriptView Raw
1'use strict'
2
3const join = require('path').join
4const readFileSync = require('fs').readFileSync
5const dirname = require('path').dirname
6
7/*
8 * Builds the CSS files.
9 *
10 * This builds the CSS using Stylus and pipes it through postcss. It's roughly
11 * equivalent to:
12 *
13 * stylus -I node_modules input.css | postcss -u autoprefixer
14 *
15 * Calls `done(err, res)` when done, where `res` is the final CSS string.
16 *
17 * Available options:
18 *
19 * - `compress` (Boolean) - compresses if true
20 * - `import` (Array of strings) - adds files to be imported
21 */
22
23module.exports = function buildCss (options, done) {
24 buildStylus(join(__dirname, '../css/docpress.styl'), options, done)
25}
26
27function 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}