gulp rev for js&css&img path in html without prejudice of other
$ npm install gulp-rev-orig --save-dev
assets\index.css
assets\index.js
test.html
gulpfile.js
//test.html
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024">
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="assets/audrey-hepburn.jpg">
<script src="assets/index.js?max_age=1024"></script>
<script src="/assets/index.js"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
</body>
</html>
//gulpfile.js
gulp.task('revOrig-default', function(argument) {
gulp.src('test.html')
.pipe(revOrig())
.pipe(gulp.dest('./dist'));
});
gulp revOrig-default
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024&v=0a1085be">
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="assets/audrey-hepburn.jpg?v=6a5f96ce">
<script src="assets/index.js?max_age=1024&v=3fffb693"></script>
<script src="/assets/index.js?v=3fffb693"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
</body>
</html>
- base
- revType
- dateFormat
- hashLength
- suffix
- fileTypes
- elementAttributes
- transformPath
- defaultDoAddElementRev
- elementAttributes.addElementRev
- createDefaultOptions
type:string
default:file.cwd
set base directory for your assets
<img src='/a/b.png'/> rev will found file in path.join(base, src)
options.cwd is obsoleted, use options.base or set gulp.src(path, {cwd:mycwd}) instead
type:['hash'|date']
default:'hash'
set rev type
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-byDate', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
revType: 'date'
}))
.pipe(gulp.dest('./dist'));
});
<!--default output-->
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024">
-->
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024&v=201512291737">
type:string
default:'yyyymmddHHMM'
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-withDateFormat', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
revType: 'date',
dateFormat: 'yymmddHHmm'
}))
.pipe(gulp.dest('./dist'));
});
<!--default output-->
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024&v=201512291712">
-->
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024&v=1512291712">
type:integer
default:8
gulp.task('revOrig-withHashLength', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
revType: 'hash',
hashLength: 5
}))
.pipe(gulp.dest('./dist'));
});
<!--default output-->
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=6a5f96ce">
-->
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=6a5f9">
type:string
default:v
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-withHashKey', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
suffix: 'hashkey'
}))
.pipe(gulp.dest('./dist'));
});
<!--default output-->
<img src="assets/audrey-hepburn.jpg?v=6a5f96ce">
-->
<img src="assets/audrey-hepburn.jpg?hashkey=6a5f96ce">
type:array
default:['js','css','img']
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-withFileTypes', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
fileTypes: ['js']
}))
.pipe(gulp.dest('./dist'));
});
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024">
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="assets/audrey-hepburn.jpg">
<script src="assets/index.js?max_age=1024&v=3fffb693"></script>
<script src="/assets/index.js?v=3fffb693"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
</body>
</html>
/*
how find target element
js: {
tagRegStr: '(<script [^>]+/?>)',
pathRegStr: '(?:(\\s+src=")([^"]+)("))'
}
*/
type:object
default:{
js: {
tagRegStr: '(<script [^>]+/?>)',
pathRegStr: '(?:(\\s+src=")([^"]+)("))'
},
css: {
tagRegStr: '(<link [^>]+/?>)',
pathRegStr: '(?:(\\s+href=")([^"]+)("))'
},
img: {
tagRegStr: '(<img [^>]+/?>)',
pathRegStr: '(?:(\\s+src=")([^"]+)("))'
}
}
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-withElementAttributes', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
fileTypes: ['img1'],
elementAttributes: {
img1: {
tagRegStr: '(<img [^>]+/?>)',
pathRegStr: '(?:(\\s+data-src=")([^"]+)("))'
}
}
}))
.pipe(gulp.dest('./dist'));
});
<!--default out-->
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=6a5f96ce">
-->
<img data-src="assets/audrey-hepburn.jpg?v=6a5f96ce"
src="assets/audrey-hepburn.jpg">
type: function
default: function (orgPath, rev) {
var regStr = '((\\?|\\&|\\&\\;)' + options.suffix + '=)([^&\\s]+)';
var reg = new RegExp(regStr, regOption);
var newpath = orgPath;
if (reg.test(orgPath)) {
newpath = orgPath.replace(reg, '$1' + rev);
} else {
newpath += ((orgPath.indexOf('?') > -1 ? '&' : '?') + options.suffix + '=' + rev);
}
return newpath;
}
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-withTransformPath', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
transformPath: function(orgpath, ver) {
var newpath = 'http://s1.cdn.com/' + orgpath + (orgpath.indexOf('?') > -1 ? '&' : '?') + 'v=' + ver;
return newpath;
}
}))
.pipe(gulp.dest('./dist'));
});
<!--default out-->
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=6a5f96ce">
-->
<img data-src="assets/audrey-hepburn.jpg"
src="http://s1.cdn.com/assets/audrey-hepburn.jpg?v=6a5f96ce">
type: function
default: function (segment, addSrcRev, elementSetting) {
elementSetting.pathReg.lastIndex = 0;
var match = elementSetting.pathReg.exec(segment);
if (match) {
var src = match[2];
var revSrc = addSrcRev(src);
return segment.replace(elementSetting.pathReg, '$1' + revSrc + '$3');
} else {
return segment;
}
}
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-withDefaultAddElementRev', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
defaultAddElementRev: function(segment, addSrcRev, elementSetting) {
elementSetting.pathReg.lastIndex = 0;
var match = elementSetting.pathReg.exec(segment);
if (match) {
var src = match[2];
var revSrc = addSrcRev(src);
if (revSrc !== src) {
var base = './xxx' + (revSrc[0] === '/' ? '' : '/');
return segment.replace(elementSetting.pathReg, '$1' + base + revSrc + '$3');
} else {
return segment;
}
} else {
return segment;
}
}
}))
.pipe(gulp.dest('./dist'));
});
<!--default out-->
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024&v=0a1085be">
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="assets/audrey-hepburn.jpg?v=6a5f96ce">
<script src="assets/index.js?max_age=1024&v=3fffb693"></script>
<script src="/assets/index.js?v=3fffb693"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
</body>
</html>
-->
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="./xxx/assets/index.css?max_age=1024&v=0a1085be">
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="./xxx/assets/audrey-hepburn.jpg?v=6a5f96ce">
<script src="./xxx/assets/index.js?max_age=1024&v=3fffb693"></script>
<script src="./xxx/assets/index.js?v=3fffb693"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
</body>
</html>
type: function
default: use options.defaultDoAddElementRev
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-withDefaultDoAddElementRev', function(argument) {
gulp.src('test.html')
.pipe(revOrig({
defaultDoAddElementRev: function(segment, addSrcRev, elementSetting) {
elementSetting.pathReg.lastIndex = 0;
var match = elementSetting.pathReg.exec(segment);
if (match) {
var src = match[2];
var revSrc = addSrcRev(src);
return segment.replace(elementSetting.pathReg, '$1[' + revSrc + ']$3');
} else {
return segment;
}
}
}))
.pipe(gulp.dest('./dist'));
});
<!--default out-->
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024&v=0a1085be">
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="assets/audrey-hepburn.jpg?v=6a5f96ce">
<script src="assets/index.js?max_age=1024&v=3fffb693"></script>
<script src="/assets/index.js?v=3fffb693"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
</body>
</html>
-->
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024">
</head>
<body>
<img data-src="[assets/audrey-hepburn.jpg?v=6a5f96ce]" src="assets/audrey-hepburn.jpg">
<script src="assets/index.js?max_age=1024&v=3fffb693"></script>
<script src="/assets/index.js?v=3fffb693"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
</body>
</html>
type: function
create default options for template
var gulp = require('gulp');
var revOrig = require('gulp-rev-orig');
gulp.task('revOrig-createDefaultOptions', function() {
var options = revOrig.createDefaultOptions();
options.elementAttributes.loadJs = {
tagRegStr: '(<js [^>]+/?>)',
pathRegStr: '(?:(\\s+src=")([^"]+)("))'
};
options.fileTypes.push('loadJs');
gulp.src('test.html')
.pipe(revOrig(options))
.pipe(gulp.dest('./dist'));
});
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024"
/>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024"
/>
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="assets/audrey-hepburn.jpg">
<script src="assets/index.js?max_age=1024"></script>
<script src="/assets/index.js"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
<loads style="display: none;">
<js load="true" src="/assets/index.js"></js>,
<js load="true" src="/assets/index.js"></js>
<js load="true" src="/assets/index.js"></js>
<js load="false" src="/assets/index.js"></js>
<js load="false" src="/assets/index.js"></js>
<js load="false" src="/assets/index.js"></js>
</loads>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024&v=0a1085be"
/>
<link type="text/css" rel="stylesheet" media="all" href="assets/index.css?max_age=1024&v=0a1085be"
/>
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg" src="assets/audrey-hepburn.jpg?v=6a5f96ce">
<script src="assets/index.js?max_age=1024&v=3fffb693"></script>
<script src="/assets/index.js?v=3fffb693"></script>
<script src="/assets/index1.js"></script>
<script src="http://assets/index.js"></script>
<loads style="display: none;">
<js load="true" src="/assets/index.js?v=3fffb693"></js>,
<js load="true" src="/assets/index.js?v=3fffb693"></js>
<js load="true" src="/assets/index.js?v=3fffb693"></js>
<js load="false" src="/assets/index.js?v=3fffb693"></js>
<js load="false" src="/assets/index.js?v=3fffb693"></js>
<js load="false" src="/assets/index.js?v=3fffb693"></js>
</loads>
</body>
</html>