【问题标题】:Throwing custom errors/warnings in Sass在 Sass 中抛出自定义错误/警告
【发布时间】:2013-12-05 18:36:24
【问题描述】:

我正在创建一个字体模块,其中包含我所有的 webfonts 和一些 Sass mixin 来写出@font-face 声明。主要的 mixin 将类似于 includeFont(name, weight, style)

我会在一些 Sass 变量中记录哪些字体的权重和样式实际上是可用的,并且通过巧妙地处理这一点,我认为我可以编写 mixin 以便检测我是否尝试请求一个不存在的字体。

但是当我检测到这种情况时,我该如何抛出错误?

【问题讨论】:

    标签: sass


    【解决方案1】:

    截至Sass 3.4.0,有一个@error 指令可用于导致致命错误:

    $stuff: fubar;
    @if ($stuff == fubar) {
        @error "stuff is fubar";
    }
    

    如果您尝试在 shell 中编译它,您将看到以下内容:

    $ sass test.scss
    Error: stuff is fubar
            on line 3 of test.scss
      Use --trace for backtrace.

    还有相关的 @warn@debug 指令在语言中使用的时间更长,以防它们对您更有用。示例:

    @debug "stuff is happening";
    @warn "stuff is happening that probably shouldn't be happening";
    
    /*
        Note that these directives do not terminate execution, and this block
        will therefore still be output.
    */
    * {
        color: pink;
    }
    

    编译时:

    $ sass test2.scss
    test2.scss:1 DEBUG: stuff is happening
    WARNING: stuff is happening that probably shouldn't be happening
             on line 2 of test2.scss
    
    /*
        Note that these directives do not terminate execution, and this block
        will therefore still be output.
    */
    * {
      color: pink; }

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多