|
| 1 | +export namespace variableCompressSplitFiles { |
| 2 | + export type skip = (variableName: string) => boolean | undefined; |
| 3 | + export type parameters = skip | string; |
| 4 | +} |
| 5 | + |
| 6 | +const postcssPlugin = 'postcss-variable-compress'; |
| 7 | + |
| 8 | +let processed = Symbol('processed'); |
| 9 | +let renamedVariables: string[] = []; |
| 10 | +let cssVariables = -1; |
| 11 | +let pureSkips: string[] = []; |
| 12 | +let scriptBasedSkips: variableCompressSplitFiles.skip[] = []; |
| 13 | +let cssVariablesMap = new Map(); |
| 14 | + |
| 15 | +function scriptCheck(val: string) { |
| 16 | + |
| 17 | + const should = scriptBasedSkips.findIndex(E => E(val)); |
| 18 | + return (should > -1); |
| 19 | + |
| 20 | +} |
| 21 | + |
| 22 | +function shouldRun(val: string) { |
| 23 | + |
| 24 | + let should = true; |
| 25 | + |
| 26 | + if (renamedVariables.indexOf(val) > -1) { |
| 27 | + should = false; |
| 28 | + } |
| 29 | + |
| 30 | + if (should && scriptCheck(val)) { |
| 31 | + should = false; |
| 32 | + } |
| 33 | + |
| 34 | + return should; |
| 35 | +} |
| 36 | + |
| 37 | +function increase() { |
| 38 | + |
| 39 | + cssVariables = cssVariables + 1; |
| 40 | + |
| 41 | + let temp = cssVariables.toString(36); |
| 42 | + |
| 43 | + pureSkips.forEach(E => { |
| 44 | + if (E === temp) { |
| 45 | + temp = cssVariables.toString(36); |
| 46 | + cssVariables = cssVariables + 1; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +function replacer(match: string) { |
| 53 | + |
| 54 | + if (!shouldRun(match)) return match; |
| 55 | + let exist = cssVariablesMap.get(match); |
| 56 | + |
| 57 | + if (!exist) { |
| 58 | + exist = '--' + (cssVariables).toString(36); |
| 59 | + increase(); |
| 60 | + cssVariablesMap.set(match, exist); |
| 61 | + renamedVariables.push(exist); |
| 62 | + } |
| 63 | + |
| 64 | + return exist; |
| 65 | +} |
| 66 | + |
| 67 | +function map(j: import('postcss').Declaration) { |
| 68 | + |
| 69 | + let prop = j.prop; |
| 70 | + if (prop && j.variable && shouldRun(prop)) { |
| 71 | + let old = prop; |
| 72 | + let exist = cssVariablesMap.get(old); |
| 73 | + if (!exist) { |
| 74 | + exist = '--' + cssVariables.toString(36); |
| 75 | + increase(); |
| 76 | + cssVariablesMap.set(old, exist); |
| 77 | + renamedVariables.push(exist); |
| 78 | + } |
| 79 | + prop = exist; |
| 80 | + j.prop = prop; |
| 81 | + } |
| 82 | + |
| 83 | + let value = j.value; |
| 84 | + if (value && value.includes('var(--') && value.length <= 1000) { |
| 85 | + value = value.replace(/--[\w-_]{1,1000}/g, replacer); |
| 86 | + j.value = value; |
| 87 | + } |
| 88 | + |
| 89 | + // @ts-ignore |
| 90 | + j[processed] = true; |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +module.exports = function variableCompressSplitFiles(opts?: variableCompressSplitFiles.parameters[]) { |
| 96 | + |
| 97 | + processed = Symbol('processed'); |
| 98 | + |
| 99 | + opts?.forEach(E => { |
| 100 | + if (typeof E === 'string') { |
| 101 | + |
| 102 | + let name = E; |
| 103 | + let cssName = E; |
| 104 | + |
| 105 | + if (E.slice(0, 2) === '--') { |
| 106 | + name = E.slice(2); |
| 107 | + } else { |
| 108 | + cssName = '--' + E; |
| 109 | + } |
| 110 | + |
| 111 | + pureSkips.push(name); |
| 112 | + cssVariablesMap.set(cssName, cssName); |
| 113 | + } |
| 114 | + else |
| 115 | + scriptBasedSkips.push(E); |
| 116 | + }); |
| 117 | + |
| 118 | + increase(); |
| 119 | + |
| 120 | + return { |
| 121 | + postcssPlugin, |
| 122 | + Declaration: { |
| 123 | + '*': map |
| 124 | + } |
| 125 | + }; |
| 126 | +}; |
| 127 | + |
| 128 | +module.exports.postcss = true; |
0 commit comments