我和gulp有个问题。我和gulp-watch以及gulp-less和gulp-clean一起运行。一切都运行得很完美。
当我编辑somefile.less并保存它时,丢失了一个分号,或者我意外地留下了一个尾随的;s,在保存时我的代码中有错误,gulp-less会在控制台中记录一个错误。修复之后,gulp-watch继续监视文件,但是gulp-less不会触发,也不会编译。当我停止gulp并在终端中再次运行它时,一切都恢复正常。
这是我的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);
});这是我的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"
}最后,下面是控制台中的消息:
[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请告诉我gulp-watch任务有什么问题,并帮助我在删除错误后运行gulp-less,而不重新启动gulp。
编辑:我编辑的gulp-less任务
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');
});
});发布于 2015-01-28 14:24:40
现在起作用了!这是我的最后一项工作,也是gulp-less的任务:
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'))
});问题是,当LESS中出现错误时,任务仍在继续,并构建了目标文件。在此基础上,我将错误日志记录函数和emit('end')作为对gulp.dest的回调。
现在,当less()的回调是错误日志和emit('end')时,一切都很完美。
发布于 2015-01-27 11:28:38
我总是使用咽喉工来捕获错误。工作非常容易,并将错误记录到控制台。
示例:
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');
});
});发布于 2015-01-27 08:59:35
我刚刚为我自己的个人项目设置了这个。根据吞咽医生,您可以只使用gulp.watch
gulp.task('watch', function() {
gulp.watch('src/main/webapp/styles/**/*.{less, css}', ['less'])
.on('error', gutil.log);
});编辑:如果这没有帮助,请将less任务更改为:
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);
});改编自这句话。
https://stackoverflow.com/questions/28166409
复制相似问题