Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用gulp jslint simple成功执行事件_Javascript_Gulp_Jslint_Gulp Watch - Fatal编程技术网

Javascript 如何使用gulp jslint simple成功执行事件

Javascript 如何使用gulp jslint simple成功执行事件,javascript,gulp,jslint,gulp-watch,Javascript,Gulp,Jslint,Gulp Watch,我正在使用该插件在保存时jslint我的javascript,它可以很好地处理以下代码: gulp.task('lint', function() { gulp.src(paths.js) .pipe(jslint.run({ node: true, vars: true })) .pipe(jslint.report({ reporter: require(styli

我正在使用该插件在保存时jslint我的javascript,它可以很好地处理以下代码:

gulp.task('lint', function() {
    gulp.src(paths.js)
        .pipe(jslint.run({
            node: true,
            vars: true
        }))
        .pipe(jslint.report({
            reporter: require(stylish).reporter
        }));
});
这基本上将jslint应用于我的javascript目录中的所有文件夹,这些文件夹被称为
path.js

我使用以下监视方法监听正在保存的javascript文件:

gulp.task('watch', function() {
    gulp.watch(paths.js, ['lint']);
    gulp.watch(paths.js, ['js']);
});
如上所述,我有另一个watch方法,它在相同的文件中压缩和缩小javascript,但是我只希望在jslinting没有返回错误的情况下运行它,这可能吗

我尝试添加一个
.on('error',errorHandler)
方法,但这只会在出现错误时出现,正如我想要的成功方法一样。我还研究了
.on('end',…)
,但这似乎在某种程度上被多次击中

简单地说,我希望能够一口气做到以下几点:

if (gulp.watch(paths.js, ['lint'])) {
    //somehow call this..
    gulp.watch(paths.js, ['js']);
});

使用
emitError
选项可以使
lint
任务在出现lint错误时失败,然后可以使
js
任务依赖于
lint
任务:

gulp.task('lint', function() {
    return gulp.src(paths.js)
        .pipe(jslint.run({
            node: true,
            vars: true
        }))
        .pipe(jslint.report({
            reporter: require(stylish).reporter,
            emitError: true
        }));
});

gulp.task('js', ['lint'], function() {
  //js stuff
});

gulp.task('watch', function() {
    gulp.watch(paths.js, ['js']);
});

你想调用另一个gulp任务还是只做一些js工作?@Callistino我有一个js函数,可以缩小文件,这很好,我希望它只在“lint”任务成功时运行。如上所述,“js”函数似乎会继续执行,而不管在['lint']调用中发现的错误,如果['lint']未成功(无错误)抱歉,我忘记了
lint
任务中的
返回值。谢谢,但唯一有效的方法是如果我不处理我们发出的错误,那么它基本上会崩溃gulp。如果我用
on('error',errorHandler)处理错误,这是我们不希望的
并在处理函数中包含以下内容:this.emit('end');
它不会崩溃,但会继续运行'js'函数,这与我的'js'函数有关吗?我的印象是,只要
['lint',其中的内容就无关紧要了
passed successfully?通过使用类似于
process.on('uncaughtException',function(err){…})的方法捕获异常,可以防止gulp崩溃
,但这不能保证gulp进程处于可用状态。@Ben您不能在
errorHandler
上设置一个全局标志,然后在“js”任务中未设置标志时执行js函数。