我可以将参数传递给一个吞咽任务吗? [英] Can I pass arguments to a gulp task?

查看:107
本文介绍了我可以将参数传递给一个吞咽任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio中使用gulp,仅作为客户端构建工具

我有很多任务因此我将它们移动到单独的文件中,并在项目的 gulpfile.js 中引用它们,如下所示:

 需要( ../路径/到/任务/文件/ fooCommon.js); //有一个名为fooCommon的任务

然后在 gulpfile中。

  gulp.task(foo,[fooCommon ],function(){}); 

这很好用,我可以在各个项目中重复使用常见任务。



但是,有些任务需要只能在 gulpfile.js - 中访问的配置,我如何将它们作为参数传递给任务?

解决方案

有两件事情浮现在脑海。第一个是创建任务,而不是创建任务 fooCommon.js ,它可以创建一些可用的 gulp 插件,所以然后使用它,你只需将你的配置参数传递给插件。如果在这种情况下不适合,那么你可以将你的任务逻辑移动到一个包装函数中,例如

fooCommon.js



  module.exports = function(gulp,config){
gulp.task(fooCommon,function(){
//使用这里的配置
});
};



Gulpfile



  //定义你需要的任何配置
var gulp = require('gulp');

var config = {...}

var taskCreator = require('../ path / to / task / files / fooCommon.js');

//调用'fooCommon.js'中的函数,它将通过配置传递,而
//将创建您共同拥有的所有任务。
taskCreator(gulp,config);

//根据导出函数创建的任务注册任务。
gulp.task(foo,[fooCommon],function(){});


I'm using gulp in Visual Studio, as a client-side build tool only.

I have lots of tasks which are common across various projects, so I moved them to separate files, and reference them in a project's gulpfile.js as follows:

require("../path/to/task/files/fooCommon.js");  // has a task called "fooCommon"

And then in the gulpfile.js I wrap it in a task:

gulp.task("foo", ["fooCommon"], function () { });

This works nicely, and I can reuse common tasks across projects.

BUT, some tasks require configurations which are only accessible in gulpfile.js - how can I pass them as arguments to a task?

解决方案

Two things come to mind. The first would be that instead of fooCommon.js creating tasks, it could create something usable as a gulp plugin, so then to use it, you just pass your config arguments to the plugin. If that doesn't fit well in this case, then you can move your task logic into a wrapper function, e.g.

fooCommon.js

module.exports = function(gulp, config){
    gulp.task("fooCommon", function () {
        // Use the config here
    });
};

Gulpfile

// Define whatever config you need
var gulp = require('gulp');

var config = {...}

var taskCreator = require('../path/to/task/files/fooCommon.js');

// Call the function in 'fooCommon.js', which will be passed the config, and
// will create all the tasks you have in common.
taskCreator(gulp, config);

// Register a task depending on the task created by the exported function.
gulp.task("foo", ["fooCommon"], function () { });

这篇关于我可以将参数传递给一个吞咽任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆