2015-09-27 14 views
0

我只是今天安裝grunt,到目前爲止它正在工作,我非常喜歡它。我在我的網站上使用XSLT,並且需要在其中插入帶有散列文件名的生成的css/js。Grunt - 通過glob獲取文件名並將其用作以後的變量

我想使用https://github.com/tnory56/grunt-xslt但我不知道生成的文件名 - 它們不是靜態的。我如何通過選擇器獲取文件名,並在以後的配置中使用它。

module.exports = function (grunt) { 
    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 
     clean: [ 
        'libs/js/combined/combined.min.v-*.js', 
        'combined.min.v-*.css'  
       ], 
     concat: { 
      css: { 
       src: [ 
        'style.css' 
       ], 
       dest: 'combined.css' 
      }, 
      js: { 
       src: [ 
        'libs/js/common.js' 
       ], 
       dest: 'libs/js/temp/combined.js' 
      } 
     }, 
     cssmin: { 
      css: { 
       src: 'combined.css', 
       dest: 'combined.min.css' 
      } 
     }, 
     uglify: { 
      js: { 
       files: { 
        'libs/js/temp/combined.min.js': ['libs/js/temp/combined.js'] 
       } 
      } 
     }, 
     hashify: { 
     simple: { 
      options: { 
      basedir: '', 
      copy: true, 
      hashmap: 'hashify-hashmap.json' 
      }, 
      files: [{ 
      src: 'combined.min.css', 
      dest: 'combined.min.v-{{hash}}.css', 
      }, 
      { 
      src: 'libs/js/temp/combined.min.js', 
      dest: 'libs/js/combined/combined.min.v-{{hash}}.js' 
      }]  
     } 
     }, 
     injector: { 

     options: { 
      template : 'libs/xsl/main_domain/xhtml.xsl', 
      destFile : 'libs/xsl/main_domain/xhtml-generated.xsl' 
     },  
     local_dependencies: { 
      files: {    
      'libs/xsl/main_domain/xhtml.xsl': ['libs/js/combined/combined.min.v-*.js', 'combined.min.v-*.css'], 
      } 
     } 

     }, 
    }); 
    grunt.loadNpmTasks('grunt-contrib-clean'); 
    grunt.loadNpmTasks('grunt-contrib-concat'); 
    grunt.loadNpmTasks('grunt-contrib-uglify'); 
    grunt.loadNpmTasks('grunt-contrib-watch'); 
    grunt.loadNpmTasks('grunt-contrib-cssmin'); 
    grunt.loadNpmTasks('grunt-hashify'); 
    grunt.loadNpmTasks('grunt-injector'); 
    grunt.registerTask('default', ['clean', 'concat:css', 'cssmin:css', 'concat:js', 'uglify:js', 'hashify:simple', 'injector']); 
}; 

更新:它似乎咕嚕,XSLT沒有與他們的榜樣,即使工作

感謝您的任何提示,請忽略噴油器,我不能在XSLT按如下方式使用它

我的配置,所以我需要另一種解決方案(與XML兼容的模板系統)

接下來我嘗試過(不兼容XML,但我認爲可以解決這個問題)是grunt-template,但是正如我通過{{hash}}沒有評估。

'template': { 
    'process-html-template': { 
     'options': { 
      'data': { 
       'js_file': 'libs/js/combined/combined.min.v-{{hash}}.js', 
      } 
     }, 
     'files': { 
      'tmp/xhtml.xsl': ['libs/xsl/main_domain/xhtml.xsl'] 
     } 
    } 
} 

回答

1

的咕嚕模板插值的語法實際上是<%= ... %>http://gruntjs.com/api/grunt.template),並請注意:它適用於配置變量,沒有任何javascript變量

所以,你應該使用:

grunt.initConfig({ 
    // ... 
    'hash' : '(...your way of computing the hash...)', 
    'template': { 
     'process-html-template': { 
      'options': { 
       'data': { 
        'js_file': 'libs/js/combined/combined.min.v-<%= hash %>.js', 
       } 
      }, 
      'files': { 
       'tmp/xhtml.xsl': ['libs/xsl/main_domain/xhtml.xsl'] 
      } 
     } 
    } 
    // ... 
}); 
相關問題