yarn add gulp-rev yarn add gulp-rev-collector
"gulp": "^3.9.1" "gulp-rev": "^8.0.0" "gulp-rev-collector": "^1.1.1"
APIcss
revFormat([options])
prefix
Type: string Default: "-"html
Prefix appended to Hash.
(hash值的前綴)
suffix
Type: string Default: ""node
Suffix appended to Hash.
(hash值的後綴)
lastExt
Type: boolean Default: falsenpm
Append formatted Hash just before last extension of a file.json
(By default, gulp-rev and this plugin will append the formated Hash just before the first . of a filename)gulp
If true, unicorn.ext1.ext2.css would become unicorn.ext1.ext2-
Note with default options, output is the same with gulp-rev: unicorn.css → unicorn-d41d8cd98f.cssapp
使用方法:
svn
- src - css - common.css - img - hsq.jpg - js - index.js - index.html - gulpfile.js
index.html代碼工具
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="./css/common.css"> </head> <body> <p> 利用gulp自動添加版本號 </p> <img src="./img/hsq.jpg" alt=""> </body> <script src="./js/index.js"></script> </html>
gulpfile.js的配置以下
const gulp = require('gulp'), rev = require('gulp-rev'), revCollector = require('gulp-rev-collector'); gulp.task('css',function(){ return gulp.src('src/css/*.*') .pipe(rev()) .pipe(gulp.dest('dist/css')) .pipe(rev.manifest()) .pipe(gulp.dest('rev/css')) }) gulp.task('js',function(){ return gulp.src("src/js/*.*") .pipe(rev()) .pipe(gulp.dest("dist/js")) .pipe(rev.manifest()) .pipe(gulp.dest('rev/js')) }) gulp.task('rev',['css','js'],function(){ return gulp.src(['rev/**/*.json','src/*.html']) .pipe(revCollector({ replaceReved: true })).pipe(gulp.dest('dist')) })
result:每次當文件變化的時候,那麼該文件hash屬性值就會變化,此時gulp-rev新造成新的json格式的manifest,當gulp-rev-collector讀取json清單以後,就會在Html(任意視圖文件)之中替換外部的連接。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="./css/common-2289731958.css"> </head> <body> <p> 利用gulp自動添加版本號 </p> <img src="./img/hsq.jpg" alt=""> </body> <script src="./js/index-8b50a3f85f.js"></script> </html>
但這樣的結果並非咱們想要的,理由以下:
解決方案:不修改被引用資源的文件名,在模板之中,其請求連接之上改成 *.js?v=hash。
手動修改這兩個插件
manifest[originalFile] = revisionedFile;/*( line 134 ) =>*/ manifest[originalFile] = originalFile + '?v=' + file.revHash;
return filename + '-' + hash + ext;/*( line 10 )=>*/ return filename + ext;
if ( !_.isString(json[key]) || path.basename(json[key]).replace(new RegExp( opts.revSuffix ), '' ) !== path.basename(key) ) { isRev = 0; } /*(line 30)=>*/ if ( !_.isString(json[key]) || path.basename(json[key]).split('?')[0] !== path.basename(key) ) { isRev = 0; }
return pattern.replace(/[\-\[\]\{\}\(\)\*\+\?\.\^\$\|\/\\]/g, "\\$&"); /*(line 50)=>*/ var rp = pattern.replace(/[\-\[\]\{\}\(\)\*\+\?\.\^\$\|\/\\]/g, "\\$&"); rp = pattern + "(\\?v=(\\d|[a-z]){8,10})*"; return rp;
patterns.push( escPathPattern( (path.dirname(key) === '.' ? '' : closeDirBySep(path.dirname(key)) ) + path.basename(key, path.extname(key)) ) + opts.revSuffix + escPathPattern( path.extname(key) ) ); /*(line(90)=>)*/ patterns.push( escPathPattern( (path.dirname(key) === '.' ? '' : closeDirBySep(path.dirname(key)) ) + path.basename(key, path.extname(key)) ) + opts.revSuffix + escPathPattern( path.extname(key) ) + "(\\?v=(\\d|[a-z]){8,10})*" );
如此便達到目標了。
結果html以下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="./css/common.css?v=2289731958"> </head> <body> <p> 利用gulp自動添加版本號 </p> <img src="./img/hsq.jpg" alt=""> </body> <script src="./js/index.js?v=8b50a3f85f"></script> </html>
到時候只須要下載下來直接替換個包的index.js文件便可;
yarn add gulp-rev-hu yarn add rev-path-hu yarn add gulp-rev-collector-hu
注意:下載我上傳的文件以後,須要將官方版本的js文件進行替換,切記不要替換package,json文件