forked from jeffjewiss/broccoli-postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (67 loc) · 2.02 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
81
82
83
84
'use strict'
const assign = require('object-assign')
const Filter = require('broccoli-persistent-filter')
const Funnel = require('broccoli-funnel')
const postcss = require('postcss')
PostcssFilter.prototype = Object.create(Filter.prototype)
PostcssFilter.prototype.constructor = PostcssFilter
PostcssFilter.prototype.extensions = ['css']
PostcssFilter.prototype.targetExtension = 'css'
function PostcssFilter (inputNode, _options) {
const options = _options || {}
if (!(this instanceof PostcssFilter)) {
return new PostcssFilter(inputNode, _options)
}
if (options.exclude || options.include) {
inputNode = new Funnel(inputNode, {
exclude: options.exclude,
include: options.include
})
}
Filter.call(this, inputNode, options)
this.options = options
this.warningStream = process.stderr
}
PostcssFilter.prototype.processString = function (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
})
}
module.exports = PostcssFilter