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(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
---|
15 |
|
---|
16 | 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; }
|
---|
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; |
---|