【问题标题】:gulp-notify - get file name, not whole path, with errorgulp-notify - 获取文件名,而不是整个路径,有错误
【发布时间】:2015-07-17 22:51:02
【问题描述】:

我正在使用gulp-notifygulp-plumber 捕获来自gulp-sass 的任何错误,并在它在gulp 中引发错误并停止整个构建过程之前通知我。但是,gulp-notify 显示的错误并不是特别有用...它包含错误文件的整个路径;我很可能知道文件的路径是什么,所以我只需要知道文件名和行号(以及列号作为附加但不是必需的奖励),以及实际的错误消息。 这是我的代码:

gulp.task('styles', function () {
    var onError = function(err) {
      notify.onError({
        title:    "Gulp",
        subtitle: "Build Error!",
        message:  "<%= error.message %>",
        sound:    "Beep"
      })(err);
      this.emit('end');
    };
    gulp.src('assets/styles/source/style.scss')
        .pipe(plumber({errorHandler: onError}))
        .pipe(sass({
            style: 'compressed',
            errLogToConsole: false
         }))
        .pipe(gulp.dest('assets/styles/build'))
        .pipe(autoprefixer({
            browsers: ['last 2 versions', 'ie 9', 'ios 6', 'android 4'],
            cascade: false
        }))
        .pipe(gulp.dest('assets/styles/build'))
        .pipe(minifyCSS({
            keepSpecialComments: '*'
        }))
        .pipe(gulp.dest('./'))
        .pipe(reload({stream: true}));
});

还有更多(比如gulp-watchbrowsersync、一些 JS 的东西),但我认为以上就是解决问题所需的全部内容。如果没有,请告诉我!

所以我的问题是,如何修改错误消息以仅显示文件名、行号和错误消息(例如 _header.scss:17 - 无效的属性名称)? 目前我我正在使用error.message,但我找不到更多可以用来获取我想要显示的内容的标签。

谢谢!

【问题讨论】:

    标签: error-handling gulp gulp-notify


    【解决方案1】:

    我是这样解决这个问题的:

    .pipe(sass(
        ({
          style: 'compressed',
          errLogToConsole: false,
          onError: function(error) {
            notify({
              title: "SASS ERROR",
              message: "line " + error.line + " in " + error.file.replace(/^.*[\\\/]/, '') + "\n" + error.message
            }).write(error);
          }
        })
      )
    )
    

    我还注意到,我只能提供一个换行符“\n”,因此我无法将我想要的每条信息都拆分到单独的行中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      相关资源
      最近更新 更多