/
gulpfile.js
99 lines (84 loc) · 2.31 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var gulp = require('gulp'), //基础库
minifycss = require('gulp-minify-css'), //css压缩
less = require('gulp-less'), //less编译
jshint = require('gulp-jshint'), //js检查
uglify = require('gulp-uglify'), //js压缩
rename = require('gulp-rename'), //重命名
concat = require('gulp-concat'), //合并文件
clean = require('gulp-clean'), //清空文件夹
notify = require('gulp-notify'),
plumber = require('gulp-plumber'),
connect = require('gulp-connect'), //webserver
port = 8888,
rootpath = '';
gulp.task('webserver', function() {
connect.server({
root: rootpath,
port: port,
livereload: true
});
});
// HTML处理
var htmlSrc = './app/*.html',
htmlDst = './build/';
var cssSrc = './app/less/**/*.less',
cssDst = './app/css/',
cssMinDst = './build/css/';
var jsSrc = './app/js/**/*.js',
jsDst ='./build/js/';
gulp.task('html', function() {
gulp.src(htmlSrc)
.pipe(connect.reload())
.pipe(gulp.dest(htmlDst))
});
var onError = function(err) {
console.log(err);
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "Error: <%= error.message %>",
sound: "Beep"
})(err);
this.emit('end');
};
// 样式处理
gulp.task('css', function () {
gulp.src(cssSrc)
.pipe(plumber({errorHandler: onError}))
.pipe(less({ style: 'expanded'}))
.pipe(gulp.dest(cssDst))
.pipe(rename({ suffix: '.min' }))
.pipe(minifycss())
.pipe(connect.reload())
.pipe(gulp.dest(cssMinDst))
});
// js处理
gulp.task('js', function () {
gulp.src(jsSrc)
.pipe(rename({ suffix: '.min' }))
.pipe(plumber({errorHandler: onError}))
.pipe(uglify())
.pipe(connect.reload())
.pipe(gulp.dest(jsDst));
});
// 清空样式、js
gulp.task('clean', function() {
gulp.src([cssDst, jsDst], {read: false})
.pipe(clean());
});
// 重建任务 清空样式、js并重建
gulp.task('rebuild', ['clean'], function(){
gulp.start('html','css','js');
});
// 监听任务 运行语句 gulp watch
gulp.task('default',['webserver'], function(){
// 监听html
gulp.watch(htmlSrc, ['html'], function(event){
})
// 监听css
gulp.watch(cssSrc, ['css'], function(){
});
// 监听js
gulp.watch(jsSrc, ['js'], function(){
});
});