在将Bootstrap 2.3.1解析为CSS时,Dotless会静默失败

本文关键字:Dotless 失败 静默 CSS Bootstrap 在将 | 更新日期: 2023-09-27 18:13:25

下面是我用来将.less文件解析为.css文件的lesstrtransform类:

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

我试图解析下载的bootstrap.css文件使用我的LessTransform类。当方法运行时,最初response.Content具有Bootstrap的内容。

呼叫dotless.Core.Less.Parse()后,response.Content为空。解析器静默失败。

为什么?

在将Bootstrap 2.3.1解析为CSS时,Dotless会静默失败

您的LESS解析没有任何错误处理,因此所有错误都将被静默忽略。如果您正在处理错误,您将看到以下内容:

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

看起来Bootstrap使用了一些LESS语法,而DotLESS还不支持。你可以尝试旧版本的Bootstrap(我以前使用过Bootstrap 2.0和DotLESS)。

要实现错误处理,您需要添加一个ILogger实现。像这样:

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;

其中LessCssLogger实现ILogger。DotLESS带有一个AspResponseLogger,您可能可以使用。