Skip to content

Commit

Permalink
1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
timtian committed Mar 7, 2015
1 parent f43dea0 commit ed87b37
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 77 deletions.
134 changes: 67 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy())
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy())
.pipe(gulp.dest("./dist"))
})
```

Expand Down Expand Up @@ -93,9 +93,9 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({cwd:'./test/'}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({cwd:'./test/'}))
.pipe(gulp.dest("./dist"))
})
```

Expand All @@ -113,20 +113,20 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({revType:'date'}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({revType:'date'}))
.pipe(gulp.dest("./dist"))
})
```

```html
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024">
href="assets/index.css?max_age=1024">
```
```html
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024&amp;v=201503061144">
href="assets/index.css?max_age=1024&amp;v=201503061144">
```

## options.dateFormat
Expand All @@ -139,16 +139,16 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({revType:'date', dateFormat:'yymmddHHmm'}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({revType:'date', dateFormat:'yymmddHHmm'}))
.pipe(gulp.dest("./dist"))
})
```
#### Output
```html
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024&amp;v=1503061144">
href="assets/index.css?max_age=1024&amp;v=1503061144">
```

## options.hashLength
Expand All @@ -161,23 +161,23 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({revType:'hash', hashLength:5}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({revType:'hash', hashLength:5}))
.pipe(gulp.dest("./dist"))
})
```
#### Output

```html
<!--default output-->
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=7c5d110d">
src="assets/audrey-hepburn.jpg?v=7c5d110d">
```
-->
```html
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=7c5d1">
src="assets/audrey-hepburn.jpg?v=7c5d1">
```

## options.suffix
Expand All @@ -190,10 +190,10 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({suffix:'hashkey'}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({suffix:'hashkey'}))
.pipe(gulp.dest("./dist"))
})
```
#### Output
Expand All @@ -217,10 +217,10 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({fileTypes:['js']}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({fileTypes:['js']}))
.pipe(gulp.dest("./dist"))
})
```
#### Output
Expand All @@ -231,7 +231,7 @@ gulp.task("reveasy", function (argument) {
<head>
<title>gulp-rev-easy</title>
<link type="text/css" rel="stylesheet" media="all"
href="assets/index.css?max_age=1024">
href="assets/index.css?max_age=1024">
</head>
<body>
<img data-src="assets/audrey-hepburn.jpg"
Expand Down Expand Up @@ -265,7 +265,7 @@ gulp.task("reveasy", function (argument) {
src: 'src'
},
css: {
name: 'link',
name: 'link[type="text/css"]',
src: 'href'
},
img:{
Expand All @@ -280,45 +280,45 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({
fileTypes:['img1'],
elementAttributes:{
img1:{
name:'img',
src:'data-src'
}
}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({
fileTypes:['img1'],
elementAttributes:{
img1:{
name:'img',
src:'data-src'
}
}))
.pipe(gulp.dest("./dist"))
})
```
#### Output
```html
<!--default out-->
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=7c5d110d">
src="assets/audrey-hepburn.jpg?v=7c5d110d">
```
-->
```html
<img data-src="assets/audrey-hepburn.jpg?v=7c5d110d"
src="assets/audrey-hepburn.jpg">
src="assets/audrey-hepburn.jpg">
```
```js
var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({
fileTypes:['img1'],
elementAttributes:{
img1:{
name:'img',
src:'data-src'
}
}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({
fileTypes:['img1'],
elementAttributes:{
img1:{
name:'img',
src:'data-src'
}
}))
.pipe(gulp.dest("./dist"))
})
```
## options.transformPath
Expand All @@ -330,27 +330,27 @@ var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
gulp.src("test.html")
.pipe(reveasy({
transformPath:function(orgpath, ver){
var newpath = "http://s1.cdn.com/" + orgpath
+ (orgpath.indexOf('?') > -1 ? "&" : "?")
+ "v=" + ver;
return newpath;
}
}))
.pipe(gulp.dest("./dist"))
gulp.src("test.html")
.pipe(reveasy({
transformPath:function(orgpath, ver){
var newpath = "http://s1.cdn.com/" + orgpath
+ (orgpath.indexOf('?') > -1 ? "&" : "?")
+ "v=" + ver;
return newpath;
}
}))
.pipe(gulp.dest("./dist"))
})
```
#### Output
```html
<!--default out-->
<img data-src="assets/audrey-hepburn.jpg"
src="assets/audrey-hepburn.jpg?v=7c5d110d">
src="assets/audrey-hepburn.jpg?v=7c5d110d">
```
-->
```html
<img data-src="assets/audrey-hepburn.jpg"
src="http://s1.cdn.com/assets/audrey-hepburn.jpg?v=7c5d110d">
```
#Other
# Other
31 changes: 22 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var fs = require('fs');
var dateformat = require('dateformat')
var url = require('url');
var _ = require('lodash');
var crypto = require('crypto');

module.exports = function (options) {

Expand All @@ -26,7 +27,7 @@ module.exports = function (options) {
src: 'src'
},
css: {
name: 'link',
name: 'link[type="text/css"]',
src: 'href'
},
img:{
Expand All @@ -45,6 +46,8 @@ module.exports = function (options) {

return through.obj(function (file, enc, cb) {


gutil.log("====begin rev:" + gutil.colors.cyan(file.path));
if (file.isNull()) {
this.push(file);
return cb();
Expand Down Expand Up @@ -73,22 +76,32 @@ module.exports = function (options) {

if(options.revType == "hash"){
var filepath = path.join(options.cwd, url.parse(src).pathname);
revv = require('crypto')
.createHash('md5')
.update(
fs.readFileSync(filepath, {encoding: 'utf8'}))
.digest("hex").substring(0, options.hashLength);
if(fs.existsSync(filepath)){
revv = crypto
.createHash('md5')
.update(
fs.readFileSync(filepath, {encoding: 'utf8'}))
.digest("hex").substring(0, options.hashLength);
}else{
gutil.log(gutil.colors.red(filepath + " not found"));
}
}else{
revv = dateformat(new Date(), options.dateFormat)
}

var newname = options.transformPath(src, revv);
$asset.attr(attributes.src, newname);
gutil.log(src +"-->" + newname);
if(revv != ""){
var newname = options.transformPath(src, revv);
$asset.attr(attributes.src, newname);
gutil.log(src + " --> ", gutil.colors.green(newname));
}else{
gutil.log(gutil.colors.blue("ignore:rev is empty "), src);
}
}
}
}
file.contents = new Buffer($.html());

gutil.log("====end rev:" + gutil.colors.cyan(file.path));
}
catch (err) {
this.emit('error', new gutil.PluginError('gulp-rev-easy', err));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-rev-easy",
"version": "1.0.7",
"version": "1.0.8",
"description": "easy add hash to asset path uc.js -> uc.js?v=afb23 or simple add date uc.js -> uc.js?v=201503031214",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit ed87b37

Please sign in to comment.