Introduction of Gulp and Gulp Plug-ins
Vargulp = require (' Gulp '),
less = require (' gulp-less '),
Assetrev = require (' Gulp-asset-rev '),
Minifycss = require (' Gulp-minify-css '),
Uglify = require (' gulp-uglify '),
Htmlmin = require (' Gulp-htmlmin '),
Rename = require (' Gulp-rename '),
Imagemin = require (' Gulp-imagemin '),
Runsequence = require (' run-sequence '),
Rev = require (' Gulp-rev '),
Revcollector = require (' Gulp-rev-collector ');
Define CSS, JS source file path
VARCSSSRC = ' Css/*.css ',
CSSMINSRC = ' Dist/css/*.css ',
JSMINSRC = ' Dist/js/*.js ',
LESSSRC = ' less/*.less ',
IMGMINSRC = ' dist/images/*. {Png,jpg,gif,ico} ',
Compile less define a less task (custom task Name)
Gulp.task (' Less ', function () {
RETURNGULP.SRC (LESSSRC)//files for this task
. Pipe (less ())/the module called by the task
. Pipe (Gulp.dest (' CSS '))//compiled path
Add a hash code for Pictures/fonts introduced in CSS
Gulp.task (' Assetrev ', function () {
RETURNGULP.SRC (CSSSRC)//files for this task
. Pipe (Assetrev ())/the module called by the task
. Pipe (Gulp.dest (' src '));//compiled path
Gulp.task (' Cssmin ', function () {
RETURNGULP.SRC (CSSSRC)//Compressed files
. Pipe (rename ({suffix: '. Min '})
. Pipe (Minifycss ())//Perform compression
. Pipe (Gulp.dest (' dist/css ')); Output folder
CSS generates file hash code and generates Rev-manifest.json file name against map
Gulp.task (' Revcss ', function () {
RETURNGULP.SRC (CSSMINSRC)
. Pipe (rev ())//filename plus MD5 suffix
. Pipe (Rev.manifest ())//You must have this method to generate a Rev-manifest.json
. Pipe (Gulp.dest (' dist/css ')); Save Rev-manifest.json to the DIST/CSS directory
Gulp.task (' uglify ', function () {
. Pipe (rename ({suffix: '. Min '})
. Pipe (Gulp.dest (' Dist/js '));
JS generates file hash code and generates Rev-manifest.json file name control map
Gulp.task (' Revjs ', function () {
RETURNGULP.SRC (JSMINSRC)
. Pipe (Gulp.dest (' Dist/js '));
Gulp.task (' Htmlmin ', function () {
Collapsewhitespace:true,//from the literal meaning should be able to see, clear the Space, compressed HTML, this is more important, the role is relatively large, resulting in the change in the volume of compression is particularly large.
Collapsebooleanattributes:true,//omitting the value of the Boolean attribute, for example: <input checked= "Checked"/> then, when you set this property, it becomes <input checked/ >.
Removecomments:true,//clear the comments in the HTML section, we should reduce the comments in the HTML page.
Removeemptyattributes:true,//Clear all empty properties.
Removescripttypeattributes:true,//Clear the Type= "Text/javascript" attribute in all script tags.
Removestylelinktypeattributes:true,//clear the Type attribute on all link labels.
Minifyjs:true,//Compress JavaScript code in HTML.
Minifycss:true//Compress CSS code in HTML.
. Pipe (Htmlmin (options))
. Pipe (Gulp.dest (' dist/html '));
HTML replace CSS, JS file version
Gulp.task (' revhtml ', function () {
RETURNGULP.SRC ([' Dist/**/*.json ', ' dist/html/*.html '])
. Pipe (Gulp.dest (' dist/html '));
Gulp.task (' Imagemin ', function () {
GULP.SRC (' images/*.{ Png,jpg,gif,ico} ')
. Pipe (Gulp.dest (' dist/images '));
Gulp.task (' Revimage ', function () {
RETURNGULP.SRC (IMGMINSRC)
. Pipe (Rev.manifest ())//must have this method
. Pipe (Gulp.dest (' dist/images '));
Gulp.task (' Default ', function (done) {
Runsequence (///There is no way to do this in the most parallel (asynchronous) Gulp.run method, Runsequence This serial method (sequential execution) to perform these tasks in sequence after running gulp and adding version numbers to the HTML