2016-03-17 29 views
0

我對grunt非常陌生。我有兩個grunt任務,一個用於部署到開發,一個用於部署測試。我想在運行任務時將相應的部署url作爲參數表傳遞。將參數傳遞給processhtml grunt任務以更新index.html文件

grunt.registerTask('deploy-dev', ['msdeploy:target1', 'msdeploy:target2']); 
grunt.registerTask('deploy-test', ['msdeploy:target3', 'msdeploy:target4']); 
//I would like to pass my respective url parameters here , how ? 

//我怎麼可以使用上面在這裏傳遞的參數來處理:此外,我想用這個傳遞的參數值在我的index.html使用processhtml

我的部署任務進行更新html?

processhtml: { 
       options: { 
        data: { 
         message: grunt.template.today(), 
        } 
       }, 
       dist: { 
        files: { 
         'dist/index.html': ['index.html'] 
        } 
       } 
      } 

的Index.html: //我想用從processhtml這裏的動態URL值進行更新

<!-- build:template !--> 
    <script> 
     var deployUrl = '<%= url%>' 
    </script> 
    <!-- /build --> 

是否有這樣做的更好的辦法?如果我正確地思考,如何去做?請幫助!

回答

0
processhtml: { 
       options: { 
        data: { 
         message: grunt.template.today(), 
         //this will be replaced dynamically based on 
         //deployment regions 
         serviceUrl: '' 
        } 
       }, 
       dist: { 
        files: { 
         'dist/index.html': ['index.html'] 
        } 
       } 
      } 

grunt.registerTask('deploy-dev', function() { 
     grunt.config.set('processhtml.options.data.serviceUrl', "http://server1/api"); 
     //run the processhtml with the new parameter 
     grunt.task.run('processhtml'); 
     //call the deployment task 
     grunt.task.run('msdeploy:server1'); 
    }); 

index.html文件的變化:

<!-- build:template !--> 
    <script> 
     var buildDateTime = '<%= message %>' 
     var serviceBase = '<%= serviceUrl %>' 
    </script> 
<!-- /build -->