Gulp Monitor file changes and copy to the specified directory

Source: Internet
Author: User

Directory modification, creation, and deletion are not supported for the time being.

varGulp = require (' Gulp '));varFS = require (' FS ');varPath = require (' path ');varless = require (' gulp-less ');varSass = require (' Gulp-sass '));varMinifycss = require (' gulp-minify-css '));varConcat = require (' Gulp-concat '));varUglify = require (' gulp-uglify '));varRename = require (' Gulp-rename '));vardel = require (' del ');varTINYLR = require (' TINY-LR '));varServer =TINYLR ();varPort = 1234;//Browser-syncvarBrowsersync = require (' Browser-sync '));//Create a multi -level catalogfunctionmkdirs (dirname, mode, callback) {fs.exists (dirname,function(exists) {if(exists) {callback (); }Else{            //Console.log (Path.dirname (dirname));Mkdirs (Path.dirname (dirname), mode,function() {fs.mkdir (dirname, mode, callback);        }); }    });}//Copy FilesfunctionCopyFile (OldPath, NewPath) {Console.log (' Copy ' +oldpath+ ' +NewPath); varStat =Fs.lstatsync (OldPath); if(Stat.isdirectory ()) {Console.log (OldPath+ ' is directory '); return false; }        varReadstream =Fs.createreadstream (OldPath); varWritestream =Fs.createwritestream (NewPath);    Readstream.pipe (Writestream); Readstream.on (' End ',function() {Console.log (' Copy End ');    }); Readstream.on (' Error ',function() {Console.log (' Copy error '); });} Gulp.task (' Default ',function() {}); Gulp.task (' CSS ',function() {    returnGULP.SRC (' Src/*.css ')//Compressed Files. Pipe (Gulp.dest (' target/css '))//output Folder. Pipe (Minifycss ());//Perform compression});//Compiling sassGulp.task (' sass ',function() {GULP.SRC ('./src/css/*.scss '). Pipe (Sass ()). Pipe (rename ({suffix:'. Min '}). Pipe (Minifycss ()). Pipe (Gulp.dest (' Target/css '));}); Gulp.task (' JS ',function() {    returnGulp.src ('./src/js/*.js ')). Pipe (Gulp.dest (' Target/js '))//output main.js to folder. Pipe (rename ({suffix: '. Min '}))//Rename the file name after compression. Pipe (Uglify ())//Compression. Pipe (Gulp.dest (' Target/js '));//Output}); Gulp.task (' HTML ',function() {    returnGulp.src ('./src/*.php ')). Pipe (Gulp.dest (' target/'));//Output});//Monitor task run Statement Gulp WatchGulp.task (' Watch ',function() {Server.listen (port,function(err) {if(err) {returnConsole.log (ERR); }                //Copy the modified fileGulp.watch (' src/**/** ',function(e) {console.log (e); varOldPath =E.path; varNewPath = Oldpath.replace (' \\src\\ ', ' \\target\\ '); varNewdirpathtemp = newpath.split ("\ \"); varCurrentpath = Fs.realpathsync ('. ')); varNewdirpath = [];  for(vari = 0; i < newdirpathtemp.length-1; i++) {Newdirpath[i]=Newdirpathtemp[i]; } Newdirpath= Newdirpath.join ("\ \"); Newdirpath= Newdirpath.replace (Currentpath, "); Newdirpath= Newdirpath.replace (/\\/g, "/"); Newdirpath= Newdirpath.replace ("/", "./"); //console.log (' current path ' +newdirpath);                        //when modified or added            if(' added ' = = E.type | | ' changed ' = = E.type | | ' Renamed ' = =e.type) {//determines whether the directory exists, does not exist, and createsFs.exists (Newdirpath,function(exists) {if(exists) {//Console.log ("folder exists");CopyFile (OldPath, NewPath); } Else {                        //Console.log ("folder does not exist, create directory");mkdirs (Newdirpath); //delay, wait for directory creation to completeSetTimeout (function() {CopyFile (OldPath, NewPath); }, 200);            }                }); } Else if(' deleted ' = = E.type) {//DeleteFs.unlink (NewPath,function(Err) {Console.log (' Delete ' +newpath+err);            });        }        }); //Monitor SassGulp.watch (' Src/css/*.scss ',function() {Gulp.run (' Sass ');        }); //Monitor JSGulp.watch ('./src/js/*.js ',function() {Gulp.run (' JS ');                }); //Listening for HTMLGulp.watch ('./src/*.php ',function() {Gulp.run (' HTML ');            });        }); //sync to browser in real timeBrowsersync.init ([' target/css/* ', ' target/js/* ', ' target/*.html ', ' target/*.php '], {        /*Static Service server: {baseDir: "Target"}*/                //Proxy ModeProxy: "Dz.com"    }); });

Gulp Monitor file changes and copy to the specified directory

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.