2013-08-27 55 views
5

我真的很想能夠擁有一個開發grunt文件並使用相同的文件生成腳本版本。用於開發/生產環境的替代grunt.js任務

我已經試過這個建議,但是當我嘗試調用dev/prod參數時,我的腳本會失敗。我相信答案是針對舊版本的grunt,或者可能是我正在使用的插件。

module.exports = function (grunt) { 

    // load all grunt tasks 
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); 

    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 
     compass: { 
      dev: { 
       options: { 
        config: 'config.rb', 
        force: true, 
        livereload: true 
       } 
      } 
     }, 
     uglify: { 
      build: { 
       src: ['docroot/js/*.js', 'docroot/components/**/*.js'], 
       dest: 'docroot/dis/main.min.js' 
      } 
     }, 
     watch: { 
      options: { 
       dateFormat: function(time) { 
        grunt.log.writeln('The watch finished in ' + time + 'ms at' + (new Date()).toString()); 
        grunt.log.writeln('Waiting for more changes...'); 
       }, 
       livereload: true 
      }, 
      sass: { 
       files: ['docroot/sass/*.scss'], 
       tasks: ['compass:dev'] 
      }, 
      /* watch and see if our javascript files change, or new packages are installed */ 
      js: { 
       files: '<%= uglify.build.src %>', 
       tasks: ['uglify'] 
      }, 
      /* watch our files for change, reload */ 
      livereload: { 
       files: ['*.html', 'docroot/css/*.css', 'docroot/img/*', 'docroot/js/{main.min.js, plugins.min.js}'], 
       options: { 
        livereload: true 
       } 
      } 
     } 
    }); 


    grunt.registerTask('default', 'watch'); 
}; 

真的,只要我可以得到兩個版本,稱他們用,例如運行:

grunt //local 
grunt prod //live 

然後我就可以玩的腳本,什麼加載。

+1

我想你要找的是什麼「選項」 - https://github.com/gruntjs/grunt/wiki/grunt.option - 我相信這仍然是正確的方法。 也檢出yeoman,因爲他們有一個乾淨的設置與各種有用的件到這個難題。所有你必須記住的是grunt服務器和grunt構建。 – Dylan

回答

14

你也可以註冊調用任務的陣列任務

grunt.registerTask('prod', ['tasks1','task2']); 

默認任務之前,這將是

$ grunt prod 
+0

真棒謝謝迪倫,我需要看看這個。你有沒有想給我一個清晰的例子? –

+0

輝煌的,我必須承認,我沒有得到它,直到我讓老人做魔術,然後看着文件。所以謝謝指點我yeoman。和registerTask。輝煌。 –

+0

感謝您與Yeoman上船,我一直在拖延;) –