forked from jonathantneal/media-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostcss.config.js
99 lines (89 loc) · 2.11 KB
/
postcss.config.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module.exports = ctx => ({
map: !process.argv.includes('--browser'),
plugins: [
// future compatibility
require('postcss-preset-env')({
features: {
'color-mod-function': { unresolved: 'warn' },
'custom-properties': { preserve: false }
},
stage: 0
})
].concat(
// neatness and compression
process.argv.includes('--browser') || process.argv.includes('--gh-pages') ? [
require('cssnano')({
normalizeUrl: false,
preset: ['default', {
normalizeUrl: false
}]
}),
compress()
] : [
require('postcss-discard-comments')(),
require('postcss-discard-duplicates')(),
require('postcss-discard-overridden')(),
require('postcss-merge-rules')(),
require('postcss-calc')(),
compress(),
prettier()
]
)
});
// tooling
const postcss = require('postcss');
// plugin
const compress = postcss.plugin('postcss-discard-tested-duplicate-declarations', (opts) => (root) => {
const testProp = opts && 'testProp' in opts ? opts.testProp : (prop) => !/^:*-/.test(prop);
const testValue = opts && 'testValue' in opts ? opts.testValue : (value) => !/(^var|^\s*-|\s+-\w+-)/.test(value);
root.walkRules((rule) => {
var propsMap = {};
rule.nodes.slice(0).forEach((decl) => {
if (testProp(decl.prop) && testValue(decl.value)) {
const prevDecl = propsMap[decl.prop];
if (prevDecl) {
if (testValue(prevDecl.value)) {
if (decl.import || !prevDecl.import) {
prevDecl.remove();
propsMap[decl.prop] = decl;
} else {
decl.remove();
}
}
} else {
propsMap[decl.prop] = decl;
}
}
})
});
});
// plugin
const prettier = postcss.plugin('postcss-prettier', () => root => {
const raws = {
decl: {
before: '\n\t',
between: ': '
},
rule: {
before: '\n\n',
between: ' ',
semicolon: true,
after: '\n'
}
};
root.walk(node => {
node.raws = Object.assign({}, raws[node.type]);
if (node.type === 'rule') {
if (node.parent.first === node) {
node.raws.before = '';
}
node.nodes = node.nodes.sort(
(a, b) => a.prop < b.prop
? -1
: a.prop > b.prop
? 1
: 0
);
}
});
});