简体   繁体   English

gulp.run已弃用。我如何撰写任务?

[英]gulp.run is deprecated. How do I compose tasks?

Here is a composed task I don't know how to replace it with task dependencies. 这是一个组合任务,我不知道如何用任务依赖项替换它。

...
gulp.task('watch', function () {
 var server = function(){
  gulp.run('jasmine');
  gulp.run('embed');
 };
 var client = function(){
  gulp.run('scripts');
  gulp.run('styles');
  gulp.run('copy');
  gulp.run('lint');
 };
 gulp.watch('app/*.js', server);
 gulp.watch('spec/nodejs/*.js', server);
 gulp.watch('app/backend/*.js', server);
 gulp.watch('src/admin/*.js', client);
 gulp.watch('src/admin/*.css', client);
 gulp.watch('src/geojson-index.json', function(){
  gulp.run('copygeojson');
 });
});

The corresponding changelog https://github.com/gulpjs/gulp/blob/master/CHANGELOG.md#35 [deprecate gulp.run] 相应的更改日志https://github.com/gulpjs/gulp/blob/master/CHANGELOG.md#35 [弃用gulp.run]

或者你可以这样做:

gulp.start('task1', 'task2');
gulp.task('watch', function () {
  var server = ['jasmine', 'embed'];
  var client = ['scripts', 'styles', 'copy', 'lint'];
  gulp.watch('app/*.js', server);
  gulp.watch('spec/nodejs/*.js', server);
  gulp.watch('app/backend/*.js', server);
  gulp.watch('src/admin/*.js', client);
  gulp.watch('src/admin/*.css', client);
  gulp.watch('src/geojson-index.json', ['copygeojson']);
});

You no longer need to pass a function (though you still can) to run tasks. 您不再需要传递函数(尽管您仍然可以)来运行任务。 You can give watch an array of task names and it will do this for you. 您可以为监视提供一系列任务名称,它将为您执行此操作。

source: https://github.com/gulpjs/gulp/issues/755 来源: https//github.com/gulpjs/gulp/issues/755

gulp.start() was never meant to be a public api nor used. gulp.start()从来就不是公共API,也不是用的。 And as stated above in comments, the task management is being replaced in the next release....so gulp.start() will be breaking. 正如上面的评论所述,任务管理正在下一个版本中被替换....所以gulp.start()将会破坏。

The true intention of the gulp design is to make regular Javascript functions, and only make the task for calling them. gulp设计的真正目的是制作常规的Javascript函数,并且只进行调用它们的任务。

Example: 例:

function getJsFiles() {
    var sourcePaths = [
        './app/scripts/**/*.js',
        '!./app/scripts/**/*.spec.js',
        '!./app/scripts/app.js'
    ];

    var sources = gulp.src(sourcePaths, { read: false }).pipe(angularFilesort());

    return gulp.src('./app/index.html')
        .pipe(injector(sources, { ignorePath: 'app', addRootSlash: false }))
        .pipe(gulp.dest('./app'));
}  

gulp.task('js', function () {
    jsStream = getJsFiles();
});

Forgive me for resurrecting an old question. 请原谅我复活旧问题。 The accepted answer does not address the issue of running tasks before setting the watches. 接受的答案没有解决在设置手表之前运行任务的问题。 The next answer uses gulp.start which is going away. 下一个答案使用gulp.start即将消失。 The third answer points out that regular functions should be used but the example seems strange. 第三个答案指出应该使用常规函数,但示例似乎很奇怪。 I did some searching but did not find a simple example. 我做了一些搜索,但没有找到一个简单的例子。

Here is my solution. 这是我的解决方案。 The idea is to define regular js functions then register them as tasks. 我们的想法是定义常规的js函数,然后将它们注册为任务。 The functions can then be called directly if needed or from within a watch. 然后,如果需要或从手表中直接调用这些功能。

var 
  gulp     = require('gulp'),
  concat   = require('gulp-concat'),
  markdown = require('gulp-showdown')
;
var scriptFiles   = [ 'ang/app.js' ];
var markdownFiles = [ 'content/articles/*.md'];

var watchTask = function() 
{
  buildTask();

  gulp.watch(scriptFiles,  ['scripts' ]);
  gulp.watch(markdownFiles,['markdown']);
};
gulp.task('watch',watchTask);

var buildTask = function()
{
  scriptsTask();
  markdownTask();
};
gulp.task('build',buildTask);

var markdownTask = function() 
{
  gulp.src(markdownFiles)
    .pipe(markdown())
    .pipe(gulp.dest('web/articles'));
};
gulp.task('markdown',markdownTask);

var scriptsTask = function() 
{
  gulp.src(scriptFiles)
    .pipe(concat('app.js'))
    .pipe(gulp.dest('web/js'));

  gulp.src(
    [
      'bower_components/angular/angular.min.js',
      'bower_components/angular-route/angular-route.min.js'
    ])
    .pipe(concat('vendor.js'))
    .pipe(gulp.dest('web/js'));

  gulp.src(
    [
      'bower_components/angular/angular.min.js.map',
      'bower_components/angular-route/angular-route.min.js.map'
    ])
    .pipe(gulp.dest('web/js'));
};
gulp.task('scripts', scriptsTask);

I am new to gulp. 我是新来的。 Please let me know if I have overlooked something obvious. 如果我忽略了一些显而易见的事情,请告诉我。

As @dman mentions that, gulp.start will be discarded in the next version. 正如@dman提到的那样, gulp.start将在下一个版本中被丢弃。 Also it can be seen in this issue of gulp . 也可以在这个gulp问题中看到。

And in the comments of the answer of @Pavel Evstigneev, @joemaller mentions that we can use run-sequence in this scenario. 在@Pavel Evstigneev的回答评论中,@ joemaller提到我们可以在这种情况下使用run-sequence

But please note that, the author of run-sequence says : 但请注意,run-sequence的作者说:

This is intended to be a temporary solution until the release of gulp 4.0 which has support for defining task dependencies in series or in parallel. 这是一个临时解决方案,直到gulp 4.0发布,它支持串行或并行定义任务依赖。

Be aware that this solution is a hack, and may stop working with a future update to gulp. 请注意,此解决方案是一个黑客攻击,并可能会停止使用未来的更新gulp。

So, before gulp 4.0, we can use run-sequence , after 4.0, we can just use gulp. 所以,在gulp 4.0之前,我们可以使用run-sequence ,在4.0之后,我们可以使用gulp。

gulp 4 吞咽4

gulp.parallel('taskName1', 'taskName2')()
gulp.series('taskName1', 'taskName2')()

I Like gulp4 ! 我喜欢gulp4!

If you need to maintain the order of running tasks you can define dependencies as described here - you just need to return stream from the dependency: 如果您需要维护正在运行的任务的顺序,您可以按照此处所述定义依赖项 - 您只需要从依赖项返回流:

gulp.task('dependency', function () {
  return gulp.src('glob')
    .pipe(plumber())
    .pipe(otherPlugin())
    .pipe(gulp.dest('destination'));
});

Define the task that depends on it: 定义依赖于它的任务:

gulp.task('depends', [ 'dependency' ], function () {
  // do work
});

And use it from watch: 并从手表中使用它:

gulp.task('watch', function () {
  watch('glob', [ 'depends' ]);
});

Now the dependecy task will complete before depends runs (for example your 'jasmine' and 'embed' tasks would be dependencies and you'd have another task 'server' that would depend on them). 现在dependecy任务之前将完成depends运行(例如您的“茉莉花”和“嵌入”任务是依赖和你有另一个任务“服务器”这将取决于他们)。 No need for any hacks. 不需要任何黑客攻击。

To run a task before starting to watch, instead of using gulp.run() or gulp.start() just run the gulp command straight up. 要在开始观察之前运行任务,而不是使用gulp.run()或gulp.start(),只需直接运行gulp命令。

So instead of: 所以代替:

var compress = function () {
    return gulp.src('js/vendor/*.js')
        .pipe(concat('vendor.js'))
        .pipe(gulp.dest('./build/js/'));
};

Just do: 做就是了:

gulp.src('js/vendor/*.js')
        .pipe(concat('vendor.js'))
        .pipe(gulp.dest('./build/js/'));

Or you can wrap that latter code in a "normal" function and call it whenever you want. 或者您可以将后一个代码包装在“普通”函数中,并随时调用它。

-- Inspired by this answer from a similar thread . - 灵感来自类似线程的答案

In Gulp 4 the only thing that seems to be working for me is: 在Gulp 4中,似乎唯一对我有用的是:

gulp.task('watch', function() {
    gulp.watch(['my-files/**/*'], gulp.series('my-func'));
});

gulp.task('my-func', function() {
    return gulp.src('[...]').pipe(gulp.dest('...'));
});

I still dont see how this actually solves the question at hand. 我仍然没有看到这实际上是如何解决手头的问题。

If i have 4 tasks with dependencies defined between them 如果我有4个任务,它们之间定义了依赖关系

A,B,C,D A B C D

where A depends on B, etc as defined by gulp.task('A',['B'],function A(){}); 其中A依赖于gulp.task('A',['B'],function A(){});定义的B等gulp.task('A',['B'],function A(){}); and then i defined a new task using gulp.watch running just the functions would duplicate the dependencies. 然后我使用gulp.watch定义了一个新任务,只运行这些函数会复制依赖项。

eg given these tasks (each tasks function exposed via name): 例如,给定这些任务(每个任务函数通过名称公开):

function A(){}
gulp.task('A',['B'],A);

function A(){}
gulp.task('A',['B'],A);

function B(){}
gulp.task('B',['C'],B);

function C(){}
gulp.task('C',['D'],C);

function D(){}
gulp.task('D',[],D);

i can write 1) 我可以写1)

gulp.task('WATCHER', ['A'], function(){
   ...
}

which would execute A->D but if eg Step B fails it would never enter the task (think of compile or test error) 这将执行A-> D,但如果步骤B失败,它将永远不会进入任务(想想编译或测试错误)

or i can write 2) 或者我可以写2)

gulp.task('WATCHER', [], function(){
   gulp.watch(...,['A'])
}

which would not run A->D until something was changed first. 在第一次更改某些内容之前,它不会运行A-> D.

or i can write 3) 或者我可以写3)

gulp.task('WATCHER', [], function(){
   D();
   C();
   B();
   A();
   gulp.watch(...,['A'])
}

which would cause duplication (and errors over time) of the dependency hierarchy. 这将导致依赖关系层次结构的重复(以及随时间的错误)。

PS: In case someone is wondering why i would want my watch task to execute if any of the dependent tasks fail that is usually because i use watch for live development. PS:如果有人想知道为什么我希望我的监视任务执行,如果任何依赖任务失败,通常是因为我使用watch进行实时开发。 eg. 例如。 i start my watch task to begin working on tests etc. and it can be that the initial code i start out with already has issues thus errors. 我开始我的手表任务开始进行测试等。可能是我开始的初始代码已经有问题因此错误。

So i would hope that gulp run or some equivalent stays for some time 所以我希望gulp运行或等效停留一段时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM