css - 如何在 LESS 编译器中抛出错误

标签 css compiler-errors less throw less-mixins

问题

有没有办法(以编程方式)在 LESS 编译器中抛出错误?

为什么?

我今天一直在摆弄混合守卫,因为我想根据元素大小和元素数量生成我的 CSS 边距。 我认为当元素不适合包装器时直接在编译时抛出错误会很酷。

信息:我正在使用 lessc编译器将我的 LESS 代码编译成 CSS。我没有使用任何 Javascript 库在执行时对其进行编译。

少源

// Variables
@wrapper-small:  830px;
@wrapper-big:   1200px;

.col-fixed(@size, @count, @wrapper)  when ((@size*@count) <= @wrapper)
{
    width: unit(@size, px);
    margin-right: unit( (@wrapper - @count * @size) / (@count - 1), px);    
}

.test_col_fixed {
    // will fail the mixin guard and output no generated CSS        
    .col-fixed(340, 3, @wrapper-small);

    // would work if not in comment
    // .col-fixed(340, 3, @wrapper-big);
}

生成的 CSS(小包装器)

没有输出,因为没有匹配到mixin guard when ((@size*@count) <= @wrapper) // 3*340 <= 830 is false不会生成代码.

生成的 CSS(带有工作解决方案,大包装器)

.test_col_fixed {
    width: 340px;
    margin-right: 90px;
}

最佳答案

建议,但严格不推荐solution by Harry

.col-fixed(@size, @count, @wrapper) {
    & when ((@size*@count) <= @wrapper) {
        width: unit(@size, px);
        margin-right: unit( (@wrapper - @count * @size) / (@count - 1), px);  
    }
    & when ((@size*@count) > @wrapper) {
        /* there is no such variable and hence when the input value is not valid,
        compiler will complain that variable is undefined */
        output: @bwahahaha;
    }
}

关于css - 如何在 LESS 编译器中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30865036/

相关文章:

css - Div 不显示在视频上

javascript - 文本 slider 在每次迭代后从左侧增加偏移量

html - 如何在启用 bootstrap 的 html 表的元素之间添加间距?

html - HTML 中的不间断非空格

java - Java,编译错误?

ruby-on-rails - 是否仍然推荐使用更多(Rails 的更少 CSS 插件)?

asp.net - 找不到“ManagementScope ”

sql-server - SQL Server中的编译错误和运行时错误之间的区别?

twitter-bootstrap - 如何在 Yii 框架中使用 Bootstrap 编辑变量和编译 .less?

css - 在 Rails Assets 管道中包含多个 LESS 文件