我最终使用git-rev-sync和gulp-replace并将{{git}}放入我的index.html源文件中.
var git = require('git-rev-sync');
return gulp.src(pathConfig.src + 'index.html')
.pipe($.replace('{{git}}', git.long()))
.pipe(gulp.dest(pathConfig.dest + 'index.html'));
Run Code Online (Sandbox Code Playgroud)
注入最新的 git 提交哈希值
您可以这样做:
git 将最新的提交哈希存储在.git/refs/heads/<branch name>.
使用以下代码读取文件的内容,然后将其放在您需要的位置
# load fs for reading /writing files
fs = require("fs"),
// define your task
gulp.task('doSometing', function() {
return gulp.src(dirs.src + '/templates/*.html')
// read the SHA-1 of the latest commit
.pipe(fs.readFile("path/to/.git/refs/heads/<branch name>",
// Default encoding for fs is binary so we have to set it to UTF-8
"utf-8",
// Process the data you have read (SHA-1)
function(err, _data) {
//do something with your data
})
.pipe(gulp.dest('destination/path));
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1906 次 |
| 最近记录: |