Gulp 给所有静态文件引用加版本号
在juqery和easyui 盛行的年代许多项目采用纯静态页面去构建前端框架从而实现前后端分离的目的。项目开发周期内往往会频繁修改更新某个文件,当你将文件更新到服务器后客户端由于缓存问题而出现显示异常的情况,这时候你会经常让客户清楚缓存,然后开始了漫长的教学过程,,,
我们也会尝试在静态资源后面加上 ”index.html?v=“ new Date().getTime(); 来解决这个问题,效果也颇为良好,但当项目为已有项目时会是个相当头疼的问题,这个时候我们就会希望有一个全局构建工具来帮我们批量添加,优秀的程序员就开发的 静态资源版本构建工具 gulp webpack 等
想要达到的效果
添加前
<script src="index.js"></script>
<a href="index.html"></a>
<a href="index.html?id=10"></a> 添加后
<script src="index.js?v=78y65gv6e2"></script>
<a href="index.html?v=998776654e"></a>
<a href="index.html?v=9877657wa2&id=10"></a>
开始:
- 安装gulp和gulp插件(前提:需要安装node.js)
打开node命令行,cd 进入到项目根文件夹(若安装失败,推荐国内的淘宝npm镜像,教程:https://blog.csdn.net/quuqu/article/details/64121812,使用方法将npm改为cnpm)
- npm install –save-dev gulp
- npm install –save-dev gulp-rev
- npm install –save-dev gulp-rev-collector
- npm install –save-dev gulp-asset-rev
- npm install –save-dev run-sequence
- 在项目跟目录新建 gulpfile.js 文件
分别一次执行
var gulp = require('gulp'),
assetRev = require('gulp-asset-rev'),
runSequence = require('run-sequence'),
rev = require('gulp-rev'),
revCollector = require('gulp-rev-collector');
var cssSrc = './src/css/**/*.css',
jsSrc = './src/js/**/*.*';
contentSrc = './src/content/*.html';
grfSrc = './src/grf/*.grf';
imagesSrc = './src/images/*.*';
resourceSrc='./src/resource/*.*';
//css
gulp.task('css', function () {
gulp.src(cssSrc)
.pipe(rev())
.pipe(gulp.dest('./dist/css'))
.pipe(rev.manifest({ meger: true }))
.pipe(gulp.dest('./dist/css'))
});
//js
gulp.task('js', function () {
gulp.src(jsSrc)
.pipe(rev())
.pipe(gulp.dest("./dist/js"))
.pipe(rev.manifest({ meger: true }))
.pipe(gulp.dest('./dist/js'))
});
//html
gulp.task('content', function () {
gulp.src(contentSrc)
.pipe(rev())
.pipe(gulp.dest("./dist/content"))
.pipe(rev.manifest({ meger: true }))
.pipe(gulp.dest('./dist/content'))
});
//grf
gulp.task('grf', function () {
gulp.src(grfSrc)
.pipe(rev())
.pipe(gulp.dest("./dist/grf"))
.pipe(rev.manifest({ meger: true }))
.pipe(gulp.dest('./dist/grf'))
});
//iamges
gulp.task('images', function () {
gulp.src(imagesSrc)
.pipe(rev())
.pipe(gulp.dest("./dist/images"))
.pipe(rev.manifest({ meger: true }))
.pipe(gulp.dest('./dist/images'))
});
//其他资源
gulp.task('resource', function () {
gulp.src(resourceSrc)
.pipe(rev())
.pipe(gulp.dest("./dist/resource"))
.pipe(rev.manifest({ meger: true }))
.pipe(gulp.dest('./dist/resource'))
});
//其他固定名称资源
gulp.task('copy', function () {
gulp.src(['./src/AilinkFlashLinker.js','./src/favicon.ico','./src/LXFlashLinker.swf','./src/LXFlashSWITCH.swf'])
.pipe(gulp.dest('./dist'));
});
//根据json配置信息生成全新html
gulp.task('revHtml', function () {
gulp.src(['./dist/js/*.json', './dist/css/*.json', './dist/content/*.json', './dist/grf/*.json', './dist/images/*.json','./dist/resource/*.json', './src/**/*.html'])
.pipe(revCollector({ replaceReved: true }))
.pipe(gulp.dest('./dist'));
});
//执行
gulp.task('default', function (done) {
condition = false;
runSequence(
['css'],
['js'],
['content'],
['grf'],
['images'],
['resource'],
['copy'],
['revHtml'],
done);
});
修改插件
»>1
打开node_modules\gulp-rev\index.js
134行: manifest[originalFile] = revisionedFile;
更新为: manifest[originalFile] = originalFile + ‘?v=’ + file.revHash;
»>2
打开 node_modules\rev-path\index.js
注意:原文这里的路径是打开nodemodules\gulp-rev\nodemodules\rev-path\index.js,根据你的具体情况进行修改。
9行: return modifyFilename(pth, (filename, ext) =>
${filename}-${hash}${ext});
更新为:return modifyFilename(pth, (filename, ext) =>
${filename}${ext});
17行: return modifyFilename(pth, (filename, ext) => filename.replace(new RegExp(
-${hash}$), ‘’) + ext);
更新为: return modifyFilename(pth, (filename, ext) => filename + ext);
»>3
打开node_modules\gulp-rev-collector\index.js
40行:var cleanReplacement = path.basename(json[key]).replace(new RegExp( opts.revSuffix ), ‘’ );
更新为:var cleanReplacement = path.basename(json[key]).split(‘?’)[0];
»>4
打开node_modules\gulp-assets-rev\index.js
78行: var verStr = (options.verConnecter || “-“) + md5;
更新为:var verStr = (options.verConnecter || “”) + md5;
80行: src = src.replace(verStr, ‘’).replace(/(.[^.]+)$/, verStr + “$1”);
更新为:src=src+”?v=”+verStr;
»>5
打开node_modules\gulp-rev-collector\index.js
173行regexp: new RegExp( prefixDelim + pattern, ‘g’ ),
更新为 regexp: new RegExp( prefixDelim + pattern + ‘(\?v=\w{10})?’, ‘g’ )
上述更改后在项目所在根目录执行 gulp 两次,第一次是根据修改生成 json 文件,第二次是根据json对照文件生成全新html 文件
生成后你会发现 原本 html?id=5 生成后 html?v=25454454?id=5 这并不是我们想要的结果,我们希望重新生成html后变为 html?v=25454454&id=5
打开node_modules\gulp-rev-collector\index.js
在173行下面新增 regexpEx: new RegExp( prefixDelim + pattern + ‘(\?v=\w{10})(\?)?’, ‘g’ ),
在176行下面新增 replacementEx: ‘$1’ + manifest[key]+’&’
在 193行下面新增 src = src.replace(r.regexpEx,r.replacementEx);
最后我们再执行两次 gulp 如愿得到想要的结果(美中不足的是在head头引用静态文件的位置版本号后面都加上了一个&,单不会影响正常使用)
2019年5月20日12:53:55 修改
有人说 gulp 太老,为何不用 webpack,我想说工具选择合适自己的就行,不在乎什么时候的
Gulp 给所有静态文件引用加版本号的更多相关文章
- Asp-Net-Core开发笔记:使用NPM和gulp管理前端静态文件
前言 本文介绍的是AspNetCore的MVC项目,WebApi+独立前端这种前后端分离的项目就不需要多此一举了~默认前端小伙伴是懂得使用前端工具链的. 为啥要用MVC这种服务端渲染技术呢? 简单项目 ...
- Web前端性能优化——如何有效提升静态文件的加载速度
WeTest 导读 此文总结了笔者在Web静态资源方面的一些优化经验. 一.如何优化 用户在访问网页时, 最直观的感受就是页面内容出来的速度,我们要做的优化工作, 也主要是为了这个目标.那么为了提高页 ...
- web前端性能优化,提升静态文件的加载速度
原文地址:传送门 WeTest 导读 此文总结了笔者在Web静态资源方面的一些优化经验. 如何优化 用户在访问网页时, 最直观的感受就是页面内容出来的速度,我们要做的优化工作, 也主要是为了这个目标. ...
- Django中静态文件引用优化
静态文件引用优化 在html文件中是用django的静态文件路径时,一般会这么写: <script type="text/javascript" src="/sta ...
- Django-2- 模板路径查找,模板变量,模板过滤器,静态文件引用
模板路径查找 路径配置 2. templates模板查找有两种方式 2.1 - 在APP目录下创建templates文件夹,在文件夹下创建模板 2.2 - 在项目根目录下创建templates文件夹, ...
- Django模板变量及静态文件引用
一.模板变量传递 1.视图向模板传递变量 视图中的列表,数组,字典,函数均可以传递给模板 在视图中定义变量通过render(content{‘name’ : value})传递给模板 模板通过{{ ...
- django在关闭debug后,admin界面 及静态文件无法加载的解决办法
当debug为true的时候,ALLOWED_HOSTS是跳过不管用的.所以这里需要将debug关掉,令debug=false,ALLOWED_HOSTS=[ '*' ]表示所有的主机都可以访问 开启 ...
- django 项目运行时static静态文件不能加载问题处理
一.首先检查网页中的加载路径是否正确,如果和文件所在路径不一致,就把html改下路径 二.加载路径和文件实际路径一致,看下配置文件: STATIC_URL = '/static/'STATIC_ROO ...
- Django模板变量,过滤器和静态文件引用
模版路径查找 首先去settings.py里面找TEMPLATES ,在TEMPLATES下面找DIRS,找到就返回,没找到就继续往下,如果APP_DIRS设置为为Ture,那么就会到上面 INSTA ...
随机推荐
- 关于org.apache.lucene.queryParser.ParseException: Encountered "" 解决方法
现象: org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, col ...
- vi 调到第一行和最后一行
gg 跳到文件第一行Shift + g 跳到文件最后一行 chmod 777 abc.txt
- [1-2] Dependence-Aware Service Function Chain Design and Mapping
文献名称:Dependence-Aware Service Function Chain Design and Mapping 文献类型(期刊.硕论.博论):会议:Globecom 发表年份:2017 ...
- C++的static_cast、dynamic_cast和const_cast用法
static_cast.dynamic_cast和const_cast static_cast: 用法: static_cast < type-id > (expression) ...
- [Web] mobx 异步操作
转载自:https://www.jianshu.com/p/66dd328726d7 异步action action只能影响正在运行的函数,而无法影响当前函数调用的异步操作 .action 包装/装饰 ...
- ROC曲线 VS PR曲线
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...
- VS2015 创建C++动态库及使用
转载:https://blog.csdn.net/w_x_myself/article/details/82252646 1.dll的特点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代 ...
- Xamarin.FormsShell基础教程(4)Shell项目内容列表页面运行效果
Xamarin.FormsShell基础教程(4)Shell项目内容列表页面运行效果 在创建好Shell解决方案后,就可以运行程序了.本小节将讲解运行后的效果. 内容列表页面 运行程序,初始效果如图1 ...
- 在线visio软件,在线流程图软件,在线绘图、在线画图
1. https://www.bullmind.com/ 推荐bullmind的在线visio软件,一种低成本的Visio替代品.bullmind是基于 网络的绘图工具,具有出色图表功能.您可以使用b ...
- Multitenancy
Multitenancy https://www.gartner.com/en/information-technology/glossary/multitenancy 公用一套物理环境,划分出不同的 ...