-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
64 lines (52 loc) · 1.74 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
var postcss = require('postcss')
var postcssrc = require('postcss-load-config')
module.exports = function (plugins, options, filterType) {
if (Array.from(arguments).length === 0) {
try {
var rc = postcssrc.sync()
plugins = rc.plugins
options = rc.options
} catch (err) {
// noop
}
}
plugins = [].concat(plugins).filter(plugin => typeof plugin === 'function')
options = options || {}
var css = postcss(plugins)
return function posthtmlPostcss (tree) {
var promises = []
tree.walk(function (node) {
var promise
if (node.tag === 'style' && node.content) {
var meetsFilter = true
if (filterType) {
var typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : ''
var meetsTypeAttr = filterType.test(typeAttr)
var meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '')
var meetsOtherType = !meetsStandardType && meetsTypeAttr
meetsFilter = meetsStandardType || meetsOtherType
}
if (meetsFilter) {
var styles = [].concat(node.content).join('')
var from = options.from || tree.options.from
promise = css.process(styles, Object.assign({}, options, { from: from }))
.then(function (result) {
node.content = [result.css]
})
promises.push(promise)
}
}
if (node.attrs && node.attrs.style) {
promise = css.process(node.attrs.style, options)
.then(function (result) {
node.attrs.style = result.css
})
promises.push(promise)
}
return node
})
return Promise.all(promises).then(function () {
return tree
})
}
}