[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 _uniqs = _interopRequireDefault(require("uniqs"));
|
---|
| 15 |
|
---|
| 16 | var _cssnanoUtils = require("cssnano-utils");
|
---|
| 17 |
|
---|
| 18 | 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); }
|
---|
| 19 |
|
---|
| 20 | 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; }
|
---|
| 21 |
|
---|
| 22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Return the greatest common divisor
|
---|
| 26 | * of two numbers.
|
---|
| 27 | */
|
---|
| 28 | function gcd(a, b) {
|
---|
| 29 | return b ? gcd(b, a % b) : a;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | function aspectRatio(a, b) {
|
---|
| 33 | const divisor = gcd(a, b);
|
---|
| 34 | return [a / divisor, b / divisor];
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | function split(args) {
|
---|
| 38 | return args.map(arg => (0, _postcssValueParser.stringify)(arg)).join('');
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | function removeNode(node) {
|
---|
| 42 | node.value = '';
|
---|
| 43 | node.type = 'word';
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | function transform(legacy, rule) {
|
---|
| 47 | const ruleName = rule.name.toLowerCase(); // We should re-arrange parameters only for `@media` and `@supports` at-rules
|
---|
| 48 |
|
---|
| 49 | if (!rule.params || !['media', 'supports'].includes(ruleName)) {
|
---|
| 50 | return;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | const params = (0, _postcssValueParser.default)(rule.params);
|
---|
| 54 | params.walk((node, index) => {
|
---|
| 55 | if (node.type === 'div' || node.type === 'function') {
|
---|
| 56 | node.before = node.after = '';
|
---|
| 57 |
|
---|
| 58 | if (node.type === 'function' && node.nodes[4] && node.nodes[0].value.toLowerCase().indexOf('-aspect-ratio') === 3) {
|
---|
| 59 | const [a, b] = aspectRatio(node.nodes[2].value, node.nodes[4].value);
|
---|
| 60 | node.nodes[2].value = a;
|
---|
| 61 | node.nodes[4].value = b;
|
---|
| 62 | }
|
---|
| 63 | } else if (node.type === 'space') {
|
---|
| 64 | node.value = ' ';
|
---|
| 65 | } else {
|
---|
| 66 | const prevWord = params.nodes[index - 2];
|
---|
| 67 |
|
---|
| 68 | if (node.value.toLowerCase() === 'all' && rule.name.toLowerCase() === 'media' && !prevWord) {
|
---|
| 69 | const nextWord = params.nodes[index + 2];
|
---|
| 70 |
|
---|
| 71 | if (!legacy || nextWord) {
|
---|
| 72 | removeNode(node);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | if (nextWord && nextWord.value.toLowerCase() === 'and') {
|
---|
| 76 | const nextSpace = params.nodes[index + 1];
|
---|
| 77 | const secondSpace = params.nodes[index + 3];
|
---|
| 78 | removeNode(nextWord);
|
---|
| 79 | removeNode(nextSpace);
|
---|
| 80 | removeNode(secondSpace);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }, true);
|
---|
| 85 | rule.params = (0, _alphanumSort.default)((0, _uniqs.default)((0, _cssnanoUtils.getArguments)(params).map(split)), {
|
---|
| 86 | insensitive: true
|
---|
| 87 | }).join();
|
---|
| 88 |
|
---|
| 89 | if (!rule.params.length) {
|
---|
| 90 | rule.raws.afterName = '';
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | function hasAllBug(browser) {
|
---|
| 95 | return ~['ie 10', 'ie 11'].indexOf(browser);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | function pluginCreator(options = {}) {
|
---|
| 99 | const browsers = (0, _browserslist.default)(null, {
|
---|
| 100 | stats: options.stats,
|
---|
| 101 | path: __dirname,
|
---|
| 102 | env: options.env
|
---|
| 103 | });
|
---|
| 104 | return {
|
---|
| 105 | postcssPlugin: 'postcss-minify-params',
|
---|
| 106 |
|
---|
| 107 | OnceExit(css) {
|
---|
| 108 | css.walkAtRules(transform.bind(null, browsers.some(hasAllBug)));
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | };
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | pluginCreator.postcss = true;
|
---|
| 115 | var _default = pluginCreator;
|
---|
| 116 | exports.default = _default;
|
---|
| 117 | module.exports = exports.default; |
---|