Gulp Add a hash (MD5) suffix to a static resource file to prevent caching

Source: Internet
Author: User
Tags md5

The first step is to install the required packages, installed with NPM, as follows:

NPM Install gulp Gulp-rev run-sequence gulp-rev-collector--save-dev

The second step is to write the configuration in the Gulpfile.js file:

Introduce Gulp and gulp plug-ins var gulp = require (' Gulp '), runsequence = require (' Run-sequence '), rev = require (' Gulp-rev '),

Revcollector = require (' Gulp-rev-collector ');


Define CSS, JS source file path var csssrc = './app/static/css/*.css ', jssrc = './app/static/js/admin/*.js '; The CSS generates the file hash code and generates Rev-manifest.json file names against the mapping Gulp.task (' Revcss ', function () {return gulp.src (CSSSRC). Pipe (Rev (
). Pipe (Gulp.dest ('./rev/css ')). Pipe (Rev.manifest ()). Pipe (Gulp.dest ('./rev/css '));


});                                JS generates a file hash code and generates Rev-manifest.json file names against the mapping Gulp.task (' Revjs ', function () {return gulp.src (JSSRC). Pipe (rev ())                       Adds a hash code to the file. Pipe (Gulp.dest ('./rev/js/admin ')). Pipe (Rev.manifest ())
Generates Rev-mainfest.json files as records. PIPE (Gulp.dest ('./rev/js '));


});
        HTML replacement CSS, JS file version gulp.task (' Revhtmlcss ', function () {return gulp.src (['./rev/css/*.json ', './app/index.html '])            . Pipe (Revcollector ())             Replaces the corresponding record in HTML. PIPE (Gulp.dest ('./view '));
Output to the folder}); Gulp.task (' Revhtmljs ', function () {return gulp.src (['./rev/js/*.json ', './view/index.html ']). PIPE (Revcollec
Tor ()). Pipe (Gulp.dest ('./view '));


});
    Develop and construct gulp.task (' Dev ', function (done) {condition = false; sequentially executes runsequence ([' Revcss '], [' Revhtmlcss '], [' Revjs '], [' Revhtmljs '], do
NE); });
Step three, open cmd, switch to the directory where the project is located, run

Gulp Dev

OK, finished ...



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.