postcss-convert-values 5.1.0 → 5.1.1

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,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-convert-values",
3
- "version": "5.1.0",
3
+ "version": "5.1.1",
4
4
  "description": "Convert values with PostCSS (e.g. ms -> s)",
5
5
  "main": "src/index.js",
6
6
  "types": "types/index.d.ts",
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "repository": "cssnano/cssnano",
26
26
  "dependencies": {
27
+ "browserslist": "^4.20.3",
27
28
  "postcss-value-parser": "^4.2.0"
28
29
  },
29
30
  "bugs": {
@@ -37,6 +38,5 @@
37
38
  },
38
39
  "peerDependencies": {
39
40
  "postcss": "^8.2.15"
40
- },
41
- "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
+ }
42
42
  }
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
  const valueParser = require('postcss-value-parser');
3
+ const browserslist = require('browserslist');
3
4
  const convert = require('./lib/convert.js');
4
5
 
5
6
  const LENGTH_UNITS = new Set([
@@ -36,6 +37,9 @@ const keepWhenZero = new Set([
36
37
  'line-height',
37
38
  ]);
38
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
+
39
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,
@@ -104,14 +108,16 @@ function clampOpacity(node) {
104
108
 
105
109
  /**
106
110
  * @param {import('postcss').Declaration} decl
111
+ * @param {string[]} browsers
107
112
  * @return {boolean}
108
113
  */
109
- function shouldKeepZeroUnit(decl) {
114
+ function shouldKeepZeroUnit(decl, browsers) {
110
115
  const { parent } = decl;
111
116
  const lowerCasedProp = decl.prop.toLowerCase();
112
117
  return (
113
118
  (decl.value.includes('%') &&
114
- (lowerCasedProp === 'max-height' || lowerCasedProp === 'height')) ||
119
+ keepZeroPercent.has(lowerCasedProp) &&
120
+ browsers.includes('ie 11')) ||
115
121
  (parent &&
116
122
  parent.parent &&
117
123
  parent.parent.type === 'atrule' &&
@@ -124,10 +130,11 @@ function shouldKeepZeroUnit(decl) {
124
130
  }
125
131
  /**
126
132
  * @param {Options} opts
133
+ * @param {string[]} browsers
127
134
  * @param {import('postcss').Declaration} decl
128
135
  * @return {void}
129
136
  */
130
- function transform(opts, decl) {
137
+ function transform(opts, browsers, decl) {
131
138
  const lowerCasedProp = decl.prop.toLowerCase();
132
139
  if (
133
140
  lowerCasedProp.includes('flex') ||
@@ -142,7 +149,7 @@ function transform(opts, decl) {
142
149
  const lowerCasedValue = node.value.toLowerCase();
143
150
 
144
151
  if (node.type === 'word') {
145
- parseWord(node, opts, shouldKeepZeroUnit(decl));
152
+ parseWord(node, opts, shouldKeepZeroUnit(decl, browsers));
146
153
  if (
147
154
  lowerCasedProp === 'opacity' ||
148
155
  lowerCasedProp === 'shape-image-threshold'
@@ -175,17 +182,23 @@ function transform(opts, decl) {
175
182
 
176
183
  const plugin = 'postcss-convert-values';
177
184
  /**
178
- * @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean}} Options */
185
+ * @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean} & browserslist.Options} Options */
179
186
  /**
180
187
  * @type {import('postcss').PluginCreator<Options>}
181
188
  * @param {Options} opts
182
189
  * @return {import('postcss').Plugin}
183
190
  */
184
191
  function pluginCreator(opts = { precision: false }) {
192
+ const browsers = browserslist(null, {
193
+ stats: opts.stats,
194
+ path: __dirname,
195
+ env: opts.env,
196
+ });
197
+
185
198
  return {
186
199
  postcssPlugin: plugin,
187
200
  OnceExit(css) {
188
- css.walkDecls(transform.bind(null, opts));
201
+ css.walkDecls((decl) => transform(opts, browsers, decl));
189
202
  },
190
203
  };
191
204
  }
package/types/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export = pluginCreator;
2
2
  /**
3
- * @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean}} Options */
3
+ * @typedef {{precision: boolean | number, angle?: boolean, time?: boolean, length?: boolean} & browserslist.Options} Options */
4
4
  /**
5
5
  * @type {import('postcss').PluginCreator<Options>}
6
6
  * @param {Options} opts
@@ -15,5 +15,6 @@ type Options = {
15
15
  angle?: boolean;
16
16
  time?: boolean;
17
17
  length?: boolean;
18
- };
18
+ } & browserslist.Options;
19
19
  declare var postcss: true;
20
+ import browserslist = require("browserslist");