From ed87b37044f0297ad215fd01ace4ef78a18ae93a Mon Sep 17 00:00:00 2001 From: timtian Date: Sat, 7 Mar 2015 12:24:30 +0800 Subject: [PATCH] 1.0.8 --- README.md | 134 +++++++++++++++++++++++++-------------------------- index.js | 31 ++++++++---- package.json | 2 +- 3 files changed, 90 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index a00de38..38e85c2 100644 --- a/README.md +++ b/README.md @@ -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")) }) ``` @@ -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")) }) ``` @@ -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 + href="assets/index.css?max_age=1024"> ``` ```html + href="assets/index.css?max_age=1024&v=201503061144"> ``` ## options.dateFormat @@ -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 + href="assets/index.css?max_age=1024&v=1503061144"> ``` ## options.hashLength @@ -161,10 +161,10 @@ 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 @@ -172,12 +172,12 @@ gulp.task("reveasy", function (argument) { ```html + src="assets/audrey-hepburn.jpg?v=7c5d110d"> ``` --> ```html + src="assets/audrey-hepburn.jpg?v=7c5d1"> ``` ## options.suffix @@ -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 @@ -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 @@ -231,7 +231,7 @@ gulp.task("reveasy", function (argument) { gulp-rev-easy + href="assets/index.css?max_age=1024"> + src="assets/audrey-hepburn.jpg?v=7c5d110d"> ``` --> ```html + src="assets/audrey-hepburn.jpg"> ``` ```js @@ -309,16 +309,16 @@ 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 @@ -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 + src="assets/audrey-hepburn.jpg?v=7c5d110d"> ``` --> ```html ``` -#Other \ No newline at end of file +# Other \ No newline at end of file diff --git a/index.js b/index.js index 53f8309..9cba65a 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -26,7 +27,7 @@ module.exports = function (options) { src: 'src' }, css: { - name: 'link', + name: 'link[type="text/css"]', src: 'href' }, img:{ @@ -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(); @@ -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)); diff --git a/package.json b/package.json index a673201..40e6efb 100644 --- a/package.json +++ b/package.json @@ -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": {