gulp监听文件变化,并拷贝到指定目录(转)---参考记录
###暂时不支持目录修改、创建、删除。
var gulp = require('gulp');
var fs = require('fs');
var path = require('path');
var less = require('gulp-less');
var sass = require('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 = tinylr();
var port = 1234; // browser-sync
var browserSync = require('browser-sync'); // 创建多层目录
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);
});
}
});
} // 拷贝文件
function copyfile(oldPath, newPath) {
console.log('复制'+oldPath+' -> '+newPath); var stat = fs.lstatSync(oldPath);
if(stat.isDirectory()) {
console.log(oldPath+'是目录');
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') //压缩的文件
.pipe(gulp.dest('target/css')) //输出文件夹
.pipe(minifycss()); //执行压缩
}); // 编译Sass
gulp.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')) //输出main.js到文件夹
.pipe(rename({suffix: '.min'})) //rename压缩后的文件名
.pipe(uglify()) //压缩
.pipe(gulp.dest('target/js')); //输出
}); gulp.task('html', function() {
return gulp.src('./src/*.php')
.pipe(gulp.dest('target/')); //输出
}); // 监听任务 运行语句 gulp watch
gulp.task('watch',function(){
server.listen(port, function(err){
if (err) {
return console.log(err);
} //拷贝修改过的文件
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('当前路径'+newDirPath); // 修改或增加时
if('added' == e.type || 'changed' == e.type || 'renamed' == e.type) {
// 判断目录是否存在,不存在则创建
fs.exists(newDirPath, function(exists){
if(exists){
//console.log("文件夹存在");
copyfile(oldPath, newPath);
} else {
//console.log("文件夹不存在,则创建目录");
mkdirs(newDirPath); // 延时,等待目录创建完成
setTimeout(function(){
copyfile(oldPath, newPath);
}, 200);
}
});
} else if('deleted' == e.type) { //删除
fs.unlink(newPath, function(err){
console.log('删除'+newPath+err);
});
}
}); // 监听sass
gulp.watch('src/css/*.scss', function(){
gulp.run('sass');
}); // 监听js
gulp.watch('./src/js/*.js', function(){
gulp.run('js');
}); // 监听html
gulp.watch('./src/*.php', function(){
gulp.run('html');
}); }); // 实时同步到浏览器
browserSync.init(['target/css/*', 'target/js/*', 'target/*.html', 'target/*.php'], {
/* 静态服务
server: {
baseDir: "target"
}
*/ // 代理模式
proxy: "dz.com"
}); });
http://www.cnblogs.com/dzut/p/4302488.html
gulp监听文件变化,并拷贝到指定目录(转)---参考记录的更多相关文章
- gulp监听文件变化,并拷贝到指定目录
暂时不支持目录修改.创建.删除var gulp = require('gulp'); var fs = require('fs'); var path = require('path'); var l ...
- 如何使用NodeJs来监听文件变化
1.前言 在我们调试修改代码的时候,每修改一次代码,哪怕只是很小的修改,我们都需要手动重新build文件,然后再运行代码,看修改的效果,这样的效率特别低,对于开发者来说简直不能忍. 2.构建自动编译工 ...
- node.js监听文件变化
前言 随着前端技术的飞速发展,前端开发也从原始的刀耕火种,向着工程化效率化的方向发展.在各种开发框架之外,打包编译等技术也是层出不穷,开发体验也是越来越好.例如HMR,让我们的更新可以即时可见,告别了 ...
- 利用WatchService监听文件变化
在实现配置中心的多种方案中,有基于JDK7+的WatchService方法,其在单机应用中还是挺有实践的意义的. 代码如下: package com.longge.mytest; import jav ...
- shell遍历文件目录,监听文件变化,拼接字符串
最近利用业余时间学习了shell 并做了个例子 实现的功能是 : 监听demo文件夹下的文件,只要新增了 .js的文件就把对应的文件名重组,拼接, 最后写入到demo.js里面. 文件结构如下 : ...
- gulp之sass 监听文件,自动编译
gulpfile.js文件如下: var gulp = require('gulp'); var sass = require('gulp-sass'); gulp.task('default', f ...
- 玩转gulp之watch监听文件自动编译
博客移至 https://www.dodoblog.cn/blog?id=5befc928e0feb34495b57035 我们在写页面的时候,用到sass less等css预处理器的时候,虽然写的很 ...
- Java NIO.2 使用Path接口来监听文件、文件夹变化
Java7对NIO进行了大的改进,新增了许多功能: 对文件系统的访问提供了全面的支持 提供了基于异步Channel的IO 这些新增的IO功能简称为 NIO.2,依然在java.nio包下. 早期的Ja ...
- 【转载】java 监听文件或者文件夹变化的几种方式
1.log4j的实现的文件内容变化监听 package com.jp.filemonitor; import org.apache.log4j.helpers.FileWatchdog; public ...
随机推荐
- redis常用性能分析命令
一.连接 src/redis-cli -h 10.20.137.141 -p 6379 >auth 123456789 src/redis-cli -h 10.20.137.141 -p 637 ...
- “The operation cannot be completed because the DbContext has been disposed” exception with lazy load disabled
http://stackoverflow.com/questions/18261732/the-operation-cannot-be-completed-because-the-dbcontext- ...
- 【iOS开发-36】Bundle Identifier的中文字符变成-的问题
在创建新项目时,Bundle Identifier=Organization Identifier+Product Name.可是它们对中文的识别统一变成短横线 - . 所以在创建多个项目的时候,须要 ...
- Android Developers:向其它应用发送用户
Android的一个非常重要的功能是,应用程序基于它要执行的一个“动作”想其它应用程序发送用户的能力.例如,如果你的应用程序要显示一个地图,你没有在你的应用程序中创建显示地图的Activity.相反, ...
- centos安装Elasticsearch步骤
1.安装JDK:centos删除openJDK,安装JDK,vim /etc/profile配置JAVA_HOME 2.官网下载elasticsearch:https://www.elastic.co ...
- 关于 NSInvocation
Invocation 调用的意思. 可想而知NSInvocation 是一个 方法调用 封装的类. 这体现了 面向对象的思想, 及一切皆对象.函数也不例外. 一般编程中,应该很少用到这个. 但是 ...
- Android:使用 DownloadManager 进行版本更新
app 以前的版本更新使用的自己写的代码从服务器下载,结果出现了下载完成以后,提示解析包错误的问题,但是呢,找到该 apk 点击安装是可以安装成功的,估计就是最后几秒安装包没有下载完成然后点击了安装出 ...
- octave画心形曲线
octave是gnu出品和matlab兼容的科学计算软件,具有体积小,兼容性好,免费的优点. 心形曲线是根据函数:( x2 + y2 -1 )3 - x2y3=0 改编而成. clear all; c ...
- 【Unity】12.1 基本概念
开发环境:Win10.Unity5.3.4.C#.VS2015 创建日期:2016-05-09 一.简介 导航网格(Navmesh)是世界坐标系中几何体的简化表示,被游戏代理用来进行全局导航.通常,代 ...
- Python fabric实践操作
前面学习了理论,下面该练练手了.两台机器:10.1.6.186.10.1.6.159.fabric部署在10.1.6.186上面. 1 执行一个简单的task任务,显示两台机器的/home/guol ...