11export namespace variableCompress {
2-
32 export type skip = ( variableName : string ) => boolean | undefined ;
43
54 export type parameters = skip | string ;
6-
75}
8- const postcssPlugin = ' postcss-variable-compress' ;
6+ const postcssPlugin = " postcss-variable-compress" ;
97
10- let processed = Symbol ( ' processed' ) ;
8+ let processed = Symbol ( " processed" ) ;
119let renamedVariables : string [ ] = [ ] ;
1210let cssVariables = - 1 ;
1311let pureSkips : string [ ] = [ ] ;
1412let scriptBasedSkips : variableCompress . skip [ ] = [ ] ;
1513let cssVariablesMap = new Map ( ) ;
1614
1715function scriptCheck ( val : string ) {
18-
19- const should = scriptBasedSkips . findIndex ( E => E ( val ) ) ;
20- return ( should > - 1 ) ;
21-
16+ const should = scriptBasedSkips . findIndex ( ( E ) => E ( val ) ) ;
17+ return should > - 1 ;
2218}
2319
2420function shouldRun ( val : string ) {
25-
2621 let should = true ;
2722
2823 if ( renamedVariables . indexOf ( val ) > - 1 ) {
@@ -37,27 +32,24 @@ function shouldRun(val: string) {
3732}
3833
3934function increase ( ) {
40-
4135 cssVariables = cssVariables + 1 ;
4236
4337 let temp = cssVariables . toString ( 36 ) ;
4438
45- pureSkips . forEach ( E => {
39+ pureSkips . forEach ( ( E ) => {
4640 if ( E === temp ) {
4741 temp = cssVariables . toString ( 36 ) ;
4842 cssVariables = cssVariables + 1 ;
4943 }
5044 } ) ;
51-
5245}
5346
5447function replacer ( match : string ) {
55-
5648 if ( ! shouldRun ( match ) ) return match ;
5749 let exist = cssVariablesMap . get ( match ) ;
5850
5951 if ( ! exist ) {
60- exist = '--' + ( cssVariables ) . toString ( 36 ) ;
52+ exist = "--" + cssVariables . toString ( 36 ) ;
6153 increase ( ) ;
6254 cssVariablesMap . set ( match , exist ) ;
6355 renamedVariables . push ( exist ) ;
@@ -66,14 +58,13 @@ function replacer(match: string) {
6658 return exist ;
6759}
6860
69- function map ( j : import ( 'postcss' ) . Declaration ) {
70-
61+ function map ( j : import ( "postcss" ) . Declaration ) {
7162 let prop = j . prop ;
7263 if ( prop && j . variable && shouldRun ( prop ) ) {
7364 let old = prop ;
7465 let exist = cssVariablesMap . get ( old ) ;
7566 if ( ! exist ) {
76- exist = '--' + cssVariables . toString ( 36 ) ;
67+ exist = "--" + cssVariables . toString ( 36 ) ;
7768 increase ( ) ;
7869 cssVariablesMap . set ( old , exist ) ;
7970 renamedVariables . push ( exist ) ;
@@ -83,54 +74,49 @@ function map(j: import('postcss').Declaration) {
8374 }
8475
8576 let value = j . value ;
86- if ( value && value . includes ( ' var(--' ) && value . length <= 1000 ) {
77+ if ( value && value . includes ( " var(--" ) && value . length <= 1000 ) {
8778 value = value . replace ( / - - [ \w - _ ] { 1 , 1000 } / g, replacer ) ;
8879 j . value = value ;
8980 }
9081
9182 // @ts -ignore
9283 j [ processed ] = true ;
93-
9484}
9585
96-
97- module . exports = function variableCompress ( opts ?: variableCompress . parameters [ ] ) {
98-
99- processed = Symbol ( 'processed' ) ;
86+ export function variableCompress ( opts ?: variableCompress . parameters [ ] ) {
87+ processed = Symbol ( "processed" ) ;
10088
10189 renamedVariables = [ ] ;
10290 cssVariables = - 1 ;
10391 pureSkips = [ ] ;
10492 scriptBasedSkips = [ ] ;
10593 cssVariablesMap = new Map ( ) ;
10694
107- opts ?. forEach ( E => {
108- if ( typeof E === 'string' ) {
109-
95+ opts ?. forEach ( ( E ) => {
96+ if ( typeof E === "string" ) {
11097 let name = E ;
11198 let cssName = E ;
11299
113- if ( E . slice ( 0 , 2 ) === '--' ) {
100+ if ( E . slice ( 0 , 2 ) === "--" ) {
114101 name = E . slice ( 2 ) ;
115102 } else {
116- cssName = '--' + E ;
103+ cssName = "--" + E ;
117104 }
118105
119106 pureSkips . push ( name ) ;
120107 cssVariablesMap . set ( cssName , cssName ) ;
121- }
122- else
123- scriptBasedSkips . push ( E ) ;
108+ } else scriptBasedSkips . push ( E ) ;
124109 } ) ;
125110
126111 increase ( ) ;
127112
128113 return {
129114 postcssPlugin,
130115 Declaration : {
131- '*' : map
132- }
116+ "*" : map ,
117+ } ,
133118 } ;
134- } ;
119+ }
135120
121+ module . exports = variableCompress ;
136122module . exports . postcss = true ;
0 commit comments