1.0.4 • Published 8 years ago

gulp-asset-revision v1.0.4

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

Revises the version of the resource referenced in the html.

Install

npm install --save-dev gulp-asset-revision

Usage

var gulp = require('gulp');
var hash = require('gulp-hash-list');
var revision = require('gulp-asset-revision');

gulp.task('hash', function() {
    return gulp.src(['./src/**/*.js','./src/**/*.css'])
        .pipe(hash({
            "template": "{name}{ext}?hash={hash}"
        }))
        .pipe(gulp.dest('./dist'))
        .pipe(hash.manifest('assets.json'))
        .pipe(gulp.dest('./manifest'));
});

gulp.task('revision', ['hash'], function() {
    return gulp.src(['./pages/*.html'])
        .pipe(revision({
            hasSuffix: false,
            manifest: './manifest/assets.json'
        }))
        .pipe(gulp.dest('./pages/'));
});

API

revision(options)

optiondefaultdescription
manifestRequired, asset manifest file path
hasSuffixfalseIf true,path matching will add pathSuffix
pathSuffix-0-9a-f{8,10}Support regular

hasSuffix和pathSuffix的说明

html中的js或css引用地址是通过{name}{ext}开头的正则进行匹配的,资源地址可以带参数,如:{name}{ext}?param=value

当路径不是{name}{ext}开头时,gulp-asset-revision在扫描文件引用地址时,无法正确匹配地址,比如:{name}-{hash}{ext}。这时需要借助pathSuffix,帮助gulp-asset-revision更新好的匹配资源地址,pathSuffix的作用是匹配{name}{ext}之间的这段,默认值是-[0-9a-f]{8,10},需要设置hasSuffixtrue时才能生效。pathSuffix支持字符串和正则表达式。