Caching gulp生成的文件中的文件src版本控制
我们分别使用gulp插件和生成图标字体和css。字体缓存在Chrome中,因此我想在styles.css中字体源字符串的末尾添加版本号 下面是我要转换的生成css的一部分Caching gulp生成的文件中的文件src版本控制,caching,gulp,Caching,Gulp,我们分别使用gulp插件和生成图标字体和css。字体缓存在Chrome中,因此我想在styles.css中字体源字符串的末尾添加版本号 下面是我要转换的生成css的一部分 @font-face { font-family: "some-icons"; url('/assets/fonts/some-icons.woff2') format('woff2'); } 所需输出为 @font-face { font-family: "some-icons"
@font-face {
font-family: "some-icons";
url('/assets/fonts/some-icons.woff2') format('woff2');
}
所需输出为
@font-face {
font-family: "some-icons";
url('/assets/fonts/some-icons.woff2?v=someVersion') format('woff2');
}
其中someVersion是一个时间戳或其他一些随机字符串,用于将文件与其以前的版本区分开来
我正在寻找一个可以解析css并在src字符串末尾添加“salt”的gulp插件
欢迎使用其他解决方案我发现了一个利用此插件的解决方案。下面是基本的吞咽任务:它在文件内容中查找some-icons.woff2,并用一些图标替换它。woff2?v=timestamp
'use strict';
const gulp = require('gulp');
const modify = require('gulp-modify-file');
const timestamp = Math.round(Date.now() / 1000);
gulp.task('modify', ()=> {
return gulp.src('tmp/styles.css')
.pipe(modify(
(content, path, file)=> {
content = content.replace('some-icons.woff2', 'some-icons.woff2?v=' + timestamp);
return `${content}`
}
))
.pipe(gulp.dest('dist'))
});
@Napolux,享受)