• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

timtian/gulp-rev-easy

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

timtian/gulp-rev-easy

开源软件地址(OpenSource Url):

https://github.com/timtian/gulp-rev-easy

开源编程语言(OpenSource Language):

JavaScript 87.5%

开源软件介绍(OpenSource Introduction):

gulp-rev-easy

upgrade to [1.2.x] add new parse mode:plain, support unwell-formed html, if you want use old mode set revMode to 'dom', now default mode is plain mode

//options
{
    revMode:'plain'
}

Install

$ npm install gulp-rev-easy --save-dev

Examples

assets\index.css
assets\index.js
test.html
gulpfile.js

Input

//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>
</body>
</html>

Useage

//gulpfile.js
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 reveasy

Output

<!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&amp;v=0a1085be">
</head>
<body>
  <img data-src="assets/audrey-hepburn.jpg" 
    src="assets/audrey-hepburn.jpg?v=7c5d110d">
  <script src="assets/index.js?max_age=1024&amp;v=3fffb693"></script>
</body>
</html>

Options

  • base
  • revMode
  • revType
  • dateFormat
  • hashLength
  • suffix
  • fileTypes
  • elementAttributes
  • patterns
  • ignorePattern
  • ignoreFilter
  • transformPath
  • findFile

options.base

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

options.revMode

revMode:['dom'|plain']
default:'plain'

set rev mode, set patterns

more and more unwell-formed documents, it's can't parse to dom-tree or sometime lost/change raw text, so add new parse mode:plain.

options.revType

type:['hash'|date']
default:'hash'

set rev type 

example

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"))
    
})
<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&amp;v=201503061144">

options.dateFormat

type:string
default:'yyyymmddHHMM'
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"))
    
})

Output

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

options.hashLength

type:integer
default:8
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"))
    
})

Output

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

-->

<img data-src="assets/audrey-hepburn.jpg" 
  src="assets/audrey-hepburn.jpg?v=7c5d1">

options.suffix

type:string
default:v
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"))
    
})

Output

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

-->

<img src="assets/audrey-hepburn.jpg?hashkey=7c5d110d">

options.fileTypes

type:array
default:['js','css','img']
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"))
    
})

Output

<!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&amp;v=3fffb693"></script>
</body>
</html>

options.elementAttributes (dom mode)

    /*
    how find target element 
     js: {
        name: 'script',
        src: 'src'
      }
      =>
      $('script').attr('src') = newpath
     use other selector in name such as
     {
        name:'.cached',
        src:'src'
      }
    */
    type:object
    default:{
      js: {
        name: 'script',
        src: 'src'
      },
      css: {
        name: 'link[type="text/css"]',
        src: 'href'
      },
      img:{
        name: 'img',
        src : 'src'
      }
    }
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"))
})

Output

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

-->

  <img data-src="assets/audrey-hepburn.jpg?v=7c5d110d" 
    src="assets/audrey-hepburn.jpg">
var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
  gulp.src("test.html")
    .pipe(reveasy({
        revMode:'dom',
        fileTypes:['img1'],
        elementAttributes:{
            img1:{
                name:'img',
                src:'data-src'
            }
        }))
    .pipe(gulp.dest("./dist"))
})

options.patterns (plain mode)

type : object
default:
    patterns:{
        js:{
            regex:/(<script[^>]*?\s+src=)("(?:.+?)"|'(?:.+?)')([^>]*?>)/gi
        },
        css:{
            regex:/(<link[^>]*?\s+rel=['"]stylesheet['"]?[^>]*?\s+href=)("(?:.+?)"|'(?:.+?)')([^>]*?>)/gi
        },
        img:{
            regex:/(<img[^>]*?\s+src=)("(?:.+?)"|'(?:.+?)')([^>]*?>)/gi
        }
    }

return $1 + ['|"] + options.transformPath($2) + $3
var gulp = require("gulp");
var reveasy = require("gulp-rev-easy");

gulp.task("reveasy", function (argument) {
  gulp.src("test.html")
    .pipe(reveasy({
            revMode:'plain',
            fileTypes:['img1', 'img'],
            patterns:{
                img1:{
                    regex:/(<img[^>]*?\s+data-src=)("(?:.+?)"|'(?:.+?)')([^>]*?>)/gi
                }
            }}))
    .pipe(gulp.dest("./dist"))
})

Output

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

-->

  <img data-src="assets/audrey-hepburn.jpg?v=7c5d110d"
    src="assets/audrey-hepburn.jpg?v=7c5d110d">

options.ignorePattern (plain mode)

type:RegExp
default:/<script\b[^<]*(?:(?!<\/script>)<[^<]*)+<\/script>/gi
ignore rev content,
default will ignore any content in <script type=text\/javascript>..</script>
if you want rev script set to false
gulp.task("reveasy-plain-mode", function (argument) {
	gulp.src("test.html")
		.pipe(reveasy({
			revMode:'plain',
			fileTypes:['img1', 'img', 'css', 'js'],
			ignorePattern:false,
			patterns:{
				img1:{
					regex:/(<img[^>]*?\s+data-src=)("(?:.+?)"|'(?:.+?)')([^>]*?>)/gi
				}
			}}))
		.pipe(gulp.dest("./dist"))
})

In

<img data-src="assets/audrey-hepburn.jpg" 
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap