Gulp Monitor file changes and copy to the specified directory

Source: Internet
Author: User

Directory modification, creation, deletion is not supported temporarily
var gulp = require (' gulp '); var fs = require (' FS '); var path = require (' path '); var = require (' gulp-less '); var sass = re Quire (' Gulp-sass '); var minifycss = require (' gulp-minify-css '); var concat = require (' Gulp-concat '); var uglify = require ( ' gulp-uglify '); var rename = require (' gulp-rename '); var del = require (' del '); var tinylr = require (' TINY-LR '); var server = t INYLR (); var port = 1234;//Browser-syncvar Browsersync = require (' Browser-sync ');//create multi-level directory function mkdirs (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 file function CopyFile (OldPath, NewPath) {console.log (' copy ' +oldpath+ ' +newpath '); var stat = Fs.lstatsync (OldPath); if (Stat.isdirectory ()) {Console.log (oldpath+ ' is directory '); return false; } var readstream = Fs.createreadstream (OldPath); var writestream = 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 () {return gulp.src (' src/*.css ')//compressed file . Pipe (Gulp.dest (' target/css '))//output folder. Pipe (Minifycss ()); Execute compression});//Compile 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 () {return gulp.src ('./src/js/*.js '). Pipe (Gulp.dest (' Target/js '))//Output Main.js to Folder . Pipe (rename ({suffix: '. Min '))//rename the compressed file name. PIPE (Uglify ())//compression. PIPE (Gulp.dest (' Target/js ') ); Output}); Gulp.task (' HTML ', function () {returnGulp.src ('./src/*.php '). Pipe (Gulp.dest (' target/')); Output});//Listener Task Run Statement Gulp Watchgulp.task (' Watch ', function () {Server.listen (port, function (ERR) {if (err) { return Console.log (ERR); }//Copy modified file Gulp.watch (' src/**/** ', function (e) {console.log (e); var oldpath = E.path; var NewPath = oldpath.replace (' \\src\\ ', ' \\target\\ '); var newdirpathtemp = newpath.split ("\ \"); var Currentpath = Fs.realpathsync ('. '); var newdirpath = []; for (var i = 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); Modified or incremented if (' Added ' = = E.type | | ' changed ' = = E.type | | ' renamed ' = = E.type) {//Determine if the directory exists, does not exist then create fs.exists (Newdirpath, function (exists) { if (exists) {//console.log ("folder exists"); CopyFile (OldPath, NewPath); } else {//console.log ("folder does not exist, then create directory"); Mkdirs (Newdirpath); Delay, wait for directory creation to complete setTimeout (function () {CopyFile (OldPath, NewPath); }, 200); } }); } else if (' deleted ' = = E.type) {//delete Fs.unlink (NewPath, function (err) {console.log (' delete ') +NEWPATH+ERR); }); } }); Monitor sass Gulp.watch (' Src/css/*.scss ', function () {Gulp.run (' sass '); }); Monitor JS Gulp.watch ('./src/js/*.js ', function () { Gulp.run (' JS '); }); Listen for HTML Gulp.watch ('./src/*.php ', function () {gulp.run (' html '); }); }); Real-time sync to browser browsersync.init ([' target/css/* ', ' target/js/* ', ' target/*.html ', ' target/*.php '), {/* static service Server: {baseDir: "target"} *///proxy mode proxy: "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.