2013-11-21 28 views
11

我正在创建一个字体模块,其中包含我所有的Web字体和一些Sass混合类型来编写@font-face声明。主要的混合类似于includeFont(name, weight, style)在Sass中投掷自定义错误/警告

我会在一些Sass变量中保留一个记录,其中字体的重量和样式实际上可用,并且通过对此进行巧妙的处理,我认为我可以编写mixin,以便我可以检测到我是否尝试并请求不存在的字体。

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

+0

你尝试在看文档? http://sass-lang.com/documentation/file.SASS_REFERENCE.html#_5 – cimmanon

+1

我做到了,但我错过了这一点 - 谢谢 – wheresrhys

回答

5

截至Sass 3.4.0,有一个@error指令,你可以用它来产生一个致命的错误:如果您尝试在shell编译这个

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

,你会看到以下内容:

$ 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; }