@@ -49,14 +49,15 @@ const defaults = {
4949 replace : true ,
5050 mediaQuery : false ,
5151 exclude : / n o d e _ m o d u l e s / i,
52- customUnitList : [ ]
52+ customUnitList : [ ] ,
53+ unitList : [ '*' ]
5354} ;
5455
5556function escapeRegExp ( string ) {
5657 return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
5758}
5859
59- function createUnitRegex ( customUnitList ) {
60+ function createUnitRegex ( customUnitList , unitList ) {
6061 let userUnits = Array . isArray ( customUnitList )
6162 ? customUnitList . filter (
6263 ( u ) => typeof u === 'string' && u . trim ( ) && / ^ [ a - z A - Z % ] + $ / . test ( u )
@@ -68,7 +69,27 @@ function createUnitRegex(customUnitList) {
6869 unitSet . add ( u ) ;
6970 }
7071
71- const unitStr = Array . from ( unitSet ) . map ( escapeRegExp ) . join ( '|' ) ;
72+ // Filter units based on unitList if provided
73+ let filteredUnits = Array . from ( unitSet ) ;
74+ if ( Array . isArray ( unitList ) ) {
75+ if ( unitList . length === 0 ) {
76+ // Empty unitList means no units should be processed
77+ filteredUnits = [ ] ;
78+ } else {
79+ const satisfyUnitList = createUnitListMatcher ( unitList ) ;
80+ filteredUnits = filteredUnits . filter ( unit => satisfyUnitList ( unit ) ) ;
81+ }
82+ }
83+
84+ const unitStr = filteredUnits . map ( escapeRegExp ) . join ( '|' ) ;
85+
86+ // If no units to process, create a regex that won't match any units
87+ if ( unitStr === '' ) {
88+ return new RegExp (
89+ `"[^"]+"|'[^']+'|url\\([^)]+\\)|var\\([^)]+\\)` ,
90+ 'g'
91+ ) ;
92+ }
7293
7394 return new RegExp (
7495 `"[^"]+"|'[^']+'|url\\([^)]+\\)|var\\([^)]+\\)|(\\d*\\.?\\d+)(${ unitStr } )` ,
@@ -102,7 +123,7 @@ function createUnitReplace(processor, unitPrecision, root) {
102123
103124 const fixedVal = toFixed ( newValue , unitPrecision ) ;
104125
105- return fixedVal === 0 ? "0" : fixedVal + newUnit ;
126+ return fixedVal + newUnit ;
106127 } ;
107128}
108129
@@ -174,15 +195,63 @@ function createPropListMatcher(propList) {
174195 } ;
175196}
176197
198+ function createUnitListMatcher ( unitList ) {
199+ const hasWild = unitList . indexOf ( "*" ) > - 1 ;
200+ const matchAll = hasWild && unitList . length === 1 ;
201+ const lists = {
202+ exact : filterPropList . exact ( unitList ) ,
203+ contain : filterPropList . contain ( unitList ) ,
204+ startWith : filterPropList . startWith ( unitList ) ,
205+ endWith : filterPropList . endWith ( unitList ) ,
206+ notExact : filterPropList . notExact ( unitList ) ,
207+ notContain : filterPropList . notContain ( unitList ) ,
208+ notStartWith : filterPropList . notStartWith ( unitList ) ,
209+ notEndWith : filterPropList . notEndWith ( unitList )
210+ } ;
211+
212+ return unit => {
213+ if ( matchAll ) {
214+ return true ;
215+ }
216+
217+ return (
218+ ( hasWild ||
219+ lists . exact . indexOf ( unit ) > - 1 ||
220+ lists . contain . some ( function ( m ) {
221+ return unit . indexOf ( m ) > - 1 ;
222+ } ) ||
223+ lists . startWith . some ( function ( m ) {
224+ return unit . indexOf ( m ) === 0 ;
225+ } ) ||
226+ lists . endWith . some ( function ( m ) {
227+ return unit . indexOf ( m ) === unit . length - m . length ;
228+ } ) ) &&
229+ ! (
230+ lists . notExact . indexOf ( unit ) > - 1 ||
231+ lists . notContain . some ( function ( m ) {
232+ return unit . indexOf ( m ) > - 1 ;
233+ } ) ||
234+ lists . notStartWith . some ( function ( m ) {
235+ return unit . indexOf ( m ) === 0 ;
236+ } ) ||
237+ lists . notEndWith . some ( function ( m ) {
238+ return unit . indexOf ( m ) === unit . length - m . length ;
239+ } )
240+ )
241+ ) ;
242+ } ;
243+ }
244+
177245module . exports = ( options = { } ) => {
178246 const opts = Object . assign ( { } , defaults , options ) ;
179247 const satisfyPropList = createPropListMatcher ( opts . propList ) ;
180248 const exclude = opts . exclude ;
181- const customUnitList = opts . customUnitList
249+ const customUnitList = opts . customUnitList ;
250+ const unitList = opts . unitList ;
182251 let isExcludeFile = false ;
183252 let unitReplace ;
184253
185- const unitRegex = createUnitRegex ( customUnitList ) ;
254+ const unitRegex = createUnitRegex ( customUnitList , unitList ) ;
186255
187256 return {
188257 postcssPlugin : "postcss-unit-processor" ,
0 commit comments