SimpleCSSCompiler 1.0.3

Suggested Alternatives

Cascadium.Compiler

dotnet add package SimpleCSSCompiler --version 1.0.3                
NuGet\Install-Package SimpleCSSCompiler -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleCSSCompiler" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleCSSCompiler --version 1.0.3                
#r "nuget: SimpleCSSCompiler, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SimpleCSSCompiler as a Cake Addin
#addin nuget:?package=SimpleCSSCompiler&version=1.0.3

// Install SimpleCSSCompiler as a Cake Tool
#tool nuget:?package=SimpleCSSCompiler&version=1.0.3                

<p align="center"> <img width="100%" height="auto" src="./.github/banner.png"> </p>


This small module can compile CSS with the following features into a legacy CSS file that is more compatible with most browsers.

It can compile CSS with nesting and single-line comments into legacy CSS code, also minifies it.

It can compile:

@import url("https://foo.bar/");

div {
    display: block;
    width: 400px; // it does support comments
    /* native multi-line comments too */

    height: 300px;

    > a {
        align-self: center;

        & :hover {
            color: red;
        }
    }

    & .test {
        font-size: 20px;
    }
}

@media (max-width: 700px) {
    div.test {
        width: 100%;
    }
}

Into:

@import url("https://foo.bar/");

div {
    display: block;
    width: 400px;
    height: 300px
}

div.test {
    font-size: 20px
}

div > a {
    align-self: center
}

div > a:hover {
    color: red
}

@media (max-width: 700px) {
    div.test {
        width: 100%
    }
}

Only with:

string css = SimpleCSS.SimpleCSSCompiler.Compile(prettyCss);

It's all you need.

Specification

The syntax is very similar to what other CSS preprocessors already deliver, the difference is that in the concatenation operator of the "&" selector, the space between the operator and the selector is ignored, and is immediately included next to the parent selector, example:

div {
    & :hover {
        color: blue;
    }
}

Will translate to:

div:hover {
    color: blue
}

If you want an space between div and :hover, just remove the & symbol before the selector.

Also, it propagates the selectors when they are separate to the same rule, for example:

div,
span {
    & :hover,
    & :active {
        color: red;
    }
}

Will translate to:

div:hover,
span:hover,
div:active,
span:active {
    color: red
}

The compiler doesn't know what you're typing, it compiles based on the tokens you type. Using the @ operator will automatically start a new style sheet inside the body, and concatenate to the parent style:

div {
    color: red;
}

@blablabla {
    div {
        color: blue;

        & .yellow {
            color: yellow;
        }
    }
}

Compiles to:

div {
    color: red
}

@blablabla {
    div {
        color: blue
    }

    div.yellow {
        color: yellow
    }
}

Additional options

You can specify compilation options under the CSSCompilerOptions parameter, which:

  • bool Pretty produces an pretty, formatted and indented output.

  • bool UseVarShortcut specifies if the compiler should rewrite --variable to var(--variable) in the value context, example:

    :root {
        --red: #FF1100;        
    }
    
    div {
        color: --red;      
    }
    

    Should be rewrited to:

    div {
        color: var(--red);
    }
    

    And it will NOT rewrite if the variable is inside an string:

    div {
        color: --red;
        background: url('--red');
    }
    
    // becomes:
    div {
        color: var(--red);
        background: url('--red');
    }
    

Considerations

  • Nesting @ blocks is not supported.
  • Properties and values is trimmed.
  • The last ; in the rule is removed.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.