node.js 如何从 grunt 任务中运行 grunt 任务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15284556/
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-09-02 13:47:16  来源:igfitidea点击:

How can I run a grunt task from within a grunt task?

javascriptnode.jsgruntjs

提问by Arron S

I've created a new grunt task and within it I want to use grunt-contrib-concat to concatenate a few files together.

我创建了一个新的 grunt 任务,在其中我想使用 grunt-contrib-concat 将几个文件连接在一起。

I looked through the docs but I don't find anything that hinted at being able to do this. It seems like a trivial use case, so I'm probably just over looking something.

我查看了文档,但没有找到任何暗示能够做到这一点的内容。这似乎是一个微不足道的用例,所以我可能只是在寻找一些东西。

Update 1:

更新 1:

I also want to be able to configure this task from within my custom task.

我还希望能够从我的自定义任务中配置此任务。

For example, I create a list of files in my custom task. After I have that list, I want to pass them to the concat task. How can I do that?

例如,我在自定义任务中创建了一个文件列表。获得该列表后,我想将它们传递给 concat 任务。我怎样才能做到这一点?

I would like to be able to do something like this.

我希望能够做这样的事情。

grunt.task.run('concat', { src: ['file1','file2'], dest: 'out.js'})

Update 2:

更新 2:

To achieve what I want, I have to manually configure the grunt task. Here's an example that showed me what I wanted.

为了实现我想要的,我必须手动配置 grunt 任务。这是一个例子,向我展示了我想要的东西。

https://github.com/gruntjs/grunt-contrib/issues/118#issuecomment-8482130

https://github.com/gruntjs/grunt-contrib/issues/118#issuecomment-8482130

回答by Arron S

Here's an example of manually configuring a task within a task and then running it.

这是在任务中手动配置任务然后运行它的示例。

https://github.com/gruntjs/grunt-contrib/issues/118#issuecomment-8482130

https://github.com/gruntjs/grunt-contrib/issues/118#issuecomment-8482130

 grunt.registerMultiTask('multicss', 'Minify CSS files in a folder', function() {
        var count = 0;
        grunt.file.expandFiles(this.data).forEach(function(file) {
            var property = 'mincss.css'+count+'.files';
            var value = {};
            value[file] = file;
            grunt.config(property, value);
            grunt.log.writeln("Minifying CSS "+file);
            count++;
        });
        grunt.task.run('mincss');
    });

回答by Pascal Belloncle

From https://github.com/gruntjs/grunt/wiki/Creating-tasks

来自https://github.com/gruntjs/grunt/wiki/Creating-tasks

grunt.registerTask('foo', 'My "foo" task.', function() {
  // Enqueue "bar" and "baz" tasks, to run after "foo" finishes, in-order.
  grunt.task.run('bar', 'baz');
  // Or:
  grunt.task.run(['bar', 'baz']);
});

回答by elmuchacho

Thx to Arron that pointed us out in the right direction to his own question. The grunt.config is the key from the example above. This task will override the src property of the browserify task

感谢 Arron,他为我们指出了他自己问题的正确方向。grunt.config 是上面例子中的关键。此任务将覆盖 browserify 任务的 src 属性

Task definition:

任务定义:

  grunt.registerTask('tests', function (spec) {

    if (spec) {
      grunt.config('browserify.tests.src', spec);
    }

    grunt.task.run(['jshint', 'browserify:tests', 'jasmine']);

  }); 

Task call:

任务调用:

grunt tests

or

或者

grunt tests:somewhere/specPath.js

回答by ruyadorno

If you are feeling lazy I ended up publishing a npm module that forwards the configs from your task into the subtask that you want to run:

如果你觉得懒惰,我最终发布了一个 npm 模块,它将你的任务中的配置转发到你想要运行的子任务中:

https://www.npmjs.org/package/extend-grunt-plugin

https://www.npmjs.org/package/extend-grunt-plugin