postcss-minify-params 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 +7 -9
- package/src/index.js +153 -0
- package/types/index.d.ts +11 -0
- package/dist/index.js +0 -119
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-minify-params",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Minify at-rule params with PostCSS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"postcss",
|
|
@@ -10,9 +10,12 @@
|
|
|
10
10
|
"optimise",
|
|
11
11
|
"params"
|
|
12
12
|
],
|
|
13
|
-
"main": "
|
|
13
|
+
"main": "src/index.js",
|
|
14
|
+
"types": "types/index.d.ts",
|
|
14
15
|
"files": [
|
|
15
|
-
"
|
|
16
|
+
"src",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"types"
|
|
16
19
|
],
|
|
17
20
|
"author": "Bogdan Chadkin <trysound@yandex.ru>",
|
|
18
21
|
"license": "MIT",
|
|
@@ -22,9 +25,8 @@
|
|
|
22
25
|
},
|
|
23
26
|
"homepage": "https://github.com/cssnano/cssnano",
|
|
24
27
|
"dependencies": {
|
|
25
|
-
"alphanum-sort": "^1.0.2",
|
|
26
28
|
"browserslist": "^4.16.6",
|
|
27
|
-
"cssnano-utils": "^3.
|
|
29
|
+
"cssnano-utils": "^3.1.0",
|
|
28
30
|
"postcss-value-parser": "^4.2.0"
|
|
29
31
|
},
|
|
30
32
|
"engines": {
|
|
@@ -36,9 +38,5 @@
|
|
|
36
38
|
"peerDependencies": {
|
|
37
39
|
"postcss": "^8.2.15"
|
|
38
40
|
},
|
|
39
|
-
"scripts": {
|
|
40
|
-
"prebuild": "rimraf dist",
|
|
41
|
-
"build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
|
|
42
|
-
},
|
|
43
41
|
"readme": "# postcss-minify-params [![Build Status][ci-img]][ci]\n\n> Minify at-rule params with PostCSS.\n\n```css\n@media only screen and ( min-width: 400px, min-height: 500px ) {\n h2{\n color:blue\n }\n}\n```\n\n```css\n@media only screen and (min-width:400px,min-height:500px) {\n h2{\n color:blue\n }\n}\n```\n\n## Usage\n\n```js\npostcss([ require('postcss-minify-params') ])\n```\n\nSee [PostCSS] docs for examples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Bogdan Chadkin](mailto:trysound@yandex.ru)\n\n[PostCSS]: https://github.com/postcss/postcss\n[ci-img]: https://travis-ci.org/cssnano/postcss-minify-params.svg\n[ci]: https://travis-ci.org/cssnano/postcss-minify-params\n"
|
|
44
42
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const browserslist = require('browserslist');
|
|
3
|
+
const valueParser = require('postcss-value-parser');
|
|
4
|
+
const { getArguments } = require('cssnano-utils');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Return the greatest common divisor
|
|
8
|
+
* of two numbers.
|
|
9
|
+
*
|
|
10
|
+
* @param {number} a
|
|
11
|
+
* @param {number} b
|
|
12
|
+
* @return {number}
|
|
13
|
+
*/
|
|
14
|
+
function gcd(a, b) {
|
|
15
|
+
return b ? gcd(b, a % b) : a;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {number} a
|
|
20
|
+
* @param {number} b
|
|
21
|
+
* @return {[number, number]}
|
|
22
|
+
*/
|
|
23
|
+
function aspectRatio(a, b) {
|
|
24
|
+
const divisor = gcd(a, b);
|
|
25
|
+
|
|
26
|
+
return [a / divisor, b / divisor];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {valueParser.Node[]} args
|
|
31
|
+
* @return {string}
|
|
32
|
+
*/
|
|
33
|
+
function split(args) {
|
|
34
|
+
return args.map((arg) => valueParser.stringify(arg)).join('');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {valueParser.Node} node
|
|
39
|
+
* @return {void}
|
|
40
|
+
*/
|
|
41
|
+
function removeNode(node) {
|
|
42
|
+
node.value = '';
|
|
43
|
+
node.type = 'word';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {unknown[]} items
|
|
48
|
+
* @return {string}
|
|
49
|
+
*/
|
|
50
|
+
function sortAndDedupe(items) {
|
|
51
|
+
const a = [...new Set(items)];
|
|
52
|
+
a.sort();
|
|
53
|
+
return a.join();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {boolean} legacy
|
|
58
|
+
* @param {import('postcss').AtRule} rule
|
|
59
|
+
* @return {void}
|
|
60
|
+
*/
|
|
61
|
+
function transform(legacy, rule) {
|
|
62
|
+
const ruleName = rule.name.toLowerCase();
|
|
63
|
+
|
|
64
|
+
// We should re-arrange parameters only for `@media` and `@supports` at-rules
|
|
65
|
+
if (!rule.params || !['media', 'supports'].includes(ruleName)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const params = valueParser(rule.params);
|
|
70
|
+
|
|
71
|
+
params.walk((node, index) => {
|
|
72
|
+
if (node.type === 'div' || node.type === 'function') {
|
|
73
|
+
node.before = node.after = '';
|
|
74
|
+
|
|
75
|
+
if (
|
|
76
|
+
node.type === 'function' &&
|
|
77
|
+
node.nodes[4] &&
|
|
78
|
+
node.nodes[0].value.toLowerCase().indexOf('-aspect-ratio') === 3
|
|
79
|
+
) {
|
|
80
|
+
const [a, b] = aspectRatio(
|
|
81
|
+
Number(node.nodes[2].value),
|
|
82
|
+
Number(node.nodes[4].value)
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
node.nodes[2].value = a.toString();
|
|
86
|
+
node.nodes[4].value = b.toString();
|
|
87
|
+
}
|
|
88
|
+
} else if (node.type === 'space') {
|
|
89
|
+
node.value = ' ';
|
|
90
|
+
} else {
|
|
91
|
+
const prevWord = params.nodes[index - 2];
|
|
92
|
+
|
|
93
|
+
if (
|
|
94
|
+
node.value.toLowerCase() === 'all' &&
|
|
95
|
+
rule.name.toLowerCase() === 'media' &&
|
|
96
|
+
!prevWord
|
|
97
|
+
) {
|
|
98
|
+
const nextWord = params.nodes[index + 2];
|
|
99
|
+
|
|
100
|
+
if (!legacy || nextWord) {
|
|
101
|
+
removeNode(node);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (nextWord && nextWord.value.toLowerCase() === 'and') {
|
|
105
|
+
const nextSpace = params.nodes[index + 1];
|
|
106
|
+
const secondSpace = params.nodes[index + 3];
|
|
107
|
+
|
|
108
|
+
removeNode(nextWord);
|
|
109
|
+
removeNode(nextSpace);
|
|
110
|
+
removeNode(secondSpace);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}, true);
|
|
115
|
+
|
|
116
|
+
rule.params = sortAndDedupe(getArguments(params).map(split));
|
|
117
|
+
|
|
118
|
+
if (!rule.params.length) {
|
|
119
|
+
rule.raws.afterName = '';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param {string} browser
|
|
125
|
+
* @return {boolean}
|
|
126
|
+
*/
|
|
127
|
+
function hasAllBug(browser) {
|
|
128
|
+
return ['ie 10', 'ie 11'].includes(browser);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @type {import('postcss').PluginCreator<browserslist.Options>}
|
|
133
|
+
* @param {browserslist.Options} options
|
|
134
|
+
* @return {import('postcss').Plugin}
|
|
135
|
+
*/
|
|
136
|
+
function pluginCreator(options = {}) {
|
|
137
|
+
const browsers = browserslist(null, {
|
|
138
|
+
stats: options.stats,
|
|
139
|
+
path: __dirname,
|
|
140
|
+
env: options.env,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
postcssPlugin: 'postcss-minify-params',
|
|
145
|
+
|
|
146
|
+
OnceExit(css) {
|
|
147
|
+
css.walkAtRules(transform.bind(null, browsers.some(hasAllBug)));
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
pluginCreator.postcss = true;
|
|
153
|
+
module.exports = pluginCreator;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export = pluginCreator;
|
|
2
|
+
/**
|
|
3
|
+
* @type {import('postcss').PluginCreator<browserslist.Options>}
|
|
4
|
+
* @param {browserslist.Options} options
|
|
5
|
+
* @return {import('postcss').Plugin}
|
|
6
|
+
*/
|
|
7
|
+
declare function pluginCreator(options?: browserslist.Options): import('postcss').Plugin;
|
|
8
|
+
declare namespace pluginCreator {
|
|
9
|
+
const postcss: true;
|
|
10
|
+
}
|
|
11
|
+
import browserslist = require("browserslist");
|
package/dist/index.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _browserslist = _interopRequireDefault(require("browserslist"));
|
|
9
|
-
|
|
10
|
-
var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
|
|
11
|
-
|
|
12
|
-
var _alphanumSort = _interopRequireDefault(require("alphanum-sort"));
|
|
13
|
-
|
|
14
|
-
var _cssnanoUtils = require("cssnano-utils");
|
|
15
|
-
|
|
16
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
-
|
|
18
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
-
|
|
20
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Return the greatest common divisor
|
|
24
|
-
* of two numbers.
|
|
25
|
-
*/
|
|
26
|
-
function gcd(a, b) {
|
|
27
|
-
return b ? gcd(b, a % b) : a;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function aspectRatio(a, b) {
|
|
31
|
-
const divisor = gcd(a, b);
|
|
32
|
-
return [a / divisor, b / divisor];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function split(args) {
|
|
36
|
-
return args.map(arg => (0, _postcssValueParser.stringify)(arg)).join('');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function removeNode(node) {
|
|
40
|
-
node.value = '';
|
|
41
|
-
node.type = 'word';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function sortAndDedupe(items) {
|
|
45
|
-
return (0, _alphanumSort.default)([...new Set(items)], {
|
|
46
|
-
insensitive: true
|
|
47
|
-
}).join();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function transform(legacy, rule) {
|
|
51
|
-
const ruleName = rule.name.toLowerCase(); // We should re-arrange parameters only for `@media` and `@supports` at-rules
|
|
52
|
-
|
|
53
|
-
if (!rule.params || !['media', 'supports'].includes(ruleName)) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const params = (0, _postcssValueParser.default)(rule.params);
|
|
58
|
-
params.walk((node, index) => {
|
|
59
|
-
if (node.type === 'div' || node.type === 'function') {
|
|
60
|
-
node.before = node.after = '';
|
|
61
|
-
|
|
62
|
-
if (node.type === 'function' && node.nodes[4] && node.nodes[0].value.toLowerCase().indexOf('-aspect-ratio') === 3) {
|
|
63
|
-
const [a, b] = aspectRatio(node.nodes[2].value, node.nodes[4].value);
|
|
64
|
-
node.nodes[2].value = a;
|
|
65
|
-
node.nodes[4].value = b;
|
|
66
|
-
}
|
|
67
|
-
} else if (node.type === 'space') {
|
|
68
|
-
node.value = ' ';
|
|
69
|
-
} else {
|
|
70
|
-
const prevWord = params.nodes[index - 2];
|
|
71
|
-
|
|
72
|
-
if (node.value.toLowerCase() === 'all' && rule.name.toLowerCase() === 'media' && !prevWord) {
|
|
73
|
-
const nextWord = params.nodes[index + 2];
|
|
74
|
-
|
|
75
|
-
if (!legacy || nextWord) {
|
|
76
|
-
removeNode(node);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (nextWord && nextWord.value.toLowerCase() === 'and') {
|
|
80
|
-
const nextSpace = params.nodes[index + 1];
|
|
81
|
-
const secondSpace = params.nodes[index + 3];
|
|
82
|
-
removeNode(nextWord);
|
|
83
|
-
removeNode(nextSpace);
|
|
84
|
-
removeNode(secondSpace);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}, true);
|
|
89
|
-
rule.params = sortAndDedupe((0, _cssnanoUtils.getArguments)(params).map(split));
|
|
90
|
-
|
|
91
|
-
if (!rule.params.length) {
|
|
92
|
-
rule.raws.afterName = '';
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function hasAllBug(browser) {
|
|
97
|
-
return ~['ie 10', 'ie 11'].indexOf(browser);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function pluginCreator(options = {}) {
|
|
101
|
-
const browsers = (0, _browserslist.default)(null, {
|
|
102
|
-
stats: options.stats,
|
|
103
|
-
path: __dirname,
|
|
104
|
-
env: options.env
|
|
105
|
-
});
|
|
106
|
-
return {
|
|
107
|
-
postcssPlugin: 'postcss-minify-params',
|
|
108
|
-
|
|
109
|
-
OnceExit(css) {
|
|
110
|
-
css.walkAtRules(transform.bind(null, browsers.some(hasAllBug)));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
pluginCreator.postcss = true;
|
|
117
|
-
var _default = pluginCreator;
|
|
118
|
-
exports.default = _default;
|
|
119
|
-
module.exports = exports.default;
|