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']; // These properties only accept percentages, so no point in trying to transform
|
---|
19 |
|
---|
20 | const notALength = new Set(['descent-override', 'ascent-override', 'font-stretch', 'size-adjust', 'line-gap-override']); // Can't change the unit on these properties when they're 0
|
---|
21 |
|
---|
22 | const keepWhenZero = new Set(['stroke-dashoffset', 'stroke-width', 'line-height']);
|
---|
23 | /*
|
---|
24 | * Numbers without digits after the dot are technically invalid,
|
---|
25 | * but in that case css-value-parser returns the dot as part of the unit,
|
---|
26 | * so we use this to remove the dot.
|
---|
27 | */
|
---|
28 |
|
---|
29 | function stripLeadingDot(item) {
|
---|
30 | if (item.charCodeAt(0) === '.'.charCodeAt(0)) {
|
---|
31 | return item.slice(1);
|
---|
32 | } else {
|
---|
33 | return item;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | function parseWord(node, opts, keepZeroUnit) {
|
---|
38 | const pair = (0, _postcssValueParser.unit)(node.value);
|
---|
39 |
|
---|
40 | if (pair) {
|
---|
41 | const num = Number(pair.number);
|
---|
42 | const u = stripLeadingDot(pair.unit);
|
---|
43 |
|
---|
44 | if (num === 0) {
|
---|
45 | node.value = 0 + (keepZeroUnit || !~LENGTH_UNITS.indexOf(u.toLowerCase()) && u !== '%' ? u : '');
|
---|
46 | } else {
|
---|
47 | node.value = (0, _convert.default)(num, u, opts);
|
---|
48 |
|
---|
49 | if (typeof opts.precision === 'number' && u.toLowerCase() === 'px' && ~pair.number.indexOf('.')) {
|
---|
50 | const precision = Math.pow(10, opts.precision);
|
---|
51 | node.value = Math.round(parseFloat(node.value) * precision) / precision + u;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | function clampOpacity(node) {
|
---|
58 | const pair = (0, _postcssValueParser.unit)(node.value);
|
---|
59 |
|
---|
60 | if (!pair) {
|
---|
61 | return;
|
---|
62 | }
|
---|
63 |
|
---|
64 | let num = Number(pair.number);
|
---|
65 |
|
---|
66 | if (num > 1) {
|
---|
67 | node.value = pair.unit === '%' ? num + pair.unit : 1 + pair.unit;
|
---|
68 | } else if (num < 0) {
|
---|
69 | node.value = 0 + pair.unit;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | function shouldKeepZeroUnit(decl) {
|
---|
74 | const {
|
---|
75 | parent
|
---|
76 | } = decl;
|
---|
77 | const lowerCasedProp = decl.prop.toLowerCase();
|
---|
78 | return ~decl.value.indexOf('%') && (lowerCasedProp === 'max-height' || lowerCasedProp === 'height') || parent.parent && parent.parent.name && parent.parent.name.toLowerCase() === 'keyframes' && lowerCasedProp === 'stroke-dasharray' || keepWhenZero.has(lowerCasedProp);
|
---|
79 | }
|
---|
80 |
|
---|
81 | function transform(opts, decl) {
|
---|
82 | const lowerCasedProp = decl.prop.toLowerCase();
|
---|
83 |
|
---|
84 | if (~lowerCasedProp.indexOf('flex') || lowerCasedProp.indexOf('--') === 0 || notALength.has(lowerCasedProp)) {
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | decl.value = (0, _postcssValueParser.default)(decl.value).walk(node => {
|
---|
89 | const lowerCasedValue = node.value.toLowerCase();
|
---|
90 |
|
---|
91 | if (node.type === 'word') {
|
---|
92 | parseWord(node, opts, shouldKeepZeroUnit(decl));
|
---|
93 |
|
---|
94 | if (lowerCasedProp === 'opacity' || lowerCasedProp === 'shape-image-threshold') {
|
---|
95 | clampOpacity(node);
|
---|
96 | }
|
---|
97 | } else if (node.type === 'function') {
|
---|
98 | if (lowerCasedValue === 'calc' || lowerCasedValue === 'min' || lowerCasedValue === 'max' || lowerCasedValue === 'clamp' || lowerCasedValue === 'hsl' || lowerCasedValue === 'hsla') {
|
---|
99 | (0, _postcssValueParser.walk)(node.nodes, n => {
|
---|
100 | if (n.type === 'word') {
|
---|
101 | parseWord(n, opts, true);
|
---|
102 | }
|
---|
103 | });
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (lowerCasedValue === 'url') {
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }).toString();
|
---|
112 | }
|
---|
113 |
|
---|
114 | const plugin = 'postcss-convert-values';
|
---|
115 |
|
---|
116 | function pluginCreator(opts = {
|
---|
117 | precision: false
|
---|
118 | }) {
|
---|
119 | return {
|
---|
120 | postcssPlugin: plugin,
|
---|
121 |
|
---|
122 | OnceExit(css) {
|
---|
123 | css.walkDecls(transform.bind(null, opts));
|
---|
124 | }
|
---|
125 |
|
---|
126 | };
|
---|
127 | }
|
---|
128 |
|
---|
129 | pluginCreator.postcss = true;
|
---|
130 | var _default = pluginCreator;
|
---|
131 | exports.default = _default;
|
---|
132 | module.exports = exports.default; |
---|