Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (63 loc) · 1.88 KB
/
Copy pathindex.js
File metadata and controls
79 lines (63 loc) · 1.88 KB
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
import { validateOptions } from './lib/validateOptions.js';
import { isString } from './lib/isString.js';
import { getContainingNode } from './lib/getContainingNode.js';
import { sortNode } from './lib/order/sortNode.js';
import { sortNodeProperties } from './lib/properties-order/sortNodeProperties.js';
/** @import { Plugin, Root } from 'postcss' */
/** @import { PluginOptions } from './lib/types.js' */
/**
* @param {PluginOptions} [opts]
* @returns {Plugin}
*/
function postcssSorting(opts) {
return {
postcssPlugin: 'postcss-sorting',
Root(css) {
plugin(css, opts);
},
};
}
postcssSorting.postcss = true;
export default postcssSorting;
/**
* @param {Root} css
* @param {PluginOptions} [opts]
*/
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;
}
}
const order = opts?.order;
const propertiesOrder = opts?.['properties-order'];
if (!order && !propertiesOrder) {
return;
}
const unspecifiedPropertiesPosition = opts?.['unspecified-properties-position'] || 'bottom';
css.walk((input) => {
const node = getContainingNode(input);
if (order) {
sortNode(node, order);
}
if (propertiesOrder) {
sortNodeProperties(node, {
order: propertiesOrder,
unspecifiedPropertiesPosition,
});
}
});
}
/** These are not a public API, and could be removed at any point */
export { sortNode, sortNodeProperties }; // eslint-disable-line unicorn/prefer-export-from