【问题标题】:sass/gulp-sass remains silent on certain errors (invalid properties)sass/gulp-sass 对某些错误(无效属性)保持沉默
【发布时间】:2016-12-18 11:28:34
【问题描述】:

是的,我的 gulpfile (gulp@3.9.1) 中的 sass 报告了许多错误(并继续监视,就像它应该做的那样......),但不是这样的错误:

=clearfix()
    &:after
        content: ""
        display: table
        clear both

contentdisplay 使其正确进入实际类(其中引用了 mixin),但 clear 没有(因为我错过了冒号)。这是一个语法错误(所以它不像rareproperty: 42px; 处理得很好)。 → 静止:没有发现错误,没有中断。

我的吞咽任务:

gulp.task('sass', function() {

    return gulp.src(src.scssMaster)
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(  sass({
                    debugInfo   : true,
                    lineNumbers : true,
                    outputStyle: 'expanded'
                })
                .on('error', function(err){
                    gutil.log(err);    // stackoverflow.com/a/30742014/444255
                    this.emit('end');
                })
            )
        .pipe(autoprefixer({
            browsers: ['iOS >= 5', 'ie 8', 'Firefox > 2'],
            cascade:   true,
            remove:    true
        }))
        .pipe(rename('lznet.css'))
        .pipe(sourcemaps.write('../maps'))
        .pipe(gulp.dest(src.cssDir))

        .pipe(reload({stream: true}))
        .on('end', () => gutil.log('sass thing done'));
});

千禧一代❤

【问题讨论】:

    标签: error-handling sass gulp gulp-watch gulp-sass


    【解决方案1】:

    这是一个语法错误 → 仍然:没有看到错误,没有中断。

    这根本不是语法错误。举个例子:

    .foo
      color: blue
      clear both
    

    这将编译为以下 CSS:

    .foo {
      color: blue; 
    }
    

    确实看起来错误指定的clear both 属性的错误被默默地忽略了。实际发生的是您定义了一个嵌套的.foo clear both 选择器。但由于您的 .foo clear both 选择器是 empty,它已被 SASS 编译器优化掉。

    当您查看另一个 SASS 示例会发生什么时,这一点就会变得清晰:

    .foo
      color: blue
      clear both
        color: red
    

    这将编译为以下 CSS:

    .foo {
      color: blue; 
    }
    .foo clear both {
      color: red; 
    }
    

    现在.foo clear both 选择器不为空,并且 CSS 不再被编译器优化掉。

    SASS 不知道也不关心在任何 HTML 规范中实际上都没有 clearboth 元素这一事实。这是一项功能,而不是错误,因为:

    • 可以在 HTML 中添加和删除元素。每当新的 HTML 版本标准化时,就需要对 SASS 进行更改。
    • 您已经可以定义custom HTML elements(尽管在这种情况下clearboth 即使对于Web 组件也不是有效的名称)。
    • CSS(以及因此的 SASS)绝不限于 HTML。您还可以使用use CSS to style XML documents,这显然可以让您定义自定义元素,例如clearboth

    【讨论】:

    • 优秀答案?
    猜你喜欢
    • 1970-01-01
    • 2015-09-30
    • 2016-04-04
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    相关资源
    最近更新 更多