gulp-notify處理報錯----gulp系列(二)


上一節,以less為例,入門了gulp,並為任務結構做了抽離。

 

 前端們,gulp該用起來了,簡單的demo入門——gulp系列(一)

 

本節學習下gulp-notify,官方這樣解釋的:

gulp-notify :

gulp plugin to send messages based on Vinyl Files or Errors to Mac OS X, Linux or Windows using the node-notifier module. Fallbacks to Growl or simply logging

 

目的:

我們用notify的功能主要有兩點,顯示報錯信息和報錯后不終止當前gulp任務。

拿系列(一)來說,less如果出現編譯錯誤,就會報錯然后終止任務,這時less修改正確后,你還得手動重啟gulp任務。

 

開始:

現在,我們在系列(一)的基礎上,新建(檢出)一個分支,添加notify功能

 

1.安裝gulp-notify: npm install --save-dev gulp-notify

 

2.在gulp文件夾里新建一個目錄,名叫util,目錄里新建文件handleErrors.js用來配置notify

                       

handleError.js代碼如下:

var notify = require("gulp-notify");

module.exports = function(){

    var args = Array.prototype.slice.call(arguments);

    notify.onError({
        title: 'compile error',
        message: '<%=error.message %>'
    }).apply(this, args);//替換為當前對象

    this.emit();//提交
}

 

3.在less中引用,現在將less.js修改為如下:

var gulp = require('gulp');
var less = require('gulp-less');
var config = require('../config').less;
var handleErrors = require('../util/handleErrors');

gulp.task('less', function(){
    return gulp.src(config.src)        //less源文件
        .pipe(less(config.settings))   //執行編譯
        .on('error', handleErrors)     //交給notify處理錯誤
        .pipe(gulp.dest(config.dest))  //輸出目錄
});

 

最終:

如果出現less錯誤,便會輸出錯誤信息並繼續gulp任務

 

還是那句話多看官方文檔。

 

>>> github 地址,請選擇  notify  分支<<<

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM