2015-04-02 60 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' 
      }); 
    }); 
}