Skip to content

Commit

Permalink
updated the minify function to exclude calc
Browse files Browse the repository at this point in the history
  • Loading branch information
jleeson committed Dec 12, 2024
1 parent f640ae2 commit 4572d5c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,20 @@ export default (options = {}) => {

/* minify css */
const minifyCSS = (content) => {
const calc_functions = [];
const calc_regex = /\bcalc\(([^)]+)\)/g;
const comments = /("(?:[^"\\]+|\\.)*"|'(?:[^'\\]+|\\.)*')|\/\*[\s\S]*?\*\//g;
const syntax = /("(?:[^"\\]+|\\.)*"|'(?:[^'\\]+|\\.)*')|\s*([{};,>~])\s*|\s*([*$~^|]?=)\s*|\s+([+-])(?=.*\{)|([[(:])\s+|\s+([\])])|\s+(:)(?![^}]*\{)|^\s+|\s+$|(\s)\s+(?![^(]*\))/g;
return content.replace(comments, "$1").replace(syntax, "$1$2$3$4$5$6$7$8").replace(/\n+/g, " ");

return content
.replace(calc_regex, (_, group) => {
calc_functions.push(group);
return "__CALC__";
})
.replace(comments, "$1")
.replace(syntax, "$1$2$3$4$5$6$7$8")
.replace(/__CALC__/g, () => `calc(${calc_functions.shift()})`)
.replace(/\n+/g, " ");
};

return {
Expand Down

0 comments on commit 4572d5c

Please sign in to comment.