繁体   English   中英

使用grunt可以简化文件选项(此处为handlebars任务)

[英]With grunt, is there a way to simplify a files option (here handlebars task)

我刚开始使用grunt,然后搜索一种更动态地声明文件选项的方法

我的配置是

  handlebars: {        
      compile: {
          options: {   
              namespace: Handlebars.Template',                    
              files: {
                  "public/media/js/module1/precompiledTemplates.js": "public/media/js/module1/templates/*.hbs",
                  "public/media/js/module2/precompiledTemplates.js": "public/media/js/module2/templates/*.hbs  ",
                  "public/media/js/module3/precompiledTemplates.js": "public/media/js/module3/templates/*.hbs  "
              }
  },

我有很多模块,有没有办法声明类似的东西?

              files: {
                  "public/media/js/$1/precompiledTemplates.js": "public/media/js/(*.)/templates/*.hbs"                    
              }

我接过来一看这个 ,但我不觉得管理这个“问题”的正确方法。

我有一个基于先前评论的解决方案:

您必须具有关于handlebars.compile(或使用的插件)的常规配置。 并在javascript中添加自定义任务和构建文件对象。

grunt.registerTask('hbs_compile', 'Your custom task', function() {
    var myFiles = {};

    // ... here you build your myFiles object, see grunt.file api ...
    // in my case :
    grunt.file.expand('my/modules/path/*').forEach(function (basePath) {
        myFiles[basePath + '/precompiledTemplates.js'] = basePath + '/templates/*.hbs'
    })
    // and you overide the configuration with your object
    grunt.config.set('handlebars.compile.files', myFiles);
    grunt.task.run('handlebars:compile');
});

现在,您可以在CLI中运行:

$ grunt hbs_compile

并添加模块,减少重新配置的麻烦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM