![](/img/trans.png)
[英]Gulp and run-sequence error : Task is not configured as a task on gulp
[英]Issue with task not executing when using gulp with run-sequence
我是gulp的新手,因此我将一个简单的脚本组合在一起:1)了解gulp的工作原理,以及2)改善开发工作流程。
我遇到的问题是,如果我将runSequence任务列表设置为jscs然后是lint的顺序,则不会执行lint任务。 但是,如果我反转它们,并先运行lint,然后运行jscs,则两个任务都将成功执行。 经过更多测试之后,我确定是我使用jscs的方式引起了问题,或者jscs出现了问题,阻止了这种方式的执行。
/* File: gulpfile.js */
/* grab the packages we will be using */
var gulp = require('gulp');
var gutil = require('gulp-util');
var jscs = require('gulp-jscs');
var jsHint = require('gulp-jshint');
var jsHintStylish = require('jshint-stylish');
var runSequence = require('run-sequence');
var console = require('better-console');
// configure the default task and add the watch task to it
gulp.task('default', ['watch']);
// configure the jscs task
gulp.task('jscs', function() {
return gulp.src('src/app/**/*.js')
.pipe(jscs('.jscsrc'));
});
// configure the lint task
gulp.task('lint', function() {
return gulp.src("src/app/**/*.js")
.pipe(jsHint('.jshintrc'))
.pipe(jsHint.reporter(jsHintStylish));
});
// configure the watch task - set up which files to watch and what tasks to use when those files change
gulp.task('watch',function() {
// Clear console
console.clear();
// setup a watch against the files that need to be checked on save
gulp.watch('src/app/**/*.js', function(){
// Clear console so only current issues are shown
console.clear();
// Execute tasks to check code for issues
runSequence('lint','jscs',function(){
gutil.log("Code Check Complete.");
});
});
});
{
"name": "my project name",
"version": "0.1.0",
"author": {
"name": "my name",
"email": "myemail@domain.com"
},
"devDependencies": {
"better-console": "^0.2.4",
"gulp": "^3.8.11",
"gulp-jscs": "^1.6.0",
"gulp-jshint": "^1.11.0",
"gulp-util": "^3.0.4",
"jshint-stylish": "^1.0.2",
"run-sequence": "^1.1.0"
}
}
(function() {
'use strict;
var x = ;
})();
'lint','jscs',function(){...}
时的示例输出: [15:10:49] Starting 'lint'...
c:\Users\anatha\My Projects\Tracks\Tracks.Client\src\app\app.js
line 3 col 17 Unclosed string.
line 4 col 1 Unclosed string.
line 5 col 14 Unclosed string.
line 6 col 1 Unclosed string.
line 7 col 6 Unclosed string.
line 8 col 1 Unclosed string.
line 3 col 5 Unclosed string.
line 1 col 13 Missing semicolon.
line 8 col 1 Missing "use strict" statement.
line 1 col 13 Unmatched '{'.
line 1 col 1 Unmatched '('.
line 8 col 1 Expected an assignment or function call and instead saw an expression.
line 8 col 1 Missing semicolon.
× 4 errors
‼ 9 warnings
[15:10:49] Finished 'lint' after 104 ms
[15:10:49] Starting 'jscs'...
[15:10:49] 'jscs' errored after 157 ms
[15:10:49] Error in plugin 'gulp-jscs'
Message:
Unexpected token ILLEGAL at app.js :
1 |(function() {
2 |
3 | 'use strict;
-----------------------^
4 |
5 | var x = ;
[15:10:49] Code Check Complete.
'jscs','lint',function(){...}
时的示例输出(注意,永远不会执行lint语句): [15:09:48] Starting 'jscs'...
[15:09:48] 'jscs' errored after 178 ms
[15:09:48] Error in plugin 'gulp-jscs'
Message:
Unexpected token ILLEGAL at app.js :
1 |(function() {
2 |
3 | 'use strict;
-----------------------^
4 |
5 | var x = ;
[15:09:48] Code Check Complete.
在花了几个小时研究了问题的原因之后,我将其范围缩小到以下事实:JSCS在确定它检查的代码不遵守用户设置的规范时抛出错误。 引发此错误时,它会禁止运行顺序中的后续任务; 但是,奇怪的是,从未生成未处理的错误事件来警告用户该问题。
这与JSHINT的操作方式不同。 当JSHINT确定代码中存在错误时,它仅输出违反代码的行为,而不会引发导致后续任务永远不会被触发的错误。
我更新了脚本,以解决此问题,方法是捕获未处理的错误,并对该错误执行最少的处理,以完成运行序列中指定的其余任务。
gulpfile.js
文件: /* File: gulpfile.js */
/* grab the packages we will be using */
var gulp = require('gulp');
var gUtil = require('gulp-util');
var jscs = require('gulp-jscs');
var jsHint = require('gulp-jshint');
var jsHintStylish = require('jshint-stylish');
var runSequence = require('run-sequence');
var console = require('better-console');
// default error handler
var handleJscsError = function(err) {
console.log("Error: " + err.toString());
this.emit('end');
}
// configure the default task and add the watch task to it
gulp.task('default', ['watch']);
// configure the jscs task
gulp.task('jscs', function() {
return gulp.src('src/app/**/*.js')
.pipe(jscs('.jscsrc'))
.on('error',handleJscsError);
});
// configure the lint task
gulp.task('lint', function() {
return gulp.src("src/app/**/*.js")
.pipe(jsHint('.jshintrc'))
.pipe(jsHint.reporter(jsHintStylish));
});
// configure the watch task - set up which files to watch and what tasks to use when those files change
gulp.task('watch', function() {
// Clear console
console.clear();
// setup a watch against the files that need to be checked on save
return gulp.watch('src/app/**/*.js', function(){
// Clear console so only current issues are shown
console.clear();
// Execute tasks to check code for issues
runSequence('jscs','lint',function(){
console.log("Tasks Completed.")
});
});
});
我尝试通过创建自定义JSCS报告程序来解决此问题。 尽管我确实做到了这一点,但对我而言,这确实是一种麻烦,而不是一个可靠的解决方案。 而且要补充一点,JSCS不像JSHINT那样提供自定义报告,这使我更加难看,因为一旦添加了对自定义报告的支持 ,我仍然必须重构代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.