[6a3a178] | 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 |
|
---|
[e29cc2e] | 44 | function sortAndDedupe(items) {
|
---|
| 45 | return (0, _alphanumSort.default)([...new Set(items)], {
|
---|
| 46 | insensitive: true
|
---|
| 47 | }).join();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[6a3a178] | 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);
|
---|
[e29cc2e] | 89 | rule.params = sortAndDedupe((0, _cssnanoUtils.getArguments)(params).map(split));
|
---|
[6a3a178] | 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; |
---|