2015-04-02 63 views
3

我想让我的gruntfile.js自然结构化,这样微任务就可以一个接一个地跟随彼此。 假设我有以下结构:注册不带grunt.initConfig的Grunt任务

grunt.initConfig({ 
    pkg: grunt.file.readJSON('package.json'), 

    clean: { 
     movedTyping: 'options...' 
    }, 
    copy: { 
     typing: 'options...', 
     lessVariables: 'options...', 
     html: 'options...' 
    }, 
    less: { 
     compile: 'options...' 
    }, 
    typescript: { 
     compile: 'options...' 
    } 
}); 


grunt.registerTask('build', [ 
    // TYPESCRIPT 
    'typescript:compileSingle', 
    'copy:typing', 
    'clean:movedTyping', 

    // LESS 
    'less:compile', 
    'copy:lessVariables', 

    // HTML 
    'copy:html' 
]); 

但我想实现其他结构:

grunt.registerTask('build', function() { 
    // TYPESCRIPT 
    grunt.task.run('typescript', 'options...'); 
    grunt.task.run('copy', 'options...'); 
    grunt.task.run('clean', 'options...'); 

    // LESS 
    grunt.task.run('less', 'options...'); 
    grunt.task.run('copy', 'options...'); 

    // HTML 
    grunt.task.run('copy', 'options...'); 
}); 

如何?

+1

在一个侧面note.You会喜欢[一饮而尽(http://gulpjs.com/)更多。 – Vishwanath 2015-04-03 15:56:44

回答

3

您可以使用设置对象的属性。 (点)符号。因此也可以设置嵌套结构数据。我还没有遇到更清洁的方法,并很乐意看到更好的方法。

grunt.registerTask('build', function() { 
    // TYPESCRIPT 
    grunt.config.set('typescript.compile','<options>'); 
    grunt.task.run('typescript'); 

    ...................... 
}); 
+1

它的工作原理和看起来很简单。所以可惜,我花了一个小时为我的解决方案http://stackoverflow.com/a/29415472/4137472 – 2015-04-02 14:33:16

1

为了实现这个我创建了NPM模块create-grunt-tasks。 现在我的呼噜声文件看起来像这样:

// Gruntfile.js 
module.exports = function (grunt) { 
    require('create-grunt-tasks')(grunt, function (create) { 
     create.task('build') 
      // Compile TypeScript and move typing 
      .sub('typescript', { 
       src: 'src/index.ts', 
       dest: 'build/index.js', 
       options: { module: 'amd', target: 'es5', declaration: true } 
      }) 
      .sub('copy', { 
       expand: true, flatten: true, 
       src: 'build/index.d.ts', 
       dest: 'build/typing/index.d.ts' 
      }) 
      .sub('clean', ['build/index.d.ts']) 
      // Copy HTML 
      .sub('copy', { 
       expand: true, flatten: true, 
       src: 'src/index.html', 
       dest: 'build/index.html' 
      }); 
    }); 
}