首页 > 怎么防止less编译出错,gulp testWatch命令不退出,还能继续编译。

怎么防止less编译出错,gulp testWatch命令不退出,还能继续编译。

配置:

//导入工具包 require('node_modules里对应模块')
var gulp = require('gulp'), //本地安装gulp所用到的地方
    less = require('gulp-less');

//定义一个testLess任务(自定义任务名称)
gulp.task('testLess', function () {
    gulp.src('src/less/*.less') //该任务针对的文件
        .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
        .pipe(less()) //该任务调用的模块
        .pipe(gulp.dest('src/css')); //将会在src/css下生成index.css
});

gulp.task('default',['testLess']); //定义默认任务

//监听预编译文件
gulp.task('testWatch',function(){
  gulp.watch('src/**/*.less',['testLess']);
});

错误提示:

$ gulp testWatch
[11:31:01] Using gulpfile F:\project\gulpfile.js
[11:31:01] Starting 'testWatch'...
[11:31:01] Finished 'testWatch' after 11 ms
[11:31:09] Starting 'testLess'...
[11:31:09] 'testLess' errored after 12 ms
[11:31:09] ReferenceError: plumber is not defined
    at Gulp.<anonymous> (F:\project\gulpfile.js:8:15)
    at module.exports (F:\project\node_modules\.npminstall\orchestrator\0.3.7\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (F:\project\node_modules\.npminstall\orchestrator\0.3.7\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (F:\project\node_modules\.npminstall\orchestrator\0.3.7\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (F:\project\node_modules\.npminstall\orchestrator\0.3.7\orchestrator\index.js:134:8)
    at Gulp.<anonymous> (F:\project\node_modules\.npminstall\gulp\3.9.1\gulp\index.js:36:18)
    at Gaze.<anonymous> (F:\project\node_modules\.npminstall\glob-watcher\0.0.6\glob-watcher\index.js:18:14)
    at emitTwo (events.js:87:13)
    at Gaze.emit (events.js:172:7)
    at Gaze.emit (F:\project\node_modules\.npminstall\gaze\0.5.2\gaze\lib\gaze.js:129:32)

为什么加了.pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
这句,less里面内容格式错误,控制台报完错后,还是退出gulp testWatch命令了。


那你需要安装:gulp-plumber

npm install --save-dev gulp-plumber
//导入工具包 require('node_modules里对应模块')
var gulp = require('gulp'), //本地安装gulp所用到的地方
    less = require('gulp-less'),
    plumber = require('gulp-plumber');

//定义一个testLess任务(自定义任务名称)
gulp.task('testLess', function () {
    gulp.src('src/less/*.less') //该任务针对的文件
        .pipe(plumber())
        .pipe(less()) //该任务调用的模块
        .pipe(gulp.dest('src/css')); //将会在src/css下生成index.css
});

gulp.task('default',['testLess']); //定义默认任务

//监听预编译文件
gulp.task('testWatch',function(){
  gulp.watch('src/**/*.less',['testLess']);
});
【热门文章】
【热门文章】