-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
80 lines (67 loc) · 1.88 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict'
const assign = require('object-assign')
const Filter = require('broccoli-persistent-filter')
const Funnel = require('broccoli-funnel')
const postcss = require('postcss')
module.exports = class extends Filter {
constructor (inputNode, options = {}) {
if (options.exclude || options.include) {
inputNode = new Funnel(inputNode, {
exclude: options.exclude,
include: options.include
})
}
super(inputNode, options)
this.inputNode = inputNode
this.options = options
this.warningStream = process.stderr
}
get extensions () {
return ['css']
}
get targetExtension () {
return 'css'
}
processString (content, relativePath) {
const warningStream = this.warningStream
const processor = postcss()
const opts = assign({
from: relativePath,
to: relativePath,
map: {
inline: false,
annotation: false
},
errors: {
showSourceCode: true,
terminalColors: true
}
}, this.options)
if (!opts.plugins || opts.plugins.length < 1) {
throw new Error('You must provide at least 1 plugin in the plugin array')
}
opts.plugins.forEach((plugin) => {
let pluginInstance
if (plugin.module) {
const pluginOptions = assign(opts, plugin.options || {})
pluginInstance = plugin.module(pluginOptions)
} else {
pluginInstance = plugin
}
processor.use(pluginInstance)
})
return processor.process(content, opts)
.then((result) => {
result.warnings().forEach(warn => warningStream.write(warn.toString()))
return result.css
})
.catch((err) => {
if (err.name === 'CssSyntaxError') {
if (opts.errors.showSourceCode) {
err.message += `\n${err.showSourceCode(opts.errors.terminalColors)}`
}
}
throw err
})
}
}