Search code examples
c#twitter-bootstraplessdotless

Dotless fails silently when parsing Bootstrap 2.3.1 to CSS


Here's my LessTransform class I use to parse .less files into .css files:

public class LessTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        response.Content = dotless.Core.Less.Parse(response.Content);
        response.ContentType = "text/css";
    }
}

I'm trying to parse the downloaded bootstrap.css file using my LessTransform class. When the method is running, initially response.Content has the contents of Bootstrap.

After the call to dotless.Core.Less.Parse(), response.Content is blank. The parser is failing silently.

Why?


Solution

  • Your LESS parsing doesn't have any error handling, so all errors are being silently ignored. If you were handling errors, you'd see the following:

    Expected '}' on line 577 in file '../bootstrap/mixins.less':
    [576]: 
    [577]:     .spanX (@index) when (@index > 0) {
           --------------------^
    [578]:       .span@{index} { .span(@index); }
    

    It looks like Bootstrap uses some LESS syntax that DotLESS does not yet support. You could try an older Bootstrap version (I've used Bootstrap 2.0 with DotLESS before).

    To implement error handling, you need to add an ILogger implementation. Something like this:

    var config = new DotlessConfiguration
    {
        LogLevel = LogLevel.Error,
        // Put your custom logger implementation here
        Logger = typeof(LessCssLogger)
    };
    
    var lessEngine = new EngineFactory(config).GetEngine();
    var outputCss = lessEngine.TransformToCss(response.Content, null);
    Response.Content = outputCss;
    

    Where LessCssLogger implements ILogger. DotLESS comes with a AspResponseLogger that you might be able to use.