2013-10-04 64 views
1

我正在使用Grunt構建我的應用程序,並且我想在每次提交時生成內部版本號。 我希望將此編號插入到我的index.html文件中,以便允許緩存清除。如何在使用Grunt構建時向index.html插入版本號?

我的index.html:

<html> 
<head> 
    <link type="text/css" rel="stylesheet" href="assets/css/main.css?v=<%VERSION%>" /> 
</head> 
<body> 
<script> 
    //require global configuration 
    var require = { 
     "urlArgs": "v=<%VERSION%>" 
    }; 
</script> 
<!--entry point to application is main.js--> 
<script data-main="main.js" src="assets/js/lib/require/require.js"></script> 
</body> 
</html> 

所以我想更換<%VERSION%>在CSS鏈接和需要配置。

有沒有可以做到這一點的任何Grunt任務?我正在使用grunt-contrib-requirejs進行優化。這可以幫助嗎?

回答

2

我最終保持簡單並使用grunt-text-replace插件用時間戳替換佔位符(@@ BUST @@)。

replace: { 
      bust: { 
       src: ['./target/*.html'], 
       overwrite: true,     // overwrite matched source files 
       replacements: [ 
        { 
         from: '@@[email protected]@', 
         to: "<%= new Date().getTime() %>" 
        } 
       ] 
      } 
     } 
相關問題