仅在gulp文件中调用了最后一个回调

问题描述 投票:1回答:1

在gulpfile中,我有3个任务,当我运行以下代码时,它仅执行最后一个任务的回调。我希望如果我运行gulp命令,在完成[[clean任务后,它应该执行copy:dbdefault任务的回调。

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

谁能告诉我我要去哪里错了?
gulp gulp-typescript
1个回答
0
投票
为了使代码按您所描述的那样工作,回调函数需要作为参数传递给.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)));

希望有帮助:)


附加说明:

  1. gulp / vars的命名约定通常是camelCase,例如:gulptypeScriptProject而不是GulpTypeScriptProject
  2. 您完全可以不用使用gulp.]]来写var {gulp, task, src, dest, watch, series, parallel} = require('gulp');

  3. 而不是直接定义任务,您可以通过使用CommonJS exports模块表示法声明任务来使代码更易于阅读。
  4. 如果您在使用引号时保持一致,则生活会更轻松一些,而不是混合使用单打和双打。两者都允许遍历

  5. 遵循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;

© www.soinside.com 2019 - 2024. All rights reserved.