javascript - GruntJS glob 作为源

标签 javascript gruntjs handlebars.js

我有一个巨大的数组,例如

[ { src:
   [ './src/branding/appLogo/template.hbs',
     './src/buttons/append/template.hbs',
     './src/buttons/button/template.hbs',
     './src/buttons/clean/template.hbs',
     './src/buttons/clear/template.hbs',
     './src/buttons/danger/template.hbs',
     './src/buttons/default/template.hbs',
     './src/buttons/disabled/template.hbs',
     './src/buttons/large/template.hbs',
     './src/buttons/link/template.hbs',
     './src/buttons/primary/template.hbs',
     './src/buttons/small/template.hbs',
     './src/buttons/success/template.hbs',
     './src/documentation/technology-overview/template.hbs',
     './src/forms/checkbox/template.hbs',
     './src/forms/fieldAppend/template.hbs',
     './src/forms/fieldDefault/template.hbs',
     './src/forms/fieldError/template.hbs',
     './src/forms/fieldPrepend/template.hbs',
     './src/forms/fieldPrependAppend/template.hbs',
     './src/forms/radioButton/template.hbs',
     './src/forms/select/template.hbs',
     './src/forms/textarea/template.hbs',
     './src/icons/appSwitch/template.hbs',
     './src/modules/alerts/template.hbs',
     './src/modules/attributions/template.hbs',
     './src/modules/avatar/template.hbs',
     './src/modules/beacon/template.hbs',
     './src/modules/d3DonutChart/template.hbs',
     './src/navigation/appSwitcher/template.hbs',
     './src/navigation/avatarDropdown/template.hbs',
     './src/navigation/contextMenu/template.hbs',
     './src/navigation/headerMenu/template.hbs',
     './src/navigation/paginate/template.hbs',
     './src/prototypes/home/template.hbs',
     './src/structures/form/template.hbs',
     './src/structures/header/template.hbs',
     './src/typography/blockquote/template.hbs',
     './src/typography/floats/template.hbs',
     './src/typography/headers/template.hbs',
     './src/typography/hidden/template.hbs',
     './src/typography/hr/template.hbs',
     './src/typography/hrText/template.hbs',
     './src/typography/lists/template.hbs',
     './src/typography/paragraph/template.hbs',
     './src/typography/pre/template.hbs',
     './src/typography/table/template.hbs',
     './src/typography/tags/template.hbs',
     './src/utilities/extends/template.hbs',
     './src/utilities/keyframes/template.hbs',
     './src/utilities/mixins/template.hbs',
     './src/utilities/svgFilterPieShrink/template.hbs',
     './src/utilities/svgFilterSubtleDropShadow/template.hbs' ],
  dest: './build/scripts/handlebars.js' } ]

我想渲染所有这些 Handlebars 模板,它们应该全部放在一个文件中。

module.exports = (grunt) ->
  config = grunt.file.readJSON("config.json")
  hbsGlob = ''
  grunt.task.loadTasks('./tasks')
  grunt.option('config', config)

  grunt.initConfig
    pkg: grunt.file.readJSON("package.json")

    handlebars:
      options:
        namespace: 'Guide'
        processName: (path) ->
          return path.replace('.js', '').replace('/', '.') + '.template'

      guide:
        files:
          @hbsGlob


  grunt.registerTask 'etch-scripts', =>
    glob = grunt.option('filteredGlob')
    glob.push "!./src/**/*.{md,js,json}"

    options =
      rename: (dest, matchedSrcPath, options) ->
        return dest

    @hbsGlob = grunt.file.expandMapping(glob,  config.build + '/scripts/handlebars.js', options)

    grunt.task.run 'handlebars:guide'

  grunt.loadNpmTasks('grunt-contrib-handlebars');

我得到的唯一输出是

Running "handlebars:guide" (handlebars) task
>> 0 files created.

Done, without errors.

知道出了什么问题吗,以便我可以使用该 glob 作为 src/dest?

最佳答案

经过大量研究后,我发现了一个小替代方案,最终效果完美!

设置 hbsGlob 变量后,我将该 var 添加到 Grunt 配置中 grunt.config.set('hbsGlob', @hbsGlob)

然后在handlebars:guide任务中我将其设置为

guide:
  files: '<%= hbsGlob %>'

一切都很完美!

编辑 -- 来源:https://stackoverflow.com/a/14780870/399742

关于javascript - GruntJS glob 作为源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24785176/

相关文章:

javascript - div标签对齐问题

node.js - Ember.js - 如果需要通过 Grunt (Node.js),则无法识别 Handlebars

ember.js - Ember 产量参数传递

javascript - 无法使用方法(传递)索引作为参数从数组中检索数据

javascript - Visual Studio JavaScript 调试速度极慢

javascript - 什么是 $$hashKey 添加到我的 JSON.stringify 结果

javascript - 转义 HTML 值是什么意思?

css - 在服务器上少编译?

javascript - Grunt JS 如何定位 * 所有文件夹

javascript - 递归 Handlebars.js 模板。如何确定深度?