在gulpfile中,我有3个任务,当我运行以下代码时,它仅执行最后一个任务的回调。我希望如果我运行gulp
命令,在完成[[clean任务后,它应该执行copy:db和default任务的回调。
Gulp.task('clean', function () {
console.log('Clean');
return Gulp.src("./dbSchema/*")
.pipe(VinylPaths(Del));
})
Gulp.task('copy:db', Gulp.series("clean"), function () {
console.log('Copy DB');
return Gulp.src("./db/*")
.pipe(Gulp.dest("./dbSchema"));
})
Gulp.task('default', Gulp.series("copy:db"), function () {
console.log('defaulp');
return TypeScriptProject.src()
.pipe(TypeScriptProject())
.js.pipe(Gulp.dest('dist'));
});
当我运行命令gulp
时,它显示以下日志。
[12:46:37] Starting 'default'... [12:46:37] Starting 'copy:db'... [12:46:37] Starting 'clean'... Clean [12:46:37] Finished 'clean' after 26 ms [12:46:37] Finished 'copy:db' after 28 ms [12:46:37] Finished 'default' after 31 ms
谁能告诉我我要去哪里错了?
.series()
。例如:Gulp.task('clean', function () {
console.log('Clean');
return Gulp.src("./dbSchema/*")
.pipe(VinylPaths(Del));
})
Gulp.task('copy:db', Gulp.series(clean, function () {
console.log('Copy DB');
return Gulp.src("./db/*")
.pipe(Gulp.dest("./dbSchema"));
}))
Gulp.task('default', Gulp.series(copy:db, function () {
console.log('defaulp');
return TypeScriptProject.src()
.pipe(TypeScriptProject())
.js.pipe(Gulp.dest('dist'));
}));
恕我直言,拥有三个完全独立的任务会更简单:
Gulp.task('clean', function () { console.log('Clean'); return Gulp.src("./dbSchema/*") .pipe(VinylPaths(Del)); }); Gulp.task('copy:db', function () { console.log('Copy DB'); return Gulp.src("./db/*") .pipe(Gulp.dest("./dbSchema")); }); Gulp.task('default', function () { console.log('defaulp'); return TypeScriptProject.src() .pipe(TypeScriptProject()) .js.pipe(Gulp.dest('dist')); });
然后用以下任一方式呼叫他们:
Gulp.task('default', gulp.series(clean, copy:db, js));
或
Gulp.task('default', gulp.series(clean, gulp.parallel(copy:db, js)));
希望有帮助:)
附加说明:
- gulp / vars的命名约定通常是camelCase,例如:
gulp
和typeScriptProject
而不是Gulp
或TypeScriptProject
。- 您完全可以不用使用
gulp.
]]来写var {gulp, task, src, dest, watch, series, parallel} = require('gulp');
- 而不是直接定义任务,您可以通过使用CommonJS
exports
模块表示法声明任务来使代码更易于阅读。- 如果您在使用引号时保持一致,则生活会更轻松一些,而不是混合使用单打和双打。两者都允许遍历
遵循Gulp自己的文档,也许是开始的地方,their sample code on github提供了一些建立基本gulpfile的好例子。如果把所有的东西都包起来,你会得到这个:
/* * Example of requires with gulp methods also requiring gulp. */ var { gulp, dest, series, parallel, src, task, watch } = require('gulp'), vinylPaths = require('vinyl-paths'), // may not be required, see note in clean func. del = require('del'), ts = require('gulp-typescript'); /* * Added a basic TypeScript Project so the example is complete and could run. */ var typeScriptProject = ts.createProject({ declaration: true }); /* * Your tasks converted to plain/standard js functions. */ function clean () { return src('dbSchema/*') .pipe(vinylPaths(del)); // Looking at your example code the vinylPaths is redundant, // as long as you’re using del ^2.0 it will return its promise, // so you could replace the above with: return del([ 'dbSchema' ]); } function copyDb () { return src('db/*') .pipe(dest('dbSchema')); } function scripts () { // example src path return src('lib/*.ts') .pipe(typeScriptProject()) .pipe(dest('dist')); } /* * By defining all the tasks separately it makes it really clear how the tasks will run. */ var build = gulp.series ( clean, gulp.parallel ( copyDb, scripts ) ); /* * Example of using `exports` module notation to declare tasks. */ exports.clean = clean; exports.copyDb = copyDb; exports.scripts = scripts; exports.build = build; /* * Good practise to define the default task as a reference to another task. */ exports.default = build;