Javascript 以编程方式将参数传递给 grunt 任务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14780622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:53:39  来源:igfitidea点击:

Programmatically pass arguments to grunt task?

javascriptnode.jsgruntjs

提问by Nick Heiner

I have a grunt task that calls other grunt tasks. I want to call a subtask with programmatically determined arguments. Is this possible? I spent some time digging around the lib/grunt.jsand lib/grunt/task.js, but couldn't figure it out.

我有一个调用其他 grunt 任务的 grunt 任务。我想使用以编程方式确定的参数调用子任务。这可能吗?我花了一些时间在lib/grunt.jslib/grunt/task.js周围挖掘,但无法弄清楚。

I'm using grunt-compasswith the following arguments specified in Gruntfile.js:

我正在使用Gruntfile.js 中grunt-compass指定的以下参数:

compass: {
  default_options: {
    src: 'components/201',
    dest: 'build',
    require: ['zurb-foundation']
  }
}

I want to be able to override them at runtime:

我希望能够在运行时覆盖它们:

tasks/my-task.js:

任务/我的-task.js

// simplified example
module.exports = function(grunt) {
  grunt.registerTask('foo', 'bar', function() {
    var chooseDest = doWork();
    grunt.task.run('compass', {src: 'src', dest: chooseDest});
  });
};

For reference:

以供参考:

$ grunt --version
grunt-cli v0.1.6
grunt v0.4.0rc6

回答by Nick Heiner

I figured it out. Use the <%= %>syntax in Gruntfile.js:

我想到了。使用<%= %>Gruntfile.js 中的语法:

compass: {
  default_options: {
    src: 'components/<%= myTask.src %>',
    dest: 'build',
    require: ['zurb-foundation']
  }
}

Then you can set it in your task:

然后你可以在你的任务中设置它:

grunt.config.set('myTask.src', getSrc());

回答by Thomas Decaux

You can edit all the Grunt config:

您可以编辑所有 Grunt 配置:

grunt.config('compass.default_options.src', 'blabla');

Just before run the task. But your solution is "cleaner".

就在运行任务之前。但是您的解决方案“更干净”。