@@ -26,61 +26,27 @@ export default (options = {}) => {
2626 return result ;
2727 } ;
2828
29- const findClosingParenthesis = ( content , from ) => {
30- let depth = 1 ;
31- for ( let i = from ; i < content . length ; i += 1 ) {
32- const char = content [ i ] ;
33- if ( char == "(" ) {
34- depth += 1 ;
35- } else if ( char == ")" ) {
36- if ( depth === 1 ) {
37- return i ;
38- }
39-
40- depth -= 1 ;
41- }
42- }
43-
44- return undefined ;
45- } ;
46-
47- const replaceCalc = ( content ) => {
48- const calc_functions = [ ] ;
49- const calc_start_regex = / \b c a l c \( [ ^ ) ] / g;
50- let match ;
51- while ( ( match = calc_start_regex . exec ( content ) ) !== null ) {
52- if ( match . index === calc_start_regex . lastIndex ) {
53- break ;
54- }
55-
56- const start = match . index + 5 ;
57- const end = findClosingParenthesis ( content , start ) ;
58- if ( end === undefined ) {
59- throw new Error ( "Missing closing parenthesis in calc() function" ) ;
60- }
61-
62- const calc_function = content . slice ( start , end ) ;
63- calc_functions . push ( calc_function ) ;
64-
65- const before = content . slice ( 0 , match . index ) ;
66- const after = content . slice ( end + 1 ) ;
67- content = `${ before } __CALC__${ after } ` ;
68- }
69-
70- return { calc_functions, result : content } ;
71- } ;
72-
7329 /* minify css */
74- const minifyCSS = ( content ) => {
75- const comments = / ( " (?: [ ^ " \\ ] + | \\ .) * " | ' (?: [ ^ ' \\ ] + | \\ .) * ' ) | \/ \* [ \s \S ] * ?\* \/ / g;
76- const syntax = / ( " (?: [ ^ " \\ ] + | \\ .) * " | ' (?: [ ^ ' \\ ] + | \\ .) * ' ) | \s * ( [ { } ; , > ~ ] ) \s * | \s * ( [ * $ ~ ^ | ] ? = ) \s * | \s + ( [ + - ] ) (? = .* \{ ) | ( [ [ ( : ] ) \s + | \s + ( [ \] ) ] ) | \s + ( : ) (? ! [ ^ } ] * \{ ) | ^ \s + | \s + $ | ( \s ) \s + (? ! [ ^ ( ] * \) ) / g;
77-
78- const { result, calc_functions } = replaceCalc ( content ) ;
79-
80- return result
81- . replace ( comments , "$1" )
82- . replace ( syntax , "$1$2$3$4$5$6$7$8" )
83- . replace ( / _ _ C A L C _ _ / g, ( ) => `calc(${ calc_functions . shift ( ) } )` )
30+ const minifyCSS = ( css ) => {
31+ /* Step 1: Remove comments but preserve quoted strings */
32+ return css . replace ( / ( " ( [ ^ " \\ ] | \\ .) * " | ' ( [ ^ ' \\ ] | \\ .) * ' ) | \/ \* [ ^ ] * ?\* \/ / g, ( _ , quoted ) => quoted || "" )
33+ /* Step 2: Remove spaces around ; and } — keep the closing brace */
34+ . replace ( / \s * ; \s * ( } ) / g, ";$1" )
35+ /* Step 3: Remove spaces around meta characters and operators */
36+ . replace ( / \s * ( [ * $ ~ ^ | ] ? = | [ { } ; , > ~ ] | ! i m p o r t a n t ) \s * / g, "$1" )
37+ /* Step 4: Remove spaces around + and - in selectors before { */
38+ . replace ( / \s * ( [ + - ] ) \s * (? = [ ^ } ] * \{ ) / g, "$1" )
39+ /* Step 5: Remove space after [, ( */
40+ . replace ( / ( [ [ ( ] ) \s + / g, "$1" )
41+ /* Step 6: Remove space before ], ) */
42+ . replace ( / \s + ( [ \] ) ] ) / g, "$1" )
43+ /* Step 7: Remove space around colon, only outside selectors */
44+ . replace ( / ( { [ ^ } ] * ?) \s * : \s * / g, "$1:" )
45+ /* Step 8: Trim leading and trailing whitespace */
46+ . replace ( / ^ \s + | \s + $ / g, "" )
47+ /* Step 9: Collapse multiple spaces into one */
48+ . replace ( / ( \s ) \s + / g, "$1" )
49+ /* Step 10: Replace newlines characters with a space. */
8450 . replace ( / \n + / g, " " ) ;
8551 } ;
8652
0 commit comments