// 定义web服务模块,增加浏览器同步浏览
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: '.'
}
});
});
// 创建Compass任务,编译Sass生成雪碧图
gulp.task('compass', function() {
gulp.src(path.dev.sass+'/*.scss')
.pipe(compass({
config_file: './config.rb', // 配置文件
css: path.dev.css, // 编译路径
sass: path.dev.sass // sass路径
//image: path.dev.image // 图片路径,用于生成雪碧图
}))
.pipe(cssver()) // CSS文件引用URl加版本号
.pipe(minifycss()) // 压缩CSS
.pipe(gulp.dest(path.dist.css)) // 发布到线上版本
.pipe(reload({stream: true}));
});
// 压缩HTML
gulp.task('html', function() {
gulp.src(path.dev.html+"/*.html")
.pipe(rev()) // html 引用文件添加版本号
.pipe(gulp.dest(path.dist.html))
.pipe(reload({stream: true}));
});
//检查脚本
gulp.task('lint', function() {
gulp.src(path.dev.js+'/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});// 图片压缩
gulp.task('image', function() {
gulp.src(path.dev.image+'/*.*')
.pipe(cache(imagemin()))
.pipe(reload({stream: true}))
.pipe(gulp.dest(path.dist.image));
//.pipe(notify({ message: '图片压缩'}));
});
// 合并压缩JS文件
gulp.task('script', function() {
gulp.src(path.dev.js+'/*.js')
//.pipe(concat('all.js')) // 合并
//.pipe(gulp.dest(path.dist.js))
//.pipe(rename('all.min.js')) // 重命名
.pipe(uglify()) // 压缩
.pipe(gulp.dest(path.dist.js))
//.pipe(notify({ message: 'JS合并压缩' }))
.pipe(reload({stream: true}));
});
// 清空文件夹
gulp.task('clean', function() {
gulp.src([path.dist.css, path.dist.js, path.dist.image], {read: false})
.pipe(clean());
});
// 默认任务
gulp.task("default", function() {
gulp.run('browser-sync', 'lint', 'compass', 'script', 'image');
// 检测文件发送变化 - 分开监听为了执行对应的命令
gulp.watch(path.dev.html+'/*.*', ['html']);
gulp.watch(path.dev.sass+'/*.scss', ['compass']);
gulp.watch(path.dev.image+'/**', ['image']);
gulp.watch(path.dev.js+'/*.js', ['lint', 'script']);
});
https://www.cnblogs.com/morong/p/4469637.html