【问题标题】:How can I run a grunt task from within a grunt task?如何从 grunt 任务中运行 grunt 任务?
【发布时间】:2013-02-23 10:34:03
【问题描述】:

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

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

更新 1:

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

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

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

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

更新 2:

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

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

【问题讨论】:

标签: javascript node.js gruntjs


【解决方案1】:

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

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');
    });

【讨论】:

    【解决方案2】:

    来自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']);
    });
    

    【讨论】:

    • 请看我的编辑。我在我的任务中添加了我想如何使用 concat 的细节。
    • 您可能需要像@ArronS 回答那样配置任务。
    • 我今天偶然发现了一件事。如果您想多次运行相同的任务但使用不同的配置,那么通过grunt.config 将选项传递给每个任务是个坏主意,因为任务被排队而不运行,因此当它们开始执行时,只有最后分配的配置值会有效。例如,如果您想安排将 JS 文件连接到一个目的地,并将 CSS 文件连接到另一个目的地,那么只有您的最后一个配置才会有效。为避免这种情况,您还需要为每个任务动态设置单独的目标。
    【解决方案3】:

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

    任务定义:

      grunt.registerTask('tests', function (spec) {
    
        if (spec) {
          grunt.config('browserify.tests.src', spec);
        }
    
        grunt.task.run(['jshint', 'browserify:tests', 'jasmine']);
    
      }); 
    

    任务调用:

    grunt tests
    

    grunt tests:somewhere/specPath.js
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        我们如何从一个任务中多次运行同一个任务,例如。

        grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
          
          var done = this.async();
          // Run some sync stuff.
          grunt.log.writeln('Processing task...');
          var versions = Object.keys(versionConf);
         
          setTimeout(function() {
            versions.forEach(function(version) {
              console.info(version);
              //process.env.version = version;
              grunt.config('port', versionConf[version].port);
              grunt.task.run('test-perf');
            })
            done();
          }, 1000);
         
        });
        

        我想根据版本每次运行带有差异端口的 test-perf。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-24
          相关资源
          最近更新 更多