postcss-convert-values 5.0.4 → 5.1.2
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 +6 -4
- package/src/index.js +55 -12
- package/src/lib/convert.js +21 -4
- package/types/index.d.ts +20 -0
- package/types/lib/convert.d.ts +6 -0
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-convert-values",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.2",
|
|
4
4
|
"description": "Convert values with PostCSS (e.g. ms -> s)",
|
|
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",
|
|
@@ -22,6 +24,7 @@
|
|
|
22
24
|
},
|
|
23
25
|
"repository": "cssnano/cssnano",
|
|
24
26
|
"dependencies": {
|
|
27
|
+
"browserslist": "^4.20.3",
|
|
25
28
|
"postcss-value-parser": "^4.2.0"
|
|
26
29
|
},
|
|
27
30
|
"bugs": {
|
|
@@ -35,6 +38,5 @@
|
|
|
35
38
|
},
|
|
36
39
|
"peerDependencies": {
|
|
37
40
|
"postcss": "^8.2.15"
|
|
38
|
-
}
|
|
39
|
-
"readme": "# [postcss][postcss]-convert-values\n\n> Convert values with PostCSS (e.g. ms -> s)\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-convert-values) do:\n\n```\nnpm install postcss-convert-values --save\n```\n\n## Example\n\nThis plugin reduces CSS size by converting values to use different units\nwhere possible; for example, `500ms` can be represented as `.5s`. You can\nread more about these units in [this article][csstricks].\n\n### Input\n\n```css\nh1 {\n font-size: 16px;\n width: 0em\n}\n```\n\n### Output\n\n```css\nh1 {\n font-size: 1pc;\n width: 0\n}\n```\n\nNote that this plugin only covers conversions for duration and absolute length\nvalues. For color conversions, use [postcss-colormin][colormin].\n\n## API\n\n### convertValues([options])\n\n#### options\n\n##### length\n\nType: `boolean`\nDefault: `true`\n\nPass `false` to disable conversion from `px` to other absolute length units,\nsuch as `pc` & `pt` & vice versa.\n\n##### time\n\nType: `boolean`\nDefault: `true`\n\nPass `false` to disable conversion from `ms` to `s` & vice versa.\n\n##### angle\n\nType: `boolean`\nDefault: `true`\n\nPass `false` to disable conversion from `deg` to `turn` & vice versa.\n\n##### precision\n\nType: `boolean|number`\nDefault: `false`\n\nSpecify any numeric value here to round `px` values to that many decimal places;\nfor example, using `{precision: 2}` will round `6.66667px` to `6.67px`, and\n`{precision: 0}` will round it to `7px`. Passing `false` (the default) will\nleave these values as is.\n\nIt is recommended for most use cases to set this option to `2`.\n\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n\n[postcss]: https://github.com/postcss/postcss\n[csstricks]: https://css-tricks.com/the-lengths-of-css/\n"
|
|
41
|
+
}
|
|
40
42
|
}
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
const valueParser = require('postcss-value-parser');
|
|
3
|
-
const
|
|
3
|
+
const browserslist = require('browserslist');
|
|
4
|
+
const convert = require('./lib/convert.js');
|
|
4
5
|
|
|
5
6
|
const LENGTH_UNITS = new Set([
|
|
6
7
|
'em',
|
|
@@ -36,10 +37,16 @@ const keepWhenZero = new Set([
|
|
|
36
37
|
'line-height',
|
|
37
38
|
]);
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
// Can't remove the % on these properties when they're 0 on IE 11
|
|
41
|
+
const keepZeroPercent = new Set(['max-height', 'height', 'min-width']);
|
|
42
|
+
|
|
43
|
+
/**
|
|
40
44
|
* Numbers without digits after the dot are technically invalid,
|
|
41
45
|
* but in that case css-value-parser returns the dot as part of the unit,
|
|
42
46
|
* so we use this to remove the dot.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} item
|
|
49
|
+
* @return {string}
|
|
43
50
|
*/
|
|
44
51
|
function stripLeadingDot(item) {
|
|
45
52
|
if (item.charCodeAt(0) === '.'.charCodeAt(0)) {
|
|
@@ -49,6 +56,12 @@ function stripLeadingDot(item) {
|
|
|
49
56
|
}
|
|
50
57
|
}
|
|
51
58
|
|
|
59
|
+
/**
|
|
60
|
+
* @param {valueParser.Node} node
|
|
61
|
+
* @param {Options} opts
|
|
62
|
+
* @param {boolean} keepZeroUnit
|
|
63
|
+
* @return {void}
|
|
64
|
+
*/
|
|
52
65
|
function parseWord(node, opts, keepZeroUnit) {
|
|
53
66
|
const pair = valueParser.unit(node.value);
|
|
54
67
|
if (pair) {
|
|
@@ -76,6 +89,10 @@ function parseWord(node, opts, keepZeroUnit) {
|
|
|
76
89
|
}
|
|
77
90
|
}
|
|
78
91
|
|
|
92
|
+
/**
|
|
93
|
+
* @param {valueParser.WordNode} node
|
|
94
|
+
* @return {void}
|
|
95
|
+
*/
|
|
79
96
|
function clampOpacity(node) {
|
|
80
97
|
const pair = valueParser.unit(node.value);
|
|
81
98
|
if (!pair) {
|
|
@@ -89,21 +106,35 @@ function clampOpacity(node) {
|
|
|
89
106
|
}
|
|
90
107
|
}
|
|
91
108
|
|
|
92
|
-
|
|
109
|
+
/**
|
|
110
|
+
* @param {import('postcss').Declaration} decl
|
|
111
|
+
* @param {string[]} browsers
|
|
112
|
+
* @return {boolean}
|
|
113
|
+
*/
|
|
114
|
+
function shouldKeepZeroUnit(decl, browsers) {
|
|
93
115
|
const { parent } = decl;
|
|
94
116
|
const lowerCasedProp = decl.prop.toLowerCase();
|
|
95
117
|
return (
|
|
96
118
|
(decl.value.includes('%') &&
|
|
97
|
-
(lowerCasedProp
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
parent.parent
|
|
119
|
+
keepZeroPercent.has(lowerCasedProp) &&
|
|
120
|
+
browsers.includes('ie 11')) ||
|
|
121
|
+
(parent &&
|
|
122
|
+
parent.parent &&
|
|
123
|
+
parent.parent.type === 'atrule' &&
|
|
124
|
+
/** @type {import('postcss').AtRule} */ (
|
|
125
|
+
parent.parent
|
|
126
|
+
).name.toLowerCase() === 'keyframes' &&
|
|
101
127
|
lowerCasedProp === 'stroke-dasharray') ||
|
|
102
128
|
keepWhenZero.has(lowerCasedProp)
|
|
103
129
|
);
|
|
104
130
|
}
|
|
105
|
-
|
|
106
|
-
|
|
131
|
+
/**
|
|
132
|
+
* @param {Options} opts
|
|
133
|
+
* @param {string[]} browsers
|
|
134
|
+
* @param {import('postcss').Declaration} decl
|
|
135
|
+
* @return {void}
|
|
136
|
+
*/
|
|
137
|
+
function transform(opts, browsers, decl) {
|
|
107
138
|
const lowerCasedProp = decl.prop.toLowerCase();
|
|
108
139
|
if (
|
|
109
140
|
lowerCasedProp.includes('flex') ||
|
|
@@ -118,7 +149,7 @@ function transform(opts, decl) {
|
|
|
118
149
|
const lowerCasedValue = node.value.toLowerCase();
|
|
119
150
|
|
|
120
151
|
if (node.type === 'word') {
|
|
121
|
-
parseWord(node, opts, shouldKeepZeroUnit(decl));
|
|
152
|
+
parseWord(node, opts, shouldKeepZeroUnit(decl, browsers));
|
|
122
153
|
if (
|
|
123
154
|
lowerCasedProp === 'opacity' ||
|
|
124
155
|
lowerCasedProp === 'shape-image-threshold'
|
|
@@ -150,12 +181,24 @@ function transform(opts, decl) {
|
|
|
150
181
|
}
|
|
151
182
|
|
|
152
183
|
const plugin = 'postcss-convert-values';
|
|
153
|
-
|
|
184
|
+
/**
|
|
185
|
+
* @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean} & browserslist.Options} Options */
|
|
186
|
+
/**
|
|
187
|
+
* @type {import('postcss').PluginCreator<Options>}
|
|
188
|
+
* @param {Options} opts
|
|
189
|
+
* @return {import('postcss').Plugin}
|
|
190
|
+
*/
|
|
154
191
|
function pluginCreator(opts = { precision: false }) {
|
|
192
|
+
const browsers = browserslist(null, {
|
|
193
|
+
stats: opts.stats,
|
|
194
|
+
path: __dirname,
|
|
195
|
+
env: opts.env,
|
|
196
|
+
});
|
|
197
|
+
|
|
155
198
|
return {
|
|
156
199
|
postcssPlugin: plugin,
|
|
157
200
|
OnceExit(css) {
|
|
158
|
-
css.walkDecls(transform
|
|
201
|
+
css.walkDecls((decl) => transform(opts, browsers, decl));
|
|
159
202
|
},
|
|
160
203
|
};
|
|
161
204
|
}
|
package/src/lib/convert.js
CHANGED
|
@@ -15,7 +15,10 @@ const angleConv = new Map([
|
|
|
15
15
|
['turn', 360],
|
|
16
16
|
['deg', 1],
|
|
17
17
|
]);
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* @param {number} number
|
|
20
|
+
* @return {string}
|
|
21
|
+
*/
|
|
19
22
|
function dropLeadingZero(number) {
|
|
20
23
|
const value = String(number);
|
|
21
24
|
|
|
@@ -31,19 +34,33 @@ function dropLeadingZero(number) {
|
|
|
31
34
|
|
|
32
35
|
return value;
|
|
33
36
|
}
|
|
34
|
-
|
|
37
|
+
/**
|
|
38
|
+
* @param {number} number
|
|
39
|
+
* @param {string} originalUnit
|
|
40
|
+
* @param {lengthConv | timeConv | angleConv} conversions
|
|
41
|
+
* @return {string}
|
|
42
|
+
*/
|
|
35
43
|
function transform(number, originalUnit, conversions) {
|
|
36
44
|
let conversionUnits = [...conversions.keys()].filter((u) => {
|
|
37
45
|
return originalUnit !== u;
|
|
38
46
|
});
|
|
39
47
|
|
|
40
|
-
const base = number * conversions.get(originalUnit);
|
|
48
|
+
const base = number * /** @type {number} */ (conversions.get(originalUnit));
|
|
41
49
|
|
|
42
50
|
return conversionUnits
|
|
43
|
-
.map(
|
|
51
|
+
.map(
|
|
52
|
+
(u) =>
|
|
53
|
+
dropLeadingZero(base / /** @type {number} */ (conversions.get(u))) + u
|
|
54
|
+
)
|
|
44
55
|
.reduce((a, b) => (a.length < b.length ? a : b));
|
|
45
56
|
}
|
|
46
57
|
|
|
58
|
+
/**
|
|
59
|
+
* @param {number} number
|
|
60
|
+
* @param {string} unit
|
|
61
|
+
* @param {{time?: boolean, length?: boolean, angle?: boolean}} options
|
|
62
|
+
* @return {string}
|
|
63
|
+
*/
|
|
47
64
|
module.exports = function (number, unit, { time, length, angle }) {
|
|
48
65
|
let value = dropLeadingZero(number) + (unit ? unit : '');
|
|
49
66
|
let converted;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export = pluginCreator;
|
|
2
|
+
/**
|
|
3
|
+
* @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean} & browserslist.Options} Options */
|
|
4
|
+
/**
|
|
5
|
+
* @type {import('postcss').PluginCreator<Options>}
|
|
6
|
+
* @param {Options} opts
|
|
7
|
+
* @return {import('postcss').Plugin}
|
|
8
|
+
*/
|
|
9
|
+
declare function pluginCreator(opts?: Options): import('postcss').Plugin;
|
|
10
|
+
declare namespace pluginCreator {
|
|
11
|
+
export { postcss, Options };
|
|
12
|
+
}
|
|
13
|
+
type Options = {
|
|
14
|
+
precision: boolean | number;
|
|
15
|
+
angle?: boolean;
|
|
16
|
+
time?: boolean;
|
|
17
|
+
length?: boolean;
|
|
18
|
+
} & browserslist.Options;
|
|
19
|
+
declare var postcss: true;
|
|
20
|
+
import browserslist = require("browserslist");
|