gulp批量添加版本號

要實現的理想效果:css

"/css/style.css" => "/dist/css/style.css?v=1d87bebe"
"/js/script1.js" => "/dist/script1.js?v=61e0be79"
"cdn/image.gif" => "//cdn8.example.dot/img/image.gif?v=35c3af8134"html

1.安裝Gulp
npm install --save-dev gulpnode

2.安裝gulp-rev、gulp-rev-collerctor
npm install gulp-rev gulp-rev-collector --save-devjquery

3.打開node_modules\gulp-rev\index.jsgit

第133行 manifest[originalFile] = revisionedFile;
更新爲: manifest[originalFile] = originalFile + '?v=' + file.revHash;

4.打開node_modules\gulp-rev\node_modules\rev-path\index.jses6

10行 return filename + '-' + hash + ext;
更新爲: return filename + ext;

5.打開node_modules\gulp-rev-collector\index.jsgithub

31行 if ( path.basename(json[key]).replace(new RegExp( opts.revSuffix ), '' ) !== path.basename(key) ) {
更新爲: if ( path.basename(json[key]).split('?')[0] !== path.basename(key) ) {

注:修改完node_modules\gulp-rev\index.js與node_modules\gulp-rev-collector\index.js 就已達到效果express

  另外附上es6的node_modules\gulp-rev-collector\index.js修改方法:npm

第40行 let cleanReplacement =  path.basename(json[key]).replace(new RegExp( opts.revSuffix ), '' );
  更新爲 let cleanReplacement =  path.basename(json[key]).split('?')[0];

 

 

目錄結構:json

 

gulpfile.js文件

var gulp = require('gulp');
var clean = require('gulp-clean');                                  //清理文件或文件夾
var minify = require('gulp-uglify');                            //- 壓縮js;
//var concat = require('gulp-concat');                            //- 多個文件合併爲一個;
var minifyCss = require('gulp-minify-css');                     //- 壓縮CSS爲一行;
var rev = require('gulp-rev');                                  //- 對文件名加MD5後綴
var revAppend = require('gulp-rev-append');                                  //- 給URL自動添加MD5版本號
var revCollector = require('gulp-rev-collector');               //- 路徑替換
var replace = require('gulp-replace');                          //替換地址
var runSequence = require('gulp-run-sequence');
var revFormat = require('gulp-rev-format');
var revReplace = require('gulp-rev-replace');

/*=====================清理構建目錄==========================*/
gulp.task('clean', function () {
    return gulp.src('dist/', {read: false})
        .pipe(clean());
});

/*=====================copy其餘靜態資源文件==========================*/
gulp.task('copy', function() {
    return gulp.src(['src/**/*'])
        .pipe(gulp.dest('dist'))
});

/*=====================壓縮js==========================*/
gulp.task('js', function(){
    return gulp.src('dist/static/js/!(lib)/**/*.js') // 匹配
        .pipe(minify())
        //.pipe(rev())
        .pipe(gulp.dest('dist/static/js'));  // 寫入 'dist/js'
});

/*=====================壓縮css==========================*/
gulp.task('concat', function(){
    return gulp.src(['dist/static/css/**/*.css'])  //- 須要處理的css文件,放到一個字符串數組裏
        .pipe(minifyCss())                                      //- 壓縮處理成一行
        .pipe(gulp.dest('dist/static/css'))                               //- 輸出文件本地
});

/*=====================壓縮html==========================*/
gulp.task('miniHtml', function() {
    return gulp.src('dist/views/*.hbs')
        .pipe(revAppend())
        .pipe(gulp.dest('dist/views/'));
});

/*=====================生成版本號清單==========================*/
gulp.task('rev', function() {
    return gulp.src(['dist/static/!(lib)/**/*.*'])
    .pipe(rev())
    .pipe(revFormat({
        prefix: '.', // 在版本號前增長字符
        suffix: '.cache', // 在版本號後增長字符
        lastExt: false
    }))
    .pipe(rev.manifest())
    .pipe(gulp.dest("config/"));
});

/*=====================路徑替換==========================*/
gulp.task('update-version', function() {
    return gulp.src(['config/*.json','dist/views/**/*'])
        .pipe(revCollector())//- 根據 .json文件 執行文件內css名的替換
        .pipe(gulp.dest('dist/views'));
});

gulp.task('build', function(done) {
    runSequence(
        ['clean'],
        ['copy'],
        ['js','concat'],
        ['rev'],
        ['update-version'],
        done);
});
gulp.task('expressgulp', ['build']);

 

 package.json文件

{
  "name": "expressgulp",
  "version": "1.0.1",
  "description": "express nodeJs hbs模板",
  "main": "app.js",
  "dependencies": {
    "amui-hbs-helper": "^2.2.0",
    "body-parser": "^1.17.1",
    "browser-sync": "^2.18.8",
    "cookie-parser": "^1.4.3",
    "express": "^4.15.2",
    "express-session": "^1.15.2",
    "fs": "0.0.1-security",
    "gulp": "^3.9.1",
    "gulp-clean": "^0.3.2",
    "gulp-minify-css": "^1.2.4",
    "gulp-replace": "^0.5.4",
    "gulp-replace-task": "^0.11.0",
    "gulp-rev": "^7.1.2",
    "gulp-rev-collector": "^1.1.1",
    "gulp-rev-format": "^1.0.4",
    "gulp-rev-replace": "^0.4.3",
    "gulp-run-sequence": "^0.3.2",
    "gulp-seajs-concat": "^1.0.5",
    "gulp-seajs-transport": "^0.4.0",
    "gulp-uglify": "^2.1.2",
    "handlebars": "^4.0.8",
    "hbs": "^4.0.1",
    "jquery": "^3.2.1",
    "merge-stream": "^1.0.1",
    "multer": "^1.3.0",
    "seajs": "^3.0.2",
    "session": "^0.1.0",
    "yargs": "^7.0.2"
  },
  "devDependencies": {
    "gulp-rev-append": "^0.1.8",
    "http": "0.0.0"
  },
  "scripts": {
    "註釋": "npm run test以debug形式啓動,另打開cmd命令node-inspector -p 8081 啓動斷點調試命令",
    "test": "supervisor --debug app.js",
    "dev": "node app.js"
  },
  "keywords": [
    "express",
    "hbs"
  ],
  "author": "qjh",
  "license": "ISC"
}

最終效果:

都已加上版本號:

 

項目地連接:https://github.com/qjhe/expressGulp

參考 http://www.cnblogs.com/givebest/p/4707432.html 

相關文章
相關標籤/搜索