Skip to content

Instantly share code, notes, and snippets.

@argentini
Last active February 27, 2024 00:02
Show Gist options
  • Save argentini/2960470e68d8809570dd2033985255fd to your computer and use it in GitHub Desktop.
Save argentini/2960470e68d8809570dd2033985255fd to your computer and use it in GitHub Desktop.
Pure dotnet (.NET) C# CSS Minifier - Minify CSS using compiled Regex
#region Compiled CSS Regex; Should be Singletons
public Regex SelectCssComments { get; } = new (@"\/\*.*?\*\/", RegexOptions.Compiled); // Select comment blocks
public Regex SelectCssLineBreaks { get; } = new (@"[\n\r]+\s*", RegexOptions.Compiled); // Select line breaks and subsequent space
// Ensure content property values use quotation marks
public Regex SelectCssContentValueWithApostrophes { get; } = new (@"content:\s*'([^']*)';", RegexOptions.Compiled); // Select content properties using apostrophes
public Regex SelectCssContentValueWithSpacePrefix { get; } = new ("""content:\s{1,}"([^\"]*)";""", RegexOptions.Compiled); Select content properties with space between the name and value
public Regex SelectCssEmptyContentValueWithSpacePrefix { get; } = new ("""content:\s*"";""", RegexOptions.Compiled); // Select empty content properties
// All checks here are excluded if inside quotations marks
public Regex SelectCssMultipleSpaces { get; } = new ("""\s+(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select multiple spaces
public Regex SelectCssLastSemicolon { get; } = new (""";}(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select last semicolon and closing brace
public Regex SelectCssSpaceThenClosingBrace { get; } = new ("""\s+}(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select spaces and closing brace
public Regex SelectCssClosingBraceThenSpace { get; } = new ("""}\s+(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select closing brace and spaces
public Regex SelectCssSpaceThenOpeningBrace { get; } = new ("""\s+{(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select spaces and opening brace
public Regex SelectCssOpeningBraceThenSpace { get; } = new ("""{\s+(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select opening brace and spaces
public Regex SelectColonThenSpace { get; } = new (""":\s+(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select spaces between property and value
public Regex SelectSpaceAfterComma { get; } = new (""",\s+(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)""", RegexOptions.Compiled); // Select spaces after commas
#endregion
var fileContent = await File.ReadAllTextAsync(localFilePath);
// Obvious prep work
fileContent = SelectCssComments.Replace(fileContent, string.Empty); // Remove comments
fileContent = SelectCssLineBreaks.Replace(fileContent, string.Empty); // Remove line breaks
// Ensure content property values use quotation marks
fileContent = SelectCssContentValueWithApostrophes.Replace(fileContent, """content:"$1";"""); // Make content values use consistent quoting
fileContent = SelectCssContentValueWithSpacePrefix.Replace(fileContent, """content:"$1";"""); // Make content values use consistent quoting
fileContent = SelectCssEmptyContentValueWithSpacePrefix.Replace(fileContent, """content:"";"""); // Make content values use consistent quoting
// All checks here are excluded if inside quotations marks
fileContent = SelectCssMultipleSpaces.Replace(fileContent, " "); // Consolidate multiple spaces to single space
fileContent = SelectCssLastSemicolon.Replace(fileContent, "}"); // Remove last semicolon before closing brace
fileContent = SelectCssSpaceThenClosingBrace.Replace(fileContent, "}"); // Remove spaces before closing brace
fileContent = SelectCssClosingBraceThenSpace.Replace(fileContent, "}"); // Remove spaces after closing brace
fileContent = SelectCssSpaceThenOpeningBrace.Replace(fileContent, "{"); // Remove spaces before opening brace
fileContent = SelectCssOpeningBraceThenSpace.Replace(fileContent, "{"); // Remove spaces after opening brace
fileContent = SelectColonThenSpace.Replace(fileContent, ":"); // Remove spaces between property and value
fileContent = SelectSpaceAfterComma.Replace(fileContent, ","); // Remove spaces after commas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment