postcss-convert-values 5.0.0 → 5.0.4

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,17 +1,12 @@
1
1
  {
2
2
  "name": "postcss-convert-values",
3
- "version": "5.0.0",
3
+ "version": "5.0.4",
4
4
  "description": "Convert values with PostCSS (e.g. ms -> s)",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
6
  "files": [
7
7
  "LICENSE-MIT",
8
- "dist"
8
+ "src"
9
9
  ],
10
- "scripts": {
11
- "prebuild": "del-cli dist",
12
- "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.js --out-dir dist --ignore \"**/__tests__/\"",
13
- "prepublish": "yarn build"
14
- },
15
10
  "keywords": [
16
11
  "css",
17
12
  "optimisation",
@@ -27,7 +22,7 @@
27
22
  },
28
23
  "repository": "cssnano/cssnano",
29
24
  "dependencies": {
30
- "postcss-value-parser": "^4.1.0"
25
+ "postcss-value-parser": "^4.2.0"
31
26
  },
32
27
  "bugs": {
33
28
  "url": "https://github.com/cssnano/cssnano/issues"
@@ -36,10 +31,10 @@
36
31
  "node": "^10 || ^12 || >=14.0"
37
32
  },
38
33
  "devDependencies": {
39
- "postcss": "^8.2.1"
34
+ "postcss": "^8.2.15"
40
35
  },
41
36
  "peerDependencies": {
42
- "postcss": "^8.2.1"
37
+ "postcss": "^8.2.15"
43
38
  },
44
- "gitHead": "0e2c3bf5835bafcdc8783bef66f730a24194c8f3"
45
- }
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"
40
+ }
package/src/index.js ADDED
@@ -0,0 +1,164 @@
1
+ 'use strict';
2
+ const valueParser = require('postcss-value-parser');
3
+ const convert = require('./lib/convert');
4
+
5
+ const LENGTH_UNITS = new Set([
6
+ 'em',
7
+ 'ex',
8
+ 'ch',
9
+ 'rem',
10
+ 'vw',
11
+ 'vh',
12
+ 'vmin',
13
+ 'vmax',
14
+ 'cm',
15
+ 'mm',
16
+ 'q',
17
+ 'in',
18
+ 'pt',
19
+ 'pc',
20
+ 'px',
21
+ ]);
22
+
23
+ // These properties only accept percentages, so no point in trying to transform
24
+ const notALength = new Set([
25
+ 'descent-override',
26
+ 'ascent-override',
27
+ 'font-stretch',
28
+ 'size-adjust',
29
+ 'line-gap-override',
30
+ ]);
31
+
32
+ // Can't change the unit on these properties when they're 0
33
+ const keepWhenZero = new Set([
34
+ 'stroke-dashoffset',
35
+ 'stroke-width',
36
+ 'line-height',
37
+ ]);
38
+
39
+ /*
40
+ * Numbers without digits after the dot are technically invalid,
41
+ * but in that case css-value-parser returns the dot as part of the unit,
42
+ * so we use this to remove the dot.
43
+ */
44
+ function stripLeadingDot(item) {
45
+ if (item.charCodeAt(0) === '.'.charCodeAt(0)) {
46
+ return item.slice(1);
47
+ } else {
48
+ return item;
49
+ }
50
+ }
51
+
52
+ function parseWord(node, opts, keepZeroUnit) {
53
+ const pair = valueParser.unit(node.value);
54
+ if (pair) {
55
+ const num = Number(pair.number);
56
+ const u = stripLeadingDot(pair.unit);
57
+ if (num === 0) {
58
+ node.value =
59
+ 0 +
60
+ (keepZeroUnit || (!LENGTH_UNITS.has(u.toLowerCase()) && u !== '%')
61
+ ? u
62
+ : '');
63
+ } else {
64
+ node.value = convert(num, u, opts);
65
+
66
+ if (
67
+ typeof opts.precision === 'number' &&
68
+ u.toLowerCase() === 'px' &&
69
+ pair.number.includes('.')
70
+ ) {
71
+ const precision = Math.pow(10, opts.precision);
72
+ node.value =
73
+ Math.round(parseFloat(node.value) * precision) / precision + u;
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ function clampOpacity(node) {
80
+ const pair = valueParser.unit(node.value);
81
+ if (!pair) {
82
+ return;
83
+ }
84
+ let num = Number(pair.number);
85
+ if (num > 1) {
86
+ node.value = pair.unit === '%' ? num + pair.unit : 1 + pair.unit;
87
+ } else if (num < 0) {
88
+ node.value = 0 + pair.unit;
89
+ }
90
+ }
91
+
92
+ function shouldKeepZeroUnit(decl) {
93
+ const { parent } = decl;
94
+ const lowerCasedProp = decl.prop.toLowerCase();
95
+ return (
96
+ (decl.value.includes('%') &&
97
+ (lowerCasedProp === 'max-height' || lowerCasedProp === 'height')) ||
98
+ (parent.parent &&
99
+ parent.parent.name &&
100
+ parent.parent.name.toLowerCase() === 'keyframes' &&
101
+ lowerCasedProp === 'stroke-dasharray') ||
102
+ keepWhenZero.has(lowerCasedProp)
103
+ );
104
+ }
105
+
106
+ function transform(opts, decl) {
107
+ const lowerCasedProp = decl.prop.toLowerCase();
108
+ if (
109
+ lowerCasedProp.includes('flex') ||
110
+ lowerCasedProp.indexOf('--') === 0 ||
111
+ notALength.has(lowerCasedProp)
112
+ ) {
113
+ return;
114
+ }
115
+
116
+ decl.value = valueParser(decl.value)
117
+ .walk((node) => {
118
+ const lowerCasedValue = node.value.toLowerCase();
119
+
120
+ if (node.type === 'word') {
121
+ parseWord(node, opts, shouldKeepZeroUnit(decl));
122
+ if (
123
+ lowerCasedProp === 'opacity' ||
124
+ lowerCasedProp === 'shape-image-threshold'
125
+ ) {
126
+ clampOpacity(node);
127
+ }
128
+ } else if (node.type === 'function') {
129
+ if (
130
+ lowerCasedValue === 'calc' ||
131
+ lowerCasedValue === 'min' ||
132
+ lowerCasedValue === 'max' ||
133
+ lowerCasedValue === 'clamp' ||
134
+ lowerCasedValue === 'hsl' ||
135
+ lowerCasedValue === 'hsla'
136
+ ) {
137
+ valueParser.walk(node.nodes, (n) => {
138
+ if (n.type === 'word') {
139
+ parseWord(n, opts, true);
140
+ }
141
+ });
142
+ return false;
143
+ }
144
+ if (lowerCasedValue === 'url') {
145
+ return false;
146
+ }
147
+ }
148
+ })
149
+ .toString();
150
+ }
151
+
152
+ const plugin = 'postcss-convert-values';
153
+
154
+ function pluginCreator(opts = { precision: false }) {
155
+ return {
156
+ postcssPlugin: plugin,
157
+ OnceExit(css) {
158
+ css.walkDecls(transform.bind(null, opts));
159
+ },
160
+ };
161
+ }
162
+
163
+ pluginCreator.postcss = true;
164
+ module.exports = pluginCreator;
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+ const lengthConv = new Map([
3
+ ['in', 96],
4
+ ['px', 1],
5
+ ['pt', 4 / 3],
6
+ ['pc', 16],
7
+ ]);
8
+
9
+ const timeConv = new Map([
10
+ ['s', 1000],
11
+ ['ms', 1],
12
+ ]);
13
+
14
+ const angleConv = new Map([
15
+ ['turn', 360],
16
+ ['deg', 1],
17
+ ]);
18
+
19
+ function dropLeadingZero(number) {
20
+ const value = String(number);
21
+
22
+ if (number % 1) {
23
+ if (value[0] === '0') {
24
+ return value.slice(1);
25
+ }
26
+
27
+ if (value[0] === '-' && value[1] === '0') {
28
+ return '-' + value.slice(2);
29
+ }
30
+ }
31
+
32
+ return value;
33
+ }
34
+
35
+ function transform(number, originalUnit, conversions) {
36
+ let conversionUnits = [...conversions.keys()].filter((u) => {
37
+ return originalUnit !== u;
38
+ });
39
+
40
+ const base = number * conversions.get(originalUnit);
41
+
42
+ return conversionUnits
43
+ .map((u) => dropLeadingZero(base / conversions.get(u)) + u)
44
+ .reduce((a, b) => (a.length < b.length ? a : b));
45
+ }
46
+
47
+ module.exports = function (number, unit, { time, length, angle }) {
48
+ let value = dropLeadingZero(number) + (unit ? unit : '');
49
+ let converted;
50
+ const lowerCaseUnit = unit.toLowerCase();
51
+ if (length !== false && lengthConv.has(lowerCaseUnit)) {
52
+ converted = transform(number, lowerCaseUnit, lengthConv);
53
+ }
54
+
55
+ if (time !== false && timeConv.has(lowerCaseUnit)) {
56
+ converted = transform(number, lowerCaseUnit, timeConv);
57
+ }
58
+
59
+ if (angle !== false && angleConv.has(lowerCaseUnit)) {
60
+ converted = transform(number, lowerCaseUnit, angleConv);
61
+ }
62
+
63
+ if (converted && converted.length < value.length) {
64
+ value = converted;
65
+ }
66
+
67
+ return value;
68
+ };
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.0](https://github.com/cssnano/cssnano/compare/postcss-convert-values@5.0.0-rc.2...postcss-convert-values@5.0.0) (2021-04-06)
7
-
8
- **Note:** Version bump only for package postcss-convert-values
9
-
10
-
11
-
12
-
13
-
14
- # [5.0.0-rc.2](https://github.com/cssnano/cssnano/compare/postcss-convert-values@5.0.0-rc.1...postcss-convert-values@5.0.0-rc.2) (2021-03-15)
15
-
16
- **Note:** Version bump only for package postcss-convert-values
17
-
18
-
19
-
20
-
21
-
22
- # [5.0.0-rc.1](https://github.com/cssnano/cssnano/compare/postcss-convert-values@5.0.0-rc.0...postcss-convert-values@5.0.0-rc.1) (2021-03-04)
23
-
24
- **Note:** Version bump only for package postcss-convert-values
25
-
26
-
27
-
28
-
29
-
30
- # 5.0.0-rc.0 (2021-02-19)
31
-
32
-
33
- ### Bug Fixes
34
-
35
- * Keep math functions together ([#895](https://github.com/cssnano/cssnano/issues/895)) ([4ddf843](https://github.com/cssnano/cssnano/commit/4ddf843ca5dc64059f488113224e76165211a669))
36
- * preservation of opacities defined as percentages ([#881](https://github.com/cssnano/cssnano/issues/881)) ([fd7b878](https://github.com/cssnano/cssnano/commit/fd7b878e72ed9bd20c145c9e2daa7b5d48cb1117))
37
- * units being removed in math functions ([#894](https://github.com/cssnano/cssnano/issues/894)) ([1ccbd5b](https://github.com/cssnano/cssnano/commit/1ccbd5b29e1b2121efaf7b24a69aa782848966dc))
38
- * **postcss-convert-values:** prevent zero units from being dropped in line-height. ([#801](https://github.com/cssnano/cssnano/issues/801)) ([d781855](https://github.com/cssnano/cssnano/commit/d78185567ae5ebcde0469cf0e55145a7a3130d3e))
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,128 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
9
-
10
- var _convert = _interopRequireDefault(require("./lib/convert"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
15
-
16
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
17
-
18
- const LENGTH_UNITS = ['em', 'ex', 'ch', 'rem', 'vw', 'vh', 'vmin', 'vmax', 'cm', 'mm', 'q', 'in', 'pt', 'pc', 'px'];
19
- /*
20
- * Numbers without digits after the dot are technically invalid,
21
- * but in that case css-value-parser returns the dot as part of the unit,
22
- * so we use this to remove the dot.
23
- */
24
-
25
- function stripLeadingDot(item) {
26
- if (item.charCodeAt(0) === '.'.charCodeAt(0)) {
27
- return item.slice(1);
28
- } else {
29
- return item;
30
- }
31
- }
32
-
33
- function parseWord(node, opts, keepZeroUnit) {
34
- const pair = (0, _postcssValueParser.unit)(node.value);
35
-
36
- if (pair) {
37
- const num = Number(pair.number);
38
- const u = stripLeadingDot(pair.unit);
39
-
40
- if (num === 0) {
41
- node.value = 0 + (keepZeroUnit || !~LENGTH_UNITS.indexOf(u.toLowerCase()) && u !== '%' ? u : '');
42
- } else {
43
- node.value = (0, _convert.default)(num, u, opts);
44
-
45
- if (typeof opts.precision === 'number' && u.toLowerCase() === 'px' && ~pair.number.indexOf('.')) {
46
- const precision = Math.pow(10, opts.precision);
47
- node.value = Math.round(parseFloat(node.value) * precision) / precision + u;
48
- }
49
- }
50
- }
51
- }
52
-
53
- function clampOpacity(node) {
54
- const pair = (0, _postcssValueParser.unit)(node.value);
55
-
56
- if (!pair) {
57
- return;
58
- }
59
-
60
- let num = Number(pair.number);
61
-
62
- if (num > 1) {
63
- node.value = pair.unit === '%' ? num + pair.unit : 1 + pair.unit;
64
- } else if (num < 0) {
65
- node.value = 0 + pair.unit;
66
- }
67
- }
68
-
69
- function shouldKeepUnit(decl) {
70
- const {
71
- parent
72
- } = decl;
73
- const lowerCasedProp = decl.prop.toLowerCase();
74
- return ~decl.value.indexOf('%') && (lowerCasedProp === 'max-height' || lowerCasedProp === 'height') || parent.parent && parent.parent.name && parent.parent.name.toLowerCase() === 'keyframes' && lowerCasedProp === 'stroke-dasharray' || lowerCasedProp === 'stroke-dashoffset' || lowerCasedProp === 'stroke-width' || lowerCasedProp === 'line-height';
75
- }
76
-
77
- function transform(opts, decl) {
78
- const lowerCasedProp = decl.prop.toLowerCase();
79
-
80
- if (~lowerCasedProp.indexOf('flex') || lowerCasedProp.indexOf('--') === 0) {
81
- return;
82
- }
83
-
84
- decl.value = (0, _postcssValueParser.default)(decl.value).walk(node => {
85
- const lowerCasedValue = node.value.toLowerCase();
86
-
87
- if (node.type === 'word') {
88
- parseWord(node, opts, shouldKeepUnit(decl));
89
-
90
- if (lowerCasedProp === 'opacity' || lowerCasedProp === 'shape-image-threshold') {
91
- clampOpacity(node);
92
- }
93
- } else if (node.type === 'function') {
94
- if (lowerCasedValue === 'calc' || lowerCasedValue === 'min' || lowerCasedValue === 'max' || lowerCasedValue === 'clamp' || lowerCasedValue === 'hsl' || lowerCasedValue === 'hsla') {
95
- (0, _postcssValueParser.walk)(node.nodes, n => {
96
- if (n.type === 'word') {
97
- parseWord(n, opts, true);
98
- }
99
- });
100
- return false;
101
- }
102
-
103
- if (lowerCasedValue === 'url') {
104
- return false;
105
- }
106
- }
107
- }).toString();
108
- }
109
-
110
- const plugin = 'postcss-convert-values';
111
-
112
- function pluginCreator(opts = {
113
- precision: false
114
- }) {
115
- return {
116
- postcssPlugin: plugin,
117
-
118
- OnceExit(css) {
119
- css.walkDecls(transform.bind(null, opts));
120
- }
121
-
122
- };
123
- }
124
-
125
- pluginCreator.postcss = true;
126
- var _default = pluginCreator;
127
- exports.default = _default;
128
- module.exports = exports.default;
@@ -1,85 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = _default;
7
- const lengthConv = {
8
- in: 96,
9
- px: 1,
10
- pt: 4 / 3,
11
- pc: 16
12
- };
13
- const timeConv = {
14
- s: 1000,
15
- ms: 1
16
- };
17
- const angleConv = {
18
- turn: 360,
19
- deg: 1
20
- };
21
-
22
- function dropLeadingZero(number) {
23
- const value = String(number);
24
-
25
- if (number % 1) {
26
- if (value[0] === '0') {
27
- return value.slice(1);
28
- }
29
-
30
- if (value[0] === '-' && value[1] === '0') {
31
- return '-' + value.slice(2);
32
- }
33
- }
34
-
35
- return value;
36
- }
37
-
38
- function transform(number, unit, conversion) {
39
- const lowerCasedUnit = unit.toLowerCase();
40
- let one, base;
41
- let convertionUnits = Object.keys(conversion).filter(u => {
42
- if (conversion[u] === 1) {
43
- one = u;
44
- }
45
-
46
- return lowerCasedUnit !== u;
47
- });
48
-
49
- if (lowerCasedUnit === one) {
50
- base = number / conversion[lowerCasedUnit];
51
- } else {
52
- base = number * conversion[lowerCasedUnit];
53
- }
54
-
55
- return convertionUnits.map(u => dropLeadingZero(base / conversion[u]) + u).reduce((a, b) => a.length < b.length ? a : b);
56
- }
57
-
58
- function _default(number, unit, {
59
- time,
60
- length,
61
- angle
62
- }) {
63
- let value = dropLeadingZero(number) + (unit ? unit : '');
64
- let converted;
65
-
66
- if (length !== false && unit.toLowerCase() in lengthConv) {
67
- converted = transform(number, unit, lengthConv);
68
- }
69
-
70
- if (time !== false && unit.toLowerCase() in timeConv) {
71
- converted = transform(number, unit, timeConv);
72
- }
73
-
74
- if (angle !== false && unit.toLowerCase() in angleConv) {
75
- converted = transform(number, unit, angleConv);
76
- }
77
-
78
- if (converted && converted.length < value.length) {
79
- value = converted;
80
- }
81
-
82
- return value;
83
- }
84
-
85
- module.exports = exports.default;