postcss-minify-params
Advanced tools
Comparing version
@@ -1,7 +0,26 @@ | ||
var postcss = require('postcss'); | ||
var valueParser = require('postcss-value-parser'); | ||
var stringify = valueParser.stringify; | ||
var sort = require('alphanum-sort'); | ||
var uniqs = require('uniqs'); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _browserslist = _interopRequireDefault(require("browserslist")); | ||
var _postcss = _interopRequireDefault(require("postcss")); | ||
var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser")); | ||
var _alphanumSort = _interopRequireDefault(require("alphanum-sort")); | ||
var _uniqs = _interopRequireDefault(require("uniqs")); | ||
var _cssnanoUtils = require("cssnano-utils"); | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } | ||
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; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -11,97 +30,85 @@ * Return the greatest common divisor | ||
*/ | ||
function gcd(a, b) { | ||
return b ? gcd(b, a % b) : a; | ||
return b ? gcd(b, a % b) : a; | ||
} | ||
function aspectRatio(a, b) { | ||
var divisor = gcd(a, b); | ||
const divisor = gcd(a, b); | ||
return [a / divisor, b / divisor]; | ||
} | ||
return [a / divisor, b / divisor]; | ||
function split(args) { | ||
return args.map(arg => (0, _postcssValueParser.stringify)(arg)).join(''); | ||
} | ||
function split(nodes, div) { | ||
var result = []; | ||
var i, max, node; | ||
var last = ''; | ||
function removeNode(node) { | ||
node.value = ''; | ||
node.type = 'word'; | ||
} | ||
for (i = 0, max = nodes.length; i < max; i += 1) { | ||
node = nodes[i]; | ||
if (node.type === 'div' && node.value === div) { | ||
result.push(last); | ||
last = ''; | ||
} else { | ||
last += stringify(node); | ||
} | ||
} | ||
function transform(legacy, rule) { | ||
const ruleName = rule.name.toLowerCase(); // We should re-arrange parameters only for `@media` and `@supports` at-rules | ||
result.push(last); | ||
if (!rule.params || !['media', 'supports'].includes(ruleName)) { | ||
return; | ||
} | ||
return result; | ||
} | ||
const params = (0, _postcssValueParser.default)(rule.params); | ||
params.walk((node, index) => { | ||
if (node.type === 'div' || node.type === 'function') { | ||
node.before = node.after = ''; | ||
function removeNode(node) { | ||
node.value = ''; | ||
node.type = 'word'; | ||
} | ||
if (node.type === 'function' && node.nodes[4] && node.nodes[0].value.toLowerCase().indexOf('-aspect-ratio') === 3) { | ||
const [a, b] = aspectRatio(node.nodes[2].value, node.nodes[4].value); | ||
node.nodes[2].value = a; | ||
node.nodes[4].value = b; | ||
} | ||
} else if (node.type === 'space') { | ||
node.value = ' '; | ||
} else { | ||
const prevWord = params.nodes[index - 2]; | ||
module.exports = postcss.plugin('postcss-minify-params', function () { | ||
return function (css) { | ||
css.walkAtRules(function (rule) { | ||
if (!rule.params) { | ||
return; | ||
} | ||
if (node.value.toLowerCase() === 'all' && rule.name.toLowerCase() === 'media' && !prevWord) { | ||
const nextWord = params.nodes[index + 2]; | ||
var params = valueParser(rule.params); | ||
if (!legacy || nextWord) { | ||
removeNode(node); | ||
} | ||
params.walk(function (node, index) { | ||
if (node.type === 'div' || node.type === 'function') { | ||
node.before = node.after = ''; | ||
if ( | ||
node.type === 'function' && | ||
node.nodes[4] && | ||
node.nodes[0].value.indexOf('-aspect-ratio') === 3 | ||
) { | ||
var ref = aspectRatio( | ||
node.nodes[2].value, | ||
node.nodes[4].value | ||
); | ||
var a = ref[0]; | ||
var b = ref[1]; | ||
node.nodes[2].value = a; | ||
node.nodes[4].value = b; | ||
} | ||
} else if (node.type === 'space') { | ||
node.value = ' '; | ||
} else if (node.type === 'word') { | ||
var prevWord = params.nodes[index - 2]; | ||
if ( | ||
node.value === 'all' && | ||
rule.name === 'media' && | ||
!prevWord | ||
) { | ||
var nextSpace = params.nodes[index + 1]; | ||
var nextWord = params.nodes[index + 2]; | ||
var secondSpace = params.nodes[index + 3]; | ||
if (nextWord && nextWord.value === 'and') { | ||
removeNode(nextWord); | ||
removeNode(nextSpace); | ||
if (secondSpace) { | ||
removeNode(secondSpace); | ||
} | ||
} | ||
removeNode(node); | ||
} | ||
} | ||
}, true); | ||
if (nextWord && nextWord.value.toLowerCase() === 'and') { | ||
const nextSpace = params.nodes[index + 1]; | ||
const secondSpace = params.nodes[index + 3]; | ||
removeNode(nextWord); | ||
removeNode(nextSpace); | ||
removeNode(secondSpace); | ||
} | ||
} | ||
} | ||
}, true); | ||
rule.params = (0, _alphanumSort.default)((0, _uniqs.default)((0, _cssnanoUtils.getArguments)(params).map(split)), { | ||
insensitive: true | ||
}).join(); | ||
rule.params = sort(uniqs(split(params.nodes, ',')), { | ||
insensitive: true | ||
}).join(); | ||
if (!rule.params.length) { | ||
rule.raws.afterName = ''; | ||
} | ||
} | ||
if (!rule.params.length) { | ||
rule.raws.afterName = ''; | ||
} | ||
}); | ||
}; | ||
function hasAllBug(browser) { | ||
return ~['ie 10', 'ie 11'].indexOf(browser); | ||
} | ||
var _default = _postcss.default.plugin('postcss-minify-params', () => { | ||
return (css, result) => { | ||
const resultOpts = result.opts || {}; | ||
const browsers = (0, _browserslist.default)(null, { | ||
stats: resultOpts.stats, | ||
path: __dirname, | ||
env: resultOpts.env | ||
}); | ||
return css.walkAtRules(transform.bind(null, browsers.some(hasAllBug))); | ||
}; | ||
}); | ||
exports.default = _default; | ||
module.exports = exports.default; |
{ | ||
"name": "postcss-minify-params", | ||
"version": "1.2.2", | ||
"version": "4.0.0-nightly.2020.7.24", | ||
"description": "Minify at-rule params with PostCSS", | ||
@@ -19,38 +19,23 @@ "keywords": [ | ||
"license": "MIT", | ||
"repository": "ben-eb/postcss-minify-params", | ||
"repository": "cssnano/cssnano", | ||
"bugs": { | ||
"url": "https://github.com/ben-eb/postcss-minify-params/issues" | ||
"url": "https://github.com/cssnano/cssnano/issues" | ||
}, | ||
"homepage": "https://github.com/ben-eb/postcss-minify-params", | ||
"homepage": "https://github.com/cssnano/cssnano", | ||
"dependencies": { | ||
"alphanum-sort": "^1.0.1", | ||
"postcss": "^5.0.2", | ||
"postcss-value-parser": "^3.0.2", | ||
"alphanum-sort": "^1.0.2", | ||
"browserslist": "^4.6.0", | ||
"cssnano-utils": "nightly", | ||
"postcss": "^7.0.16", | ||
"postcss-value-parser": "^3.3.1", | ||
"uniqs": "^2.0.0" | ||
}, | ||
"scripts": { | ||
"prepublish": "del-cli dist && cross-env BABEL_ENV=publish buble src -o dist", | ||
"pretest": "eslint src", | ||
"report": "nyc report --reporter=html", | ||
"test": "cross-env BABEL_ENV=test nyc mocha test --compilers js:buble/register" | ||
"prebuild": "", | ||
"build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.js --out-dir dist --ignore \"**/__tests__/\"", | ||
"prepublish": "" | ||
}, | ||
"devDependencies": { | ||
"buble": "^0.12.5", | ||
"cross-env": "^2.0.0", | ||
"del-cli": "^0.2.0", | ||
"eslint": "^2.13.1", | ||
"eslint-config-postcss": "^2.0.2", | ||
"mocha": "^2.5.3", | ||
"nyc": "^7.0.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "postcss", | ||
"env": { | ||
"mocha": true | ||
} | ||
}, | ||
"nyc": { | ||
"sourceMap": true, | ||
"instrument": true | ||
"engines": { | ||
"node": ">=6.9.0" | ||
} | ||
} | ||
} |
@@ -29,6 +29,12 @@ # postcss-minify-params [![Build Status][ci-img]][ci] | ||
## Contributors | ||
See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md). | ||
## License | ||
MIT © [Bogdan Chadkin](mailto:trysound@yandex.ru) | ||
[PostCSS]: https://github.com/postcss/postcss | ||
[ci-img]: https://travis-ci.org/ben-eb/postcss-minify-params.svg | ||
[ci]: https://travis-ci.org/ben-eb/postcss-minify-params | ||
[ci-img]: https://travis-ci.org/cssnano/postcss-minify-params.svg | ||
[ci]: https://travis-ci.org/cssnano/postcss-minify-params |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
6873
0.69%0
-100%0
-100%40
17.65%6
50%4
-20%89
-3.26%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated