Gulp Css,js Compression Merge encryption replacement

Source: Internet
Author: User

To prevent the client from caching static resources, we need to rename the static resources by MD5 or timestamps every time we update the CSS or JS. Allows the client to re-request the resource instead of fetching it from the cache. The SRC in the HTML template is then modified accordingly. Of course, there is an additional requirement that static resources need to be optimized for themselves (compression merges).

Here is the code for my gulpfile.js:

varGulp = require ('Gulp'),//Base LibraryClean = require ('Gulp-clean'),//Clear FolderMinify = require ('Gulp-minify-css'),//CSS CompressionRename = require ('Gulp-rename'),//file RenameRev = require ('Gulp-rev'),//Change the version nameCollector = require ('Gulp-rev-collector'),//Gulp-rev plug-in for HTML file change reference pathConcat = require ('Gulp-concat'),//Merging multiple FilesNotify = require ('gulp-notify');//Tips      Gulp.task (' Clean', function () {returnGULP.SRC ('Build', {read:false})//The second parameter of the src {read:false}, is to not read the file, speeding up the program. . Pipe (Clean ());}) Gulp.task ('Index',[' Clean'],function () {returnGULP.SRC ('app/index.min.html'). Pipe (Rename (path) {Path.basename='Index'; Path.extname=". html"; }). Pipe (Gulp.dest ('Build')) }) Gulp.task ('CSS',['Index'],function (CB) {returnGULP.SRC ('App/**/*.css'). Pipe (Minify ()). Pipe (Concat ('Main.css') . Pipe (Rename (path) {Path.basename+='. Min'; Path.extname=". CSS"; }). Pipe (rev ()). Pipe (Gulp.dest ('Build/css') . Pipe (Rev.manifest ()). Pipe (Gulp.dest ('Build/rev')); CB ();}) Gulp.task ('Rev',['CSS'],function () {returnGULP.SRC (['Build/rev/rev-manifest.json','build/index.html']). Pipe (Collector ({replacereved:true}). Pipe (Gulp.dest ('build/'). Pipe (Notify ("Success!!!"))})

My directory structure:

 

The app is the original code directory, build is gulp generated

Gulp Css,js Compression Merge encryption replacement

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.