咕噜条件选项 [英] Grunt conditional option

查看:91
本文介绍了咕噜条件选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Jade任务,该任务的构建和开发任务具有非常相似的选项,除了开发布尔和另一个目标。最简单的方法是:

  jade:{
dev:{
选项:{
data:{
dev:true,// dev true
config:...,
pkg:...,
帮助者:。 ..
}
},
文件:[{
dest:'<%= config.workingDir%>',
...
}]
},
build:{
选项:{
data:{//无开发
配置:...,
pkg: ...,
助手:...
}
},
文件:[{
dest:'<%= config.buildDir%>' ,
...
}]
}
}



虽然这里有相当多的重复,特别是如果我想在轨道上添加更多选项的话。所以我试图创建一个可以从命令行和监视任务中工作的构建任务。



最近我得到了这个设置,在那里我可以从命令行运行 grunt jade --dev ,但不能在watch任务中设置开发布尔值。



< pre $ build:{
options:{
data:{
dev:'<%= grunt.option(dev)%> ',
config:...,
pkg:...,
helpers:...
}
},
files:[{
dest:'<%= grunt.option(dev)? config.workingDir:config.buildDir%>',
...
}]
}

手表任务:

 手表:{
jade:{
...
tasks:['jade'] //选项为false
}
}

我也尝试创建一个自定义任务来设置选项,然后运行手表,当收听手表事件时,我可以看到选项设置正确。

  grunt.registerTask('dev','Grunt dev mode',function(){
grunt.option('dev',true) ;
grunt.task.run('watch');
});
$ b grunt.event.on('watch',function(action,filepath,target){
console.log(grunt.option('dev')); // true
});

即使日志显示开发布尔值为true,也会传递错误的任务选项。所以,说了这么多之后,我是否会以正确的方式设置选项,或者我坚持让构建和开发任务能够重复相同的信息?

I我也尝试过使用grunt.util ._。expand方法,但很少成功。
data:<%= grunt.util ._。extend(jade.options.data,{dev:true})%>,

解决方案

您可以指定 选项 任务然后在目标目标(dev vs build)特定选项 $ c>级别。这样可以避免在多个目标之间重复相同的配置:

  jade :{
选项:{
配置:...,
pkg:...,
助手:...
},
dev: {
options:{
dev:true
},
files:[{
dest:'<%= config.workingDir%>',
$ ... b $ b}]
},
build:{
files:[{
dest:'<%= config.buildDir%>' ,
...
}]
}
}

更新



根据 jade 情况下的评论,所有选项都需要在 data ,如果使用上面的方法,本地 data 将完全覆盖全局数据,你可以这样做,因为Gruntfile.js只是JavaScript:

  var dataConfigBuild = { 
config:...,
pkg:...,
助手:...
};

//使用您最喜欢的方式克隆一个对象
var dataConfigDev = {};
(dataConfigBuild中的var propName){
dataConfigDev [propName] = dataConfigBuild [propName];
}

//并添加特定的开发工具
dataConfigDev.dev = true;

然后在您的任务配置中使用如下所示:

  jade:{
dev:{
options:{
data:dataConfigDev
},
files:[ {
dest:'<%= config.workingDir%>',
...
}]
},
构建:{
选项:{
data:dataConfigBuild
},
files:[{
dest:'<%= config.buildDir%>',
...
}]
}
}

注意,这段代码是没有测试过。



请注意,您没有看到您在手表手表中看不到预期配置的原因相关的代码很可能是因为您需要设置 spawn:false


I'm attempting to create a Jade task that will have a build and dev task that have very similar options, except a dev boolean, and a different destination. The simplest way I've been able to achieve this is:

jade: {
    dev: {
        options: {
            data: {
                dev: true, // dev true
                config: ...,
                pkg: ...,
                helpers: ...
            }
        },
        files: [{
            dest: '<%= config.workingDir %>',
            ...
        }]
    },
    build: {
        options: {
            data: {  // no dev
                config: ...,
                pkg: ...,
                helpers: ...
            }
        },
        files: [{
            dest: '<%= config.buildDir %>',
            ...
        }]
    }
}

There's considerable repetition in this though, particularly if I want to add more options down the track. So I'm trying to create one build task that will work both from command line and from a watch task.

The closest I've gotten is this setup, where I can run grunt jade --dev from the command line but can't set the dev boolean in the watch task.

build: {
    options: {
        data: {
            dev: '<%= grunt.option("dev") %>',
            config: ...,
            pkg: ...,
            helpers: ...
        }
    },
    files: [{
        dest: '<%= grunt.option("dev") ? config.workingDir : config.buildDir %>',
        ...
    }]
}

The watch task:

watch: {
    jade: {
        ...
        tasks: ['jade'] // the option is false
    }
}

I've also attempted to create a custom task that sets the option and then runs watch, which when listening to the watch event I can see the option is correctly set

grunt.registerTask('dev', 'Grunt dev mode', function(){
    grunt.option('dev', true);
    grunt.task.run('watch');
});

grunt.event.on('watch', function(action, filepath, target) {
    console.log(grunt.option('dev')); // true
});

Even though the log shows the dev boolean as true the wrong task options are being passed. So, with all that said, am I going about setting options the right way or am I just stuck with having a build and dev task that repeat the same information?

I've also tried using the grunt.util._.expand method with little success. data: "<%= grunt.util._.extend(jade.options.data, {dev: true}) %>",

解决方案

You can specify options at the task level that will serve as the default and then further specify target (dev vs build) specific options at the target level. That way you avoid repeating the same configuration between multiple targets:

jade: {
    options : {
        config: ...,
        pkg: ...,
        helpers: ...
    },
    dev: {
        options: {
            dev : true
        },
        files: [{
            dest: '<%= config.workingDir %>',
            ...
        }]
    },
    build: {
        files: [{
            dest: '<%= config.buildDir %>',
            ...
        }]
    }
}

UPDATE

Based on the comment that in the jade case the options all need to be in data, and if the method above was used, the local data would completely override the global data, you could do this since the Gruntfile.js is just JavaScript:

var dataConfigBuild = {
    config: ...,
    pkg: ...,
    helpers: ...
};

//use your favorite way of "cloning" an object
var dataConfigDev = {};
for(var propName in dataConfigBuild) {
  dataConfigDev[propName] = dataConfigBuild[propName];
}

//and add the dev specific stuff
dataConfigDev.dev = true;

And then use like this in your task configuration:

jade: {
    dev: {
        options: {
            data : dataConfigDev
        },
        files: [{
            dest: '<%= config.workingDir %>',
            ...
        }]
    },
    build: {
        options : {
            data : dataConfigBuild
        },
        files: [{
            dest: '<%= config.buildDir %>',
            ...
        }]
    }
}

Note, this code is not tested.

As a side note, the reason you are not seeing your not seeing the expected config in the watch related code is most likely because you need to set spawn : false.

这篇关于咕噜条件选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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