2015-01-27 14 views
11

Ho un problema con gulp. Corro gulp-watch insieme a gulp-less e gulp-clean. Tutto funziona perfettamente.Gulp.js smette di compilare MENO quando guardato dopo che c'è stato un errore nei file LESS

Quando posso modificare somefile.less li risparmio con un punto e virgola mancante o forse mi sbaglio lasciare un trailing ;s, basta avere errori nel mio codice durante il salvataggio, gulp-less registra un errore nella console. Dopo averlo risolto, lo gulp-watch continua a guardare i file, ma gulp-less non si avvia e non viene compilato. Quando mi fermo gulp ed eseguo di nuovo nel terminale, tutto torna alla normalità.

Ecco il mio gulpfile.js:

var gulp = require('gulp'); 
var clean = require('gulp-clean'); 
var gutil = require('gulp-util'); 
var less = require('gulp-less'); 
var watch = require('gulp-watch'); 
var path = require('path'); 

gulp.task('clean', function() { 
    return gulp.src('src/main/webapp/styles/build', {read: false}) 
    .pipe(clean().on('error', gutil.log)) 
}); 

gulp.task('less', function() { 
    return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'}) 
    .pipe(less().on('error', gutil.log)) 
    .pipe(gulp.dest('src/main/webapp/styles/build')) 
    .on('error', gutil.log); 
}); 

gulp.task('watch', function() { 
    watch('src/main/webapp/styles/**/*.{less, css}', function() { 
     gulp.start('less') 
     .on('error', gutil.log); 
    }) 
}); 

gulp.task('default', ['clean'], function() { 
    gulp.start(['less', 'watch']) 
    .on('error', gutil.log); 
}); 

Ed ecco i miei devDependencies:

"devDependencies": { 
    "gulp": "^3.8.10", 
    "gulp-clean": "^0.3.1", 
    "gulp-less": "^2.0.1", 
    "gulp-util": "^3.0.2", 
    "gulp-watch": "^3.0.0" 
} 

Infine, ecco il messaggio nella console:

[10:21:03] imports/productSearchPage.less was changed 
[10:21:03] Starting 'less'... 
[10:21:03] { [Error: Unrecognised input. Possibly missing something in file /src/main/webapp/styles/imports/productSearchPage.less line no. 1008] 
    type: 'Parse', 
    filename: '/src/main/webapp/styles/imports/productSearchPage.less', 
    index: 19127, 
    line: 1008, 
    callLine: NaN, 
    callExtract: undefined, 
    column: 0, 
    extract: [ '', '', undefined ], 
    message: 'Unrecognised input. Possibly missing something in file /src/main/webapp/styles/imports/productSearchPage.less line no. 1008', 
    stack: undefined, 
    lineNumber: 1008, 
    fileName: '/src/main/webapp/styles/imports/productSearchPage.less', 
    name: 'Error', 
    showStack: false, 
    showProperties: true, 
    plugin: 'gulp-less', 
    __safety: { toString: [Function] } } 
[10:21:04] imports/productSearchPage.less was changed 
[10:21:08] imports/productSearchPage.less was changed 
^C 

Puoi informi Cosa c'è che non va nell'attività gulp-watch e aiutami ma ke esegue gulp-less dopo che gli errori sono stati rimossi, senza riavviare gulp.

EDIT: mio modificati gulp-less compito

gulp.task('less', function() { 
    return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'}) 
    .pipe(less().on('error', gutil.log)) 
    .pipe(gulp.dest('src/main/webapp/styles/build')) 
    .on('error', function(err) { 
     gutil.log(err); 
     this.emit('end'); 
    }); 
}); 

risposta

18

ora funziona! Qui è la mia ultima, e di lavoro, gulp-less compito:

gulp.task('less', function() { 
    return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'}) 
    .pipe(less().on('error', function(err){ 
     gutil.log(err); 
     this.emit('end'); 
    })) 
    .pipe(gulp.dest('src/main/webapp/styles/build')) 
}); 

Il problema era che, quando c'è stato un errore nel LESS compito ancora andato su e costruito il file di destinazione. Inoltre ho inserito la funzione di registrazione degli errori e emit('end') come richiamata a gulp.dest.

Ora, quando il callback di less() è il log degli errori e emit('end') tutto funziona perfettamente.

+1

Questo bit ha avuto il minimo da rewatching per me this.emit ('end'). Grazie per il tuo post! – ctong

0

Ho appena impostare questa funzione per il mio progetto personale. Secondo il Gulp docs, si può semplicemente utilizzare gulp.watch:

gulp.task('watch', function() { 
    gulp.watch('src/main/webapp/styles/**/*.{less, css}', ['less']) 
     .on('error', gutil.log); 
}); 

EDIT: Se questo non risolve il problema, cambiare il vostro compito less a:

gulp.task('less', function() { 
    return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'}) 
    .pipe(less()) 
    .on('error', function (err) { 
     gutil.log(err); 
     this.emit('end'); 
    }) 
    .pipe(gulp.dest('src/main/webapp/styles/build')) 
    .on('error', gutil.log); 
}); 

Adattato da this comment.

+0

Se questa risposta funziona, la mia risposta è stato adattato da questa domanda: http://stackoverflow.com/questions/21905875/gulp-run-is-deprecated-how-do-i-compose-tasks – Tom

+0

Il problema rimane. Inoltre, 'gulp-util' non registra l'errore nella console. – turbalan

+0

@PavelKuts - Penso di aver trovato il tuo problema. Vedi la mia risposta aggiornata. – Tom

2

Io uso sempre gulp-plumber per il mio errore di cattura. Funziona davvero facilmente e registra l'errore nella console.

Esempio:

gulp.task('less', function() { 
    return gulp.src(['src/main/webapp/styles/main.less'], {base: 'src/main/webapp/styles/'}) 
    .pipe(plumber()) 
    .pipe(less().on('error', gutil.log)) 
    .pipe(gulp.dest('src/main/webapp/styles/build')) 
    .on('error', function(err) { 
     gutil.log(err); 
     this.emit('end'); 
    }); 
}); 
+1

Anche io lo uso. Rileva errori nel flusso, prima che abbiano la possibilità di romperlo. –

0

la soluzione migliore che ho provato; https://github.com/gulpjs/gulp/blob/master/docs/recipes/combining-streams-to-handle-errors.md

var combiner = require('stream-combiner2'); 
gulp.task('multi:less', function(done) { 
    var combined = combiner.obj([ 
     gulp.src(srcs), 
     less(), 
     autoprefixer({ 
      browsers: ['last 6 versions'], 
      cascade: false 
     }), 
     isDev ? null : cleanCss(), 
     gulp.dest(targetDir + 'css/multi/'), 
    ].filter(v => v)); 

    // any errors in the above streams will get caught 
    // by this listener, instead of being thrown: 
    combined.on('error', console.error.bind(console)); 
    combined.on('end',() => {}); //done have been call when return combined; 
    return combined; 

}