本文介绍了从task.run传递Grunt配置选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试了几件事情,似乎无法让这个工作,但我会认为这是很简单的事情。我试图在任务运行时将变量|选项传递给Grunt初始化配置。



当前我有两个单独的配置:

  sass:{
dev:{
options:{
style:'expanded'
},
文件:[{
expand:true,
cwd:source +'scss /',
src:'* .scss',
dest:destination +' css',
ext:'.css'
}]
},
制作:{
选项:{
style:'compressed'
$,
文件:[{
expand:true,
cwd:source +'scss /',
src:'* .scss',
dest :destination +'css',
ext:'.css'
}]
}
}

正如您所看到的,除了样式选项外,它们是相同的。我想要做的是:

  sass:{
build:{
options:{
style:style
},
files:[{
expand:true,
cwd:source +'scss /',
src:'*。 scss',
dest:destination +'css',
ext:'.css'
}]
}
}

。 ..

grunt.registerTask(sass,function(){
grunt.task.run('sass:build:style = expanded');
});

但我无法解决如何将选项从grunt.task.run传递到初始化配置这条路。任何Grunt专家都知道如何做到这一点?谷歌似乎也没有答案...不知道我是否接近这个错误?



感谢您的帮助!

解决方案

请参阅 Grunt API示例 - 这正是你在找什么。

  $ grunt --type = dev 


$ b

现在你可以通过以下方式得到这个值:

  grunt.registerTask('default','description ...',function(){
var type = grunt.option('type')||'dev'; //如果没有任何东西从cli传入设置为'dev'
if(type =='dev'){
grant.task.run('sass:dev');
} else if(type =='production' ){
grant.task.run('sass:production');
}
});

我认为它必须像我上面指出的那样。


Tried a few things and cannot seem to get this to work, but I would have thought it was something quite simple. I am trying to pass a variable|option into a Grunt initialize config when the task is run.

At the minute I have two separate configs:

sass: {
  dev: {
    options: {
      style: 'expanded'
    },
    files: [{
      expand: true,
      cwd: source + 'scss/',
      src: '*.scss',
      dest: destination + 'css',
      ext: '.css'
    }]
  },
  production: {
    options: {
      style: 'compressed'
    },
    files: [{
      expand: true,
      cwd: source + 'scss/',
      src: '*.scss',
      dest: destination + 'css',
      ext: '.css'
    }]
  }
}

As you can see, these are the same except the style option. What I would like to do is:

sass: {
  build: {
    options: {
      style: style
    },
    files: [{
      expand: true,
      cwd: source + 'scss/',
      src: '*.scss',
      dest: destination + 'css',
      ext: '.css'
    }]
  }
}

...

grunt.registerTask("sass", function () {
  grunt.task.run('sass:build:style=expanded');
});

But I cannot work out how to pass the option from grunt.task.run to the initialize config in this way. Any Grunt experts know how to do this? Google didn't seem to have the answer either... Not sure if I'm approaching this wrong?

Thanks for any help!

解决方案

see Grunt API example - it is exactly what are you looking for.

$ grunt --type=dev

Now you can get this value by:

grunt.registerTask('default','description...', function() {
    var type = grunt.option('type') || 'dev'; //if nothing passed from cli set to 'dev'
    if (type == 'dev') {
        grant.task.run('sass:dev');
    } else if (type == 'production') {
        grant.task.run('sass:production');
    }
});

I think it must be something like I pointed above..

这篇关于从task.run传递Grunt配置选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:47