Gulp implementation of the front-end static resource compression, add MD5 stamp, replace the reference, plus CDN prefix

Source: Internet
Author: User
has been using grunt, the total sense of Grunt parameter configuration method is very cumbersome, not a clearer flow, coupled with their own gulp flow operations more interested in the decision to go to gulp.

Believe that the front-end ER in the release of their own code, if not using some of the front-end building tools, for frequent online projects, their own manual compression and packaging to replace the CDN prefix that is a terrible thing. So using the Gulp front-end build tool is a very sensible choice.


First of all, what do you need to do when you publish your project:
1. Compression consolidation for static resources
2. Js,css,img and other resources to add MD5 stamp
3. References in the replacement file for an HTML file that references the above MD5 stamp.

4. Add CDN prefix for references in HTML file


Although it looks like these 4 steps, but the task is still difficult to write.

Code on:

[JavaScript]  View Plain  copy   Var gulp = require (' Gulp ');        Var minimist = require (' minimist ');       var uglify =  require (' gulp-uglify ');       var minifyhtml = require (' Gulp-minify-html ');       var minifycss = require (' Gulp-minify-css ');       var rev = require (' Gulp-rev ');        var revreplace = require (' gulp-rev-replace ');        var prefix = require (' Gulp-prefix ');       var  zip = require (' Gulp-zip ');       var gulpsequence =  require (' gulp-sequence ');       var cmd = {       string:&nbSP; ' V ',       default: { v:  ' 1.0 '  }        };       var option = {        src: ' poster ',       dest: ' Poster/build ',       cdn: ' Http://sns_wf.cdn.sohusce.com '///poster/       }        var options = minimist (Process.argv.slice (2),  cmd);        var version = options.v;       //after unification plus MD5 replace references        gulp.task (' CDN ', Function () {       var revall =  new revall ({dontrenamefile:[/^\/.*.html/]});// ,/^\/.*.jpg|png/        GULP.SRC ([option.src+ '/templates-dev/*.html ', option.src+ '/static/** '])         .pIpe (Revall.revision ())        .pipe (gulp.dest (option.dest+ '/' +version))         .pipe (Revall.versionfile ())        .pipe (Gulp.dest ( option.dest+ '/' +version)        .pipe (Revall.manifestfile ())         .pipe (Gulp.dest (option.dest+ '/' +version));        });        gulp.task ("Rep"  ,function () {       var  MANIFEST = GULP.SRC (option.dest +  '/' + version +  "/rev-manifest.json") );       console.log (option.dest + '/' + version);        RETURN GULP.SRC (option.dest + '/' + version +  '/templates-dev/*.html ') )        .pipe (Revreplace ({manifest: manifest}))         .pipe (GULP.DEST (option.src + '/templates '  ));       });    &

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.