To gulp, to achieve 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:

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: ' 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; Replace reference Gulp.task (' CDN ', function () {var revall = new Revall ({dontrenamefile:[/^\/.*.html/]}) after unification plus MD5; |PNG/GULP.SRC ([option.src+ '/templates-dev/*.html ', option.src+ '/static/** ']). Pipe (Revall.revision ()). Pipe (gu
Lp.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 '));
    });
    Gulp.task (' prefix ', function () {Console.log (' Add CDN prefix ... '); Return gulp.src (option.src+ '/templates/*.html '). Pipe (prefix (OPTION.CDN, NULL)). PIPE (gulp.dest "option.src+"/templ
    Ates/')); }) Gulp.task (' Htmlmin ', function () {return gulp.src ([option.src + '/templates/*.html '],{base:option.src}). pip
    E (minifyhtml ()). Pipe (Gulp.dest (OPTION.SRC)); }) Gulp.task (' Jsmin ', function () {return gulp.src (option.dest + '/' + version + '/static/js/*.js ', option.dest + '/' + Version + '/static/imgcut/js/*.JS '],{base:option.dest + '/' + version} '. Pipe (Uglify ()). Pipe (Gulp.dest (option.dest + '/' + version)); }) Gulp.task (' Cssmin ', function () {return gulp.src (option.dest + '/' + version + '/static/css/*.css ', Option.dest + '/ ' + version + '/static/imgcut/css/*.css '],{base:option.dest + '/' + version} '. Pipe (MINIFYCSS ()) pipe (Gulp.dest (Opti
    On.dest + '/' + version);
    }) Gulp.task (' Zip ', function () {Console.log (' compression ... ') return gulp.src ([option.dest+ '/' +version+ '/static/** ',])
    . Pipe (Zip (' static.zip ')). Pipe (Gulp.dest (option.dest+ '/' +version)); }) Gulp.task (' R ', function (CB) {gulpsequence (' rep ', ' prefix ', [' htmlmin ', ' jsmin ', ' cssmin '], ' zip ', CB)})

Here the plug-in name can be used to go to NPM website search function and use method, here no longer detailed description, mainly about the next several key plug-ins:
Gulp-rev-all adds a MD5 stamp to the static resource, generating a Rev-manifest.json file (which is critical) of the source file name and the stamped MD5 filename. Gulp-prefix replaces references and CDN prefixes with HTML files that reference static resources minimist Add parameters to the execution of tasks Eg:gulp R--v 1.0 gulp-sequence Executes the task in the specified order . Some important plug-ins are finished, let's talk about our mission.
The CDN is detailed in the plug-in introduction Gulp-rev-all. Rep See Gulp-prefix htmlmin,cssmin,jsmin,zip to the static resources compressed into a ZIP package to facilitate the deployment of CDN. R organizes the execution of tasks to prevent asynchronous operations in the execution of subtasks from disrupting the execution of the entire task. there's a flaw in the R mission here. The CDN task is in it, because the CDN task contains multiple asynchronous tasks, yes, it creates the illusion of the synchronization task in the R task, which leads to subsequent operation and the resulting error. So you need to perform the following two-step tasks at the terminal:
Gulp CDN--v 1.0//Build 1.0 version Gulp R Some configuration parameters are written in the option object, and students can modify them according to their needs . Where dest is the task output directory, the packaged zip file is in the bulid/x.x directory.
Introduction Finished ~

Come and join me in gulp.

Need to communicate. ~~~~~ here.

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.