Skip to content

Commit

Permalink
improved css minifier
Browse files Browse the repository at this point in the history
  • Loading branch information
jleeson committed Oct 10, 2024
1 parent f338a72 commit 842aaff
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,15 @@ export default (options = {}) => {
/* minify css */
const minifyCSS = (content) => {
const comments = /("(?:[^"\\]+|\\.)*"|'(?:[^'\\]+|\\.)*')|\/\*[\s\S]*?\*\//g;
const everythingElse = /("(?:[^"\\]+|\\.)*"|'(?:[^'\\]+|\\.)*')|\s*([{};,>~])\s*|\s*([*$~^|]?=)\s*|\s*([+-])\s*(?=[^}]*{)|([[(:])\s+|\s+([\])])|\s+(:)(?![^}]*\{)|^\s+|\s+$|(\s)\s+/g;
const syntax = /("(?:[^"\\]+|\\.)*"|'(?:[^'\\]+|\\.)*')|\s*([{};,>~])\s*|\s*([*$~^|]?=)\s*|\s+([+-])(?=.*\{)|([[(:])\s+|\s+([\])])|\s+(:)(?![^}]*\{)|^\s+|\s+$|(\s)\s+(?![^(]*\))/g;

const searchPatterns = [comments, everythingElse];
const replacePatterns = ["$1", "$1$2$3$4$5$6$7$8"];
content = content.replace(comments, "$1");
content = content.replace(syntax, "$1$2$3$4$5$6$7$8");
content = content.replace(/\n+/g, "");

let result = content;
searchPatterns.forEach((pattern, index) => {
result = result.replace(pattern, replacePatterns[index]);
});

/* Remove any extra newlines that may still be present */
result = result.replace(/\n+/g, "");

return result;
return content;
};


return {
name: "import-css",

Expand Down

0 comments on commit 842aaff

Please sign in to comment.