Skip to content

Commit 30c0bac

Browse files
committed
fixed minification support for nested css
1 parent 7da33d5 commit 30c0bac

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ export default (options = {}) => {
3232

3333
/* minify css */
3434
const minifyCSS = (css) => {
35-
/* Step 1: Remove comments but preserve quoted strings */
36-
return css.replace(/("([^"\\]|\\.)*"|'([^'\\]|\\.)*')|\/\*[^]*?\*\//g, (_, quoted) => quoted || "")
35+
const functions = [];
36+
37+
/* collect function expressions and insert placeholders */
38+
return css.replace(/\b(calc|min|max|clamp)\(([^()]*(\([^()]*\))*[^()]*)\)/g, (match) => { functions.push(match); return `___F${functions.length - 1}___`; })
39+
/* Step 1: Remove comments but preserve quoted strings */
40+
.replace(/("([^"\\]|\\.)*"|'([^'\\]|\\.)*')|\/\*[^]*?\*\//g, (_, quoted) => quoted || "")
3741
/* Step 2: Remove spaces around ; and } — keep the closing brace */
3842
.replace(/\s*;\s*(})/g, ";$1")
3943
/* Step 3: Remove spaces around meta characters and operators */
@@ -51,7 +55,9 @@ export default (options = {}) => {
5155
/* Step 9: Collapse multiple spaces into one */
5256
.replace(/(\s)\s+/g, "$1")
5357
/* Step 10: Replace newlines characters with a space. */
54-
.replace(/\n+/g, " ");
58+
.replace(/\n+/g, " ")
59+
/* Step 11: Restore all function expressions */
60+
.replace(/___F(\d+)___/g, (_, index) => functions[index]);
5561
};
5662

5763
return {

0 commit comments

Comments
 (0)