首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当在较少的文件中出现错误时,Gulp.js停止编译。

当在较少的文件中出现错误时,Gulp.js停止编译。
EN

Stack Overflow用户
提问于 2015-01-27 08:51:16
回答 4查看 6.9K关注 0票数 11

我和gulp有个问题。我和gulp-watch以及gulp-lessgulp-clean一起运行。一切都运行得很完美。

当我编辑somefile.less并保存它时,丢失了一个分号,或者我意外地留下了一个尾随的;s,在保存时我的代码中有错误,gulp-less会在控制台中记录一个错误。修复之后,gulp-watch继续监视文件,但是gulp-less不会触发,也不会编译。当我停止gulp并在终端中再次运行它时,一切都恢复正常。

这是我的gulpfile.js

代码语言:javascript
复制
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

代码语言:javascript
复制
"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"
}

最后,下面是控制台中的消息:

代码语言:javascript
复制
[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任务

代码语言:javascript
复制
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');
    });
});
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-01-28 14:24:40

现在起作用了!这是我的最后一项工作,也是gulp-less的任务:

代码语言:javascript
复制
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')时,一切都很完美。

票数 22
EN

Stack Overflow用户

发布于 2015-01-27 11:28:38

我总是使用咽喉工来捕获错误。工作非常容易,并将错误记录到控制台。

示例:

代码语言:javascript
复制
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');
    });
});
票数 2
EN

Stack Overflow用户

发布于 2015-01-27 08:59:35

我刚刚为我自己的个人项目设置了这个。根据吞咽医生,您可以只使用gulp.watch

代码语言:javascript
复制
gulp.task('watch', function() {
    gulp.watch('src/main/webapp/styles/**/*.{less, css}', ['less'])
        .on('error', gutil.log);
});

编辑:如果这没有帮助,请将less任务更改为:

代码语言:javascript
复制
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);
});

改编自这句话

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28166409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档