|
1 | | -/** |
2 | | - * @param {string[]} opts prefix your input with -- like any css variable |
3 | | - */ |
4 | | -module.exports = (opts = []) => { |
5 | | - const VariablesMap = new Map(); |
6 | | - |
7 | | - const processed = Symbol('processed'); |
8 | | - |
9 | | - const History = []; |
10 | | - let Variables = -1; |
11 | | - let Skips = []; |
12 | | - /** |
13 | | - * @param {string} val |
14 | | - */ |
15 | | - function shouldRun (val) { |
16 | | - if (History.indexOf(val) > -1) { |
17 | | - return false; |
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const postcssPlugin = 'postcss-variable-compress'; |
| 4 | +let processed = Symbol('processed'); |
| 5 | +let renamedVariables = []; |
| 6 | +let cssVariables = -1; |
| 7 | +let pureSkips = []; |
| 8 | +let scriptBasedSkips = []; |
| 9 | +let cssVariablesMap = new Map(); |
| 10 | +function scriptCheck(val) { |
| 11 | + const should = scriptBasedSkips.findIndex(E => E(val)); |
| 12 | + return (should > -1); |
| 13 | +} |
| 14 | +function shouldRun(val) { |
| 15 | + let should = true; |
| 16 | + if (renamedVariables.indexOf(val) > -1) { |
| 17 | + should = false; |
18 | 18 | } |
19 | | - return true; |
20 | | - } |
21 | | - |
22 | | - function increase () { |
23 | | - Variables = Variables + 1; |
24 | | - let temp = Variables.toString(36); |
25 | | - Skips.forEach(E => { |
26 | | - if (E === temp) { |
27 | | - temp = Variables.toString(36); |
28 | | - Variables = Variables + 1; |
29 | | - } |
| 19 | + if (should && scriptCheck(val)) { |
| 20 | + should = false; |
| 21 | + } |
| 22 | + return should; |
| 23 | +} |
| 24 | +function increase() { |
| 25 | + cssVariables = cssVariables + 1; |
| 26 | + let temp = cssVariables.toString(36); |
| 27 | + pureSkips.forEach(E => { |
| 28 | + if (E === temp) { |
| 29 | + temp = cssVariables.toString(36); |
| 30 | + cssVariables = cssVariables + 1; |
| 31 | + } |
30 | 32 | }); |
31 | | - } |
32 | | - |
33 | | - /** |
34 | | - * @param {string} match |
35 | | - */ |
36 | | - function replacer (match) { |
37 | | - |
38 | | - if (!shouldRun(match)) return match; |
39 | | - let exist = VariablesMap.get(match); |
40 | | - |
| 33 | +} |
| 34 | +function replacer(match) { |
| 35 | + if (!shouldRun(match)) |
| 36 | + return match; |
| 37 | + let exist = cssVariablesMap.get(match); |
41 | 38 | if (!exist) { |
42 | | - exist = '--' + (Variables).toString(36); |
43 | | - increase(); |
44 | | - VariablesMap.set(match, exist); |
45 | | - History.push(exist); |
| 39 | + exist = '--' + (cssVariables).toString(36); |
| 40 | + increase(); |
| 41 | + cssVariablesMap.set(match, exist); |
| 42 | + renamedVariables.push(exist); |
46 | 43 | } |
47 | | - |
48 | 44 | return exist; |
49 | | - } |
50 | | - |
51 | | - /** |
52 | | - * @param {import('postcss').Declaration} j |
53 | | - */ |
54 | | - function map (j) { |
55 | | - |
| 45 | +} |
| 46 | +function map(j) { |
56 | 47 | let prop = j.prop; |
57 | 48 | if (prop && j.variable && shouldRun(prop)) { |
58 | | - let old = prop; |
59 | | - let exist = VariablesMap.get(old); |
60 | | - if (!exist) { |
61 | | - exist = '--' + Variables.toString(36); |
62 | | - increase(); |
63 | | - VariablesMap.set(old, exist); |
64 | | - History.push(exist); |
65 | | - } |
66 | | - prop = exist; |
67 | | - j.prop = prop; |
| 49 | + let old = prop; |
| 50 | + let exist = cssVariablesMap.get(old); |
| 51 | + if (!exist) { |
| 52 | + exist = '--' + cssVariables.toString(36); |
| 53 | + increase(); |
| 54 | + cssVariablesMap.set(old, exist); |
| 55 | + renamedVariables.push(exist); |
| 56 | + } |
| 57 | + prop = exist; |
| 58 | + j.prop = prop; |
68 | 59 | } |
69 | | - |
70 | 60 | let value = j.value; |
71 | 61 | if (value && value.includes('var(--') && value.length <= 1000) { |
72 | | - value = value.replace(/--[\w-_]{1,1000}/g, replacer); |
73 | | - j.value = value; |
| 62 | + value = value.replace(/--[\w-_]{1,1000}/g, replacer); |
| 63 | + j.value = value; |
74 | 64 | } |
75 | | - |
| 65 | + // @ts-ignore |
76 | 66 | j[processed] = true; |
77 | | - |
78 | | - } |
79 | | - |
80 | | - opts.forEach(E => VariablesMap.set(E, E)); |
81 | | - opts.forEach(E => Skips.push(E.replace('--', ''))); |
82 | | - |
83 | | - increase(); |
84 | | - |
85 | | - return { |
86 | | - postcssPlugin: 'postcss-variable-compress', |
87 | | - Declaration: { |
88 | | - '*': map |
89 | | - } |
90 | | - }; |
| 67 | +} |
| 68 | +module.exports = function variableCompress(opts) { |
| 69 | + processed = Symbol('processed'); |
| 70 | + renamedVariables = []; |
| 71 | + cssVariables = -1; |
| 72 | + pureSkips = []; |
| 73 | + scriptBasedSkips = []; |
| 74 | + cssVariablesMap = new Map(); |
| 75 | + opts.forEach(E => { |
| 76 | + if (typeof E === 'string') { |
| 77 | + pureSkips.push(E.replace('--', '')); |
| 78 | + cssVariablesMap.set(E, E); |
| 79 | + } |
| 80 | + else |
| 81 | + scriptBasedSkips.push(E); |
| 82 | + }); |
| 83 | + increase(); |
| 84 | + return { |
| 85 | + postcssPlugin, |
| 86 | + Declaration: { |
| 87 | + '*': map |
| 88 | + } |
| 89 | + }; |
91 | 90 | }; |
92 | | - |
93 | 91 | module.exports.postcss = true; |
0 commit comments