Grunt-template未按预期工作

时间:2015-01-14 11:40:38

标签: gruntjs

我尝试使用config.js模块创建grunt-template文件,但它没有按预期创建任何文件。

我已经安装了grunt-template npm install grunt-template --save-dev

当我运行grunt build时,我希望创建一个配置文件,其中包含在模板配置中设置的正确内容。我在下面添加了gruntfile,config.js.tpl和grunt构建结果。会出现什么问题?

Gruntfile.js

module.exports = function (grunt) {

grunt.initConfig({

....

'template': {
            'config': {
                'options': {
                    'data': {
                        'backend': '127.0.0.1:8000'
                    }
                }
            },
            'files': {
                'config.js': ['config.js.tpl']
            }
        }
}

grunt.loadNpmTasks('grunt-template');
grunt.registerTask('build', [
    'template'

]);

}   

config.js.tpl

angular.module('report-constants',[])
    .constant('env', {
        'backend': ''
    });

grunt构建结果

执行时间(2015-01-14 11:33:59 UTC) 装载任务4ms▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇44% 模板:配置3ms▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇33% 模板:文件1ms▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇11% 总计9毫秒

1 个答案:

答案 0 :(得分:5)

filesoptions应位于模板任务的config目标下,请尝试以下代码

 template: {
     config: {
         options: {
             data: {
                 'backend': '127.0.0.1:8000'
             }
         },
         files: {
             'config.js': ['config.js.tpl']
         }
    }
}

同样在config.js.tpl中,请参阅您在backend目标

中定义的config变量
angular.module('report-constants',[])
  .constant('env', {
    'backend': '<%= backend %>'
});
相关问题