【问题标题】:Grunt-template not working as expectedGrunt-template 未按预期工作
【发布时间】:2015-01-14 11:40:38
【问题描述】:

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

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

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

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% 44% 模板:配置 3ms ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 33% 模板:文件 1ms ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 11% 总共 9 毫秒

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    filesoptions应该在模板任务的config目标下,试试下面的代码

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

    同样在 config.js.tpl 中,参考您在 config 目标中定义的 backend 变量

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-04
      • 2022-01-24
      • 2015-05-11
      • 2020-05-15
      • 2014-10-31
      • 2018-02-12
      • 2014-01-20
      • 2015-01-13
      相关资源
      最近更新 更多