admin管理员组文章数量:1333201
I'm striving to create a Gulp task that does nothing except calling a custom function. No, I have, no source files, and no, I have no destination files. I just want to call a custom function in a standalone task, so I can have other tasks depending on it.
For the love of me, I've checked Google and SO and I couldn't find an example. The closest I've e with is this
var through = require('through2');
gulp.task(
'my-custom-task',
function ()
{
return through.obj(
function write(chunk, enc, callback) {
// here is where the custom function is called
myCustomFunction('foo', 'bar');
callback(null, chunk);
}
);
}
);
Now this does call myCustomFunction
, but when I run the task with gulp my-custom-task
, I can see the task starting but not finishing.
[10:55:37] Starting 'clean:tmp'... [10:55:37] Finished 'clean:tmp' after 46 ms [10:55:37] Starting 'my-custom-task'...
How should I write my task correctly?
I'm striving to create a Gulp task that does nothing except calling a custom function. No, I have, no source files, and no, I have no destination files. I just want to call a custom function in a standalone task, so I can have other tasks depending on it.
For the love of me, I've checked Google and SO and I couldn't find an example. The closest I've e with is this
var through = require('through2');
gulp.task(
'my-custom-task',
function ()
{
return through.obj(
function write(chunk, enc, callback) {
// here is where the custom function is called
myCustomFunction('foo', 'bar');
callback(null, chunk);
}
);
}
);
Now this does call myCustomFunction
, but when I run the task with gulp my-custom-task
, I can see the task starting but not finishing.
[10:55:37] Starting 'clean:tmp'... [10:55:37] Finished 'clean:tmp' after 46 ms [10:55:37] Starting 'my-custom-task'...
How should I write my task correctly?
Share Improve this question asked Oct 30, 2016 at 17:19 J. DoeJ. Doe 1011 silver badge5 bronze badges1 Answer
Reset to default 8If you just want a task that runs some function, then just do that:
gulp.task('my-custom-task', function () {
myCustomFunction('foo', 'bar');
});
If your function does something asynchronously, it should call a callback at the end, so gulp is able to know when it’s done:
gulp.task('my-custom-task', function (callback) {
myCustomFunction('foo', 'bar', callback);
});
As for why your solution does not work, using through
is a way to work with streams. You can use it to create handlers which you can .pipe()
into gulp streams. Since you have nothing actually using gulp streams, there is no need for you to create a stream handler and/or use through here.
本文标签: javascriptHow do I write a Gulp task that simply calls a functionStack Overflow
版权声明:本文标题:javascript - How do I write a Gulp task that simply calls a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742285544a2446861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论