-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
58 lines (47 loc) · 1.39 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
const validateOptions = require('./lib/validateOptions');
const isString = require('./lib/isString');
const getContainingNode = require('./lib/getContainingNode');
const sortNode = require('./lib/order/sortNode');
const sortNodeProperties = require('./lib/properties-order/sortNodeProperties');
module.exports = (opts) => {
return {
postcssPlugin: 'postcss-sorting',
Root(css) {
plugin(css, opts);
},
};
};
module.exports.postcss = true;
function plugin(css, opts) {
const validatedOptions = validateOptions(opts);
if (validatedOptions !== true) {
const throwValidateErrors = (opts && opts['throw-validate-errors']) || false;
if (throwValidateErrors) {
if (isString(validatedOptions)) {
throw new Error(validatedOptions);
}
throw new Error(`postcss-sorting: Invalid config.`);
} else {
// eslint-disable-next-line no-console
if (console && console.warn && isString(validatedOptions)) {
console.warn(validatedOptions); // eslint-disable-line no-console
}
return;
}
}
if (opts.order) {
css.walk((input) => {
const node = getContainingNode(input);
sortNode(node, opts.order);
});
}
if (opts['properties-order']) {
css.walk((input) => {
const node = getContainingNode(input);
sortNodeProperties(node, {
order: opts['properties-order'],
unspecifiedPropertiesPosition: opts['unspecified-properties-position'] || 'bottom',
});
});
}
}