|
1 | | -const postcss = require('postcss'); |
2 | | -const postcssrc = require('postcss-load-config'); |
3 | | - |
4 | | -module.exports = function (plugins, options, filterType) { |
5 | | - if (arguments.length === 0) { |
6 | | - const rc = postcssrc.sync(); |
7 | | - plugins = rc.plugins; |
8 | | - options = rc.options; |
| 1 | +import postcss from 'postcss' |
| 2 | +import postcssConfig from 'postcss-load-config' |
| 3 | + |
| 4 | +const plugin = (plugins = null, options = null, filterType = null) => { |
| 5 | + if (plugins === null && options === null && filterType === null) { |
| 6 | + const rc = postcssConfig.sync() |
| 7 | + plugins = rc.plugins |
| 8 | + options = rc.options |
9 | 9 | } |
10 | 10 |
|
11 | | - plugins = [].concat(plugins).filter(Boolean); |
12 | | - options = options || {}; |
| 11 | + plugins = [].concat(plugins).filter(Boolean) |
| 12 | + options = options || {} |
13 | 13 |
|
14 | | - const css = postcss(plugins); |
| 14 | + const css = postcss(plugins) |
15 | 15 |
|
16 | 16 | return function (tree) { |
17 | | - const promises = []; |
| 17 | + const promises = [] |
18 | 18 |
|
19 | 19 | tree.walk(node => { |
20 | | - let promise; |
| 20 | + let promise |
21 | 21 |
|
22 | 22 | if (node.tag === 'style' && node.content) { |
23 | | - let meetsFilter = true; |
| 23 | + let meetsFilter = true |
| 24 | + |
24 | 25 | if (filterType) { |
25 | | - const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : ''; |
26 | | - const meetsTypeAttr = filterType.test(typeAttr); |
27 | | - const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === ''); |
28 | | - const meetsOtherType = !meetsStandardType && meetsTypeAttr; |
29 | | - meetsFilter = meetsStandardType || meetsOtherType; |
| 26 | + const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : '' |
| 27 | + const meetsTypeAttr = filterType.test(typeAttr) |
| 28 | + const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '') |
| 29 | + const meetsOtherType = !meetsStandardType && meetsTypeAttr |
| 30 | + meetsFilter = meetsStandardType || meetsOtherType |
30 | 31 | } |
31 | 32 |
|
32 | 33 | if (meetsFilter) { |
33 | | - const styles = [].concat(node.content).join(''); |
34 | | - const from = options.from || tree.options.from; |
| 34 | + const styles = [].concat(node.content).join('') |
| 35 | + const from = options.from || tree.options.from |
| 36 | + |
35 | 37 | promise = css.process(styles, {...options, from}) |
36 | 38 | .then(result => { |
37 | | - node.content = [result.css]; |
38 | | - }); |
| 39 | + node.content = [result.css] |
| 40 | + }) |
39 | 41 |
|
40 | | - promises.push(promise); |
| 42 | + promises.push(promise) |
41 | 43 | } |
42 | 44 | } |
43 | 45 |
|
44 | 46 | if (node.attrs && node.attrs.style) { |
45 | 47 | promise = css.process(node.attrs.style, options) |
46 | 48 | .then(result => { |
47 | | - node.attrs.style = result.css; |
48 | | - }); |
| 49 | + node.attrs.style = result.css |
| 50 | + }) |
49 | 51 |
|
50 | | - promises.push(promise); |
| 52 | + promises.push(promise) |
51 | 53 | } |
52 | 54 |
|
53 | | - return node; |
54 | | - }); |
| 55 | + return node |
| 56 | + }) |
55 | 57 |
|
56 | 58 | return Promise.all(promises).then(() => { |
57 | | - return tree; |
58 | | - }); |
59 | | - }; |
60 | | -}; |
| 59 | + return tree |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export default plugin |
0 commit comments