将参数传递给processhtml grunt任务以更新index.html文件

时间:2016-03-17 13:20:01

标签: javascript angularjs gruntjs

我是grunt的新手。我有两个grunt任务,一个用于部署到dev,另一个用于部署测试。我想在运行任务时将相应的部署URL作为pararmeter传递。此外,我想使用processhtml

在我的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 ?

//如何使用上面传递的参数来处理html?

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

的index.html: //我想使用processhtml中的动态网址值来更新

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

有更好的方法吗?如果我正在思考正确的方法,该怎么做?请帮助!

1 个答案:

答案 0 :(得分: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 -->