postcss-discard-unused 5.0.2 → 5.0.3

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.
Files changed (3) hide show
  1. package/package.json +3 -8
  2. package/src/index.js +174 -0
  3. package/dist/index.js +0 -203
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "postcss-discard-unused",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "description": "Discard unused counter styles, keyframes and fonts.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
6
  "files": [
7
- "bin",
8
7
  "LICENSE-MIT",
9
- "dist"
8
+ "src"
10
9
  ],
11
10
  "keywords": [
12
11
  "css",
@@ -39,9 +38,5 @@
39
38
  "peerDependencies": {
40
39
  "postcss": "^8.2.15"
41
40
  },
42
- "scripts": {
43
- "prebuild": "rimraf dist",
44
- "build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
45
- },
46
41
  "readme": "# [postcss][postcss]-discard-unused\n\n> Discard unused counter styles, keyframes and fonts.\n\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-discard-unused) do:\n\n```\nnpm install postcss-discard-unused --save\n```\n\n\n## Example\n\nThis module will discard unused at rules in your CSS file, if it cannot find\nany selectors that make use of them. It works on `@counter-style`, `@keyframes`\nand `@font-face`.\n\n### Input\n\n```css\n@counter-style custom {\n system: extends decimal;\n suffix: \"> \"\n}\n\n@counter-style custom2 {\n system: extends decimal;\n suffix: \"| \"\n}\n\na {\n list-style: custom\n}\n```\n\n### Output\n\n```css\n@counter-style custom {\n system: extends decimal;\n suffix: \"> \"\n}\n\na {\n list-style: custom\n}\n```\n\nNote that this plugin is not responsible for normalising font families, as it\nmakes the assumption that you will write your font names consistently, such that\nit considers these two declarations differently:\n\n```css\nh1 {\n font-family: \"Helvetica Neue\"\n}\n\nh2 {\n font-family: Helvetica Neue\n}\n```\n\nHowever, you can mitigate this by including [postcss-minify-font-values][mfv]\n*before* this plugin, which will take care of normalising quotes, and\ndeduplicating. For more examples, see the [tests](src/__tests__/index.js).\n\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n\n## API\n\n### discardUnused([options])\n\n#### options\n\n##### fontFace\n\nType: `boolean` \nDefault: `true`\n\nPass `false` to disable discarding unused font face rules.\n\n##### counterStyle\n\nType: `boolean` \nDefault: `true`\n\nPass `false` to disable discarding unused counter style rules.\n\n##### keyframes\n\nType: `boolean` \nDefault: `true`\n\nPass `false` to disable discarding unused keyframe rules.\n\n##### namespace\n\nType: `boolean` \nDefault: `true`\n\nPass `false` to disable discarding unused namespace rules.\n\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n\n[postcss]: https://github.com/postcss/postcss\n[mfv]: https://github.com/trysound/postcss-minify-font-values\n"
47
42
  }
package/src/index.js ADDED
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+ const selectorParser = require('postcss-selector-parser');
3
+
4
+ const atrule = 'atrule';
5
+ const decl = 'decl';
6
+ const rule = 'rule';
7
+ /**
8
+ * @param {{value: string}} arg
9
+ * @param {(input: string) => string[]} comma
10
+ * @param {(input: string) => string[]} space
11
+ * @return {string[]}
12
+ */
13
+ function splitValues({ value }, comma, space) {
14
+ let result = [];
15
+ for (const val of comma(value)) {
16
+ result = result.concat(space(val));
17
+ }
18
+ return result;
19
+ }
20
+
21
+ function filterAtRule({ atRules, values }) {
22
+ const uniqueValues = new Set(values);
23
+ atRules.forEach((node) => {
24
+ const hasAtRule = uniqueValues.has(node.params);
25
+
26
+ if (!hasAtRule) {
27
+ node.remove();
28
+ }
29
+ });
30
+ }
31
+
32
+ function filterNamespace({ atRules, rules }) {
33
+ const uniqueRules = new Set(rules);
34
+ atRules.forEach((atRule) => {
35
+ const { 0: param, length: len } = atRule.params.split(' ').filter(Boolean);
36
+
37
+ if (len === 1) {
38
+ return;
39
+ }
40
+
41
+ const hasRule = uniqueRules.has(param) || uniqueRules.has('*');
42
+
43
+ if (!hasRule) {
44
+ atRule.remove();
45
+ }
46
+ });
47
+ }
48
+
49
+ function hasFont(fontFamily, cache, comma) {
50
+ return comma(fontFamily).some((font) => cache.some((c) => c.includes(font)));
51
+ }
52
+
53
+ // fonts have slightly different logic
54
+ function filterFont({ atRules, values }, comma) {
55
+ values = [...new Set(values)];
56
+ atRules.forEach((r) => {
57
+ const families = r.nodes.filter(({ prop }) => prop === 'font-family');
58
+
59
+ // Discard the @font-face if it has no font-family
60
+ if (!families.length) {
61
+ return r.remove();
62
+ }
63
+
64
+ families.forEach((family) => {
65
+ if (!hasFont(family.value.toLowerCase(), values, comma)) {
66
+ r.remove();
67
+ }
68
+ });
69
+ });
70
+ }
71
+
72
+ function pluginCreator(opts) {
73
+ const { fontFace, counterStyle, keyframes, namespace } = Object.assign(
74
+ {},
75
+ {
76
+ fontFace: true,
77
+ counterStyle: true,
78
+ keyframes: true,
79
+ namespace: true,
80
+ },
81
+ opts
82
+ );
83
+
84
+ return {
85
+ postcssPlugin: 'postcss-discard-unused',
86
+
87
+ prepare() {
88
+ const counterStyleCache = { atRules: [], values: [] };
89
+ const keyframesCache = { atRules: [], values: [] };
90
+ const namespaceCache = { atRules: [], rules: [] };
91
+ const fontCache = { atRules: [], values: [] };
92
+
93
+ return {
94
+ OnceExit(css, { list }) {
95
+ const { comma, space } = list;
96
+ css.walk((node) => {
97
+ const { type, prop, selector, name } = node;
98
+
99
+ if (type === rule && namespace && selector.includes('|')) {
100
+ if (selector.includes('[')) {
101
+ // Attribute selector, so we should parse further.
102
+ selectorParser((ast) => {
103
+ ast.walkAttributes(({ namespace: ns }) => {
104
+ namespaceCache.rules = namespaceCache.rules.concat(ns);
105
+ });
106
+ }).process(selector);
107
+ } else {
108
+ // Use a simple split function for the namespace
109
+ namespaceCache.rules = namespaceCache.rules.concat(
110
+ selector.split('|')[0]
111
+ );
112
+ }
113
+ return;
114
+ }
115
+
116
+ if (type === decl) {
117
+ if (counterStyle && /list-style|system/.test(prop)) {
118
+ counterStyleCache.values = counterStyleCache.values.concat(
119
+ splitValues(node, comma, space)
120
+ );
121
+ }
122
+
123
+ if (
124
+ fontFace &&
125
+ node.parent.type === rule &&
126
+ /font(|-family)/.test(prop)
127
+ ) {
128
+ fontCache.values = fontCache.values.concat(
129
+ comma(node.value.toLowerCase())
130
+ );
131
+ }
132
+
133
+ if (keyframes && /animation/.test(prop)) {
134
+ keyframesCache.values = keyframesCache.values.concat(
135
+ splitValues(node, comma, space)
136
+ );
137
+ }
138
+
139
+ return;
140
+ }
141
+
142
+ if (type === atrule) {
143
+ if (counterStyle && /counter-style/.test(name)) {
144
+ counterStyleCache.atRules.push(node);
145
+ }
146
+
147
+ if (fontFace && name === 'font-face' && node.nodes) {
148
+ fontCache.atRules.push(node);
149
+ }
150
+
151
+ if (keyframes && /keyframes/.test(name)) {
152
+ keyframesCache.atRules.push(node);
153
+ }
154
+
155
+ if (namespace && name === 'namespace') {
156
+ namespaceCache.atRules.push(node);
157
+ }
158
+
159
+ return;
160
+ }
161
+ });
162
+
163
+ counterStyle && filterAtRule(counterStyleCache);
164
+ fontFace && filterFont(fontCache, comma);
165
+ keyframes && filterAtRule(keyframesCache);
166
+ namespace && filterNamespace(namespaceCache);
167
+ },
168
+ };
169
+ },
170
+ };
171
+ }
172
+
173
+ pluginCreator.postcss = true;
174
+ module.exports = pluginCreator;
package/dist/index.js DELETED
@@ -1,203 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
- const atrule = 'atrule';
13
- const decl = 'decl';
14
- const rule = 'rule';
15
-
16
- function addValues(cache, {
17
- value
18
- }, comma, space) {
19
- return comma(value).reduce((memo, val) => [...memo, ...space(val)], cache);
20
- }
21
-
22
- function filterAtRule({
23
- atRules,
24
- values
25
- }) {
26
- values = new Set(values);
27
- atRules.forEach(node => {
28
- const hasAtRule = values.has(node.params);
29
-
30
- if (!hasAtRule) {
31
- node.remove();
32
- }
33
- });
34
- }
35
-
36
- function filterNamespace({
37
- atRules,
38
- rules
39
- }) {
40
- rules = new Set(rules);
41
- atRules.forEach(atRule => {
42
- const {
43
- 0: param,
44
- length: len
45
- } = atRule.params.split(' ').filter(Boolean);
46
-
47
- if (len === 1) {
48
- return;
49
- }
50
-
51
- const hasRule = rules.has(param) || rules.has('*');
52
-
53
- if (!hasRule) {
54
- atRule.remove();
55
- }
56
- });
57
- }
58
-
59
- function hasFont(fontFamily, cache, comma) {
60
- return comma(fontFamily).some(font => cache.some(c => c.includes(font)));
61
- } // fonts have slightly different logic
62
-
63
-
64
- function filterFont({
65
- atRules,
66
- values
67
- }, comma) {
68
- values = [...new Set(values)];
69
- atRules.forEach(r => {
70
- const families = r.nodes.filter(({
71
- prop
72
- }) => prop === 'font-family'); // Discard the @font-face if it has no font-family
73
-
74
- if (!families.length) {
75
- return r.remove();
76
- }
77
-
78
- families.forEach(family => {
79
- if (!hasFont(family.value.toLowerCase(), values, comma)) {
80
- r.remove();
81
- }
82
- });
83
- });
84
- }
85
-
86
- function pluginCreator(opts) {
87
- const {
88
- fontFace,
89
- counterStyle,
90
- keyframes,
91
- namespace
92
- } = Object.assign({}, {
93
- fontFace: true,
94
- counterStyle: true,
95
- keyframes: true,
96
- namespace: true
97
- }, opts);
98
- return {
99
- postcssPlugin: 'postcss-discard-unused',
100
-
101
- prepare() {
102
- const counterStyleCache = {
103
- atRules: [],
104
- values: []
105
- };
106
- const keyframesCache = {
107
- atRules: [],
108
- values: []
109
- };
110
- const namespaceCache = {
111
- atRules: [],
112
- rules: []
113
- };
114
- const fontCache = {
115
- atRules: [],
116
- values: []
117
- };
118
- return {
119
- OnceExit(css, {
120
- list
121
- }) {
122
- const {
123
- comma,
124
- space
125
- } = list;
126
- css.walk(node => {
127
- const {
128
- type,
129
- prop,
130
- selector,
131
- name
132
- } = node;
133
-
134
- if (type === rule && namespace && selector.includes('|')) {
135
- if (selector.includes('[')) {
136
- // Attribute selector, so we should parse further.
137
- (0, _postcssSelectorParser.default)(ast => {
138
- ast.walkAttributes(({
139
- namespace: ns
140
- }) => {
141
- namespaceCache.rules = namespaceCache.rules.concat(ns);
142
- });
143
- }).process(selector);
144
- } else {
145
- // Use a simple split function for the namespace
146
- namespaceCache.rules = namespaceCache.rules.concat(selector.split('|')[0]);
147
- }
148
-
149
- return;
150
- }
151
-
152
- if (type === decl) {
153
- if (counterStyle && /list-style|system/.test(prop)) {
154
- counterStyleCache.values = addValues(counterStyleCache.values, node, comma, space);
155
- }
156
-
157
- if (fontFace && node.parent.type === rule && /font(|-family)/.test(prop)) {
158
- fontCache.values = fontCache.values.concat(comma(node.value.toLowerCase()));
159
- }
160
-
161
- if (keyframes && /animation/.test(prop)) {
162
- keyframesCache.values = addValues(keyframesCache.values, node, comma, space);
163
- }
164
-
165
- return;
166
- }
167
-
168
- if (type === atrule) {
169
- if (counterStyle && /counter-style/.test(name)) {
170
- counterStyleCache.atRules.push(node);
171
- }
172
-
173
- if (fontFace && name === 'font-face' && node.nodes) {
174
- fontCache.atRules.push(node);
175
- }
176
-
177
- if (keyframes && /keyframes/.test(name)) {
178
- keyframesCache.atRules.push(node);
179
- }
180
-
181
- if (namespace && name === 'namespace') {
182
- namespaceCache.atRules.push(node);
183
- }
184
-
185
- return;
186
- }
187
- });
188
- counterStyle && filterAtRule(counterStyleCache);
189
- fontFace && filterFont(fontCache, comma);
190
- keyframes && filterAtRule(keyframesCache);
191
- namespace && filterNamespace(namespaceCache);
192
- }
193
-
194
- };
195
- }
196
-
197
- };
198
- }
199
-
200
- pluginCreator.postcss = true;
201
- var _default = pluginCreator;
202
- exports.default = _default;
203
- module.exports = exports.default;