postcss-discard-unused 5.0.3 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -2
- package/src/index.js +46 -7
- package/types/index.d.ts +18 -0
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-discard-unused",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Discard unused counter styles, keyframes and fonts.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
6
7
|
"files": [
|
|
7
8
|
"LICENSE-MIT",
|
|
8
|
-
"src"
|
|
9
|
+
"src",
|
|
10
|
+
"types"
|
|
9
11
|
],
|
|
10
12
|
"keywords": [
|
|
11
13
|
"css",
|
package/src/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const rule = 'rule';
|
|
|
11
11
|
* @return {string[]}
|
|
12
12
|
*/
|
|
13
13
|
function splitValues({ value }, comma, space) {
|
|
14
|
+
/** @type {string[]} */
|
|
14
15
|
let result = [];
|
|
15
16
|
for (const val of comma(value)) {
|
|
16
17
|
result = result.concat(space(val));
|
|
@@ -18,6 +19,10 @@ function splitValues({ value }, comma, space) {
|
|
|
18
19
|
return result;
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
/**
|
|
23
|
+
* @param {{atRules: import('postcss').AtRule[], values: string[]}} arg
|
|
24
|
+
* @return {void}
|
|
25
|
+
*/
|
|
21
26
|
function filterAtRule({ atRules, values }) {
|
|
22
27
|
const uniqueValues = new Set(values);
|
|
23
28
|
atRules.forEach((node) => {
|
|
@@ -29,6 +34,10 @@ function filterAtRule({ atRules, values }) {
|
|
|
29
34
|
});
|
|
30
35
|
}
|
|
31
36
|
|
|
37
|
+
/**
|
|
38
|
+
* @param {{atRules: import('postcss').AtRule[], rules: (string | true)[]}} arg
|
|
39
|
+
* @return {void}
|
|
40
|
+
*/
|
|
32
41
|
function filterNamespace({ atRules, rules }) {
|
|
33
42
|
const uniqueRules = new Set(rules);
|
|
34
43
|
atRules.forEach((atRule) => {
|
|
@@ -46,15 +55,32 @@ function filterNamespace({ atRules, rules }) {
|
|
|
46
55
|
});
|
|
47
56
|
}
|
|
48
57
|
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} fontFamily
|
|
60
|
+
* @param {string[]} cache
|
|
61
|
+
* @param {(input: string) => string[]} comma
|
|
62
|
+
* @return {boolean}
|
|
63
|
+
*/
|
|
49
64
|
function hasFont(fontFamily, cache, comma) {
|
|
50
65
|
return comma(fontFamily).some((font) => cache.some((c) => c.includes(font)));
|
|
51
66
|
}
|
|
52
67
|
|
|
53
|
-
|
|
68
|
+
/**
|
|
69
|
+
* fonts have slightly different logic
|
|
70
|
+
|
|
71
|
+
* @param {{atRules: import('postcss').AtRule[], values: string[]}} cache
|
|
72
|
+
* @param {(input: string) => string[]} comma
|
|
73
|
+
* @return {void}
|
|
74
|
+
*/
|
|
54
75
|
function filterFont({ atRules, values }, comma) {
|
|
55
76
|
values = [...new Set(values)];
|
|
56
77
|
atRules.forEach((r) => {
|
|
57
|
-
|
|
78
|
+
/** @type {import('postcss').Declaration[]} */
|
|
79
|
+
const families = /** @type {import('postcss').Declaration[]} */ (
|
|
80
|
+
r.nodes.filter(
|
|
81
|
+
(node) => node.type === 'decl' && node.prop === 'font-family'
|
|
82
|
+
)
|
|
83
|
+
);
|
|
58
84
|
|
|
59
85
|
// Discard the @font-face if it has no font-family
|
|
60
86
|
if (!families.length) {
|
|
@@ -69,6 +95,12 @@ function filterFont({ atRules, values }, comma) {
|
|
|
69
95
|
});
|
|
70
96
|
}
|
|
71
97
|
|
|
98
|
+
/**@typedef {{fontFace?: boolean, counterStyle?: boolean, keyframes?: boolean, namespace?: boolean}} Options */
|
|
99
|
+
/**
|
|
100
|
+
* @type {import('postcss').PluginCreator<Options>}
|
|
101
|
+
* @param {Options} opts
|
|
102
|
+
* @return {import('postcss').Plugin}
|
|
103
|
+
*/
|
|
72
104
|
function pluginCreator(opts) {
|
|
73
105
|
const { fontFace, counterStyle, keyframes, namespace } = Object.assign(
|
|
74
106
|
{},
|
|
@@ -85,35 +117,40 @@ function pluginCreator(opts) {
|
|
|
85
117
|
postcssPlugin: 'postcss-discard-unused',
|
|
86
118
|
|
|
87
119
|
prepare() {
|
|
120
|
+
/** @type {{atRules: import('postcss').AtRule[], values: string[]}} */
|
|
88
121
|
const counterStyleCache = { atRules: [], values: [] };
|
|
122
|
+
/** @type {{atRules: import('postcss').AtRule[], values: string[]}} */
|
|
89
123
|
const keyframesCache = { atRules: [], values: [] };
|
|
124
|
+
/** @type {{atRules: import('postcss').AtRule[], rules: (string | true)[]}} */
|
|
90
125
|
const namespaceCache = { atRules: [], rules: [] };
|
|
126
|
+
/** @type {{atRules: import('postcss').AtRule[], values: string[]}} */
|
|
91
127
|
const fontCache = { atRules: [], values: [] };
|
|
92
128
|
|
|
93
129
|
return {
|
|
94
130
|
OnceExit(css, { list }) {
|
|
95
131
|
const { comma, space } = list;
|
|
96
132
|
css.walk((node) => {
|
|
97
|
-
const { type
|
|
133
|
+
const { type } = node;
|
|
98
134
|
|
|
99
|
-
if (type === rule && namespace && selector.includes('|')) {
|
|
100
|
-
if (selector.includes('[')) {
|
|
135
|
+
if (type === rule && namespace && node.selector.includes('|')) {
|
|
136
|
+
if (node.selector.includes('[')) {
|
|
101
137
|
// Attribute selector, so we should parse further.
|
|
102
138
|
selectorParser((ast) => {
|
|
103
139
|
ast.walkAttributes(({ namespace: ns }) => {
|
|
104
140
|
namespaceCache.rules = namespaceCache.rules.concat(ns);
|
|
105
141
|
});
|
|
106
|
-
}).process(selector);
|
|
142
|
+
}).process(node.selector);
|
|
107
143
|
} else {
|
|
108
144
|
// Use a simple split function for the namespace
|
|
109
145
|
namespaceCache.rules = namespaceCache.rules.concat(
|
|
110
|
-
selector.split('|')[0]
|
|
146
|
+
node.selector.split('|')[0]
|
|
111
147
|
);
|
|
112
148
|
}
|
|
113
149
|
return;
|
|
114
150
|
}
|
|
115
151
|
|
|
116
152
|
if (type === decl) {
|
|
153
|
+
const { prop } = node;
|
|
117
154
|
if (counterStyle && /list-style|system/.test(prop)) {
|
|
118
155
|
counterStyleCache.values = counterStyleCache.values.concat(
|
|
119
156
|
splitValues(node, comma, space)
|
|
@@ -122,6 +159,7 @@ function pluginCreator(opts) {
|
|
|
122
159
|
|
|
123
160
|
if (
|
|
124
161
|
fontFace &&
|
|
162
|
+
node.parent !== undefined &&
|
|
125
163
|
node.parent.type === rule &&
|
|
126
164
|
/font(|-family)/.test(prop)
|
|
127
165
|
) {
|
|
@@ -140,6 +178,7 @@ function pluginCreator(opts) {
|
|
|
140
178
|
}
|
|
141
179
|
|
|
142
180
|
if (type === atrule) {
|
|
181
|
+
const { name } = node;
|
|
143
182
|
if (counterStyle && /counter-style/.test(name)) {
|
|
144
183
|
counterStyleCache.atRules.push(node);
|
|
145
184
|
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export = pluginCreator;
|
|
2
|
+
/**@typedef {{fontFace?: boolean, counterStyle?: boolean, keyframes?: boolean, namespace?: boolean}} Options */
|
|
3
|
+
/**
|
|
4
|
+
* @type {import('postcss').PluginCreator<Options>}
|
|
5
|
+
* @param {Options} opts
|
|
6
|
+
* @return {import('postcss').Plugin}
|
|
7
|
+
*/
|
|
8
|
+
declare function pluginCreator(opts: Options): import('postcss').Plugin;
|
|
9
|
+
declare namespace pluginCreator {
|
|
10
|
+
export { postcss, Options };
|
|
11
|
+
}
|
|
12
|
+
type Options = {
|
|
13
|
+
fontFace?: boolean;
|
|
14
|
+
counterStyle?: boolean;
|
|
15
|
+
keyframes?: boolean;
|
|
16
|
+
namespace?: boolean;
|
|
17
|
+
};
|
|
18
|
+
declare var postcss: true;
|