postcss-discard-unused 5.0.1 → 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 CHANGED
@@ -1,18 +1,14 @@
1
1
  {
2
2
  "name": "postcss-discard-unused",
3
- "version": "5.0.1",
3
+ "version": "5.1.0",
4
4
  "description": "Discard unused counter styles, keyframes and fonts.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
+ "types": "types/index.d.ts",
6
7
  "files": [
7
- "bin",
8
8
  "LICENSE-MIT",
9
- "dist"
9
+ "src",
10
+ "types"
10
11
  ],
11
- "scripts": {
12
- "prebuild": "del-cli dist",
13
- "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\"",
14
- "prepublish": "yarn build"
15
- },
16
12
  "keywords": [
17
13
  "css",
18
14
  "minify",
@@ -44,5 +40,5 @@
44
40
  "peerDependencies": {
45
41
  "postcss": "^8.2.15"
46
42
  },
47
- "gitHead": "28c247175032fa03f04911cde56ad82d74d211cc"
48
- }
43
+ "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"
44
+ }
package/src/index.js ADDED
@@ -0,0 +1,213 @@
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
+ /** @type {string[]} */
15
+ let result = [];
16
+ for (const val of comma(value)) {
17
+ result = result.concat(space(val));
18
+ }
19
+ return result;
20
+ }
21
+
22
+ /**
23
+ * @param {{atRules: import('postcss').AtRule[], values: string[]}} arg
24
+ * @return {void}
25
+ */
26
+ function filterAtRule({ atRules, values }) {
27
+ const uniqueValues = new Set(values);
28
+ atRules.forEach((node) => {
29
+ const hasAtRule = uniqueValues.has(node.params);
30
+
31
+ if (!hasAtRule) {
32
+ node.remove();
33
+ }
34
+ });
35
+ }
36
+
37
+ /**
38
+ * @param {{atRules: import('postcss').AtRule[], rules: (string | true)[]}} arg
39
+ * @return {void}
40
+ */
41
+ function filterNamespace({ atRules, rules }) {
42
+ const uniqueRules = new Set(rules);
43
+ atRules.forEach((atRule) => {
44
+ const { 0: param, length: len } = atRule.params.split(' ').filter(Boolean);
45
+
46
+ if (len === 1) {
47
+ return;
48
+ }
49
+
50
+ const hasRule = uniqueRules.has(param) || uniqueRules.has('*');
51
+
52
+ if (!hasRule) {
53
+ atRule.remove();
54
+ }
55
+ });
56
+ }
57
+
58
+ /**
59
+ * @param {string} fontFamily
60
+ * @param {string[]} cache
61
+ * @param {(input: string) => string[]} comma
62
+ * @return {boolean}
63
+ */
64
+ function hasFont(fontFamily, cache, comma) {
65
+ return comma(fontFamily).some((font) => cache.some((c) => c.includes(font)));
66
+ }
67
+
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
+ */
75
+ function filterFont({ atRules, values }, comma) {
76
+ values = [...new Set(values)];
77
+ atRules.forEach((r) => {
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
+ );
84
+
85
+ // Discard the @font-face if it has no font-family
86
+ if (!families.length) {
87
+ return r.remove();
88
+ }
89
+
90
+ families.forEach((family) => {
91
+ if (!hasFont(family.value.toLowerCase(), values, comma)) {
92
+ r.remove();
93
+ }
94
+ });
95
+ });
96
+ }
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
+ */
104
+ function pluginCreator(opts) {
105
+ const { fontFace, counterStyle, keyframes, namespace } = Object.assign(
106
+ {},
107
+ {
108
+ fontFace: true,
109
+ counterStyle: true,
110
+ keyframes: true,
111
+ namespace: true,
112
+ },
113
+ opts
114
+ );
115
+
116
+ return {
117
+ postcssPlugin: 'postcss-discard-unused',
118
+
119
+ prepare() {
120
+ /** @type {{atRules: import('postcss').AtRule[], values: string[]}} */
121
+ const counterStyleCache = { atRules: [], values: [] };
122
+ /** @type {{atRules: import('postcss').AtRule[], values: string[]}} */
123
+ const keyframesCache = { atRules: [], values: [] };
124
+ /** @type {{atRules: import('postcss').AtRule[], rules: (string | true)[]}} */
125
+ const namespaceCache = { atRules: [], rules: [] };
126
+ /** @type {{atRules: import('postcss').AtRule[], values: string[]}} */
127
+ const fontCache = { atRules: [], values: [] };
128
+
129
+ return {
130
+ OnceExit(css, { list }) {
131
+ const { comma, space } = list;
132
+ css.walk((node) => {
133
+ const { type } = node;
134
+
135
+ if (type === rule && namespace && node.selector.includes('|')) {
136
+ if (node.selector.includes('[')) {
137
+ // Attribute selector, so we should parse further.
138
+ selectorParser((ast) => {
139
+ ast.walkAttributes(({ namespace: ns }) => {
140
+ namespaceCache.rules = namespaceCache.rules.concat(ns);
141
+ });
142
+ }).process(node.selector);
143
+ } else {
144
+ // Use a simple split function for the namespace
145
+ namespaceCache.rules = namespaceCache.rules.concat(
146
+ node.selector.split('|')[0]
147
+ );
148
+ }
149
+ return;
150
+ }
151
+
152
+ if (type === decl) {
153
+ const { prop } = node;
154
+ if (counterStyle && /list-style|system/.test(prop)) {
155
+ counterStyleCache.values = counterStyleCache.values.concat(
156
+ splitValues(node, comma, space)
157
+ );
158
+ }
159
+
160
+ if (
161
+ fontFace &&
162
+ node.parent !== undefined &&
163
+ node.parent.type === rule &&
164
+ /font(|-family)/.test(prop)
165
+ ) {
166
+ fontCache.values = fontCache.values.concat(
167
+ comma(node.value.toLowerCase())
168
+ );
169
+ }
170
+
171
+ if (keyframes && /animation/.test(prop)) {
172
+ keyframesCache.values = keyframesCache.values.concat(
173
+ splitValues(node, comma, space)
174
+ );
175
+ }
176
+
177
+ return;
178
+ }
179
+
180
+ if (type === atrule) {
181
+ const { name } = node;
182
+ if (counterStyle && /counter-style/.test(name)) {
183
+ counterStyleCache.atRules.push(node);
184
+ }
185
+
186
+ if (fontFace && name === 'font-face' && node.nodes) {
187
+ fontCache.atRules.push(node);
188
+ }
189
+
190
+ if (keyframes && /keyframes/.test(name)) {
191
+ keyframesCache.atRules.push(node);
192
+ }
193
+
194
+ if (namespace && name === 'namespace') {
195
+ namespaceCache.atRules.push(node);
196
+ }
197
+
198
+ return;
199
+ }
200
+ });
201
+
202
+ counterStyle && filterAtRule(counterStyleCache);
203
+ fontFace && filterFont(fontCache, comma);
204
+ keyframes && filterAtRule(keyframesCache);
205
+ namespace && filterNamespace(namespaceCache);
206
+ },
207
+ };
208
+ },
209
+ };
210
+ }
211
+
212
+ pluginCreator.postcss = true;
213
+ module.exports = pluginCreator;
@@ -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;
package/CHANGELOG.md DELETED
@@ -1,63 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- ## [5.0.1](https://github.com/cssnano/cssnano/compare/postcss-discard-unused@5.0.0...postcss-discard-unused@5.0.1) (2021-05-19)
7
-
8
- **Note:** Version bump only for package postcss-discard-unused
9
-
10
-
11
-
12
-
13
-
14
- # [5.0.0](https://github.com/cssnano/cssnano/compare/postcss-discard-unused@5.0.0-rc.2...postcss-discard-unused@5.0.0) (2021-04-06)
15
-
16
- **Note:** Version bump only for package postcss-discard-unused
17
-
18
-
19
-
20
-
21
-
22
- # [5.0.0-rc.2](https://github.com/cssnano/cssnano/compare/postcss-discard-unused@5.0.0-rc.1...postcss-discard-unused@5.0.0-rc.2) (2021-03-15)
23
-
24
- **Note:** Version bump only for package postcss-discard-unused
25
-
26
-
27
-
28
-
29
-
30
- # [5.0.0-rc.1](https://github.com/cssnano/cssnano/compare/postcss-discard-unused@5.0.0-rc.0...postcss-discard-unused@5.0.0-rc.1) (2021-03-04)
31
-
32
- **Note:** Version bump only for package postcss-discard-unused
33
-
34
-
35
-
36
-
37
-
38
- # 5.0.0-rc.0 (2021-02-19)
39
-
40
-
41
- ### chore
42
-
43
- * minimum require version of node is 10.13 ([#871](https://github.com/cssnano/cssnano/issues/871)) ([28bda24](https://github.com/cssnano/cssnano/commit/28bda243e32ce3ba89b3c358a5f78727b3732f11))
44
-
45
-
46
- ### Features
47
-
48
- * migarete to PostCSS 8 ([#975](https://github.com/cssnano/cssnano/issues/975)) ([40b82dc](https://github.com/cssnano/cssnano/commit/40b82dca7f53ac02cd4fe62846dec79b898ccb49))
49
-
50
-
51
- ### BREAKING CHANGES
52
-
53
- * minimum supported `postcss` version is `8.2.1`
54
- * minimum require version of node is 10.13
55
-
56
-
57
-
58
- ## 4.1.1 (2018-09-24)
59
-
60
-
61
- ### Bug Fixes
62
-
63
- * **postcss-merge-longhand:** not mangle border output ([#555](https://github.com/cssnano/cssnano/issues/555)) ([9a70605](https://github.com/cssnano/cssnano/commit/9a706050b621e7795a9bf74eb7110b5c81804ffe)), closes [#553](https://github.com/cssnano/cssnano/issues/553) [#554](https://github.com/cssnano/cssnano/issues/554)
package/dist/index.js DELETED
@@ -1,209 +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 uniqs(items) {
17
- return items.filter(function (item, i) {
18
- return i === items.indexOf(item);
19
- });
20
- }
21
-
22
- function addValues(cache, {
23
- value
24
- }, comma, space) {
25
- return comma(value).reduce((memo, val) => [...memo, ...space(val)], cache);
26
- }
27
-
28
- function filterAtRule({
29
- atRules,
30
- values
31
- }) {
32
- values = uniqs(values);
33
- atRules.forEach(node => {
34
- const hasAtRule = values.some(value => value === node.params);
35
-
36
- if (!hasAtRule) {
37
- node.remove();
38
- }
39
- });
40
- }
41
-
42
- function filterNamespace({
43
- atRules,
44
- rules
45
- }) {
46
- rules = uniqs(rules);
47
- atRules.forEach(atRule => {
48
- const {
49
- 0: param,
50
- length: len
51
- } = atRule.params.split(' ').filter(Boolean);
52
-
53
- if (len === 1) {
54
- return;
55
- }
56
-
57
- const hasRule = rules.some(r => r === param || r === '*');
58
-
59
- if (!hasRule) {
60
- atRule.remove();
61
- }
62
- });
63
- }
64
-
65
- function hasFont(fontFamily, cache, comma) {
66
- return comma(fontFamily).some(font => cache.some(c => ~c.indexOf(font)));
67
- } // fonts have slightly different logic
68
-
69
-
70
- function filterFont({
71
- atRules,
72
- values
73
- }, comma) {
74
- values = uniqs(values);
75
- atRules.forEach(r => {
76
- const families = r.nodes.filter(({
77
- prop
78
- }) => prop === 'font-family'); // Discard the @font-face if it has no font-family
79
-
80
- if (!families.length) {
81
- return r.remove();
82
- }
83
-
84
- families.forEach(family => {
85
- if (!hasFont(family.value.toLowerCase(), values, comma)) {
86
- r.remove();
87
- }
88
- });
89
- });
90
- }
91
-
92
- function pluginCreator(opts) {
93
- const {
94
- fontFace,
95
- counterStyle,
96
- keyframes,
97
- namespace
98
- } = Object.assign({}, {
99
- fontFace: true,
100
- counterStyle: true,
101
- keyframes: true,
102
- namespace: true
103
- }, opts);
104
- return {
105
- postcssPlugin: 'postcss-discard-unused',
106
-
107
- prepare() {
108
- const counterStyleCache = {
109
- atRules: [],
110
- values: []
111
- };
112
- const keyframesCache = {
113
- atRules: [],
114
- values: []
115
- };
116
- const namespaceCache = {
117
- atRules: [],
118
- rules: []
119
- };
120
- const fontCache = {
121
- atRules: [],
122
- values: []
123
- };
124
- return {
125
- OnceExit(css, {
126
- list
127
- }) {
128
- const {
129
- comma,
130
- space
131
- } = list;
132
- css.walk(node => {
133
- const {
134
- type,
135
- prop,
136
- selector,
137
- name
138
- } = node;
139
-
140
- if (type === rule && namespace && ~selector.indexOf('|')) {
141
- if (~selector.indexOf('[')) {
142
- // Attribute selector, so we should parse further.
143
- (0, _postcssSelectorParser.default)(ast => {
144
- ast.walkAttributes(({
145
- namespace: ns
146
- }) => {
147
- namespaceCache.rules = namespaceCache.rules.concat(ns);
148
- });
149
- }).process(selector);
150
- } else {
151
- // Use a simple split function for the namespace
152
- namespaceCache.rules = namespaceCache.rules.concat(selector.split('|')[0]);
153
- }
154
-
155
- return;
156
- }
157
-
158
- if (type === decl) {
159
- if (counterStyle && /list-style|system/.test(prop)) {
160
- counterStyleCache.values = addValues(counterStyleCache.values, node, comma, space);
161
- }
162
-
163
- if (fontFace && node.parent.type === rule && /font(|-family)/.test(prop)) {
164
- fontCache.values = fontCache.values.concat(comma(node.value.toLowerCase()));
165
- }
166
-
167
- if (keyframes && /animation/.test(prop)) {
168
- keyframesCache.values = addValues(keyframesCache.values, node, comma, space);
169
- }
170
-
171
- return;
172
- }
173
-
174
- if (type === atrule) {
175
- if (counterStyle && /counter-style/.test(name)) {
176
- counterStyleCache.atRules.push(node);
177
- }
178
-
179
- if (fontFace && name === 'font-face' && node.nodes) {
180
- fontCache.atRules.push(node);
181
- }
182
-
183
- if (keyframes && /keyframes/.test(name)) {
184
- keyframesCache.atRules.push(node);
185
- }
186
-
187
- if (namespace && name === 'namespace') {
188
- namespaceCache.atRules.push(node);
189
- }
190
-
191
- return;
192
- }
193
- });
194
- counterStyle && filterAtRule(counterStyleCache);
195
- fontFace && filterFont(fontCache, comma);
196
- keyframes && filterAtRule(keyframesCache);
197
- namespace && filterNamespace(namespaceCache);
198
- }
199
-
200
- };
201
- }
202
-
203
- };
204
- }
205
-
206
- pluginCreator.postcss = true;
207
- var _default = pluginCreator;
208
- exports.default = _default;
209
- module.exports = exports.default;