-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
66 lines (56 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const autoprefixer = require('autoprefixer')
const postcss = require('postcss')
const map = require('multi-stage-sourcemap')
const path = require('path')
/**
* Returns a stylus function that autoprefixes css.
*
* - Grabs any arguments passed, filters down to only string
* args, as anything other than a string crashes autoprefixer
* - Returns a function for stylus to use
* - This function uses the `end` event, and runs autoprefixer on
* the css, applying any arguments if present
*
* @return {Function} - stylus plugin function
*/
module.exports = function(opts = {}) {
// pull `hideWarnings` out so we can pass opts to autoprefixer
const showWarnings = !opts.hideWarnings
delete opts.hideWarnings
return function(style) {
style = this || style
const filename = style.options.filename
style.on('end', function(err, css) {
if (err) throw new Error(err)
// configure the options to be passed to autoprefixer
const processOpts = {
from: filename,
to:
path.join(
path.dirname(filename),
path.basename(filename, path.extname(filename))
) + '.css'
}
// if there is a stylus sourcemap, ensure autoprefixer also generates one
if (style.sourcemap) {
processOpts.map = { annotation: false }
}
// run autoprefixer
var res = postcss([autoprefixer(opts)]).process(css, processOpts)
// if sourcemaps are generated, combine the two
if (res.map && style.sourcemap) {
var combinedMap = map.transfer({
fromSourceMap: res.map.toString(),
toSourceMap: style.sourcemap
})
// then set the combined result as the new sourcemap
style.sourcemap = JSON.parse(combinedMap)
}
if (showWarnings) {
res.warnings().forEach(console.error)
}
// return the css output
return res.css
})
}
}